001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: StructurePanel.java 3669 2007-02-26 13:51:23Z gbevin $
007: */
008: package com.uwyn.rife.gui.old;
009:
010: import java.awt.*;
011: import java.awt.event.*;
012: import java.awt.geom.GeneralPath;
013: import java.awt.geom.Rectangle2D;
014: import java.util.ArrayList;
015: import java.util.Collection;
016:
017: import javax.swing.JLayeredPane;
018: import javax.swing.JPanel;
019: import javax.swing.JScrollPane;
020: import javax.swing.SwingUtilities;
021: import javax.swing.event.PopupMenuEvent;
022: import javax.swing.event.PopupMenuListener;
023:
024: import com.uwyn.rife.config.Config;
025: import com.uwyn.rife.swing.Cursors;
026:
027: public class StructurePanel extends JLayeredPane implements
028: ElementListener, MouseListener, MouseMotionListener,
029: KeyListener, PopupMenuListener {
030: private static final float SCALE_FACTOR_UPPER_LIMIT = 4f;
031: private static final float SCALE_FACTOR_LOWER_LIMIT = 0.125f;
032:
033: public static final int SELECTION_TOOL = 0;
034: public static final int ZOOMIN_TOOL = 1;
035: public static final int ZOOMOUT_TOOL = 2;
036: public static final int ELEMENT_TOOL = 3;
037: public static final int CONNECTOR_TOOL = 4;
038:
039: private float mScaleFactor = 1f;
040:
041: private JScrollPane mScrollPane = null;
042:
043: private int mActiveTool = SELECTION_TOOL;
044:
045: private int mWidth = 0;
046: private int mHeight = 0;
047:
048: private ElementStyle mElementStyleOrig = new ElementStyle(1f);
049: private ElementStyle mElementStyleScaled = new ElementStyle(
050: mScaleFactor);
051:
052: private ArrayList<Element> mElements = new ArrayList<Element>();
053: private ArrayList<Element> mSelectedElements = new ArrayList<Element>();
054: private ElementPropertyEditor mElementPropertyEditor = null;
055: private ElementProperty mHighlightedProperty = null;
056: private ElementProperty mHighlightedPropertyHidden = null;
057:
058: private Point mDragElementStartPoint = null;
059: private Element mDragElement = null;
060: private Element mDragElementLeftMost = null;
061: private Element mDragElementTopMost = null;
062:
063: private Point mSelectionRectangleStartPoint = null;
064: private GeneralPath mSelectionRectanglePath = null;
065: private Point mSelectionRectangleCurrentPoint = null;
066: private Stroke mSelectionRectangleDashedStroke = new BasicStroke(1,
067: BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 0f,
068: new float[] { 3f }, 0f);
069:
070: private boolean mScrollActive = false;
071:
072: private boolean mPopupMenuActive = false;
073:
074: public StructurePanel(JScrollPane scrollPane) {
075: setDoubleBuffered(true);
076: setScrollPane(scrollPane);
077: setOpaque(true);
078: setBackground(Color.white);
079: setLayout(null);
080: addMouseListener(this );
081: addMouseMotionListener(this );
082:
083: calculateDimension();
084: }
085:
086: public void setScrollPane(JScrollPane scrollPane) {
087: mScrollPane = scrollPane;
088: }
089:
090: public JScrollPane getScrollPane() {
091: return mScrollPane;
092: }
093:
094: public Collection<Element> getElements() {
095: return mElements;
096: }
097:
098: public int getCalculatedWidth() {
099: return mWidth;
100: }
101:
102: public int getCalculatedHeight() {
103: return mHeight;
104: }
105:
106: public Stroke getSelectionRectangleStroke() {
107: return mSelectionRectangleDashedStroke;
108: }
109:
110: public void paintComponent(Graphics g) {
111: super .paintComponent(g);
112: if (Config.getRepInstance().getBool("GRID_SHOW")) {
113: Graphics2D g2d = (Graphics2D) g;
114: g2d.setColor(Color.gray);
115: Rectangle clip_bounds = g2d.getClipBounds();
116: int grid_size = Config.getRepInstance().getInt("GRID_SIZE");
117: double grid_size_scaled = grid_size * mScaleFactor;
118: if (grid_size_scaled > 0) {
119: while (grid_size_scaled < 5) {
120: grid_size_scaled = grid_size_scaled * 2;
121: }
122: double offset_x = clip_bounds.x
123: - (clip_bounds.x % grid_size_scaled);
124: double offset_y = clip_bounds.y
125: - (clip_bounds.y % grid_size_scaled);
126: double new_clip_width = clip_bounds.width
127: + (clip_bounds.x % grid_size_scaled);
128: double new_clip_height = clip_bounds.height
129: + (clip_bounds.y % grid_size_scaled);
130: int real_x = 0;
131: int real_y = 0;
132: for (double x = 0; x <= new_clip_width; x += grid_size_scaled) {
133: for (double y = 0; y <= new_clip_height; y += grid_size_scaled) {
134: real_x = (int) (offset_x + x);
135: real_y = (int) (offset_y + y);
136: g2d.drawLine(real_x, real_y, real_x, real_y);
137: }
138: }
139: }
140: }
141: }
142:
143: private void addElementMouseListeners() {
144: for (Element element : mElements) {
145: element.addMouseListener(element);
146: element.addMouseMotionListener(element);
147: }
148: }
149:
150: private void removeElementMouseListeners() {
151: for (Element element : mElements) {
152: element.removeMouseListener(element);
153: element.removeMouseMotionListener(element);
154: }
155: }
156:
157: public void setActiveTool(int tool) {
158: mActiveTool = tool;
159: switch (mActiveTool) {
160: case SELECTION_TOOL:
161: setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
162: addElementMouseListeners();
163: break;
164: case ZOOMIN_TOOL:
165: setCursor(Cursors.getRepInstance().getCursor("zoomin"));
166: removeElementMouseListeners();
167: break;
168: case ZOOMOUT_TOOL:
169: setCursor(Cursors.getRepInstance().getCursor("zoomout"));
170: removeElementMouseListeners();
171: break;
172: case ELEMENT_TOOL:
173: setCursor(Cursors.getRepInstance().getCursor("element"));
174: removeElementMouseListeners();
175: break;
176: case CONNECTOR_TOOL:
177: setCursor(Cursors.getRepInstance().getCursor("connector"));
178: removeElementMouseListeners();
179: break;
180: }
181: }
182:
183: public int getActiveTool() {
184: return mActiveTool;
185: }
186:
187: public void setScaleFactor(float scaleFactor) {
188: if (scaleFactor > SCALE_FACTOR_UPPER_LIMIT) {
189: scaleFactor = SCALE_FACTOR_UPPER_LIMIT;
190: }
191: if (scaleFactor < SCALE_FACTOR_LOWER_LIMIT) {
192: scaleFactor = SCALE_FACTOR_LOWER_LIMIT;
193: }
194: mScaleFactor = scaleFactor;
195: }
196:
197: public float getScaleFactor() {
198: return mScaleFactor;
199: }
200:
201: public void addElement(String name) {
202: Element element = new Element(this , name, mElementStyleOrig,
203: mElementStyleScaled);
204: if (mActiveTool == SELECTION_TOOL) {
205: element.addMouseListener(element);
206: element.addMouseMotionListener(element);
207: }
208:
209: int exits = (int) (Math.random() * 6);
210: int consumeds = (int) (Math.random() * 6);
211: // int useds = (int)(Math.random()*6);
212: int addeds = (int) (Math.random() * 6);
213: for (int i = 0; i < exits; i++) {
214: element.addExit("exit" + i);
215: }
216: for (int i = 0; i < consumeds; i++) {
217: element.addConsumedParameter("input" + i);
218: }
219: // for(int i=0; i < useds; i++)
220: // {
221: // element.addUsedParameter("used"+i);
222: // }
223: for (int i = 0; i < addeds; i++) {
224: element.addAddedParameter("output" + i);
225: }
226: Color body_color = new Color(155 + (int) (Math.random() * 100),
227: 155 + (int) (Math.random() * 100), 155 + (int) (Math
228: .random() * 100));
229: element.setElementColor(body_color);
230: element.addElementListener(this );
231: mElements.add(element);
232:
233: this .add(element);
234: element.setBounds((int) (Math.random() * 800), (int) (Math
235: .random() * 600), element.getWidth(), element
236: .getHeight());
237:
238: calculateDimension();
239: }
240:
241: public void removeElement(Element element) {
242: int index = mElements.indexOf(element);
243: if (index != -1) {
244: mElements.remove(index);
245: }
246: index = mSelectedElements.indexOf(element);
247: if (index != -1) {
248: mSelectedElements.remove(index);
249: }
250: remove(element);
251: calculateDimension();
252: mScrollPane.revalidate();
253: repaint();
254: }
255:
256: void editElementProperty(ElementProperty property, Point location) {
257: mElementPropertyEditor = new ElementPropertyEditor(property,
258: location);
259: elementPropertyHighlighted(property);
260: }
261:
262: void removeElementPropertyEditor() {
263: if (mElementPropertyEditor != null) {
264: mElementPropertyEditor.destroy();
265: mElementPropertyEditor = null;
266: synchronizeHighlightedProperty();
267: }
268: }
269:
270: boolean isElementPropertyBeingEdited() {
271: if (mElementPropertyEditor == null) {
272: return false;
273: } else {
274: return true;
275: }
276: }
277:
278: private void calculateDimension() {
279: mWidth = 0;
280: mHeight = 0;
281:
282: int tmp_width = 0;
283: for (Element element : mElements) {
284: tmp_width = element.getWidth() + element.getX();
285: if (tmp_width > mWidth) {
286: mWidth = tmp_width;
287: }
288: }
289:
290: int tmp_height = 0;
291: for (Element element : mElements) {
292: tmp_height = element.getHeight() + element.getY();
293: if (tmp_height > mHeight) {
294: mHeight = tmp_height;
295: }
296: }
297:
298: if (mWidth == 0 || mHeight == 0) {
299: mWidth = 800;
300: mHeight = 600;
301: }
302: }
303:
304: public Dimension getMinimumSize() {
305: return new Dimension(mWidth, mHeight);
306: }
307:
308: public Dimension getPreferredSize() {
309: return new Dimension(mWidth, mHeight);
310: }
311:
312: private void deselectOtherElements(Element selected_element) {
313: for (Element element : mSelectedElements) {
314: if (element != selected_element) {
315: element.deselectElement();
316: }
317: }
318:
319: mSelectedElements = new ArrayList<Element>();
320: }
321:
322: public void elementRepositioned(Element element) {
323: calculateDimension();
324: mScrollPane.revalidate();
325: }
326:
327: public void elementRaised(Element element) {
328: if (mActiveTool == SELECTION_TOOL) {
329: this .moveToFront(element);
330: }
331: }
332:
333: public void elementSelected(Element selectedElement, int modifiers) {
334: removeElementPropertyEditor();
335:
336: if (mActiveTool == SELECTION_TOOL) {
337: if ((modifiers & MouseEvent.SHIFT_MASK) == 0) {
338: deselectOtherElements(selectedElement);
339: selectedElement.fireElementRaised();
340: }
341:
342: selectedElement.selectElement();
343: mSelectedElements.add(selectedElement);
344:
345: this .repaint();
346: }
347: }
348:
349: public void elementDeselected(Element deselectedElement,
350: int modifiers) {
351: removeElementPropertyEditor();
352:
353: if ((modifiers & MouseEvent.SHIFT_MASK) != 0
354: && mSelectedElements.size() > 1) {
355: mSelectedElements.remove(deselectedElement);
356: deselectedElement.deselectElement();
357: deselectedElement.repaint();
358: }
359: }
360:
361: private void findLeftTopMostDragElements(int startX, int startY) {
362: int left_most_x = startX;
363: int top_most_y = startY;
364:
365: for (Element element : mSelectedElements) {
366: element.startDrag();
367: if (element.getX() <= left_most_x) {
368: left_most_x = element.getX();
369: mDragElementLeftMost = element;
370: }
371: if (element.getY() <= top_most_y) {
372: top_most_y = element.getY();
373: mDragElementTopMost = element;
374: }
375: }
376: }
377:
378: public void elementDragStart(Element initiatingElement,
379: Point dragStartPoint) {
380: this .setCursor(new Cursor(Cursor.MOVE_CURSOR));
381:
382: mDragElement = initiatingElement;
383: mDragElementStartPoint = dragStartPoint;
384:
385: findLeftTopMostDragElements(initiatingElement.getX(),
386: initiatingElement.getY());
387: }
388:
389: public void repositionElementsDuringDrag(Element initiatingElement,
390: int offsetX, int offsetY) {
391: if (mDragElementLeftMost.getX() + offsetX < 0) {
392: offsetX = -1 * mDragElementLeftMost.getX();
393: }
394: if (mDragElementTopMost.getY() + offsetY < 0) {
395: offsetY = -1 * mDragElementTopMost.getY();
396: }
397: initiatingElement.repositionElementDuringDrag(offsetX, offsetY);
398: if (mSelectedElements.size() > 0) {
399: for (Element element : mSelectedElements) {
400: if (initiatingElement != element) {
401: element.repositionElementDuringDrag(offsetX,
402: offsetY);
403: }
404: }
405: }
406: }
407:
408: public void elementDragged(Element initiatingElement, int dragX,
409: int dragY) {
410: if (mDragElement == initiatingElement) {
411: Point element_location = initiatingElement.getLocation();
412: Point offset = new Point(dragX - mDragElementStartPoint.x,
413: dragY - mDragElementStartPoint.y);
414:
415: repositionElementsDuringDrag(initiatingElement, offset.x,
416: offset.y);
417:
418: if (isLocationOutsideStructurepanelView(element_location.x
419: + dragX, element_location.y + dragY)) {
420: new AutoScrollElementDrag(this , mDragElementStartPoint,
421: new Point(dragX, dragY), initiatingElement);
422: }
423: }
424: }
425:
426: public boolean isLocationOutsideStructurepanelView(
427: int locationOnStructurePanelX, int locationOnStructurePanelY) {
428: Rectangle visible_rect = mScrollPane.getViewport()
429: .getViewRect();
430: if (locationOnStructurePanelX < visible_rect.x
431: || locationOnStructurePanelX > visible_rect.x
432: + visible_rect.width
433: || locationOnStructurePanelY < visible_rect.y
434: || locationOnStructurePanelY > visible_rect.y
435: + visible_rect.height) {
436: return true;
437: } else {
438: return false;
439: }
440: }
441:
442: public void elementDragEnd() {
443: this .setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
444:
445: mDragElement = null;
446: mDragElementStartPoint = null;
447:
448: for (Element element : mSelectedElements) {
449: element.endDrag();
450: }
451:
452: this .repaint();
453: }
454:
455: public void elementPropertyHighlighted(ElementProperty property) {
456: if (!isDragActive() && !isElementPropertyBeingEdited()
457: && !isPopupMenuActive()) {
458: ElementProperty previous_highlight = mHighlightedProperty;
459: mHighlightedProperty = property;
460: if (previous_highlight != null) {
461: previous_highlight.getElement().repaint(
462: previous_highlight.getHotSpot().getBounds());
463: }
464: if (mHighlightedProperty != null) {
465: mHighlightedProperty.getElement().repaint(
466: mHighlightedProperty.getHotSpot().getBounds());
467: }
468: }
469:
470: if (isElementPropertyBeingEdited() || isPopupMenuActive()) {
471: mHighlightedPropertyHidden = property;
472: }
473: }
474:
475: void drawHighlightedProperty(Element element, Graphics2D g2d) {
476: if (mHighlightedProperty != null
477: && mHighlightedProperty.getElement() == element
478: && !isDragActive() && !isScrollActive()) {
479: mHighlightedProperty.drawActive(g2d);
480: }
481: }
482:
483: private void synchronizeHighlightedProperty() {
484: elementPropertyHighlighted(mHighlightedPropertyHidden);
485: mHighlightedPropertyHidden = null;
486: }
487:
488: public void changeZoom(float multiplier) {
489: changeZoom(multiplier, null);
490: }
491:
492: public void changeZoom(float multiplier, Point centerPoint) {
493: Cursor previous_cursor = this .getCursor();
494:
495: if (mScaleFactor * multiplier > SCALE_FACTOR_UPPER_LIMIT) {
496: multiplier = SCALE_FACTOR_UPPER_LIMIT / mScaleFactor;
497: }
498: if (mScaleFactor * multiplier < SCALE_FACTOR_LOWER_LIMIT) {
499: multiplier = SCALE_FACTOR_LOWER_LIMIT / mScaleFactor;
500: }
501:
502: if (multiplier != 1) {
503: this .setCursor(new Cursor(Cursor.WAIT_CURSOR));
504:
505: JPanel panel = new JPanel();
506: panel.setBackground(getBackground());
507: mScrollPane.getViewport().setView(panel);
508: setScaleFactor(mScaleFactor * multiplier);
509:
510: mElementStyleScaled.calculateStyle(mScaleFactor);
511:
512: for (Element element : mElements) {
513: element.scalePrecalculatedAreas(multiplier);
514: }
515:
516: mWidth *= multiplier;
517: mHeight *= multiplier;
518:
519: revalidate();
520:
521: if (centerPoint != null) {
522: centerPoint.x *= multiplier;
523: centerPoint.y *= multiplier;
524: }
525: SwingUtilities.invokeLater(new StructurePanelRepositioner(
526: this , centerPoint, previous_cursor));
527: }
528: }
529:
530: public boolean isDragActive() {
531: if (mDragElement != null) {
532: return true;
533: } else {
534: return false;
535: }
536: }
537:
538: public boolean isSelectionRectangleActive() {
539: if (mSelectionRectangleStartPoint != null) {
540: return true;
541: } else {
542: return false;
543: }
544: }
545:
546: public boolean isPopupMenuActive() {
547: return mPopupMenuActive;
548: }
549:
550: void resetSelectionRectangle() {
551: mSelectionRectanglePath = null;
552: }
553:
554: private void initializeSelectionRectangle() {
555: mSelectionRectanglePath = new GeneralPath();
556: }
557:
558: void eraseSelectionRectangle() {
559: Graphics2D g2d = (Graphics2D) getGraphics();
560: if (g2d != null && mSelectionRectanglePath != null) {
561: g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
562: RenderingHints.VALUE_STROKE_PURE);
563: g2d.setStroke(mSelectionRectangleDashedStroke);
564: g2d.setXORMode(Color.white);
565:
566: g2d.draw(mSelectionRectanglePath);
567: }
568: }
569:
570: private void drawSelectionRectangle(Point currentPoint) {
571: Graphics2D g2d = (Graphics2D) getGraphics();
572: g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
573: RenderingHints.VALUE_STROKE_PURE);
574: g2d.setStroke(mSelectionRectangleDashedStroke);
575: g2d.setXORMode(Color.white);
576:
577: createSelectionRectanglePath(currentPoint);
578: g2d.draw(mSelectionRectanglePath);
579: }
580:
581: private void createSelectionRectanglePath(Point currentPoint) {
582: mSelectionRectangleCurrentPoint = currentPoint;
583:
584: mSelectionRectanglePath.reset();
585: mSelectionRectanglePath.moveTo(mSelectionRectangleStartPoint.x,
586: mSelectionRectangleStartPoint.y);
587: mSelectionRectanglePath.lineTo(
588: mSelectionRectangleCurrentPoint.x,
589: mSelectionRectangleStartPoint.y);
590: mSelectionRectanglePath.lineTo(
591: mSelectionRectangleCurrentPoint.x,
592: mSelectionRectangleCurrentPoint.y);
593: mSelectionRectanglePath.lineTo(mSelectionRectangleStartPoint.x,
594: mSelectionRectangleCurrentPoint.y);
595: mSelectionRectanglePath.lineTo(mSelectionRectangleStartPoint.x,
596: mSelectionRectangleStartPoint.y);
597: }
598:
599: public void setScrollActive(boolean active) {
600: mScrollActive = active;
601: }
602:
603: public boolean isScrollActive() {
604: return mScrollActive;
605: }
606:
607: public void mouseClicked(MouseEvent e) {
608: if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
609: switch (mActiveTool) {
610: case SELECTION_TOOL:
611: deselectOtherElements(null);
612: this .repaint();
613: break;
614: case ZOOMIN_TOOL:
615: case ZOOMOUT_TOOL:
616: Point center_point = e.getPoint();
617: if (mActiveTool == ZOOMIN_TOOL) {
618: changeZoom(2f, center_point);
619: } else {
620: changeZoom(0.5f, center_point);
621: }
622: break;
623: }
624: }
625: }
626:
627: public void mousePressed(MouseEvent e) {
628: if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
629: switch (mActiveTool) {
630: case SELECTION_TOOL:
631: removeElementMouseListeners();
632: case ZOOMIN_TOOL:
633: case ZOOMOUT_TOOL:
634: mSelectionRectangleStartPoint = e.getPoint();
635: break;
636: }
637: }
638: }
639:
640: public void mouseReleased(MouseEvent e) {
641: removeElementPropertyEditor();
642: if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
643: switch (mActiveTool) {
644: case SELECTION_TOOL:
645: addElementMouseListeners();
646: case ZOOMIN_TOOL:
647: case ZOOMOUT_TOOL:
648: if (mSelectionRectangleStartPoint != null
649: && mSelectionRectangleCurrentPoint == null) {
650: mSelectionRectangleStartPoint = null;
651: mSelectionRectanglePath = null;
652: } else if (mSelectionRectangleStartPoint != null
653: && mSelectionRectangleCurrentPoint != null) {
654: float rect_x = 0;
655: float rect_y = 0;
656: float rect_width = 0;
657: float rect_height = 0;
658: if (mSelectionRectangleStartPoint.x <= mSelectionRectangleCurrentPoint.x
659: && mSelectionRectangleStartPoint.y <= mSelectionRectangleCurrentPoint.y) {
660: rect_x = mSelectionRectangleStartPoint.x;
661: rect_y = mSelectionRectangleStartPoint.y;
662: rect_width = mSelectionRectangleCurrentPoint.x
663: - mSelectionRectangleStartPoint.x;
664: rect_height = mSelectionRectangleCurrentPoint.y
665: - mSelectionRectangleStartPoint.y;
666: } else if (mSelectionRectangleStartPoint.x >= mSelectionRectangleCurrentPoint.x
667: && mSelectionRectangleStartPoint.y <= mSelectionRectangleCurrentPoint.y) {
668: rect_x = mSelectionRectangleCurrentPoint.x;
669: rect_y = mSelectionRectangleStartPoint.y;
670: rect_width = mSelectionRectangleStartPoint.x
671: - mSelectionRectangleCurrentPoint.x;
672: rect_height = mSelectionRectangleCurrentPoint.y
673: - mSelectionRectangleStartPoint.y;
674: } else if (mSelectionRectangleStartPoint.x <= mSelectionRectangleCurrentPoint.x
675: && mSelectionRectangleStartPoint.y >= mSelectionRectangleCurrentPoint.y) {
676: rect_x = mSelectionRectangleStartPoint.x;
677: rect_y = mSelectionRectangleCurrentPoint.y;
678: rect_width = mSelectionRectangleCurrentPoint.x
679: - mSelectionRectangleStartPoint.x;
680: rect_height = mSelectionRectangleStartPoint.y
681: - mSelectionRectangleCurrentPoint.y;
682: } else if (mSelectionRectangleStartPoint.x >= mSelectionRectangleCurrentPoint.x
683: && mSelectionRectangleStartPoint.y >= mSelectionRectangleCurrentPoint.y) {
684: rect_x = mSelectionRectangleCurrentPoint.x;
685: rect_y = mSelectionRectangleCurrentPoint.y;
686: rect_width = mSelectionRectangleStartPoint.x
687: - mSelectionRectangleCurrentPoint.x;
688: rect_height = mSelectionRectangleStartPoint.y
689: - mSelectionRectangleCurrentPoint.y;
690: }
691:
692: switch (mActiveTool) {
693: case SELECTION_TOOL:
694: if ((e.getModifiers() & MouseEvent.SHIFT_MASK) == 0) {
695: deselectOtherElements(null);
696: }
697:
698: // get elements in the correct Z order
699: Component[] components = getComponents();
700: Element element = null;
701: for (int i = components.length - 1; i >= 0; i--) {
702: if (components[i] instanceof Element) {
703: element = (Element) components[i];
704: if (element
705: .getBoundingAreaScaled()
706: .intersects(
707: new Rectangle2D.Float(
708: rect_x
709: - element
710: .getX(),
711: rect_y
712: - element
713: .getY(),
714: rect_width,
715: rect_height))
716: && !element.isSelected()) {
717: mSelectedElements.add(element);
718: element.selectElement();
719: }
720: }
721: }
722: break;
723: case ZOOMIN_TOOL: {
724: float width_factor = this .getParent()
725: .getWidth()
726: / rect_width;
727: float height_factor = this .getParent()
728: .getHeight()
729: / rect_height;
730: float factor = 0;
731: if (width_factor < height_factor) {
732: factor = width_factor;
733: } else {
734: factor = height_factor;
735: }
736: changeZoom(factor, new Point(
737: (int) (rect_x + rect_width / 2),
738: (int) (rect_y + rect_height / 2)));
739: }
740: break;
741: case ZOOMOUT_TOOL: {
742: float width_factor = rect_width
743: / this .getParent().getWidth();
744: float height_factor = rect_height
745: / this .getParent().getHeight();
746: float factor = 0;
747: if (width_factor < height_factor) {
748: factor = width_factor;
749: } else {
750: factor = height_factor;
751: }
752: changeZoom(factor, new Point(
753: (int) (rect_x + rect_width / 2),
754: (int) (rect_y + rect_height / 2)));
755: }
756: break;
757: }
758:
759: eraseSelectionRectangle();
760:
761: mSelectionRectangleStartPoint = null;
762: mSelectionRectangleCurrentPoint = null;
763: mSelectionRectanglePath = null;
764:
765: this .repaint();
766: }
767: break;
768: }
769: }
770: }
771:
772: public void mouseEntered(MouseEvent e) {
773: }
774:
775: public void mouseExited(MouseEvent e) {
776: if (isSelectionRectangleActive()) {
777: new AutoScrollSelectionRectangle(this , e.getPoint(),
778: mSelectionRectangleStartPoint);
779: }
780: }
781:
782: public void mouseDragged(MouseEvent e) {
783: Point current_point = e.getPoint();
784:
785: if (isSelectionRectangleActive()) {
786: Rectangle view_rect = mScrollPane.getViewport()
787: .getViewRect();
788:
789: if (current_point.x - view_rect.getX() > view_rect
790: .getWidth()
791: || current_point.y - view_rect.getY() > view_rect
792: .getHeight()) {
793: new AutoScrollSelectionRectangle(this , e.getPoint(),
794: mSelectionRectangleStartPoint);
795: } else {
796: if (mSelectionRectanglePath == null) {
797: initializeSelectionRectangle();
798: } else {
799: eraseSelectionRectangle();
800: }
801:
802: drawSelectionRectangle(current_point);
803: }
804: }
805: }
806:
807: public void mouseMoved(MouseEvent e) {
808: elementPropertyHighlighted(null);
809: }
810:
811: public void keyTyped(KeyEvent e) {
812: }
813:
814: public void keyPressed(KeyEvent e) {
815: if (mActiveTool == ZOOMIN_TOOL
816: && e.getKeyCode() == KeyEvent.VK_SHIFT) {
817: setActiveTool(ZOOMOUT_TOOL);
818: }
819: }
820:
821: public void keyReleased(KeyEvent e) {
822: if (mActiveTool == ZOOMOUT_TOOL
823: && e.getKeyCode() == KeyEvent.VK_SHIFT) {
824: setActiveTool(ZOOMIN_TOOL);
825: }
826: }
827:
828: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
829: mPopupMenuActive = true;
830: }
831:
832: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
833: mPopupMenuActive = false;
834: synchronizeHighlightedProperty();
835: }
836:
837: public void popupMenuCanceled(PopupMenuEvent e) {
838: }
839: }
|