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: import java.awt.Rectangle;
045: import java.util.ArrayList;
046: import java.util.List;
047:
048: import javax.swing.Icon;
049:
050: import org.netbeans.modules.sql.framework.ui.graph.IGraphFieldNode;
051: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
052: import org.netbeans.modules.sql.framework.ui.graph.IOperatorField;
053: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfo;
054:
055: import com.nwoods.jgo.JGoBrush;
056: import com.nwoods.jgo.JGoObject;
057: import com.nwoods.jgo.JGoPen;
058: import com.nwoods.jgo.JGoRectangle;
059: import com.nwoods.jgo.JGoText;
060:
061: /**
062: * @author Ritesh Adval
063: * @version $Revision$
064: */
065: public class OperatorGraphNode extends BasicCanvasArea {
066:
067: private static final GradientBrush BRUSH_TITLE = new GradientBrush(
068: new Color(221, 221, 255), // light
069: // magenta
070: new Color(160, 186, 213)); // navy
071:
072: private static final Color DEFAULT_BG_COLOR = new Color(254, 253,
073: 235);
074:
075: private static final JGoPen PEN_DEFAULT = JGoPen
076: .makeStockPen(Color.WHITE);
077:
078: protected ArrayList fieldList = new ArrayList();;
079: protected int verticalGap = 0;
080: protected JGoRectangle columnRect;
081:
082: protected OperatorGraphFieldNode resultField;
083:
084: public OperatorGraphNode(String displayName, String toolTip,
085: Icon icon) {
086: super ();
087: columnRect = new JGoRectangle();
088: columnRect.setSelectable(true);
089: columnRect.setPen(PEN_DEFAULT);
090: columnRect.setBrush(JGoBrush.makeStockBrush(DEFAULT_BG_COLOR));
091: addObjectAtHead(columnRect);
092:
093: titleArea = new TitleArea(displayName);
094: titleArea.setShowDot(false);
095: titleArea.setTextAndExpandedImgGap(10);
096: titleArea.setToolTipText(toolTip);
097: titleArea.setTitleImage(icon);
098: titleArea.setPen(PEN_DEFAULT);
099: titleArea.setBrush(BRUSH_TITLE);
100: this .addObjectAtTail(titleArea);
101:
102: this .setResizable(false);
103: this .setSelectable(true);
104: this .setPickableBackground(true);
105:
106: // RJR - remove setting of fieldlist which was causing a NPE
107: }
108:
109: /**
110: * Creates a new instance of OperatorGraphNode using GUI info contained in the given
111: * configuration object.
112: *
113: * @param info IOperatorXmlInfo object containing operator layout info.
114: */
115: public OperatorGraphNode(IOperatorXmlInfo info) {
116: this (info.getDisplayName(), info.getToolTip(), info.getIcon());
117: }
118:
119: /**
120: * Initialize this instance using GUI info contained in the given configuration
121: * object.
122: *
123: * @param info IOperatorXmlInfo object containing operator layout info.
124: */
125: protected void initialize(IOperatorXmlInfo info) {
126: // add input graph field
127: for (int i = 0; i < info.getInputCount(); i++) {
128: IOperatorField field = (IOperatorField) info
129: .getInputFields().get(i);
130:
131: OperatorGraphFieldNode fieldNode = new OperatorGraphFieldNode(
132: BasicCellArea.LEFT_PORT_AREA, field);
133:
134: this .addObjectAtTail(fieldNode);
135: fieldList.add(fieldNode);
136: }
137:
138: // add output graph field
139: for (int i = 0; i < info.getOutputCount(); i++) {
140: IOperatorField field = (IOperatorField) info
141: .getOutputFields().get(i);
142:
143: OperatorGraphFieldNode fieldNode = new OperatorGraphFieldNode(
144: BasicCellArea.RIGHT_PORT_AREA, field,
145: JGoText.ALIGN_CENTER);
146:
147: // there is only one result field for operator
148: resultField = fieldNode;
149: this .addObjectAtTail(fieldNode);
150: fieldList.add(fieldNode);
151: }
152: }
153:
154: /**
155: * Gets the minimum height of this area
156: *
157: * @return minimum height
158: */
159: public int getMinimumHeight() {
160: int minHeight = 0;
161: minHeight = getInsets().top + getInsets().bottom;
162: minHeight += titleArea.getMinimumHeight();
163:
164: return minHeight;
165: }
166:
167: /**
168: * Gets maximum height
169: *
170: * @return max height
171: */
172: public int getMaximumHeight() {
173: int maxHeight = 0;
174: maxHeight = getInsets().top + getInsets().bottom;
175: maxHeight += titleArea.getMaximumHeight();
176:
177: for (int i = 0; i < fieldList.size(); i++) {
178: CanvasArea fieldNode = (CanvasArea) fieldList.get(i);
179: maxHeight += fieldNode.getMaximumHeight() + verticalGap;
180: }
181:
182: return maxHeight;
183: }
184:
185: /**
186: * Gets the maximum width
187: *
188: * @return max width
189: */
190: public int getMinimumWidth() {
191: int minWidth = titleArea.getMinimumWidth();
192:
193: for (int i = 0; i < fieldList.size(); i++) {
194: CanvasArea fieldNode = (CanvasArea) fieldList.get(i);
195:
196: int w = fieldNode.getMinimumWidth();
197: if (w > minWidth) {
198: minWidth = w;
199: }
200: }
201:
202: minWidth += getInsets().left + getInsets().right;
203: return minWidth;
204: }
205:
206: public int getMaximumWidth() {
207: int maxWidth = titleArea.getMaximumWidth();
208:
209: for (int i = 0; i < fieldList.size(); i++) {
210: CanvasArea fieldNode = (CanvasArea) fieldList.get(i);
211:
212: int w = fieldNode.getMaximumWidth();
213: if (w > maxWidth) {
214: maxWidth = w;
215: }
216: }
217:
218: maxWidth += getInsets().left + getInsets().right;
219: return maxWidth;
220: }
221:
222: /**
223: * Constrains instance to the maximum height; do not allow resize in height. Also,
224: * enforces minimum width for the operator.
225: *
226: * @param left left
227: * @param top top
228: * @param width width
229: * @param height height
230: */
231: public void setBoundingRect(int left, int top, int width, int height) {
232: super .setBoundingRect(left, top, width, Math.min(height,
233: getMaximumHeight()));
234: }
235:
236: protected boolean geometryChangeChild(JGoObject child,
237: Rectangle prevRect) {
238: // do nothing as we do not want to listen to changes in children
239: return true;
240: }
241:
242: /**
243: * Handles geometry change
244: *
245: * @param prevRect previous bounds rectangle
246: */
247: protected void geometryChange(Rectangle prevRect) {
248: // handle any size changes by repositioning all the items
249: if (prevRect.width != getWidth()
250: || prevRect.height != getHeight()) {
251: layoutChildren();
252: } else {
253: super .geometryChange(prevRect);
254: }
255: }
256:
257: /**
258: * Lays out this area's child objects.
259: */
260: public void layoutChildren() {
261: columnRect.setBoundingRect(this .getBoundingRect());
262:
263: int rectleft = getLeft();
264: int recttop = getTop();
265: int rectwidth = getWidth();
266: int rectheight = getHeight();
267:
268: int left = rectleft + insets.left;
269: int top = recttop + insets.top;
270: int width = rectwidth - insets.left - insets.right;
271: int height = rectheight - insets.top - insets.bottom;
272:
273: titleArea.setBoundingRect(left, top, width, titleArea
274: .getMinimumHeight());
275:
276: int aggrHeight = top + titleArea.getMinimumHeight();
277:
278: for (int i = 0; i < fieldList.size(); i++) {
279: CanvasArea fieldNode = (CanvasArea) fieldList.get(i);
280:
281: if (aggrHeight < top + height) {
282: fieldNode.setVisible(true);
283: fieldNode.setBoundingRect(left, aggrHeight, width,
284: fieldNode.getHeight() + verticalGap);
285: } else {
286: fieldNode.setVisible(false);
287: fieldNode.setBoundingRect(left, top, width, fieldNode
288: .getHeight()
289: + verticalGap);
290: }
291: aggrHeight += fieldNode.getHeight() + verticalGap;
292: }
293: }
294:
295: /**
296: * Gets field name associated with the given port
297: *
298: * @param graphPort graph port
299: * @return field name
300: */
301: public String getFieldName(IGraphPort graphPort) {
302: for (int i = 0; i < fieldList.size(); i++) {
303: IGraphFieldNode fieldNode = (IGraphFieldNode) fieldList
304: .get(i);
305:
306: if (graphPort.equals(fieldNode.getLeftGraphPort())
307: || graphPort.equals(fieldNode.getRightGraphPort())) {
308: return fieldNode.getName();
309: }
310: }
311: return null;
312: }
313:
314: /**
315: * Gets input graph port, if any, associated with the given field name
316: *
317: * @param fieldName field name
318: * @return graph port
319: */
320: public IGraphPort getInputGraphPort(String fieldName) {
321:
322: if (fieldName == null) {
323: return null;
324: }
325:
326: for (int i = 0; i < fieldList.size(); i++) {
327: IGraphFieldNode fieldNode = (IGraphFieldNode) fieldList
328: .get(i);
329:
330: if (fieldName.equals(fieldNode.getName())) {
331: return fieldNode.getLeftGraphPort();
332: }
333: }
334: return null;
335: }
336:
337: /**
338: * Gets output graph port, if any, associated with the given field name
339: *
340: * @param fieldName field name
341: * @return graph port
342: */
343: public IGraphPort getOutputGraphPort(String fieldName) {
344: return resultField.getRightGraphPort();
345: }
346:
347: /**
348: * Gets object, if any, associated with the given port
349: *
350: * @param graphPort graph port
351: * @return object for the given port
352: */
353: Object getObject(IGraphPort graphPort) {
354: return null;
355: }
356:
357: /**
358: * Gets port, if any, associated with the given object
359: *
360: * @param graphPort graph port
361: * @return object for the given port
362: */
363: IGraphPort getGraphPort(Object obj) {
364: return null;
365: }
366:
367: /**
368: * Gets a list of all input and output links
369: *
370: * @return list of input links
371: */
372: public List getAllLinks() {
373: ArrayList list = new ArrayList();
374:
375: for (int i = 0; i < fieldList.size(); i++) {
376: IGraphFieldNode fieldNode = (IGraphFieldNode) fieldList
377: .get(i);
378: IGraphPort port = fieldNode.getLeftGraphPort();
379: if (port != null) {
380: addLinks(port, list);
381: }
382: port = fieldNode.getRightGraphPort();
383: if (port != null) {
384: addLinks(port, list);
385: }
386: }
387:
388: return list;
389: }
390: }
|