001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mapper.basicmapper.canvas.jgo;
021:
022: import java.awt.Rectangle;
023: import java.beans.PropertyChangeEvent;
024: import java.beans.PropertyChangeListener;
025:
026: import javax.swing.SwingUtilities;
027:
028: import com.nwoods.jgo.JGoBrush;
029: import com.nwoods.jgo.JGoLink;
030: import com.nwoods.jgo.JGoPen;
031: import com.nwoods.jgo.JGoPort;
032: import com.nwoods.jgo.JGoSelection;
033: import com.nwoods.jgo.JGoView;
034: import org.netbeans.modules.soa.mapper.basicmapper.canvas.jgo.util.DrawPort;
035: import org.netbeans.modules.soa.mapper.common.basicmapper.canvas.gtk.ICanvasMapperLink;
036: import org.netbeans.modules.soa.mapper.common.basicmapper.canvas.gtk.ICanvasView;
037: import org.netbeans.modules.soa.mapper.common.IMapperLink;
038: import org.netbeans.modules.soa.mapper.common.IMapperNode;
039: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeNode;
040: import org.netbeans.modules.soa.mapper.common.gtk.ICanvasLinkLabel;
041: import org.netbeans.modules.soa.mapper.common.gtk.ICanvasNode;
042:
043: /**
044: * <p>
045: *
046: * Title: </p> AbstractCanvasLink <p>
047: *
048: * Description: </p> AbstractCanvasLink provides the basic implemenation of a
049: * canvas link with a JGoLink. <p>
050: *
051: * @author Un Seng Leong
052: * @created December 4, 2002
053: */
054: public abstract class AbstractCanvasLink extends JGoLink implements
055: ICanvasMapperLink {
056:
057: /**
058: * the canvas of this link
059: */
060: private ICanvasView mCanvas;
061:
062: /**
063: * the mapper link of this canvas link.
064: */
065: private IMapperLink mMapperLink;
066:
067: /**
068: * the pixels of the middle segement indent
069: */
070: private static final int MID_SEGEMTNT_IDENT = 5;
071:
072: /**
073: * the mid point of this link, this is for debugging purpose.
074: */
075: private int mMidPoint = 0;
076:
077: /**
078: * the offset pixels that counts to be overlap segement.
079: */
080: private static final int OVERLAP_OFFSET_PIXELS = 2;
081:
082: /**
083: * The value indicates the mid segement should go left.
084: */
085: private static final int INDENT_TO_LEFT = -1;
086:
087: /**
088: * The value indicates the mid segement should go right.
089: */
090: private static final int INDENT_TO_RIGHT = 1;
091:
092: protected JGoPort endPort;
093: protected JGoPort startPort;
094:
095: private JGoPen pen;
096: private JGoBrush brush;
097: private boolean mIsSelected;
098:
099: /**
100: * Creates a new AbstractCanvasLink object, with a specified mapper link.
101: *
102: * @param link the mapper link this canvas link repersents.
103: */
104: public AbstractCanvasLink(IMapperLink link) {
105: mMapperLink = link;
106: this .setRelinkable(false);
107: this .setOrthogonal(true);
108: this .setJumpsOver(true);
109:
110: this .pen = this .getPen();
111: this .brush = this .getBrush();
112: }
113:
114: protected AbstractCanvasLink(JGoPort fromPort, JGoPort toPort) {
115: super (fromPort, toPort);
116: this .setJumpsOver(true);
117: }
118:
119: /**
120: * Return the destination node of this link.
121: *
122: * @return the destination node of this link.
123: */
124: public ICanvasNode getDestinationNode() {
125: if (mMapperLink.getEndNode() instanceof ICanvasNode) {
126: return (ICanvasNode) mMapperLink.getEndNode();
127: }
128:
129: return null;
130: }
131:
132: /**
133: * Return the label of this link.
134: *
135: * @return always null.
136: */
137: public ICanvasLinkLabel getLabel() {
138: return null;
139: }
140:
141: /**
142: * Return the canvas of this link.
143: *
144: * @return the canvas view of this link.
145: */
146: public ICanvasView getMapperCanvas() {
147: return mCanvas;
148: }
149:
150: /**
151: * Return the mapper link.
152: *
153: * @return the mapper link.
154: */
155: public IMapperLink getMapperLink() {
156: return mMapperLink;
157: }
158:
159: /**
160: * Return the source node fo this link.
161: *
162: * @return the source node fo this link.
163: */
164: public ICanvasNode getSourceNode() {
165: if (mMapperLink.getStartNode() instanceof ICanvasNode) {
166: return (ICanvasNode) mMapperLink.getStartNode();
167: }
168:
169: return null;
170: }
171:
172: /**
173: * Set the label of this link
174: *
175: * @param label the label of this link.
176: */
177: public void setLabel(ICanvasLinkLabel label) {
178: }
179:
180: /**
181: * Set if the label is visible.
182: *
183: * @param val the label visible value
184: */
185: public void setLabelVisible(boolean val) {
186: }
187:
188: /**
189: * Set the canvas of this link.
190: *
191: * @param canvas the canvas of this link.
192: */
193: public void setMapperCanvas(ICanvasView canvas) {
194: mCanvas = canvas;
195: pen = JGoPen.makeStockPen(DEFAULT_LINK_COLOR);
196: brush = JGoBrush.makeStockBrush(DEFAULT_LINK_COLOR);
197: this .setPen(pen);
198: this .setBrush(brush);
199: }
200:
201: /**
202: * update the label text
203: *
204: * @param name the text of the lable of this link
205: */
206: public void updateLabelText(String name) {
207: }
208:
209: /**
210: * Sets the dataObject attribute of the AbstractCanvasLink object
211: *
212: * @param obj The new dataObject value
213: */
214: public void setDataObject(Object obj) {
215: }
216:
217: /**
218: * Gets the dataObject attribute of the AbstractCanvasLink object
219: *
220: * @return The dataObject value
221: */
222: public Object getDataObject() {
223: return null;
224: }
225:
226: /**
227: * Return true if this link is not hidding from the screen, such as by
228: * scroll panel.
229: *
230: * @param viewRect the rectangle of the viewable area
231: * @return true if this link is not hidding from the screen, such
232: * as by scroll panel.
233: */
234: public boolean isDisplaying(Rectangle viewRect) {
235: return true;// ???
236: }
237:
238: public void setPosition(int pos, int collectionSize) {
239: mPosition = pos;
240: mCollectionSize = collectionSize;
241: }
242:
243: int mPosition = 0;
244: int mCollectionSize = 0;
245:
246: /**
247: * Return the mid point of this orthoal link. Override getMidOrthoPosition in JGoLink.
248: *
249: * @param from the from point related to the mid point
250: * @param to the to point related to the mid point
251: * @param vertical true if the mid point is related to a vertical segment, false otherwise.
252: * @return The mid orthoal position value
253: */
254: protected int getMidOrthoPosition(int from, int to, boolean vertical) {
255: int result = super .getMidOrthoPosition(from, to, vertical);
256: if (mPosition > 0) {
257: int newPos = from + (mPosition * 6);
258: if (newPos < result) {
259: result = newPos;
260: }
261: }
262: return result;
263: }
264:
265: protected class NodeYChangeListener implements
266: PropertyChangeListener {
267: // PENDING -- the y axis difference between the tree view and
268: // the canvas. This need to be calculate dynamically.
269:
270: /**
271: * the difference between tree view and the canvas in y coordination.
272: */
273: protected static final int Y_AXIS_DIFF = 1;
274:
275: /**
276: * the port of the link.
277: */
278: private JGoPort mPort;
279:
280: /**
281: * the view contains this link.
282: */
283: private JGoView mView;
284:
285: /**
286: * Creates a new NodeYChangeListener object.
287: *
288: * @param view the view contains this link.
289: * @param port the port of the link.
290: */
291: public NodeYChangeListener(JGoView view, JGoPort port) {
292: mPort = port;
293: mView = view;
294: }
295:
296: /**
297: * Change the port of this link to alone with the mapper node y
298: * coordination.
299: *
300: * @param e the PropertyChangeEvent
301: */
302: public void propertyChange(PropertyChangeEvent e) {
303: if (e.getPropertyName().equals(IMapperNode.Y_CHANGE)) {
304:
305: final int topLoc = ((IMapperNode) e.getSource()).getY()
306: + Y_AXIS_DIFF + mView.getViewPosition().y;
307: SwingUtilities.invokeLater(new Runnable() {
308: public void run() {
309: mPort.setTop(topLoc);
310: }
311: });
312: }
313: }
314: }
315:
316: protected void gainedSelection(JGoSelection jgoselection) {
317: mIsSelected = true;
318: super .gainedSelection(jgoselection);
319: startHighlighting();
320: if (getMapperCanvas().isPathHighlightingEnabled()) {
321: new NetworkHighlightTraverser(true).visit(this );
322: }
323: }
324:
325: protected void lostSelection(JGoSelection jgoselection) {
326: mIsSelected = false;
327: super .lostSelection(jgoselection);
328: stopHighlighting();
329: if (getMapperCanvas().isPathHighlightingEnabled()) {
330: new NetworkHighlightTraverser(false).visit(this );
331: }
332: }
333:
334: /**
335: * start highlighting this link
336: */
337: public void startHighlighting() {
338: this .setPen(JGoPen.make(JGoPen.SOLID, mIsSelected ? 2 : 1,
339: DEFAULT_LINK_SELECTED_COLOR));
340: this .setBrush(JGoBrush
341: .makeStockBrush(DEFAULT_LINK_SELECTED_COLOR));
342: highlightSingleLink(mMapperLink, getMapperCanvas());
343: }
344:
345: /**
346: * stop highlighting this link
347: */
348: public void stopHighlighting() {
349: if (!mIsSelected) {
350: this .setPen(pen);
351: this .setBrush(brush);
352: unHighlightSingleLink(mMapperLink, getMapperCanvas());
353: }
354: }
355:
356: public JGoPort getStartPort() {
357: return this .startPort;
358: }
359:
360: public JGoPort getEndPort() {
361: return this .endPort;
362: }
363:
364: public static void initializeArrowHeads(JGoLink link) {
365: link.setArrowLength(7.5);
366: link.setArrowShaftLength(6.5);
367: link.setArrowWidth(4);
368: }
369:
370: private void highlightSingleLink(IMapperLink link,
371: ICanvasView canvasView) {
372: IMapperNode startNode = link.getStartNode();
373: if (startNode instanceof IMapperTreeNode) {
374: IMapperTreeNode startTreeNode = (IMapperTreeNode) startNode;
375: startTreeNode.setHighlightLink(true);
376: startTreeNode.setSelectedLink(mIsSelected);
377: canvasView.getParentView().getViewManager().getSourceView()
378: .getTree().scrollPathToVisible(
379: startTreeNode.getPath());
380: canvasView.getParentView().getViewManager().getSourceView()
381: .getTree().repaint();
382: }
383:
384: IMapperNode endNode = link.getEndNode();
385: if (endNode instanceof IMapperTreeNode) {
386: IMapperTreeNode endTreeNode = (IMapperTreeNode) endNode;
387: endTreeNode.setHighlightLink(true);
388: endTreeNode.setSelectedLink(mIsSelected);
389: canvasView.getParentView().getViewManager().getDestView()
390: .getTree().scrollPathToVisible(
391: endTreeNode.getPath());
392: canvasView.getParentView().getViewManager().getDestView()
393: .getTree().repaint();
394: }
395:
396: setPortActivated(startPort, true);
397: setPortActivated(endPort, true);
398: }
399:
400: private void unHighlightSingleLink(IMapperLink link,
401: ICanvasView canvasView) {
402: IMapperNode startNode = link.getStartNode();
403: if (startNode instanceof IMapperTreeNode) {
404: IMapperTreeNode startTreeNode = (IMapperTreeNode) startNode;
405: startTreeNode.setHighlightLink(false);
406: startTreeNode.setSelectedLink(false);
407: canvasView.getParentView().getViewManager().getSourceView()
408: .getTree().repaint();
409:
410: }
411:
412: IMapperNode endNode = link.getEndNode();
413: if (endNode instanceof IMapperTreeNode) {
414: IMapperTreeNode endTreeNode = (IMapperTreeNode) endNode;
415: endTreeNode.setHighlightLink(false);
416: endTreeNode.setSelectedLink(false);
417: canvasView.getParentView().getViewManager().getDestView()
418: .getTree().repaint();
419: }
420:
421: setPortActivated(startPort, false);
422: setPortActivated(endPort, false);
423: }
424:
425: private static void setPortActivated(JGoPort port,
426: boolean isActivated) {
427: if (port instanceof BasicCanvasPort) {
428: DrawPort drawnPort = ((BasicCanvasPort) port).getDrawPort();
429: if (drawnPort != null) {
430: drawnPort.setIsActivated(isActivated);
431: }
432: }
433: }
434: }
|