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.Graphics2D;
044: import java.awt.Insets;
045: import java.awt.Point;
046: import java.awt.Rectangle;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.util.ArrayList;
050: import java.util.List;
051:
052: import org.netbeans.modules.sql.framework.ui.graph.ICanvasInterface;
053: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
054:
055: import com.nwoods.jgo.JGoArea;
056: import com.nwoods.jgo.JGoLink;
057: import com.nwoods.jgo.JGoListPosition;
058: import com.nwoods.jgo.JGoObject;
059: import com.nwoods.jgo.JGoPort;
060: import com.nwoods.jgo.JGoView;
061:
062: /**
063: * @author Ritesh Adval
064: * @version $Revision$
065: */
066: public class CanvasArea extends JGoArea implements ICanvasInterface {
067:
068: private String toolTip;
069:
070: private ArrayList listeners = new ArrayList();
071:
072: /**
073: * The insets around this area
074: */
075: protected Insets insets = new Insets(0, 0, 0, 0);
076:
077: /** Creates a new instance of CanvasArea */
078: public CanvasArea() {
079: super ();
080: //by default any canvas area is not selectable
081: //not resizeable and also does not grab selection of non selectable
082: //children
083: this .setSelectable(false);
084: this .setResizable(false);
085: }
086:
087: /**
088: * set the tool tip
089: *
090: * @param toolTip
091: */
092: public void setToolTipText(String tTip) {
093: this .toolTip = tTip;
094: }
095:
096: /**
097: * get the tooltip text
098: *
099: * @return tooltip text
100: */
101: @Override
102: public String getToolTipText() {
103: return toolTip;
104: }
105:
106: /**
107: * set the insets of this area
108: *
109: * @param insets -
110: */
111: public void setInsets(Insets insets) {
112: this .insets = insets;
113: }
114:
115: /**
116: * get the insets
117: *
118: * @return insets
119: */
120: public Insets getInsets() {
121: return this .insets;
122: }
123:
124: /**
125: * remove all the children from this area
126: */
127: public void removeAll() {
128: JGoListPosition pos = this .getFirstObjectPos();
129: while (pos != null) {
130: JGoObject obj = this .getObjectAtPos(pos);
131: if (obj instanceof CanvasArea) {
132: ((CanvasArea) obj).removeAll();
133: }
134: this .removeObject(obj);
135: pos = this .getNextObjectPos(pos);
136: }
137:
138: }
139:
140: /**
141: * handle geometry change
142: *
143: * @param prevRect previous bounds rectangle
144: */
145: @Override
146: protected void geometryChange(Rectangle prevRect) {
147: // handle any size changes by repositioning all the items
148: if (prevRect.width != getWidth()
149: || prevRect.height != getHeight()) {
150: layoutChildren();
151: } else {
152: super .geometryChange(prevRect);
153: }
154: }
155:
156: /**
157: * get maximum height
158: *
159: * @return max height
160: */
161: public int getMaximumHeight() {
162: return this .getHeight();
163: }
164:
165: /**
166: * get the maximum width
167: *
168: * @return max width
169: */
170: public int getMaximumWidth() {
171: return this .getWidth();
172: }
173:
174: /**
175: * get the minimum height
176: *
177: * @return min height
178: */
179: public int getMinimumHeight() {
180: return this .getHeight();
181: }
182:
183: /**
184: * get the minimum width
185: *
186: * @return min width
187: */
188: public int getMinimumWidth() {
189: return this .getWidth();
190: }
191:
192: /**
193: * layout the children
194: */
195: public void layoutChildren() {
196: }
197:
198: /**
199: * add a property change listener
200: *
201: * @param l listener
202: */
203: public void addPropertyChangeListener(PropertyChangeListener l) {
204: listeners.add(l);
205: }
206:
207: /**
208: * remove a property change listener
209: *
210: * @param l listener
211: */
212: public void removePropertyChangeListener(PropertyChangeListener l) {
213: listeners.remove(l);
214: }
215:
216: protected synchronized void firePropertyChangeEvent(
217: String propertyName, Object oldValue, Object newValue) {
218:
219: PropertyChangeEvent evt = new PropertyChangeEvent(this ,
220: propertyName, oldValue, newValue);
221:
222: for (int i = 0; i < listeners.size(); i++) {
223: PropertyChangeListener l = (PropertyChangeListener) listeners
224: .get(i);
225: l.propertyChange(evt);
226: }
227: }
228:
229: /**
230: * add link of a port in the list
231: *
232: * @param gPort port
233: * @param list list
234: */
235: protected void addLinks(IGraphPort gPort, List<JGoLink> list) {
236: if (gPort == null) {
237: return;
238: }
239:
240: JGoPort port = (JGoPort) gPort;
241: JGoListPosition pos = port.getFirstLinkPos();
242: while (pos != null) {
243: JGoLink link = port.getLinkAtPos(pos);
244: list.add(link);
245: pos = port.getNextLinkPos(pos);
246: }
247: }
248:
249: /**
250: * Overrides default implementation of handleResize to enforce minimum dimensions when
251: * user attempts to resize an object.
252: *
253: * @param g the graphics context to draw on
254: * @param view the view we're being resizing in
255: * @param origRect the object's original bounding rectangle
256: * @param newPoint the location of the new point
257: * @param whichHandle the handle number of the point being moved
258: * @param event one of: JGoView.EventMouseUp, JGoView.EventMouseMove,
259: * JGoView.EventMouseDown
260: * @param minWidth the minimum width of the object (defaults to zero)
261: * @param minHeight the minimum height of the object (defaults to zero)
262: * @return Rectangle representing location and dimension of resize outline; null if
263: * outline is to be handled by application
264: */
265: protected Rectangle handleResize(Graphics2D g, JGoView view,
266: Rectangle origRect, Point newPoint, int whichHandle,
267: int event, int minWidth, int minHeight) {
268: return super.handleResize(g, view, origRect, newPoint,
269: whichHandle, event, getMinimumWidth(),
270: getMaximumHeight());
271: }
272: }
|