001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.pm.graphic.graph;
051:
052: import java.awt.BasicStroke;
053: import java.awt.Cursor;
054: import java.awt.Frame;
055: import java.awt.Graphics2D;
056: import java.awt.Point;
057: import java.awt.Rectangle;
058: import java.awt.Shape;
059: import java.awt.Toolkit;
060: import java.awt.event.MouseEvent;
061: import java.awt.event.MouseListener;
062: import java.awt.event.MouseMotionListener;
063: import java.awt.event.MouseWheelEvent;
064: import java.awt.event.MouseWheelListener;
065: import java.awt.geom.Line2D;
066: import java.awt.geom.Rectangle2D;
067: import java.io.Serializable;
068:
069: import javax.swing.JOptionPane;
070: import javax.swing.SwingUtilities;
071:
072: import com.projity.dialog.DependencyDialog;
073: import com.projity.graphic.configuration.GraphicConfiguration;
074: import com.projity.pm.graphic.IconManager;
075: import com.projity.pm.graphic.model.cache.GraphicDependency;
076: import com.projity.pm.graphic.model.cache.GraphicNode;
077:
078: /**
079: *
080: */
081: public abstract class GraphInteractor implements MouseListener,
082: MouseMotionListener, MouseWheelListener, Serializable {
083: protected static final int NOTHING_SELECTED = 0;
084: protected static final int LINK_CREATION = 1;
085: protected static final int LINK_SELECTION = 2;
086: protected static final int BAR_MOVE = 3;
087: protected GraphUI ui;
088: protected int state;
089: protected Object selected = null;
090: protected boolean selection;
091: protected double x0, y0;
092: protected GraphicConfiguration config;
093: protected GraphPopupMenu popup = null;
094: protected DependencyDialog dependencyPropertiesDialog;
095:
096: /**
097: *
098: */
099: public GraphInteractor(GraphUI ui) {
100: this .ui = ui;
101: config = GraphicConfiguration.getInstance();
102: state = NOTHING_SELECTED;
103: selection = true;
104: init();
105: }
106:
107: protected void init() {
108: ui.getGraph().addMouseListener(this );
109: ui.getGraph().addMouseMotionListener(this );
110: ui.getGraph().addMouseWheelListener(this );
111: defaultCursor = getGraph().getCursor();
112: }
113:
114: protected void reset() {
115: lastShadowX = -1;
116: lastShadowY = -1;
117: lastLinkShadowX = -1;
118: lastLinkShadowY = -1;
119: sourceNode = null;
120: destinationNode = null;
121: selection = true;
122: }
123:
124: protected boolean selectedIsNonSummaryNode() {
125: return ((selected instanceof GraphicNode) && !((GraphicNode) selected)
126: .isSummary());
127: }
128:
129: public Graph getGraph() {
130: return ui.getGraph();
131: }
132:
133: public void showDependencyPropertiesDialog(
134: GraphicDependency dependency) {
135: if (dependencyPropertiesDialog == null) {
136: Frame parent = JOptionPane.getFrameForComponent(getGraph());
137: dependencyPropertiesDialog = new DependencyDialog(parent,
138: dependency.getDependency());
139: }
140: boolean didAction = DependencyDialog.doDialog(
141: dependencyPropertiesDialog, dependency.getDependency());
142: }
143:
144: //CURSORS
145: protected Cursor defaultCursor;
146: protected Cursor progressCursor;
147:
148: protected Cursor getProgressCursor() {
149: if (progressCursor == null) {
150: try {
151: progressCursor = Toolkit
152: .getDefaultToolkit()
153: .createCustomCursor(
154: IconManager
155: .getImage("gantt.progress.cursor"),
156: new Point(15, 5), "ProgressCursor");
157: } catch (Exception e) {
158: progressCursor = new Cursor(Cursor.HAND_CURSOR);
159: }
160: }
161: return progressCursor;
162: }
163:
164: protected Cursor linkCursor;
165:
166: protected Cursor getLinkCursor() {
167: if (linkCursor == null) {
168: try {
169: linkCursor = Toolkit.getDefaultToolkit()
170: .createCustomCursor(
171: IconManager
172: .getImage("gantt.link.cursor"),
173: new Point(7, 3), "linkCursor");
174: } catch (Exception e) {
175: linkCursor = new Cursor(Cursor.HAND_CURSOR);
176: }
177: }
178: return linkCursor;
179: }
180:
181: protected Cursor splitCursor;
182:
183: protected Cursor getSplitCursor() {
184: if (splitCursor == null) {
185: try {
186: splitCursor = Toolkit
187: .getDefaultToolkit()
188: .createCustomCursor(
189: IconManager
190: .getImage("gantt.split.cursor"),
191: new Point(10, 4), "splitCursor");
192: } catch (Exception e) {
193: splitCursor = new Cursor(Cursor.HAND_CURSOR);
194: }
195: }
196: return splitCursor;
197: }
198:
199: public Cursor selectCursor() {
200: Cursor cursor = defaultCursor;
201: switch (state) {
202: case BAR_MOVE:
203: cursor = new Cursor(Cursor.MOVE_CURSOR);
204: break;
205: case LINK_CREATION:
206: cursor = getLinkCursor();
207: break;
208: case LINK_SELECTION:
209: cursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
210: break;
211: }
212: getGraph().setCursor(cursor);
213: return cursor;
214: }
215:
216: //Drawings
217: protected void findState(double x, double y) {
218: state = NOTHING_SELECTED;
219: if (selected instanceof GraphicNode) {
220: computeNodeSelection(x, y);
221: } else if (selected instanceof GraphicDependency) {
222: state = LINK_SELECTION;
223: }
224: }
225:
226: protected Graphics2D initGraphics() {
227: Graph graph = getGraph();
228: Graphics2D g = (Graphics2D) graph.getGraphics();
229: g.setColor(graph.getForeground());
230: g.setXORMode(graph.getBackground().darker());
231: return g;
232: }
233:
234: protected double lastShadowX = -1;
235: protected double lastShadowY = -1;
236:
237: protected void drawBarShadow(double x, double y, boolean alternate) {
238: if (x == -1)
239: return;
240: Graphics2D g = initGraphics();
241:
242: Shape bounds = getBarShadowBounds(x, y);
243: if (bounds == null)
244: return;
245: g.setStroke(new BasicStroke(3));
246: g.draw(bounds);
247: if (alternate) {
248: lastShadowX = (lastShadowX == -1) ? x : -1;
249: lastShadowY = (lastShadowY == -1) ? y : -1;
250: }
251: }
252:
253: protected GraphicNode sourceNode = null;
254: protected GraphicNode destinationNode = null;
255:
256: protected void drawLinkSelectionBarShadow(GraphicNode node) {
257: if (node == null)
258: return;
259: Graphics2D g = initGraphics();
260: Rectangle2D selectionRectangle = getLinkSelectionShadowBounds(node);
261: if (selectionRectangle == null)
262: return;
263: g.setStroke(new BasicStroke(3));
264: g.draw(selectionRectangle);
265: }
266:
267: protected double lastLinkShadowX = -1;
268: protected double lastLinkShadowY = -1;
269: protected double x0link, y0link;
270:
271: private void drawLinkShadow(double x, double y, boolean alternate) {
272: if (x == -1 || y == -1)
273: return;
274: Graphics2D g = initGraphics();
275:
276: Line2D line = new Line2D.Double(x0link, y0link, x, y);
277: g.setStroke(new BasicStroke(2));
278: g.draw(line);
279:
280: if (alternate) {
281: lastLinkShadowX = (lastLinkShadowX == -1) ? x : -1;
282: lastLinkShadowY = (lastLinkShadowY == -1) ? y : -1;
283: }
284: }
285:
286: //Mouse
287: public void mouseClicked(MouseEvent e) {
288: }
289:
290: public void mouseWheelMoved(MouseWheelEvent e) {
291: }
292:
293: public void mousePressed(MouseEvent e) {
294: if (isReadOnly())
295: return;
296: if (SwingUtilities.isRightMouseButton(e)) {
297: if (popup != null)
298: popup.show(getGraph(), e.getX(), e.getY());
299: } else {
300: if (selected == null)
301: return;
302: if (isMove()) {
303: selection = false;
304: x0 = e.getX();
305: y0 = e.getY();
306: drawBarShadow(x0, y0, true);
307: } else if (isDirectAction()) {
308: executeAction(e.getX(), e.getY());
309: state = NOTHING_SELECTED;
310: //select(e.getX(),e.getY()); //TODO commented to avoid second action when spliting
311: }
312: }
313: }
314:
315: public void mouseReleased(MouseEvent e) {
316: if (isReadOnly())
317: return;
318: if (!SwingUtilities.isLeftMouseButton(e))
319: return;
320: if (selected == null || state == NOTHING_SELECTED)
321: return;
322: if (isRepaintOnRelease())
323: getGraph().repaint();
324:
325: double x1 = e.getX();
326: double y1 = e.getY();
327: executeAction(x1, y1);
328: reset();
329: findState(x1, y1);
330: }
331:
332: public void mouseEntered(MouseEvent e) {
333: }
334:
335: public void mouseExited(MouseEvent e) {
336: }
337:
338: private void scrollToVisible(int x, int y) {
339: int scrollingDistance = 100;
340: getGraph().scrollRectToVisible(
341: new Rectangle(x - scrollingDistance, y
342: - scrollingDistance, scrollingDistance * 2,
343: scrollingDistance * 2));
344: }
345:
346: public void mouseDragged(MouseEvent e) {
347: if (isReadOnly())
348: return;
349: if (!SwingUtilities.isLeftMouseButton(e))
350: return;
351: if (state != LINK_CREATION && selected instanceof GraphicNode) {
352: GraphicNode node = (GraphicNode) selected;
353: if (switchOnLinkCreation(e.getX(), e.getY())) {
354: drawBarShadow(lastShadowX, lastLinkShadowY, true);
355: state = LINK_CREATION;
356: selectCursor();
357:
358: sourceNode = (GraphicNode) selected;
359: drawLinkSelectionBarShadow(sourceNode);
360:
361: setLinkOrigin();
362: }
363: }
364: if (state == LINK_CREATION) {
365: GraphicNode newDestinationNode = ui.getNodeAt(e.getX(), e
366: .getY());
367: drawLinkSelectionBarShadow(destinationNode);
368: drawLinkShadow(lastLinkShadowX, lastLinkShadowY, true);
369: scrollToVisible(e.getX(), e.getY());
370: drawLinkShadow(e.getX(), e.getY(), true);
371: if (newDestinationNode != null
372: && newDestinationNode.isLinkable()
373: && sourceNode != newDestinationNode) {
374: destinationNode = newDestinationNode;
375: drawLinkSelectionBarShadow(destinationNode);
376: } else
377: destinationNode = null;
378: } else if (isMove()) {
379: drawBarShadow(lastShadowX, lastShadowY, true);
380: scrollToVisible(e.getX(), e.getY());
381: drawBarShadow(e.getX(), e.getY(), true);
382: }
383: }
384:
385: public void mouseMoved(MouseEvent e) {
386: if (isReadOnly())
387: return;
388: select(e.getX(), e.getY());
389: }
390:
391: public boolean isReadOnly() {
392: return getGraph().getModel().isReadOnly();
393: }
394:
395: protected abstract void computeNodeSelection(double x, double y);
396:
397: protected abstract Shape getBarShadowBounds(double x, double y);
398:
399: protected abstract Rectangle2D getLinkSelectionShadowBounds(
400: GraphicNode node);
401:
402: protected abstract void setLinkOrigin();
403:
404: protected abstract boolean switchOnLinkCreation(double x, double y);
405:
406: public abstract boolean executeAction(double x, double y);
407:
408: protected void select(int x, int y) {
409: if (selection) {
410: selected = ui.getObjectAt(x, y);
411: if (selected == null) {
412: state = NOTHING_SELECTED;
413: } else {
414: findState(x, y);
415: }
416: selectCursor();
417: }
418: }
419:
420: protected boolean isMove() {
421: return state == BAR_MOVE;
422: }
423:
424: protected boolean isDirectAction() {
425: return state == LINK_SELECTION;
426: }
427:
428: protected boolean isRepaintOnRelease() {
429: return state == BAR_MOVE || state == LINK_CREATION;
430: }
431:
432: }
|