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.gantt;
051:
052: import java.awt.Cursor;
053: import java.awt.Shape;
054: import java.awt.geom.Rectangle2D;
055: import java.util.Calendar;
056:
057: import com.projity.association.InvalidAssociationException;
058: import com.projity.functor.IntervalConsumer;
059: import com.projity.pm.dependency.DependencyService;
060: import com.projity.pm.dependency.DependencyType;
061: import com.projity.pm.dependency.HasDependencies;
062: import com.projity.pm.graphic.graph.GraphInteractor;
063: import com.projity.pm.graphic.graph.GraphUI;
064: import com.projity.pm.graphic.model.cache.GraphicDependency;
065: import com.projity.pm.graphic.model.cache.GraphicNode;
066: import com.projity.pm.graphic.timescale.CoordinatesConverter;
067: import com.projity.pm.scheduling.Schedule;
068: import com.projity.pm.scheduling.ScheduleInterval;
069: import com.projity.pm.scheduling.ScheduleService;
070: import com.projity.timescale.CalendarUtil;
071: import com.projity.util.Alert;
072:
073: /**
074: *
075: */
076: public class GanttInteractor extends GraphInteractor {
077: private static final long serialVersionUID = -555882007216388246L;
078: protected static final int BAR_MOVE_START = 4;
079: protected static final int BAR_MOVE_END = 5;
080: protected static final int PROGRESS_BAR_MOVE = 6;
081: protected static final int SPLIT = 7;
082: protected ScheduleInterval selectedInterval;
083: protected int selectedIntervalNumber;
084: protected double t;
085:
086: /**
087: *
088: */
089: public GanttInteractor(GraphUI ui) {
090: super (ui);
091: popup = new GanttPopupMenu(this );
092: }
093:
094: private class NodeSelectionIntervalConsumer implements
095: IntervalConsumer {
096: private boolean consumed = false;
097: private GraphicNode node;
098: private double deltaResize0;
099: private double deltaResize1;
100: private double deltaOutside;
101: private double completedDeltaT0;
102: private double completedDeltaT1;
103: private ScheduleInterval completedInterval;
104: private double t;
105: private CoordinatesConverter coord;
106:
107: public NodeSelectionIntervalConsumer init(double x,
108: GraphicNode node) {
109: this .t = getCoord().toTime(x);
110: consumed = false;
111: selectedInterval = null;
112: completedInterval = null;
113: selectedIntervalNumber = 0;
114: // GraphicNode node=(GraphicNode)selected;
115: // long completedT=node.getCompleted();
116: coord = getCoord();
117: completedDeltaT0 = coord.toDuration(config
118: .getSelectionProgress0());
119: completedDeltaT1 = coord.toDuration(config
120: .getSelectionProgress1());
121: deltaResize0 = coord.toDuration(config
122: .getSelectionResize0());
123: deltaResize1 = coord.toDuration(config
124: .getSelectionResize1());
125: deltaOutside = coord
126: .toDuration(config.getSelectionSquare());
127: this .node = node;
128: return this ;
129: }
130:
131: public void consumeInterval(ScheduleInterval interval) {
132: if (consumed)
133: return; //Consumer need to consume all the intervals
134: if (coord != null) {
135: long completedT = ((GraphicNode) selected)
136: .getCompleted();
137: if (completedT >= interval.getStart()
138: && completedT <= interval.getEnd()) {
139: completedInterval = new ScheduleInterval(interval
140: .getStart(), completedT);
141: if (interval.getEnd() > interval.getStart())
142: completedInterval = coord
143: .adaptSmallBarTimeInterval(
144: completedInterval, node, config);
145: }
146: interval = coord.adaptSmallBarTimeInterval(interval,
147: node, config);
148:
149: }
150: if (t < interval.getStart()
151: && t >= interval.getStart() - deltaOutside) {
152: if (selectedIsNonSummaryNode())
153: state = BAR_MOVE_START;
154: } else if (t > interval.getEnd()
155: && t <= interval.getEnd() + deltaOutside) {
156: if (selectedIsNonSummaryNode())
157: state = BAR_MOVE_END;
158: } else if (t < interval.getStart() || t > interval.getEnd()) {
159: selectedIntervalNumber++;
160: return;
161: } else if (completedInterval != null
162: && t >= completedInterval.getEnd()
163: - completedDeltaT0
164: && t <= completedInterval.getEnd()
165: + completedDeltaT1) {
166: if (selectedIsNonSummaryNode())
167: state = PROGRESS_BAR_MOVE;
168: } else if (t <= interval.getStart() + deltaResize0) {
169: if (selectedIsNonSummaryNode())
170: state = BAR_MOVE_START;
171: } else if (t >= interval.getEnd() - deltaResize1) {
172: if (selectedIsNonSummaryNode())
173: state = BAR_MOVE_END;
174: } else
175: state = BAR_MOVE;
176: selectedInterval = interval;
177: consumed = true;
178: }
179: }
180:
181: private NodeSelectionIntervalConsumer nodeSelectionIntervalConsumer = new NodeSelectionIntervalConsumer();
182:
183: protected void computeNodeSelection(double x, double y) {
184: //would have prefered an iterator
185: GraphicNode node = (GraphicNode) selected;
186: node.consumeIntervals(nodeSelectionIntervalConsumer.init(x,
187: node));
188: }
189:
190: protected Shape getBarShadowBounds(double x, double y) {
191: double deltaX = x - x0;
192: CoordinatesConverter coord = getCoord();
193: GraphicNode node = (GraphicNode) selected;
194: Rectangle2D bounds;
195: double xStart = coord
196: .toX((selectedIntervalNumber == 0 && state == BAR_MOVE) ? node
197: .getStart()
198: : selectedInterval.getStart());
199: if (state == PROGRESS_BAR_MOVE) {
200: double completedX = coord.toX(node.getCompleted());//CoordinatesConverter.adaptSmallBarEndX(xStart,coord.toX(node.getCompleted()),config);
201: bounds = new Rectangle2D.Double(xStart, ((GanttUI) ui)
202: .getBarY(node.getRow())
203: + node.getGanttShapeOffset()
204: + (node.getGanttShapeHeight() - config
205: .getGanttProgressBarHeight()) / 2,
206: completedX - xStart + deltaX, config
207: .getGanttProgressBarHeight());
208: } else {
209: double xEnd = (selectedIntervalNumber == 0 && state == BAR_MOVE) ? CoordinatesConverter
210: .adaptSmallBarEndX(coord.toX(node.getStart()),
211: coord.toX(node.getEnd()), node, config)
212: : coord.toX(selectedInterval.getEnd());
213: double w = xEnd - xStart;
214: switch (state) {
215: case BAR_MOVE:
216: bounds = new Rectangle2D.Double(xStart + deltaX,
217: ((GanttUI) ui).getBarY(node.getRow())
218: + node.getGanttShapeOffset(), w, node
219: .getGanttShapeHeight());
220: break;
221: case BAR_MOVE_START:
222: bounds = new Rectangle2D.Double(xStart + deltaX,
223: ((GanttUI) ui).getBarY(node.getRow())
224: + node.getGanttShapeOffset(), w
225: - deltaX, node.getGanttShapeHeight());
226: break;
227: case BAR_MOVE_END:
228: bounds = new Rectangle2D.Double(xStart, ((GanttUI) ui)
229: .getBarY(node.getRow())
230: + node.getGanttShapeOffset(), w + deltaX, node
231: .getGanttShapeHeight());
232: break;
233: default:
234: return null;
235: }
236: }
237: return bounds;
238: }
239:
240: protected Rectangle2D getLinkSelectionShadowBounds(GraphicNode node) {
241: CoordinatesConverter coord = getCoord();
242: double xStart = coord.toX(node.getStart());
243: double xEnd = coord.toX(node.getEnd());
244: xEnd = CoordinatesConverter.adaptSmallBarEndX(xStart, xEnd,
245: node, config);
246: Rectangle2D selectionRectangle = new Rectangle2D.Double(xStart,
247: ((GanttUI) ui).getBarY(node.getRow())
248: + node.getGanttShapeOffset(), xEnd - xStart,
249: node.getGanttShapeHeight());
250: return selectionRectangle;
251: }
252:
253: public CoordinatesConverter getCoord() {
254: return ((GanttUI) ui).getCoord();
255: }
256:
257: protected void setLinkOrigin() {
258: GraphicNode node = (GraphicNode) selected;
259: CoordinatesConverter coord = getCoord();
260: double xStart = coord.toX((selectedIntervalNumber == 0) ? node
261: .getStart() : selectedInterval.getStart());
262: double xEnd = selectedIntervalNumber == 0 ? CoordinatesConverter
263: .adaptSmallBarEndX(coord.toX(node.getStart()), coord
264: .toX(node.getEnd()), node, config)
265: : coord.toX(selectedInterval.getEnd());
266: x0link = (xStart + xEnd) / 2;
267: y0link = ((GanttUI) ui).getBarY(node.getRow())
268: + node.getGanttShapeOffset()
269: + node.getGanttShapeHeight() / 2;
270:
271: }
272:
273: protected boolean switchOnLinkCreation(double x, double y) {
274: GraphicNode node = (GraphicNode) selected;
275: return node.getNode().getImpl() instanceof HasDependencies
276: && ((int) y) / ((Gantt) getGraph()).getRowHeight() != node
277: .getRow();
278: }
279:
280: public Cursor selectCursor() {
281: Cursor cursor = null;
282: switch (state) {
283: case BAR_MOVE_START:
284: cursor = new Cursor(Cursor.W_RESIZE_CURSOR);
285: break;
286: case BAR_MOVE_END:
287: cursor = new Cursor(Cursor.E_RESIZE_CURSOR);
288: break;
289: case PROGRESS_BAR_MOVE:
290: cursor = getProgressCursor();
291: break;
292: case SPLIT:
293: cursor = getSplitCursor();
294: break;
295: }
296: if (cursor == null)
297: super .selectCursor();
298: else
299: getGraph().setCursor(cursor);
300: return cursor;
301: }
302:
303: public boolean executeAction(double x, double y) {
304: if (x == x0 || selected == null)
305: return false;
306: if (state == BAR_MOVE || state == BAR_MOVE_START
307: || state == BAR_MOVE_END || state == PROGRESS_BAR_MOVE
308: || state == SPLIT) {
309: if (!(selected instanceof GraphicNode))
310: return false;
311: sourceNode = (GraphicNode) selected;
312: }
313: long t = (long) getCoord().toTime(x);
314: long dt = (long) getCoord().toDuration(x - x0);
315: switch (state) {
316: case BAR_MOVE:
317: ScheduleService.getInstance().setInterval(
318: this ,
319: (Schedule) sourceNode.getNode().getImpl(),
320: selectedInterval.getStart() + dt,
321: selectedInterval.getEnd() + dt,
322: selectedInterval,
323: ui.getGraph().getProject().getUndoController()
324: .getEditSupport());
325: return true;
326: case BAR_MOVE_START:
327: ScheduleService.getInstance().setInterval(
328: this ,
329: (Schedule) sourceNode.getNode().getImpl(),
330: selectedInterval.getStart() + dt,
331: selectedInterval.getEnd(),
332: selectedInterval,
333: ui.getGraph().getProject().getUndoController()
334: .getEditSupport());
335: return true;
336: case BAR_MOVE_END:
337: ScheduleService.getInstance().setInterval(
338: this ,
339: (Schedule) sourceNode.getNode().getImpl(),
340: selectedInterval.getStart(),
341: selectedInterval.getEnd() + dt,
342: selectedInterval,
343: ui.getGraph().getProject().getUndoController()
344: .getEditSupport());
345: return true;
346: case PROGRESS_BAR_MOVE:
347: ScheduleService.getInstance().setCompleted(
348: this ,
349: (Schedule) sourceNode.getNode().getImpl(),
350: t,
351: ui.getGraph().getProject().getUndoController()
352: .getEditSupport());
353: return true;
354: case LINK_CREATION:
355: try {
356: if (sourceNode != null
357: && destinationNode != null
358: && sourceNode.getNode().getImpl() instanceof HasDependencies
359: && destinationNode.getNode().getImpl() instanceof HasDependencies) {
360: DependencyService.getInstance().newDependency(
361: (HasDependencies) sourceNode.getNode()
362: .getImpl(),
363: (HasDependencies) destinationNode.getNode()
364: .getImpl(), DependencyType.FS, 0,
365: this );
366: }
367: } catch (InvalidAssociationException e) {
368: Alert.error(e.getMessage());
369: }
370: return true;
371: case LINK_SELECTION:
372: showDependencyPropertiesDialog((GraphicDependency) selected);
373: return true;
374: case SPLIT:
375: ScheduleService.getInstance().split(
376: this ,
377: (Schedule) sourceNode.getNode().getImpl(),
378: t,
379: t,
380: ui.getGraph().getProject().getUndoController()
381: .getEditSupport());
382: return true;
383: }
384: return false;
385: }
386:
387: public void setSplitMode() {
388: state = SPLIT;
389: selectCursor();
390: }
391:
392: protected void select(int x, int y) {
393: if (selection) {
394: selected = ui.getObjectAt(x, y);
395: if (state == SPLIT)
396: return;
397: if (selected == null) {
398: state = NOTHING_SELECTED;
399: } else {
400: findState(x, y);
401: }
402: selectCursor();
403: }
404: }
405:
406: protected boolean isMove() {
407: return state == BAR_MOVE || state == BAR_MOVE_END
408: || state == BAR_MOVE_START
409: || state == PROGRESS_BAR_MOVE;
410: }
411:
412: protected boolean isDirectAction() {
413: return state == SPLIT || super .isDirectAction();
414: }
415:
416: protected boolean isRepaintOnRelease() {
417: return state == BAR_MOVE || state == BAR_MOVE_END
418: || state == BAR_MOVE_START
419: || state == PROGRESS_BAR_MOVE || state == LINK_CREATION;
420: }
421:
422: }
|