001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.graph.impl;
042:
043: import java.awt.Color;
044:
045: import org.netbeans.modules.sql.framework.ui.graph.IGraphLink;
046: import org.netbeans.modules.sql.framework.ui.graph.IGraphNode;
047: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
048:
049: import com.nwoods.jgo.JGoBrush;
050: import com.nwoods.jgo.JGoLink;
051: import com.nwoods.jgo.JGoPen;
052: import com.nwoods.jgo.JGoPort;
053: import com.nwoods.jgo.JGoSelection;
054:
055: /**
056: * @author Ritesh Adval
057: * @version $Revision$
058: */
059: public class GraphLink extends JGoLink implements IGraphLink {
060:
061: private Object obj;
062: private JGoPen pen;
063: private JGoBrush brush;
064:
065: /** Creates a new instance of GraphLink */
066: public GraphLink(IGraphPort fromP, IGraphPort toP) {
067: super ((JGoPort) fromP, (JGoPort) toP);
068: this .setOrthogonal(true);
069: this .setAvoidsNodes(false);
070: this .setRelinkable(false);
071: this .setArrowHeads(false, true);
072:
073: this .setPen(JGoPen.lightGray);
074: this .setBrush(JGoBrush.lightGray);
075: this .pen = this .getPen();
076: this .brush = this .getBrush();
077: }
078:
079: public void setDefaultPen(JGoPen pen) {
080: this .pen = pen;
081: this .setPen(pen);
082: }
083:
084: public void setDefaultBrush(JGoBrush brush) {
085: this .brush = brush;
086: this .setBrush(brush);
087: }
088:
089: /**
090: * get from port of this link
091: *
092: * @return from port
093: */
094: public IGraphPort getFromGraphPort() {
095: return (IGraphPort) this .getFromPort();
096: }
097:
098: /**
099: * get to port of this link
100: *
101: * @return to port
102: */
103: public IGraphPort getToGraphPort() {
104: return (IGraphPort) this .getToPort();
105: }
106:
107: /**
108: * get data object stored in this link
109: *
110: * @return graph node
111: */
112: public Object getDataObject() {
113: return obj;
114: }
115:
116: /**
117: * set the data object on this link
118: *
119: * @param dObj data object
120: */
121: public void setDataObject(Object dObj) {
122: this .obj = dObj;
123: }
124:
125: /**
126: * start highlighting this link
127: */
128: public void startHighlighting() {
129: this .setPen(JGoPen.make(JGoPen.SOLID, 2, Color.ORANGE));
130: this .setBrush(JGoBrush.makeStockBrush(Color.ORANGE));
131: }
132:
133: /**
134: * stop highlighting this link
135: */
136: public void stopHighlighting() {
137: this .setPen(pen);
138: this .setBrush(brush);
139: }
140:
141: /**
142: * set the link pen
143: *
144: * @param pen pen
145: */
146: public void setGraphLinkPen(JGoPen newPen) {
147: this .pen = newPen;
148: this .setPen(pen);
149: }
150:
151: /**
152: * set the link brush
153: *
154: * @param brush brush
155: */
156: public void setGraphLinkBrush(JGoBrush newBrush) {
157: this .brush = newBrush;
158: this .setBrush(newBrush);
159: }
160:
161: public void gainedSelection(JGoSelection selection) {
162: super .gainedSelection(selection);
163: startHighlighting();
164: }
165:
166: public void lostSelection(JGoSelection selection) {
167: super .lostSelection(selection);
168: stopHighlighting();
169: }
170:
171: public int getMidPoint() {
172: return midPoint;
173: }
174:
175: private int midPoint = -1;
176:
177: private boolean isMidPointTaken(int newMidPoint) {
178: IGraphPort fromPort = this .getFromGraphPort();
179: IGraphNode fromNode = fromPort.getDataNode();
180: java.util.List links = fromNode.getAllLinks();
181:
182: java.util.Iterator it = links.iterator();
183: while (it.hasNext()) {
184: GraphLink link = (GraphLink) it.next();
185: if (link == this ) {
186: continue;
187: }
188: int p = link.getMidPoint();
189: if (p == newMidPoint) {
190: return true;
191: }
192: }
193:
194: return false;
195: }
196:
197: protected int getMidOrthoPosition(int from, int to, boolean vertical) {
198: int pp = from;
199: midPoint = pp;
200:
201: while (isMidPointTaken(midPoint)) {
202: midPoint = midPoint + 10;
203: }
204:
205: return midPoint;
206: }
207:
208: }
|