View Javadoc

1   package org.catacomb.graph.gui;
2   
3   
4   import org.catacomb.be.Position;
5   import org.catacomb.interlish.interact.ClickListener;
6   import org.catacomb.report.E;
7   
8   import java.awt.Color;
9   
10  
11  
12  public class PickHandler extends MouseHandler {
13  
14      int xoff = 0;
15      int yoff = 0;
16  
17      WorldTransform worldTransform;
18  
19      PickStore pickStore;
20  
21      Pickable activePick;
22  
23      PickListener pickListener;
24      ClickListener clickListener;
25  
26      Pickable echoItem;
27  
28      HoverTimer hTimer;
29      Pickable hoverItem;
30  
31  
32      boolean inTrash;
33  
34  
35      public PickHandler(PickStore ps, WorldTransform wt) {
36          super();
37          pickStore = ps;
38          worldTransform = wt;
39          hTimer = new HoverTimer(this);
40      }
41  
42  
43  
44      public void setPickListener(PickListener pl) {
45          pickListener = pl;
46      }
47  
48  
49  
50      boolean motionAware() {
51          return true;
52      }
53  
54  
55      boolean motionChange(Mouse m) {
56          int xc = m.getX();
57          int yc = m.getY();
58  
59          double wx = worldTransform.pubWopx(xc);
60          double wy = worldTransform.pubWopy(yc);
61  
62  
63  
64          boolean changed = false;
65          if (echoItem == null) {
66              echoItem = pickStore.getClaimant(xc, yc, wx, wy);
67              if (echoItem != null) {
68                  changed = true;
69              }
70  
71          } else {
72              if (pickStore.itemClaims(echoItem, xc, yc, wx, wy)) {
73                  // nothing to do - same item as before;
74  
75              } else {
76                  Pickable pei = echoItem;
77                  echoItem = pickStore.getClaimant(xc, yc, wx, wy);
78                  changed = true;
79  
80  
81                  if (pei == echoItem) {
82                      E.error(" - same claimant but failed to claim");
83                  }
84              }
85          }
86  
87          if (changed) {
88              hoverItem = null;
89              if (echoItem == null) {
90                  hTimer.clear();
91              } else {
92                  hTimer.start();
93              }
94          }
95  
96          return changed;
97      }
98  
99  
100 
101     public void echoPaint(Painter p, boolean tips) {
102         if (echoItem != null) {
103 
104             if (echoItem instanceof PickablePoint) {
105                 p.setColor(Color.white);
106                 p.drawRectangle(pickStore.getEchoBox((PickablePoint)echoItem));
107 
108 
109 
110             } else if (echoItem instanceof PickableRegion) {
111                 PickableRegion pr = (PickableRegion)echoItem;
112                 p.setColor(Color.white);
113                 p.drawPolygon(pr.getXPts(), pr.getYPts());
114 
115                 if (tips || (hoverItem == echoItem)) {
116                     String s = pr.getRegionTag();
117                     if (s != null) {
118                         p.drawOffsetCenteredLabel(s, pr.getXReference(), pr.getYReference());
119                     }
120                 }
121             }
122 
123         }
124     }
125 
126 
127 
128     public void init(Mouse m) {
129         int xm = m.getX();
130         int ym = m.getY();
131 
132         double wx = worldTransform.pubWopx(xm);
133         double wy = worldTransform.pubWopy(ym);
134 
135 
136         activePick = pickStore.getClaimant(xm, ym, wx, wy);
137 
138         // offset between where the mouse was pressed and actual center;
139         if (activePick != null) {
140             xoff = pickStore.getClaimantRefX() - xm;
141             yoff = pickStore.getClaimantRefY() - ym;
142         }
143 
144 
145         if (activePick == null) {
146             setClaimOut();
147         } else {
148             setClaimIn();
149         }
150 
151         inTrash = false;
152     }
153 
154 
155     public void advance(Mouse m) {
156 
157         // never called since init makes decision?;
158         E.warning("advance called in PickHandler? ");
159 
160     }
161 
162 
163     public void missedPress(Mouse m) {
164         if (pickListener != null) {
165 
166             if (m.getX() > worldTransform.getCanvasWidth() - 20 &&
167                     m.getY() > worldTransform.getCanvasHeight() - 20) {
168                 pickListener.trashPressed();
169 
170             } else {
171                 pickListener.backgroundPressed(m.getButton(), m.getX(), m.getY());
172             }
173         }
174     }
175 
176 
177     public void hovered() {
178         hoverItem = echoItem;
179         // E.info("set hover item " + hoverItem);
180         if (hoverItem != null) {
181             pickListener.pickHovered(hoverItem);
182         }
183     }
184 
185 
186     public void applyOnDown(Mouse m) {
187         if (pickListener != null) {
188             pickListener.pickPressed(activePick, m.getButton(), m.getX(), m.getY());
189             // TODO xoff, yoff?
190         }
191     }
192 
193 
194     public void applyOnDrag(Mouse m) {
195 
196         if (pickListener != null) {
197             int ix = m.getX() + xoff;
198             int iy = m.getY() + yoff;
199             Position pos = worldTransform.getWorldPosition(ix, iy);
200             pickListener.pickDragged(activePick, pos, m.getButton(), ix, iy);
201 
202 
203             if (inTrash(ix, iy)) {
204                 if (inTrash) {
205                     // as ist;
206                 } else {
207                     inTrash = true;
208                     pickListener.pickEnteredTrash(activePick);
209                 }
210             } else {
211                 if (inTrash) {
212                     inTrash = false;
213                     pickListener.pickLeftTrash(activePick);
214                 }
215             }
216 
217             setFullRepaint();
218         }
219 
220     }
221 
222 
223     public boolean inTrash(int ix, int iy) {
224         return (ix > worldTransform.getCanvasWidth() - 28 && iy > worldTransform.getCanvasHeight() - 28);
225     }
226 
227 
228 
229     public void applyOnRelease(Mouse m) {
230 
231         if (pickListener != null) {
232             pickListener.pickReleased(activePick, m.getButton());
233 
234             if (inTrash) {
235                 pickListener.pickTrashed(activePick);
236             }
237         }
238 
239     }
240 
241 
242 
243     public void setClickListener(ClickListener cl) {
244         clickListener = cl;
245 
246     }
247 
248 
249 
250 }