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.view.graph;
042:
043: import java.awt.Color;
044: import java.awt.Point;
045: import java.awt.Rectangle;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.beans.PropertyChangeEvent;
049: import java.beans.PropertyChangeListener;
050: import java.net.URL;
051: import java.util.Iterator;
052:
053: import javax.swing.ImageIcon;
054: import javax.swing.JMenuItem;
055: import javax.swing.SwingUtilities;
056:
057: import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
058: import org.netbeans.modules.sql.framework.model.SQLCastOperator;
059: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
060: import org.netbeans.modules.sql.framework.ui.graph.IHighlightConfigurator;
061: import org.netbeans.modules.sql.framework.ui.graph.IOperatorField;
062: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfo;
063: import org.netbeans.modules.sql.framework.ui.graph.impl.BasicCellArea;
064: import org.netbeans.modules.sql.framework.ui.graph.impl.CanvasArea;
065: import org.netbeans.modules.sql.framework.ui.graph.impl.HighlightConfiguratorImpl;
066: import org.netbeans.modules.sql.framework.ui.graph.impl.OperatorGraphFieldNode;
067: import org.openide.windows.WindowManager;
068:
069: import com.nwoods.jgo.JGoBrush;
070: import com.nwoods.jgo.JGoPen;
071: import com.nwoods.jgo.JGoText;
072: import com.nwoods.jgo.JGoView;
073: import net.java.hulp.i18n.Logger;
074: import org.netbeans.modules.etl.logger.Localizer;
075: import org.netbeans.modules.etl.logger.LogUtil;
076:
077: /**
078: * @author Ritesh Adval
079: * @version $Revision$
080: */
081: public class SQLCastAsGraphNode extends SQLOperatorGraphNode implements
082: PropertyChangeListener {
083:
084: private static final URL propertiesUrl = SQLCastAsGraphNode.class
085: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/properties.png");
086:
087: private static transient final Logger mLogger = LogUtil
088: .getLogger(SQLCastAsGraphNode.class.getName());
089:
090: private static transient final Localizer mLoc = Localizer.get();
091: /* contains literal value + link node */
092: private OperatorGraphFieldNode valueNode;
093:
094: private JMenuItem propsItem;
095:
096: private static final JGoBrush BRUSH_OUTPUT_HIGHLIGHTED = SQLOperatorGraphNode.BRUSH_INPUT_HIGHLIGHTED;
097:
098: /**
099: * Creates a new instance of SQLLiteralGraphNode, using the given visual configuration
100: * information.
101: *
102: * @param info GUI configuration information
103: */
104: public SQLCastAsGraphNode(IOperatorXmlInfo info) {
105: super (info);
106: }
107:
108: /**
109: * Initializes the UI look and feel based on the given visual configuration
110: * information.
111: *
112: * @param info GUI configuration information
113: */
114: protected void initialize(IOperatorXmlInfo info) {
115: // Collect lists of input graph and parameter fields.
116: for (int i = 0; i < info.getInputCount(); i++) {
117: IOperatorField field = (IOperatorField) info
118: .getInputFields().get(i);
119:
120: // Ignore static field input - type input and precision/scale attributes are set via dialog.
121: if (!field.isStatic()) {
122: OperatorGraphFieldNode fieldNode = new OperatorGraphFieldNode(
123: BasicCellArea.LEFT_PORT_AREA, field);
124: fieldNode.setBrush(BRUSH_INPUT_REGULAR);
125: fieldNode.setTextColor(TEXT_COLOR_INPUT);
126: fieldNode.setLinePen(PEN_DEFAULT);
127: fieldNode.setHighlightEnabled(true);
128: fieldNode.getHighlightConfigurator().setHoverBrush(
129: BRUSH_INPUT_HIGHLIGHTED);
130:
131: fieldList.add(fieldNode);
132: }
133: }
134:
135: Iterator iter = fieldList.iterator();
136: while (iter.hasNext()) {
137: CanvasArea fieldNode = (CanvasArea) iter.next();
138: this .addObjectAtTail(fieldNode);
139: }
140:
141: //add one output graph field
142: for (int i = 0; i < info.getOutputCount(); i++) {
143: IOperatorField field = (IOperatorField) info
144: .getOutputFields().get(i);
145:
146: valueNode = new CastTypeFieldNode(
147: BasicCellArea.RIGHT_PORT_AREA, field);
148: valueNode.addPropertyChangeListener(this );
149: valueNode.setTextAlignment(JGoText.ALIGN_CENTER);
150: valueNode.setLinePen(JGoPen.makeStockPen(Color.WHITE));
151:
152: valueNode.setTextColor(TEXT_COLOR_LITERAL);
153: valueNode.setTextEditable(false);
154: valueNode.setHighlightEnabled(true);
155:
156: this .addObjectAtTail(valueNode);
157: fieldList.add(valueNode);
158: }
159:
160: this .setResizable(true);
161: }
162:
163: /**
164: * Extends OperatorGraphFieldNode to allow for activation of properties dialog via
165: * double-click mouse gesture.
166: *
167: * @author Jonathan Giron
168: * @version $Revision$
169: */
170: class CastTypeFieldNode extends OperatorGraphFieldNode {
171: public CastTypeFieldNode(int type, IOperatorField field) {
172: super (type, field);
173: }
174:
175: /**
176: * Overrides parent implementation to display CastAsDialog.
177: *
178: * @see com.nwoods.jgo.JGoObject#doMouseDblClick(int, java.awt.Point, java.awt.Point, com.nwoods.jgo.JGoView)
179: */
180: public boolean doMouseDblClick(int modifiers, Point dc,
181: Point vc, JGoView aView) {
182: SwingUtilities.invokeLater(new Runnable() {
183: public void run() {
184: SQLCastOperator castOp = (SQLCastOperator) dataObject;
185: if (castOp != null
186: && isEditable(castOp.getJdbcType())) {
187: SQLCastAsGraphNode.this .showCastAsDialog();
188: }
189: }
190: });
191: return true;
192: }
193: }
194:
195: /* (non-Javadoc)
196: * @see org.netbeans.modules.sql.framework.ui.view.graph.SQLOperatorGraphNode#initializePopUpMenu()
197: */
198: protected void initializePopUpMenu() {
199: super .initializePopUpMenu();
200:
201: // Properties menu
202: popUpMenu.addSeparator();
203: String nbBundle1 = mLoc.t("PRSR001: Properties");
204: propsItem = new JMenuItem(Localizer.parse(nbBundle1),
205: new ImageIcon(propertiesUrl));
206: propsItem.addActionListener(new ActionListener() {
207: public void actionPerformed(ActionEvent e) {
208: SQLCastAsGraphNode.this .showCastAsDialog();
209: }
210: });
211: popUpMenu.add(propsItem);
212: }
213:
214: /**
215: * @param jdbcType
216: * @return
217: */
218: private boolean isEditable(int jdbcType) {
219: return SQLUtils.isPrecisionRequired(jdbcType);
220: }
221:
222: /**
223: * Gets maximum height
224: *
225: * @return max height
226: */
227: public int getMaximumHeight() {
228: return super .getMaximumHeight();
229: }
230:
231: /**
232: * @see org.netbeans.modules.sql.framework.ui.graph.ICanvasInterface#getMaximumWidth()
233: */
234: public int getMaximumWidth() {
235: // Establish max width of 'literal' label, other components in parent, versus type
236: // label.
237: return Math.max(super .getMaximumWidth(), valueNode
238: .getMaximumWidth());
239: }
240:
241: /**
242: * @see org.netbeans.modules.sql.framework.ui.graph.ICanvasInterface#getMinimumWidth()
243: */
244: public int getMinimumWidth() {
245: return Math.max(super .getMinimumWidth(), valueNode
246: .getMinimumWidth());
247: }
248:
249: /**
250: * Gets output graph port, given its field name.
251: *
252: * @param fieldName field name
253: * @return graph port
254: */
255: public IGraphPort getOutputGraphPort(String fieldName) {
256: return valueNode.getRightGraphPort();
257: }
258:
259: /**
260: * Sets data object for which this UI element provides a view.
261: *
262: * @param obj data object
263: */
264: public void setDataObject(Object obj) {
265: this .dataObject = obj;
266:
267: updateValueNode();
268: }
269:
270: /**
271: * layout the children
272: */
273: public void layoutChildren() {
274: Rectangle boundingRect = new Rectangle(this .getBoundingRect());
275:
276: int minLayoutWidth = getMinimumWidth();
277: if (boundingRect.width < minLayoutWidth || !isExpandedState()) {
278: boundingRect.width = minLayoutWidth;
279: setBoundingRect(boundingRect);
280: }
281:
282: columnRect.setBoundingRect(boundingRect);
283:
284: int rectleft = getLeft();
285: int recttop = getTop();
286: int rectwidth = getWidth();
287: int rectheight = getHeight();
288:
289: int left = rectleft + insets.left;
290: int top = recttop + insets.top;
291: int width = rectwidth - insets.left - insets.right;
292: int height = rectheight - insets.top - insets.bottom;
293:
294: titleArea.setBoundingRect(left, top, width, titleArea
295: .getMinimumHeight());
296:
297: int aggrHeight = top + titleArea.getMinimumHeight();
298:
299: for (int i = 0; i < fieldList.size(); i++) {
300: OperatorGraphFieldNode fieldNode = (OperatorGraphFieldNode) fieldList
301: .get(i);
302:
303: if (aggrHeight < top + height) {
304: fieldNode.setVisible(true);
305: fieldNode.setBoundingRect(left, aggrHeight, width,
306: fieldNode.getHeight() + verticalGap);
307: } else {
308: fieldNode.setVisible(false);
309: fieldNode.setBoundingRect(left, top, width, fieldNode
310: .getHeight()
311: + verticalGap);
312: }
313: aggrHeight += fieldNode.getHeight() + verticalGap;
314: }
315: }
316:
317: /**
318: * Handles change to a bound property as indicated by the given event.
319: *
320: * @param evt A PropertyChangeEvent object describing the event source and the
321: * property that has changed.
322: */
323: public void propertyChange(PropertyChangeEvent evt) {
324: }
325:
326: private SQLCastOperator getCastOperator() {
327: return (SQLCastOperator) dataObject;
328: }
329:
330: private void updateValueNode() {
331: SQLCastOperator castOp = getCastOperator();
332: IHighlightConfigurator hc = new HighlightConfiguratorImpl(
333: BRUSH_OUTPUT_REGULAR, BRUSH_OUTPUT_HIGHLIGHTED);
334: Color textColor = TEXT_COLOR_LITERAL;
335:
336: if (castOp != null) {
337: try {
338: int jdbcType = castOp.getJdbcType();
339: int precision = castOp.getPrecision();
340: int scale = castOp.getScale();
341:
342: String sqlTypeStr = SQLUtils.getStdSqlType(jdbcType);
343: StringBuilder labelBuffer = new StringBuilder(
344: sqlTypeStr);
345:
346: if (SQLUtils.isPrecisionRequired(jdbcType)) {
347: labelBuffer.append("(");
348: labelBuffer.append(precision);
349: if (SQLUtils.isScaleRequired(jdbcType) && scale > 0) {
350: labelBuffer.append(",").append(scale);
351: }
352: labelBuffer.append(")");
353: } else {
354: hc.setHoverBrush(BRUSH_OUTPUT_REGULAR);
355: textColor = TEXT_COLOR_RESULT;
356: }
357:
358: valueNode.setText(labelBuffer.toString());
359: valueNode.setTextColor(textColor);
360: valueNode.setHighlightConfigurator(hc);
361: } catch (IllegalArgumentException ignore) {
362: String nbBundle2 = mLoc.t("PRSR001: Unknown");
363: final String typeLabel = Localizer.parse(nbBundle2);
364: valueNode.setText(typeLabel);
365: }
366:
367: if (propsItem != null) {
368: propsItem.setEnabled(isEditable(castOp.getJdbcType()));
369: }
370: }
371:
372: this .layoutChildren();
373: }
374:
375: public void showCastAsDialog() {
376: String nbBundle3 = mLoc
377: .t("PRSR001: Cast-As Operator Properties");
378: CastAsDialog castDlg = new CastAsDialog(WindowManager
379: .getDefault().getMainWindow(), Localizer
380: .parse(nbBundle3), true);
381:
382: SQLCastOperator castOp = (SQLCastOperator) dataObject;
383:
384: castDlg.setJdbcType(castOp.getJdbcType());
385: castDlg.setJdbcTypeEditable(false);
386: castDlg.setPrecision(castOp.getPrecision());
387: castDlg.setScale(castOp.getScale());
388:
389: castDlg.show();
390: if (castDlg.isCanceled()) {
391: return;
392: }
393:
394: castOp.setJdbcType(castDlg.getJdbcType());
395: castOp.setPrecision(castDlg.getPrecision());
396: castOp.setScale(castDlg.getScale());
397:
398: updateValueNode();
399: }
400: }
|