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:
042: package org.netbeans.modules.sql.framework.ui.graph.impl;
043:
044: import java.awt.Color;
045: import java.awt.Dimension;
046: import java.awt.Point;
047: import java.awt.Rectangle;
048:
049: import javax.swing.Icon;
050:
051: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
052:
053: import com.nwoods.jgo.JGoArea;
054: import com.nwoods.jgo.JGoBrush;
055: import com.nwoods.jgo.JGoDrawable;
056: import com.nwoods.jgo.JGoEllipse;
057: import com.nwoods.jgo.JGoObject;
058: import com.nwoods.jgo.JGoPen;
059: import com.nwoods.jgo.JGoPort;
060: import com.nwoods.jgo.JGoRectangle;
061: import com.nwoods.jgo.JGoView;
062:
063: /**
064: * @author Ritesh Adval
065: * @version $Revision$
066: */
067: public class PortArea extends JGoArea {
068:
069: private JGoRectangle rect;
070: private GraphPort port;
071: private JGoPen arrowPen;
072: private JGoBrush arrowBrush;
073:
074: private JGoPen currentArrowPen;
075: private JGoBrush currentArrowBrush;
076:
077: private boolean drawBoundingRect = false;
078:
079: /**
080: * the out port for link
081: */
082: private JGoEllipse mShape;
083:
084: private Dimension portDrawableSize = new Dimension(7, 7);
085:
086: private boolean arrowPort = true;
087:
088: private boolean drawArrowPort = true;
089:
090: /** Creates a new instance of PortArea */
091: public PortArea() {
092: super ();
093: this .setSelectable(false);
094: this .setResizable(false);
095:
096: if (arrowPort) {
097: initializeArrowPort();
098: } else {
099: initializeWithoutArrowPort();
100: }
101:
102: }
103:
104: private void initializeWithoutArrowPort() {
105: port = new GraphPort();
106: port.setStyle(JGoPort.StyleHidden);
107: port.setValidDestination(true);
108: port.setValidSource(true);
109: this .addObjectAtTail(port);
110:
111: mShape = new JGoEllipse();
112: mShape.setSize(new Dimension(5, 5));
113: mShape.setDraggable(false);
114: mShape.setSelectable(false);
115: mShape.setResizable(false);
116: mShape.setPen(JGoPen.makeStockPen(Color.lightGray));
117: mShape.setBrush(JGoBrush.makeStockBrush(Color.lightGray));
118:
119: this .addObjectAtHead(mShape);
120: }
121:
122: private void initializeArrowPort() {
123: //add bounding rectangle
124: rect = new JGoRectangle();
125: rect.setSelectable(false);
126: rect.setResizable(false);
127:
128: rect.setPen(JGoPen.makeStockPen(Color.lightGray));
129: rect
130: .setBrush(JGoBrush.makeStockBrush(new Color(254, 253,
131: 235)));
132: //rect.setSize(20, 16);
133: this .addObjectAtHead(rect);
134:
135: //add port which will be hidden
136: port = new GraphPort();
137: port.setStyle(JGoPort.StyleHidden);
138: //default port can be a source and destination of a link
139: port.setValidSource(true);
140: port.setValidDestination(true);
141: this .addObjectAtTail(port);
142:
143: //set the default pen and brush for arrows
144: this .setDefaultArrowPen(JGoPen.makeStockPen(new Color(73, 117,
145: 183)));
146: this .setDefaultArrowBrush(JGoBrush
147: .makeStockBrush(Color.lightGray));
148:
149: this .setSize(20, 20);
150: }
151:
152: /**
153: * set whether to draw a bounding rectangle
154: *
155: * @param draw draw
156: */
157: public void setDrawBoundingRect(boolean draw) {
158: this .drawBoundingRect = draw;
159: }
160:
161: /**
162: * is there a bounding rectangle drawn around this area
163: *
164: * @return whether a bounding rectangle is drawn
165: */
166: public boolean isDrawBoundingRect() {
167: return drawBoundingRect;
168: }
169:
170: /**
171: * user should call this method if this port area needs to be a source only of a link
172: *
173: * @param source boolean
174: */
175: public void setValidSource(boolean source) {
176: port.setValidSource(source);
177: }
178:
179: /**
180: * user should call this method if this port area needs to be a destination only of a
181: * link
182: *
183: * @param destination boolean
184: */
185: public void setValidDestination(boolean destination) {
186: port.setValidDestination(destination);
187: }
188:
189: /**
190: * set the from spot
191: *
192: * @param s from spot constants
193: */
194: public void setFromSpot(int s) {
195: port.setFromSpot(s);
196: }
197:
198: /**
199: * set the to spot
200: *
201: * @param s to spot constants
202: */
203: public void setToSpot(int s) {
204: port.setToSpot(s);
205: }
206:
207: /**
208: * get the graph port area
209: *
210: * @return graph port area
211: */
212: public IGraphPort getGraphPort() {
213: return port;
214: }
215:
216: /**
217: * set the icon in the area
218: *
219: * @param icon icon
220: */
221: public void setIcon(Icon icon) {
222: }
223:
224: public void setDefaultArrowPen(JGoPen pen) {
225: this .arrowPen = pen;
226: this .setCurrentArrowPen(pen);
227: }
228:
229: public void setDefaultArrowBrush(JGoBrush brush) {
230: this .arrowBrush = brush;
231: this .setCurrentArrowBrush(brush);
232: }
233:
234: public JGoPen getDefaultArrowPen() {
235: return this .arrowPen;
236: }
237:
238: public JGoBrush getDefaultArrowBrush() {
239: return this .arrowBrush;
240: }
241:
242: public void setCurrentArrowPen(JGoPen pen) {
243: this .currentArrowPen = pen;
244: }
245:
246: public void setCurrentArrowBrush(JGoBrush brush) {
247: this .currentArrowBrush = brush;
248: }
249:
250: public JGoPen getCurrentArrowPen() {
251: return this .currentArrowPen;
252: }
253:
254: public JGoBrush getCurrentArrowBrush() {
255: return this .currentArrowBrush;
256: }
257:
258: /**
259: * paint this area and draw arrow for port
260: *
261: * @param g graphics
262: * @param view view
263: */
264: public void paint(java.awt.Graphics2D g, JGoView view) {
265: super .paint(g, view);
266:
267: if (!drawArrowPort) {
268: return;
269: }
270:
271: int wGap = this .getWidth() - portDrawableSize.width;
272: int hGap = this .getHeight() - portDrawableSize.height;
273:
274: //triangles right side point
275: // ...>.
276: int x1 = this .getLeft() + this .getWidth() - wGap / 2;
277: int y1 = this .getTop() + this .getHeight() / 2;
278:
279: //triangles' top left point
280: int x2 = this .getLeft() + wGap / 2;
281: int y2 = this .getTop() + hGap / 2;
282:
283: //triangles' bottom left point
284: int x3 = x2;
285: int y3 = this .getTop() + this .getWidth() - hGap / 2;
286:
287: int[] xPoints = { x1, x2, x3 };
288: int[] yPoints = { y1, y2, y3 };
289:
290: JGoDrawable.drawPolygon(g, getCurrentArrowPen(),
291: getCurrentArrowBrush(), xPoints, yPoints, 3);
292:
293: }
294:
295: /**
296: * override this method to handle the changes in the geometry of this area we will lay
297: * out all the children again again
298: *
299: * @param prevRect previous rectangle bounds
300: */
301: protected void geometryChange(Rectangle prevRect) {
302: // handle any size changes by repositioning all the items
303: if (prevRect.width != getWidth()
304: || prevRect.height != getHeight()) {
305: layoutChildren();
306: } else {
307: super .geometryChange(prevRect);
308: }
309: }
310:
311: /**
312: * layout the children of this cell area
313: */
314: protected void layoutChildren() {
315: if (arrowPort) {
316: layoutChildrenForArrowPort();
317: } else {
318: layoutChildrenWithoutArrowPort();
319: }
320:
321: }
322:
323: private void layoutChildrenForArrowPort() {
324: Rectangle rectangle = this .getBoundingRect();
325: if (isDrawBoundingRect()) {
326: rect.setBoundingRect(rectangle);
327: }
328: port.setBoundingRect(rectangle);
329: }
330:
331: private void layoutChildrenWithoutArrowPort() {
332: Rectangle rectangle = this .getBoundingRect();
333: port.setBoundingRect(rectangle);
334: mShape
335: .setSpotLocation(JGoObject.Center, this ,
336: JGoObject.Center);
337: }
338:
339: public boolean doMouseEntered(int modifiers, Point dc, Point vc,
340: JGoView view) {
341: this .setCurrentArrowPen(JGoPen.makeStockPen(Color.orange));
342: this .update();
343: return true;
344: }
345:
346: public boolean doMouseExited(int modifiers, Point dc, Point vc,
347: JGoView view) {
348: this .setCurrentArrowPen(this .getDefaultArrowPen());
349: this .update();
350: return true;
351: }
352:
353: public void setDrawPortArrow(boolean draw) {
354: this .drawArrowPort = draw;
355: }
356:
357: public void setBackgroundColor(Color c) {
358: if (rect != null) {
359: rect.setBrush(JGoBrush.makeStockBrush(c));
360: }
361: }
362: }
|