1 | /* |
---|
2 | * $Id: PaneProxy.java,v 1.35 2003/09/19 23:14:24 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | |
---|
13 | package gov.noaa.pmel.sgt; |
---|
14 | |
---|
15 | import gov.noaa.pmel.sgt.swing.Draggable; |
---|
16 | import gov.noaa.pmel.sgt.beans.Panel; |
---|
17 | |
---|
18 | import gov.noaa.pmel.util.Debug; |
---|
19 | |
---|
20 | import java.awt.Graphics2D; |
---|
21 | import java.awt.Stroke; |
---|
22 | |
---|
23 | import java.awt.Dimension; |
---|
24 | import java.awt.Image; |
---|
25 | import java.awt.Graphics; |
---|
26 | import java.awt.Rectangle; |
---|
27 | import java.awt.Point; |
---|
28 | import java.awt.PrintGraphics; |
---|
29 | import java.awt.Container; |
---|
30 | import java.awt.Component; |
---|
31 | import java.awt.Color; |
---|
32 | import java.awt.Cursor; |
---|
33 | |
---|
34 | import java.awt.event.MouseEvent; |
---|
35 | import java.awt.event.InputEvent; |
---|
36 | |
---|
37 | import java.util.Vector; |
---|
38 | import java.util.Properties; |
---|
39 | |
---|
40 | import java.beans.PropertyChangeListener; |
---|
41 | import java.beans.PropertyChangeSupport; |
---|
42 | |
---|
43 | /** |
---|
44 | * PaneProxy implements the functionality common to <code>JPane</code> |
---|
45 | * and <code>Pane</code>. |
---|
46 | * |
---|
47 | * @author Donald Denbo |
---|
48 | * @version $Revision: 1.35 $, $Date: 2003/09/19 23:14:24 $ |
---|
49 | */ |
---|
50 | class PaneProxy { |
---|
51 | public static String SGTVersion = "3.0"; |
---|
52 | public static boolean Java2D = false; |
---|
53 | public static StrokeDrawer strokeDrawer = null; |
---|
54 | |
---|
55 | private Container pane_; |
---|
56 | |
---|
57 | private String ident_; |
---|
58 | private Dimension panesize_; |
---|
59 | private Image offscreen_; |
---|
60 | private Dimension pagesize_; |
---|
61 | private Point pageOrigin_ = new Point(0, 0); |
---|
62 | private boolean printer_ = false; |
---|
63 | private boolean opaque_ = true; |
---|
64 | private int halign_ = AbstractPane.CENTER; |
---|
65 | private int valign_ = AbstractPane.MIDDLE; |
---|
66 | private int printMode_ = AbstractPane.SHRINK_TO_FIT; |
---|
67 | private Object selectedobject_; |
---|
68 | private Object old_selectedobject_ = null; |
---|
69 | private Rectangle selectedRect_; |
---|
70 | private Rectangle zoom_rect_ = new Rectangle(0, 0, 0, 0); |
---|
71 | private Rectangle old_zoom_rect_; |
---|
72 | private Point zoom_start_ = new Point(0, 0); |
---|
73 | private boolean in_zoom_ = false; |
---|
74 | private boolean in_select_ = false; |
---|
75 | private boolean in_move_ = false; |
---|
76 | private boolean moved_ = false; |
---|
77 | private boolean draggable_ = false; |
---|
78 | private boolean moveable_ = false; |
---|
79 | private Point move_ref_; |
---|
80 | private boolean batch_ = true; |
---|
81 | private boolean modified_ = false; |
---|
82 | private boolean ignoreModified_ = false; |
---|
83 | private boolean firstDraw_ = true; |
---|
84 | private boolean mouseEventsEnabled_ = true; |
---|
85 | |
---|
86 | private PropertyChangeSupport changes_ = null; |
---|
87 | |
---|
88 | public PaneProxy(Container pane, String id, Dimension size) { |
---|
89 | ident_ = id; |
---|
90 | panesize_ = size; |
---|
91 | pane_ = pane; |
---|
92 | changes_ = new PropertyChangeSupport(pane_); |
---|
93 | // |
---|
94 | testJava2D(); |
---|
95 | } |
---|
96 | /** |
---|
97 | * @since 3.0 |
---|
98 | */ |
---|
99 | public static String getVersion() { |
---|
100 | return SGTVersion; |
---|
101 | } |
---|
102 | |
---|
103 | private void testJava2D() { |
---|
104 | Class cl; |
---|
105 | boolean java2d = true; |
---|
106 | try { |
---|
107 | cl = Class.forName("java.awt.Graphics2D"); |
---|
108 | } catch (ClassNotFoundException e) { |
---|
109 | java2d = false; |
---|
110 | } |
---|
111 | Java2D = java2d; |
---|
112 | if(Java2D) { |
---|
113 | strokeDrawer = new StrokeDrawer2(); |
---|
114 | } else { |
---|
115 | strokeDrawer = new StrokeDrawer1(); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | public Dimension getSize() { |
---|
120 | return panesize_; |
---|
121 | } |
---|
122 | |
---|
123 | void draw() { |
---|
124 | ignoreModified_ = true; |
---|
125 | if(Debug.DEBUG) System.out.println("PaneProxy: [" + ident_ + "] " + |
---|
126 | " draw(), batch=" + batch_); |
---|
127 | printer_ = false; |
---|
128 | Graphics g = pane_.getGraphics(); |
---|
129 | // |
---|
130 | // test existance of graphics context. |
---|
131 | // |
---|
132 | if(g == null) { |
---|
133 | ignoreModified_ = false; |
---|
134 | return; |
---|
135 | } |
---|
136 | if(firstDraw_) { |
---|
137 | ((AbstractPane)pane_).init(); |
---|
138 | firstDraw_ = false; |
---|
139 | } |
---|
140 | Graphics goff; |
---|
141 | Dimension isze = pane_.getSize(); |
---|
142 | if(offscreen_ == (Image) null) { |
---|
143 | offscreen_ = pane_.createImage(isze.width, isze.height); |
---|
144 | } else { |
---|
145 | if(isze.width != panesize_.width || isze.height != panesize_.height) { |
---|
146 | offscreen_ = pane_.createImage(isze.width, isze.height); |
---|
147 | } |
---|
148 | } |
---|
149 | panesize_ = isze; |
---|
150 | goff = offscreen_.getGraphics(); |
---|
151 | // super.paint(g); |
---|
152 | Rectangle clip = pane_.getBounds(); |
---|
153 | goff.setClip(0, 0, clip.width, clip.height); |
---|
154 | |
---|
155 | drawLayers(goff); |
---|
156 | g.drawImage(offscreen_, 0, 0, pane_); |
---|
157 | drawDraggableItems(g); |
---|
158 | modified_ = false; |
---|
159 | ignoreModified_ = false; |
---|
160 | pane_.repaint(); |
---|
161 | } |
---|
162 | |
---|
163 | void draw(Graphics g) { |
---|
164 | Rectangle clip = pane_.getBounds(); |
---|
165 | draw(g, clip.width, clip.height); |
---|
166 | } |
---|
167 | |
---|
168 | void draw(Graphics g, int width, int height) { |
---|
169 | ignoreModified_ = true; |
---|
170 | if(Debug.DEBUG) System.out.println("PaneProxy: [" + ident_ + "] draw(g), batch="+batch_); |
---|
171 | if(g instanceof PrintGraphics) { |
---|
172 | printer_ = true; |
---|
173 | pagesize_ = ((PrintGraphics)g).getPrintJob().getPageDimension(); |
---|
174 | } else { |
---|
175 | printer_ = false; |
---|
176 | pagesize_ = null; |
---|
177 | } |
---|
178 | g.setClip(0, 0, width, height); |
---|
179 | drawLayers(g); |
---|
180 | drawDraggableItems(g); |
---|
181 | modified_ = false; |
---|
182 | ignoreModified_ = false; |
---|
183 | } |
---|
184 | |
---|
185 | void drawPage(Graphics g, double width, double height) { |
---|
186 | ignoreModified_ = true; |
---|
187 | if(Debug.DEBUG) System.out.println("PaneProxy: [" + ident_ + "] drawPage(g), batch="+batch_); |
---|
188 | printer_ = true; |
---|
189 | |
---|
190 | Color saved = g.getColor(); |
---|
191 | g.setColor(pane_.getBackground()); |
---|
192 | Rectangle r = pane_.getBounds(); |
---|
193 | g.fillRect(r.x, r.y, r.width, r.height); |
---|
194 | g.setColor(saved); |
---|
195 | |
---|
196 | drawLayers(g); |
---|
197 | drawDraggableItems(g); |
---|
198 | modified_ = false; |
---|
199 | ignoreModified_ = false; |
---|
200 | printer_ = false; |
---|
201 | } |
---|
202 | void setOpaque(boolean opaque) { |
---|
203 | opaque_ = opaque; |
---|
204 | } |
---|
205 | boolean isOpaque() { |
---|
206 | return opaque_; |
---|
207 | } |
---|
208 | void drawLayers(Graphics g) { |
---|
209 | if(!printer_) { |
---|
210 | if(opaque_) { |
---|
211 | Rectangle r = pane_.getBounds(); |
---|
212 | g.setColor(pane_.getBackground()); |
---|
213 | g.fillRect(0, 0, r.width, r.height); |
---|
214 | } |
---|
215 | g.setColor(pane_.getForeground()); |
---|
216 | } |
---|
217 | // System.out.println("PaneProxy.drawLayers("+getId()+"): print = " + printer_ + ", pane = " + pane_.getBounds()); |
---|
218 | // g.drawRect(2,2,r.width-5,r.height-5); |
---|
219 | // |
---|
220 | // draw Layers |
---|
221 | // |
---|
222 | Component[] comps = pane_.getComponents(); |
---|
223 | for(int i=0; i < comps.length; i++) { |
---|
224 | try { |
---|
225 | if(comps[i] instanceof Layer) { |
---|
226 | ((Layer)comps[i]).draw(g); |
---|
227 | } else if(comps[i] instanceof LayerControl) { |
---|
228 | ((LayerControl)comps[i]).draw(g); |
---|
229 | } |
---|
230 | /* if(printer_ && comps[i] instanceof Panel) { |
---|
231 | ((Panel)comps[i]).paintBorder(g); |
---|
232 | } */ |
---|
233 | } catch (PaneNotFoundException e) {} |
---|
234 | } |
---|
235 | } |
---|
236 | void drawDraggableItems(Graphics g) { |
---|
237 | if(Debug.DEBUG) System.out.println("PaneProxy: [" + ident_ + |
---|
238 | "] drawDraggableItems, batch="+batch_); |
---|
239 | // |
---|
240 | // draw draggable items in each layer |
---|
241 | // |
---|
242 | Component[] comps = pane_.getComponents(); |
---|
243 | for(int i=0; i < comps.length; i++) { |
---|
244 | try { |
---|
245 | if(comps[i] instanceof LayerControl) { |
---|
246 | ((LayerControl)comps[i]).drawDraggableItems(g); |
---|
247 | } |
---|
248 | } catch (PaneNotFoundException e) {} |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | void paint(Graphics g) { |
---|
253 | if(Debug.DEBUG || Debug.DRAW_TRACE) |
---|
254 | System.out.println("PaneProxy: [" + ident_ + |
---|
255 | "] paint(g): "+g.getClipBounds()+ |
---|
256 | ", batch="+batch_+", modified="+modified_); |
---|
257 | Dimension isze = pane_.getSize(); |
---|
258 | if(isze.width != panesize_.width || isze.height != panesize_.height) offscreen_ = null; |
---|
259 | if(offscreen_ != null && !modified_) { |
---|
260 | g.drawImage(offscreen_, 0, 0, pane_); |
---|
261 | drawDraggableItems(g); |
---|
262 | } else { |
---|
263 | if(Debug.DRAW_TRACE) System.out.println("PaneProxy: [" + ident_ + |
---|
264 | "] paint: calling draw(), batch="+batch_); |
---|
265 | draw(); |
---|
266 | } |
---|
267 | modified_ = false; |
---|
268 | } |
---|
269 | |
---|
270 | String getId() { |
---|
271 | return ident_; |
---|
272 | } |
---|
273 | void setId(String id) { |
---|
274 | ident_ = id; |
---|
275 | } |
---|
276 | void setPageAlign(int vert, int horz) { |
---|
277 | valign_ = vert; |
---|
278 | halign_ = horz; |
---|
279 | } |
---|
280 | void setPageVAlign(int vert) { |
---|
281 | valign_ = vert; |
---|
282 | } |
---|
283 | void setPageHAlign(int horz) { |
---|
284 | halign_ = horz; |
---|
285 | } |
---|
286 | int getPageVAlign() { |
---|
287 | return valign_; |
---|
288 | } |
---|
289 | int getPageHAlign() { |
---|
290 | return halign_; |
---|
291 | } |
---|
292 | void setPageOrigin(Point p) { |
---|
293 | pageOrigin_ = p; |
---|
294 | } |
---|
295 | Point getPageOrigin() { |
---|
296 | return pageOrigin_; |
---|
297 | } |
---|
298 | boolean isPrinter() { |
---|
299 | return printer_; |
---|
300 | } |
---|
301 | Dimension getPageSize() { |
---|
302 | return pagesize_; |
---|
303 | } |
---|
304 | void setSize(Dimension d) { |
---|
305 | panesize_ = d; |
---|
306 | offscreen_ = null; |
---|
307 | } |
---|
308 | Layer getFirstLayer() { |
---|
309 | Component[] comps = pane_.getComponents(); |
---|
310 | for(int i=0; i < comps.length; i++) { |
---|
311 | if(comps[i] instanceof Layer) { |
---|
312 | return (Layer)comps[i]; |
---|
313 | } |
---|
314 | } |
---|
315 | return null; |
---|
316 | } |
---|
317 | Layer getLayer(String id) throws LayerNotFoundException { |
---|
318 | Component[] comps = pane_.getComponents(); |
---|
319 | for(int i=0; i < comps.length; i++) { |
---|
320 | if(comps[i] instanceof Layer) { |
---|
321 | if(((Layer)comps[i]).getId() == id) return (Layer)comps[i]; |
---|
322 | } else if(comps[i] instanceof Panel) { |
---|
323 | if(((Panel)comps[i]).hasLayer(id)) return (Layer)((Panel)comps[i]).getLayer(id); |
---|
324 | } |
---|
325 | } |
---|
326 | throw new LayerNotFoundException(); |
---|
327 | } |
---|
328 | |
---|
329 | Layer getLayerFromDataId(String id) throws LayerNotFoundException { |
---|
330 | Component[] comps = pane_.getComponents(); |
---|
331 | for(int i=0; i < comps.length; i++) { |
---|
332 | if(comps[i] instanceof Layer) { |
---|
333 | if(((Layer)comps[i]).isDataInLayer(id)) return (Layer)comps[i]; |
---|
334 | } else if(comps[i] instanceof Panel) { |
---|
335 | if(((Panel)comps[i]).isDataInPanel(id)) return (Layer)((Panel)comps[i]).getLayerFromDataId(id); |
---|
336 | } |
---|
337 | } |
---|
338 | throw new LayerNotFoundException(); |
---|
339 | } |
---|
340 | Object getSelectedObject() { |
---|
341 | return selectedobject_; |
---|
342 | } |
---|
343 | void setSelectedObject(Object obj) { |
---|
344 | old_selectedobject_ = selectedobject_; |
---|
345 | selectedobject_ = obj; |
---|
346 | } |
---|
347 | Rectangle getZoomBounds() { |
---|
348 | return zoom_rect_; |
---|
349 | } |
---|
350 | /** |
---|
351 | * @since 3.0 |
---|
352 | */ |
---|
353 | Point getZoomStart() { |
---|
354 | return zoom_start_; |
---|
355 | } |
---|
356 | |
---|
357 | Object getObjectAt(int x, int y) { |
---|
358 | Object obj = null; |
---|
359 | Component[] comps = pane_.getComponents(); |
---|
360 | if(comps.length != 0) { |
---|
361 | Layer ly; |
---|
362 | for(int i=0; i < comps.length; i++) { |
---|
363 | if(comps[i] instanceof Layer) { |
---|
364 | obj = ((Layer)comps[i]).getObjectAt(x, y, false); |
---|
365 | if(obj != null) return obj; |
---|
366 | } else if(comps[i] instanceof Panel) { |
---|
367 | obj = ((Panel)comps[i]).getObjectAt(x, y, false); |
---|
368 | if(obj != null) return obj; |
---|
369 | } |
---|
370 | } |
---|
371 | } |
---|
372 | return obj; |
---|
373 | } |
---|
374 | /** |
---|
375 | * @since 3.0 |
---|
376 | */ |
---|
377 | Object[] getObjectsAt(int x, int y) { |
---|
378 | Vector obList = new Vector(); |
---|
379 | Object obj = null; |
---|
380 | Component[] comps = pane_.getComponents(); |
---|
381 | if(comps.length != 0) { |
---|
382 | Layer ly; |
---|
383 | for(int i=0; i < comps.length; i++) { |
---|
384 | if(comps[i] instanceof Layer) { |
---|
385 | obj = ((Layer)comps[i]).getObjectAt(x, y, false); |
---|
386 | if(obj != null) obList.addElement(obj); |
---|
387 | } else if(comps[i] instanceof Panel) { |
---|
388 | obj = ((Panel)comps[i]).getObjectAt(x, y, false); |
---|
389 | if(obj != null) obList.addElement(obj); |
---|
390 | } |
---|
391 | } |
---|
392 | } |
---|
393 | return obList.toArray(); |
---|
394 | } |
---|
395 | |
---|
396 | boolean processMouseEvent(MouseEvent event) { |
---|
397 | boolean event_handled = false; |
---|
398 | if(!mouseEventsEnabled_) return event_handled; |
---|
399 | if (event.getID() == MouseEvent.MOUSE_CLICKED) { |
---|
400 | event_handled = Pane_MouseClicked(event); |
---|
401 | } else if (event.getID() == MouseEvent.MOUSE_PRESSED) { |
---|
402 | event_handled = Pane_MouseDown(event); |
---|
403 | } else if (event.getID() == MouseEvent.MOUSE_RELEASED) { |
---|
404 | event_handled = Pane_MouseUp(event); |
---|
405 | } |
---|
406 | return event_handled; |
---|
407 | } |
---|
408 | |
---|
409 | boolean processMouseMotionEvent(MouseEvent event) { |
---|
410 | boolean event_handled = false; |
---|
411 | if(!mouseEventsEnabled_) return event_handled; |
---|
412 | if (event.getID() == MouseEvent.MOUSE_DRAGGED) { |
---|
413 | event_handled = Pane_MouseDrag(event); |
---|
414 | } else if(event.getID() == MouseEvent.MOUSE_MOVED) { |
---|
415 | event_handled = Pane_MouseMoved(event); |
---|
416 | } |
---|
417 | return event_handled; |
---|
418 | } |
---|
419 | |
---|
420 | private boolean Pane_MouseClicked(MouseEvent event) { |
---|
421 | Object obj; |
---|
422 | Rectangle rect; |
---|
423 | Selectable savedobj = null; |
---|
424 | int mod = event.getModifiers(); |
---|
425 | // |
---|
426 | // button 1 must be clicked. Modifiers are now allowed! |
---|
427 | // |
---|
428 | // if(!(((mod & MouseEvent.BUTTON1_MASK) != 0) && |
---|
429 | // ((mod - MouseEvent.BUTTON1_MASK) == 0))) return false; |
---|
430 | if(!((mod & MouseEvent.BUTTON1_MASK) != 0)) return false; |
---|
431 | in_zoom_ = false; |
---|
432 | in_select_ = false; |
---|
433 | // if(in_move_ && !moved_ && selectedobject_ instanceof Selectable) { |
---|
434 | if(!moved_ && selectedobject_ instanceof Selectable) { |
---|
435 | if(Debug.DEBUG) System.out.println("MouseClicked (in_move_ && !moved_ && Selectable)"); |
---|
436 | // |
---|
437 | // second click of selected object de-select |
---|
438 | // |
---|
439 | if(((Selectable)selectedobject_).isSelected()) { |
---|
440 | if(Debug.DEBUG) System.out.println("MouseClicked (isSelected)"); |
---|
441 | savedobj = (Selectable)selectedobject_; |
---|
442 | // |
---|
443 | // remove box |
---|
444 | // |
---|
445 | Graphics g = pane_.getGraphics(); |
---|
446 | if(moveable_) { |
---|
447 | g.setColor(Color.red); |
---|
448 | } else { |
---|
449 | g.setColor(Color.blue); |
---|
450 | } |
---|
451 | g.setXORMode(pane_.getBackground()); |
---|
452 | g.drawRect(selectedRect_.x, selectedRect_.y, |
---|
453 | selectedRect_.width, selectedRect_.height); |
---|
454 | g.setPaintMode(); |
---|
455 | savedobj.setSelected(false); |
---|
456 | pane_.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
---|
457 | return false; |
---|
458 | } |
---|
459 | } |
---|
460 | // old_selectedobject_ = selectedobject_; |
---|
461 | selectedobject_ = null; |
---|
462 | Component[] comps = pane_.getComponents(); |
---|
463 | if(comps.length != 0) { |
---|
464 | Layer ly; |
---|
465 | for(int i=0; i < comps.length; i++) { |
---|
466 | if(comps[i] instanceof Layer) { |
---|
467 | obj = ((Layer)comps[i]).getObjectAt(event.getX(), event.getY()); |
---|
468 | if(obj != null) { |
---|
469 | selectedobject_ = obj; |
---|
470 | break; |
---|
471 | } |
---|
472 | } else if(comps[i] instanceof Panel) { |
---|
473 | obj = ((Panel)comps[i]).getObjectAt(event.getX(), event.getY(), false); |
---|
474 | if(obj != null) { |
---|
475 | selectedobject_ = obj; |
---|
476 | break; |
---|
477 | } |
---|
478 | } |
---|
479 | } |
---|
480 | if(selectedobject_ instanceof Selectable) { |
---|
481 | if(Debug.DEBUG) System.out.println("MouseClicked (new Selectable)"); |
---|
482 | if(!selectedobject_.equals(savedobj) && |
---|
483 | ((Selectable)selectedobject_).isSelectable()) { |
---|
484 | if(Debug.DEBUG) System.out.println("MouseClicked (new isSelectable)"); |
---|
485 | ((Selectable)selectedobject_).setSelected(true); |
---|
486 | // |
---|
487 | // mouseclick on selectable object select object |
---|
488 | // |
---|
489 | in_select_ = true; |
---|
490 | if(!in_move_) { |
---|
491 | if(Debug.DEBUG) System.out.println("MouseClicked (new !in_move_)"); |
---|
492 | // |
---|
493 | // if in_move_ = true, already have drawn box |
---|
494 | // |
---|
495 | // |
---|
496 | // draw initial box |
---|
497 | // |
---|
498 | selectedRect_ = new Rectangle(((Selectable)selectedobject_).getBounds()); |
---|
499 | Graphics g = pane_.getGraphics(); |
---|
500 | if(moveable_) { |
---|
501 | g.setColor(Color.red); |
---|
502 | } else { |
---|
503 | g.setColor(Color.blue); |
---|
504 | } |
---|
505 | g.setXORMode(pane_.getBackground()); |
---|
506 | g.drawRect(selectedRect_.x, selectedRect_.y, |
---|
507 | selectedRect_.width, selectedRect_.height); |
---|
508 | g.setPaintMode(); |
---|
509 | } |
---|
510 | in_move_ = false; |
---|
511 | changes_.firePropertyChange("objectSelected", |
---|
512 | old_selectedobject_, |
---|
513 | selectedobject_); |
---|
514 | } |
---|
515 | } else if(selectedobject_ != null) { |
---|
516 | // |
---|
517 | // selectedobject is in a Key |
---|
518 | // |
---|
519 | changes_.firePropertyChange("objectSelected", |
---|
520 | old_selectedobject_, |
---|
521 | selectedobject_); |
---|
522 | } |
---|
523 | } |
---|
524 | return false; // pass event to next level |
---|
525 | } |
---|
526 | private boolean Pane_MouseMoved(MouseEvent event) { |
---|
527 | if(in_select_) { |
---|
528 | if(selectedRect_.contains(event.getX(), event.getY())) { |
---|
529 | pane_.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); |
---|
530 | } else { |
---|
531 | pane_.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
---|
532 | } |
---|
533 | return true; |
---|
534 | } else { |
---|
535 | pane_.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
---|
536 | } |
---|
537 | return false; |
---|
538 | } |
---|
539 | private boolean Pane_MouseDown(MouseEvent event) { |
---|
540 | Object obj; |
---|
541 | Selectable savedobj = null; |
---|
542 | // |
---|
543 | // continue only if button1 is pressed |
---|
544 | // |
---|
545 | if((event.getModifiers() & InputEvent.BUTTON1_MASK) == 0) return false; |
---|
546 | // |
---|
547 | // reset logicals |
---|
548 | // |
---|
549 | |
---|
550 | old_zoom_rect_ = new Rectangle(zoom_rect_); |
---|
551 | |
---|
552 | zoom_rect_.x = 0; |
---|
553 | zoom_rect_.y = 0; |
---|
554 | zoom_rect_.width = 0; |
---|
555 | zoom_rect_.height = 0; |
---|
556 | |
---|
557 | if(event.isShiftDown()) { |
---|
558 | // |
---|
559 | // shift mousedown start zoom |
---|
560 | // |
---|
561 | in_zoom_ = true; |
---|
562 | zoom_start_.x = event.getX(); |
---|
563 | zoom_start_.y = event.getY(); |
---|
564 | zoom_rect_.x = event.getX(); |
---|
565 | zoom_rect_.y = event.getY(); |
---|
566 | zoom_rect_.width = 1; |
---|
567 | zoom_rect_.height = 1; |
---|
568 | Graphics g = pane_.getGraphics(); |
---|
569 | g.setColor(pane_.getForeground()); |
---|
570 | g.setXORMode(pane_.getBackground()); |
---|
571 | g.drawRect(zoom_rect_.x, zoom_rect_.y, |
---|
572 | zoom_rect_.width, zoom_rect_.height); |
---|
573 | g.setPaintMode(); |
---|
574 | return true; // handled event here! |
---|
575 | } else if(event.isControlDown()) { |
---|
576 | // |
---|
577 | // control mousedown ends zoom |
---|
578 | // |
---|
579 | return false; // pass event to next level |
---|
580 | } else if(in_select_ && moveable_ && |
---|
581 | selectedRect_.contains(event.getX(), event.getY())) { |
---|
582 | if(Debug.DEBUG) System.out.println("MouseDown (in_select_ && in Rect)"); |
---|
583 | // |
---|
584 | // object selected start move operation |
---|
585 | // |
---|
586 | in_move_ = true; |
---|
587 | moved_ = false; |
---|
588 | move_ref_ = new Point(event.getX(), event.getY()); |
---|
589 | return true; |
---|
590 | } else if(in_select_ && !moveable_ && |
---|
591 | selectedRect_.contains(event.getX(), event.getY())) { |
---|
592 | in_move_ = false; |
---|
593 | moved_ = false; |
---|
594 | return true; |
---|
595 | } else { |
---|
596 | // |
---|
597 | // object not selected begin move operation |
---|
598 | // |
---|
599 | if(selectedobject_ instanceof Selectable) |
---|
600 | savedobj = (Selectable)selectedobject_; |
---|
601 | else |
---|
602 | savedobj = null; |
---|
603 | selectedobject_ = null; |
---|
604 | Component[] comps = pane_.getComponents(); |
---|
605 | if(comps.length != 0) { |
---|
606 | Layer ly; |
---|
607 | for(int i=0; i < comps.length; i++) { |
---|
608 | if(comps[i] instanceof Layer) { |
---|
609 | obj = ((Layer)comps[i]).getObjectAt(event.getX(), event.getY()); |
---|
610 | if(obj != null) { |
---|
611 | selectedobject_ = obj; |
---|
612 | break; |
---|
613 | } |
---|
614 | } else if(comps[i] instanceof Panel) { |
---|
615 | obj = ((Panel)comps[i]).getObjectAt(event.getX(), event.getY(), false); |
---|
616 | if(obj != null) { |
---|
617 | selectedobject_ = obj; |
---|
618 | break; |
---|
619 | } |
---|
620 | } |
---|
621 | } |
---|
622 | if(selectedobject_ instanceof Selectable) { |
---|
623 | if(Debug.DEBUG) System.out.println("MouseDown (Selectable)"); |
---|
624 | // |
---|
625 | // found selectable object begin single operation move |
---|
626 | // |
---|
627 | if(((Selectable)selectedobject_).isSelectable()) { |
---|
628 | if(Debug.DEBUG) System.out.println("MouseDown (isSelectable)"); |
---|
629 | draggable_ = selectedobject_ instanceof Draggable; |
---|
630 | if(selectedobject_ instanceof Moveable) { |
---|
631 | moveable_ = ((Moveable)selectedobject_).isMoveable(); |
---|
632 | } else { |
---|
633 | moveable_ = false; |
---|
634 | } |
---|
635 | if(moveable_ || draggable_) { |
---|
636 | in_move_ = true; |
---|
637 | } else { |
---|
638 | in_move_ = false; |
---|
639 | } |
---|
640 | moved_ = false; |
---|
641 | if(Debug.DEBUG) System.out.println("MouseDown (isDraggable) " + draggable_); |
---|
642 | ((Selectable)selectedobject_).setSelected(false); |
---|
643 | selectedRect_ = new Rectangle(((Selectable)selectedobject_).getBounds()); |
---|
644 | if(!draggable_ && moveable_) { |
---|
645 | // |
---|
646 | // draw initial box |
---|
647 | // |
---|
648 | Graphics g = pane_.getGraphics(); |
---|
649 | g.setColor(Color.red); |
---|
650 | g.setXORMode(pane_.getBackground()); |
---|
651 | g.drawRect(selectedRect_.x, selectedRect_.y, |
---|
652 | selectedRect_.width, selectedRect_.height); |
---|
653 | g.setPaintMode(); |
---|
654 | } |
---|
655 | if(!draggable_) pane_.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); |
---|
656 | move_ref_ = new Point(event.getX(), event.getY()); |
---|
657 | return true; |
---|
658 | } |
---|
659 | } |
---|
660 | } |
---|
661 | // |
---|
662 | // mousedown start zoom if object not found |
---|
663 | // |
---|
664 | in_zoom_ = true; |
---|
665 | zoom_start_.x = event.getX(); |
---|
666 | zoom_start_.y = event.getY(); |
---|
667 | zoom_rect_.x = event.getX(); |
---|
668 | zoom_rect_.y = event.getY(); |
---|
669 | zoom_rect_.width = 1; |
---|
670 | zoom_rect_.height = 1; |
---|
671 | Graphics g = pane_.getGraphics(); |
---|
672 | g.setColor(pane_.getForeground()); |
---|
673 | g.setXORMode(pane_.getBackground()); |
---|
674 | g.drawRect(zoom_rect_.x, zoom_rect_.y, |
---|
675 | zoom_rect_.width, zoom_rect_.height); |
---|
676 | g.setPaintMode(); |
---|
677 | return true; // handled event here! |
---|
678 | } |
---|
679 | // return false; |
---|
680 | } |
---|
681 | private boolean Pane_MouseDrag(MouseEvent event) { |
---|
682 | boolean handled = false; |
---|
683 | // |
---|
684 | // continue only if button1 is pressed |
---|
685 | // |
---|
686 | // if((event.getModifiers() & InputEvent.BUTTON1_MASK) == 0) return handled; |
---|
687 | |
---|
688 | if(in_zoom_) { |
---|
689 | handled = true; |
---|
690 | Graphics g = pane_.getGraphics(); |
---|
691 | g.setColor(pane_.getForeground()); |
---|
692 | g.setXORMode(pane_.getBackground()); |
---|
693 | g.drawRect(zoom_rect_.x, zoom_rect_.y, |
---|
694 | zoom_rect_.width, zoom_rect_.height); |
---|
695 | zoom_rect_.width = event.getX() - zoom_start_.x; |
---|
696 | if(zoom_rect_.width < 0) { |
---|
697 | zoom_rect_.x = event.getX(); |
---|
698 | zoom_rect_.width = Math.abs(zoom_rect_.width); |
---|
699 | } else { |
---|
700 | zoom_rect_.x = zoom_start_.x; |
---|
701 | } |
---|
702 | zoom_rect_.height = event.getY() - zoom_start_.y; |
---|
703 | if(zoom_rect_.height < 0) { |
---|
704 | zoom_rect_.y = event.getY(); |
---|
705 | zoom_rect_.height = Math.abs(zoom_rect_.height); |
---|
706 | } else { |
---|
707 | zoom_rect_.y = zoom_start_.y; |
---|
708 | } |
---|
709 | g.drawRect(zoom_rect_.x, zoom_rect_.y, |
---|
710 | zoom_rect_.width, zoom_rect_.height); |
---|
711 | g.setPaintMode(); |
---|
712 | } else if(in_move_) { |
---|
713 | handled = true; |
---|
714 | moved_ = true; |
---|
715 | Graphics g = pane_.getGraphics(); |
---|
716 | if(!draggable_ && moveable_) { |
---|
717 | g.setColor(Color.red); |
---|
718 | g.setXORMode(pane_.getBackground()); |
---|
719 | g.drawRect(selectedRect_.x, selectedRect_.y, |
---|
720 | selectedRect_.width, selectedRect_.height); |
---|
721 | } else if(draggable_){ |
---|
722 | ((LayerChild)selectedobject_).setVisible(false); |
---|
723 | Rectangle rect = ((LayerChild)selectedobject_).getBounds(); |
---|
724 | pane_.repaint(rect.x - 1, rect.y - 1, |
---|
725 | rect.width + 2, rect.height + 2); |
---|
726 | } |
---|
727 | selectedRect_.x += event.getX() - move_ref_.x; |
---|
728 | selectedRect_.y += event.getY() - move_ref_.y; |
---|
729 | if(!draggable_ && moveable_) { |
---|
730 | g.drawRect(selectedRect_.x, selectedRect_.y, |
---|
731 | selectedRect_.width, selectedRect_.height); |
---|
732 | g.setPaintMode(); |
---|
733 | } else if(draggable_) { |
---|
734 | try { |
---|
735 | ((LayerChild)selectedobject_).setVisible(true); |
---|
736 | ((Draggable)selectedobject_).setLocation(new |
---|
737 | Point(selectedRect_.x, selectedRect_.y), false); |
---|
738 | // ((Draggable)selectedobject_).setLocationNoVeto(selectedRect_.x, |
---|
739 | // selectedRect_.y); |
---|
740 | ((LayerChild)selectedobject_).draw(g); |
---|
741 | } catch (LayerNotFoundException e) {} |
---|
742 | } |
---|
743 | move_ref_ = new Point(event.getX(), event.getY()); |
---|
744 | } |
---|
745 | return handled; |
---|
746 | } |
---|
747 | private boolean Pane_MouseUp(MouseEvent event) { |
---|
748 | // |
---|
749 | // continue only if button1 is pressed |
---|
750 | // |
---|
751 | if((event.getModifiers() & InputEvent.BUTTON1_MASK) == 0) return false; |
---|
752 | |
---|
753 | if(in_zoom_) { |
---|
754 | // |
---|
755 | // finish zoom |
---|
756 | // |
---|
757 | in_zoom_ = false; |
---|
758 | Graphics g = pane_.getGraphics(); |
---|
759 | g.setColor(pane_.getForeground()); |
---|
760 | g.setXORMode(pane_.getBackground()); |
---|
761 | g.drawRect(zoom_rect_.x, zoom_rect_.y, |
---|
762 | zoom_rect_.width, zoom_rect_.height); |
---|
763 | changes_.firePropertyChange("zoomRectangle", old_zoom_rect_, zoom_rect_); |
---|
764 | // if(!event.isShiftDown()) return true; // abort zoom! |
---|
765 | return false; // pass event to next level |
---|
766 | } else if(in_move_ && moved_) { |
---|
767 | if(Debug.DEBUG) System.out.println("MouseUp (in_move_ && moved_)"); |
---|
768 | // |
---|
769 | // finish move |
---|
770 | // |
---|
771 | Graphics g = pane_.getGraphics(); |
---|
772 | if(!draggable_ && moveable_) { |
---|
773 | g.setColor(Color.red); |
---|
774 | g.setXORMode(pane_.getBackground()); |
---|
775 | g.drawRect(selectedRect_.x, selectedRect_.y, |
---|
776 | selectedRect_.width, selectedRect_.height); |
---|
777 | g.setPaintMode(); |
---|
778 | } |
---|
779 | // redraw, but should this only be if actually moved? |
---|
780 | if(Debug.DRAW_TRACE)System.out.println("PaneProxy: Pane_MouseUp: calling draw(), batch="+batch_); |
---|
781 | Point loc = new Point(selectedRect_.x, selectedRect_.y); |
---|
782 | if(draggable_) { |
---|
783 | ((Draggable)selectedobject_).setLocation(loc); |
---|
784 | paint(g); |
---|
785 | } else if(moveable_){ |
---|
786 | ((Moveable)selectedobject_).setLocation(loc); |
---|
787 | //draw(); |
---|
788 | modified_ = true; |
---|
789 | pane_.repaint(); |
---|
790 | } |
---|
791 | in_move_ = false; |
---|
792 | in_select_ = false; |
---|
793 | moved_ = false; |
---|
794 | draggable_ = false; |
---|
795 | moveable_ = false; |
---|
796 | ((Selectable)selectedobject_).setSelected(false); |
---|
797 | pane_.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
---|
798 | return true; |
---|
799 | } |
---|
800 | return false; |
---|
801 | } |
---|
802 | Dimension getMaximumSize() { |
---|
803 | return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); |
---|
804 | } |
---|
805 | /** |
---|
806 | * Returns the minimum size of this <code>Pane</code>. |
---|
807 | * |
---|
808 | * @return minimum size |
---|
809 | */ |
---|
810 | Dimension getMinimumSize() { |
---|
811 | return new Dimension(panesize_.width, panesize_.height); |
---|
812 | } |
---|
813 | /** |
---|
814 | * Returns the preferred size of this <code>Pane</code>. |
---|
815 | * |
---|
816 | * @return preferred size |
---|
817 | */ |
---|
818 | Dimension getPreferredSize() { |
---|
819 | return new Dimension(panesize_.width, panesize_.height); |
---|
820 | } |
---|
821 | /** |
---|
822 | * Get a <code>String</code> representatinof the |
---|
823 | * <code>Pane</code>. |
---|
824 | * |
---|
825 | * @return <code>String</code> representation |
---|
826 | */ |
---|
827 | public String toString() { |
---|
828 | String name = pane_.getClass().getName(); |
---|
829 | return name.substring(name.lastIndexOf(".")+1) + ": " + ident_; |
---|
830 | } |
---|
831 | void setBatch(boolean batch, String msg) { |
---|
832 | batch_ = batch; |
---|
833 | if(Debug.EVENT) System.out.println("PaneProxy: [" + ident_ + |
---|
834 | "] setBatch("+batch_+"): "+msg); |
---|
835 | if(!batch_ && modified_) { |
---|
836 | /* |
---|
837 | * batching turned off and environment has been modified |
---|
838 | * re-draw pane() |
---|
839 | */ |
---|
840 | if(Debug.EVENT) |
---|
841 | System.out.println("PaneProxy: [" + ident_ + |
---|
842 | "] modified=true, draw(): batch off"); |
---|
843 | //draw(); |
---|
844 | if(Debug.DRAW_TRACE)System.out.println("PaneProxy: [" + ident_ + |
---|
845 | "] setBatch: calling repaint(), batch="+batch_ |
---|
846 | +", modified="+modified_); |
---|
847 | pane_.repaint(); |
---|
848 | // modified_ = false; |
---|
849 | } |
---|
850 | } |
---|
851 | boolean isBatch() { |
---|
852 | return batch_; |
---|
853 | } |
---|
854 | void setIgnoreModified(boolean ig) { |
---|
855 | ignoreModified_ = ig; |
---|
856 | } |
---|
857 | void clearModified() { |
---|
858 | modified_ = false; |
---|
859 | } |
---|
860 | void setModified(boolean mod, String mess) { |
---|
861 | if(ignoreModified_) return; |
---|
862 | modified_ = mod; |
---|
863 | if(Debug.EVENT && batch_) { |
---|
864 | System.out.println("PaneProxy: [" + ident_ + |
---|
865 | "] setModified("+modified_+"), Batch on: " + mess); |
---|
866 | } |
---|
867 | if(modified_ && !batch_) { |
---|
868 | if(Debug.EVENT) |
---|
869 | System.out.println("PaneProxy: [" + ident_ + |
---|
870 | "] setModified("+modified_+"), Batch off: " + mess); |
---|
871 | //draw(); |
---|
872 | if(Debug.DRAW_TRACE)System.out.println("PaneProxy.setModified: calling repaint(), batch="+batch_ |
---|
873 | + ", modified="+modified_); |
---|
874 | pane_.repaint(); |
---|
875 | } |
---|
876 | } |
---|
877 | boolean isModified() { |
---|
878 | return modified_; |
---|
879 | } |
---|
880 | /** |
---|
881 | * @since 3.0 |
---|
882 | */ |
---|
883 | void setMouseEventsEnabled(boolean enable) { |
---|
884 | mouseEventsEnabled_ = enable; |
---|
885 | } |
---|
886 | /** |
---|
887 | * @since 3.0 |
---|
888 | */ |
---|
889 | boolean isMouseEventsEnabled() { |
---|
890 | return mouseEventsEnabled_; |
---|
891 | } |
---|
892 | /** |
---|
893 | * @since 3.0 |
---|
894 | */ |
---|
895 | void setPageScaleMode(int mode) { |
---|
896 | printMode_ = mode; |
---|
897 | } |
---|
898 | /** |
---|
899 | * @since 3.0 |
---|
900 | */ |
---|
901 | int getPageScaleMode() { |
---|
902 | return printMode_; |
---|
903 | } |
---|
904 | /* |
---|
905 | * Pane PropertyChange methods |
---|
906 | */ |
---|
907 | public void addPropertyChangeListener(PropertyChangeListener l) { |
---|
908 | changes_.addPropertyChangeListener(l); |
---|
909 | } |
---|
910 | public void removePropertyChangeListener(PropertyChangeListener l) { |
---|
911 | changes_.removePropertyChangeListener(l); |
---|
912 | } |
---|
913 | } |
---|