Project

General

Profile

Bug #1250 » DragandDrop.rb

the source - Gabor Vitez, 04/21/2012 01:44 PM

 
1
require 'java'
2

    
3
import "eu.webtoolkit.jwt.WContainerWidget"
4
import "eu.webtoolkit.jwt.WApplication"
5
import "eu.webtoolkit.jwt.WText"
6
import "eu.webtoolkit.jwt.WLength"
7
import "eu.webtoolkit.jwt.WImage"
8
import "eu.webtoolkit.jwt.WLink"
9
import "eu.webtoolkit.jwt.WFileResource"
10
import "eu.webtoolkit.jwt.WColor"
11
import "eu.webtoolkit.jwt.Orientation"
12

    
13
class Character < WContainerWidget
14
  def initialize(parent, color, name)
15
    super parent
16
    
17
    setMinimumSize WLength.new("120px"),WLength.new("120px")
18
    getDecorationStyle.setBackgroundColor(WColor.new color)
19
    
20
    n = WText.new name, self
21
    n.setInline false
22
    @name = name
23
    
24
    @message = WText.new self
25
    @message.setInline false
26

    
27
  end
28
  
29
  def dropEvent(e)
30
    #super without () or arguments simply forwards the
31
    #arguments
32
    super
33
    
34
    w = e.getSource
35

    
36
    #move the food from the plate
37
    #to the character
38
    w.getParent.removeWidget w
39
    addWidget w
40

    
41
    @message.setText "Thanks for the " + e.getMimeType
42

    
43
  end
44
end
45

    
46
class DragandDrop < WApplication
47
  def initialize(env)
48
    super
49
    setTitle("Drag And Drop")
50

    
51
    @plate = WContainerWidget.new getRoot
52
    t = WText.new "Plate", @plate
53
    t.setInline false
54
    
55
    @pizza = WImage.new WLink.new(WFileResource.new("image/png","pizza.png")),"Pizza", @plate
56
    @chocolate = WImage.new WLink.new(WFileResource.new("image/png","chocolate.png")),"Chocolate", @plate
57
    @fc = WImage.new WLink.new(WFileResource.new("image/png","fishandchips.png")),"Fish and Chips", @plate
58

    
59
    #the duplicated images, later will be used as dragonly resources
60
    #these lines are commented out for the setDraggable(java.lang.String mimeType) version
61
    dpizza = WImage.new WLink.new(WFileResource.new("image/png","pizza.png")),"Pizza", @plate
62
    dchocolate = WImage.new WLink.new(WFileResource.new("image/png","chocolate.png")),"Chocolate", @plate
63
    dfc = WImage.new WLink.new(WFileResource.new("image/png","fishandchips.png")),"Fish and Chips", @plate
64

    
65

    
66
    #this version works well: 
67
    @pizza.setDraggable "pizza", dpizza, true
68
    @chocolate.setDraggable "chocolate", dchocolate, true
69
    @fc.setDraggable "fish and chips", dfc, true
70
    
71
    #this does not work well:
72
    #@pizza.setDraggable "pizza"
73
    #@chocolate.setDraggable "chocolate"
74
    #@fc.setDraggable "fish and chips"
75

    
76
    @joe = Character.new(getRoot, "lightblue","Joe")
77
    @joe.acceptDrops "pizza"
78
    @joe.acceptDrops "chocolate"
79

    
80
    @jane = Character.new(getRoot, "pink","Jane")
81
    @jane.acceptDrops "fish and chips"
82
    @jane.acceptDrops "chocolate"
83

    
84
  end
85

    
86
end
(1-1/2)