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.Rectangle;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.net.URL;
048: import java.sql.Types;
049:
050: import javax.swing.Icon;
051: import javax.swing.ImageIcon;
052: import javax.swing.SwingUtilities;
053:
054: import org.netbeans.modules.sql.framework.common.jdbc.SQLUtils;
055: import org.netbeans.modules.sql.framework.model.SQLLiteral;
056: import org.netbeans.modules.sql.framework.model.VisibleSQLLiteral;
057: import org.netbeans.modules.sql.framework.ui.graph.IGraphPort;
058: import org.netbeans.modules.sql.framework.ui.graph.IOperatorField;
059: import org.netbeans.modules.sql.framework.ui.graph.IOperatorXmlInfo;
060: import org.netbeans.modules.sql.framework.ui.graph.impl.BasicCellArea;
061: import org.netbeans.modules.sql.framework.ui.graph.impl.CanvasArea;
062: import org.netbeans.modules.sql.framework.ui.graph.impl.OperatorGraphFieldNode;
063:
064: import com.nwoods.jgo.JGoPen;
065: import com.nwoods.jgo.JGoRectangle;
066: import com.nwoods.jgo.JGoText;
067: import net.java.hulp.i18n.Logger;
068: import org.netbeans.modules.etl.logger.Localizer;
069: import org.netbeans.modules.etl.logger.LogUtil;
070:
071: /**
072: * Graphical representation of literal element.
073: *
074: * @author Ritesh Adval
075: * @author Jonathan Giron
076: * @version $Revision$
077: */
078: public class SQLLiteralGraphNode extends SQLOperatorGraphNode implements
079: PropertyChangeListener {
080:
081: /* contains literal value + link node */
082: private OperatorGraphFieldNode valueNode;
083:
084: private static final URL URL_NUMBER_ICON = SQLLiteralGraphNode.class
085: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/Math.png");
086: private static final URL URL_DATE_ICON = SQLLiteralGraphNode.class
087: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/NOW2.png");
088: private static final URL URL_TEXT_ICON = SQLLiteralGraphNode.class
089: .getResource("/org/netbeans/modules/sql/framework/ui/resources/images/literal.png");
090: private static transient final Logger mLogger = LogUtil
091: .getLogger(SQLLiteralGraphNode.class.getName());
092: private static transient final Localizer mLoc = Localizer.get();
093: private static Icon numberIcon;
094:
095: private static Icon dateIcon;
096:
097: private static Icon textIcon;
098:
099: /**
100: * Creates a new instance of SQLLiteralGraphNode, using the given visual configuration
101: * information.
102: *
103: * @param info GUI configuration information
104: */
105: public SQLLiteralGraphNode(IOperatorXmlInfo info) {
106: super (info);
107: }
108:
109: /**
110: * Initializes the UI look and feel based on the given visual configuration
111: * information.
112: *
113: * @param info GUI configuration information
114: */
115: protected void initialize(IOperatorXmlInfo info) {
116: //add one output graph field
117: for (int i = 0; i < info.getOutputCount(); i++) {
118: IOperatorField field = (IOperatorField) info
119: .getOutputFields().get(i);
120:
121: valueNode = new OperatorGraphFieldNode(
122: BasicCellArea.RIGHT_PORT_AREA, field);
123: valueNode.addPropertyChangeListener(this );
124: valueNode.setTextAlignment(JGoText.ALIGN_CENTER);
125: valueNode.setLinePen(JGoPen.makeStockPen(Color.WHITE));
126: valueNode.setBrush(BRUSH_OUTPUT_REGULAR);
127: valueNode.setTextColor(TEXT_COLOR_LITERAL);
128:
129: this .addObjectAtTail(valueNode);
130: fieldList.add(valueNode);
131: }
132:
133: this .setResizable(true);
134: }
135:
136: /**
137: * get maximum height
138: *
139: * @return max height
140: */
141: public int getMaximumHeight() {
142: return super .getMaximumHeight();
143: }
144:
145: /**
146: * @see org.netbeans.modules.sql.framework.ui.graph.ICanvasInterface#getMaximumWidth()
147: */
148: public int getMaximumWidth() {
149: // Establish max width of 'literal' label, other components in parent, versus type
150: // label.
151: return Math.max(super .getMaximumWidth(), valueNode
152: .getMaximumWidth());
153: }
154:
155: /**
156: * Gets output graph port, given its field name.
157: *
158: * @param fieldName field name
159: * @return graph port
160: */
161: public IGraphPort getOutputGraphPort(String fieldName) {
162: return valueNode.getRightGraphPort();
163: }
164:
165: /**
166: * Sets data object for which this UI element provides a view.
167: *
168: * @param obj data object
169: */
170: public void setDataObject(Object obj) {
171: this .dataObject = obj;
172: valueNode.setText(((VisibleSQLLiteral) obj).getValue());
173:
174: SQLLiteral lit = (SQLLiteral) obj;
175:
176: if (lit != null) {
177: try {
178: int jdbcType = lit.getJdbcType();
179: switch (jdbcType) {
180: case Types.NUMERIC:
181: case Types.FLOAT:
182: case Types.DOUBLE:
183: case Types.INTEGER:
184: titleArea.setTitleImage(getNumberIcon());
185: break;
186:
187: case Types.TIME:
188: case Types.TIMESTAMP:
189: titleArea.setTitleImage(getDateIcon());
190: break;
191:
192: case Types.VARCHAR:
193: case Types.CHAR:
194: default:
195: titleArea.setTitleImage(getTextIcon());
196: break;
197: }
198:
199: titleArea.setTitle(SQLUtils.getStdSqlType(jdbcType));
200: } catch (IllegalArgumentException ignore) {
201: String nbBundle1 = mLoc.t("PRSR001: SQL type:");
202: final String typeLabel = Localizer.parse(nbBundle1);
203: titleArea.setTitle(typeLabel);
204: }
205: }
206:
207: this .layoutChildren();
208: }
209:
210: private static Icon getNumberIcon() {
211: if (numberIcon == null) {
212: numberIcon = new ImageIcon(URL_NUMBER_ICON);
213: }
214:
215: return numberIcon;
216: }
217:
218: private static Icon getDateIcon() {
219: if (dateIcon == null) {
220: dateIcon = new ImageIcon(URL_DATE_ICON);
221: }
222:
223: return dateIcon;
224: }
225:
226: private static Icon getTextIcon() {
227: if (textIcon == null) {
228: textIcon = new ImageIcon(URL_TEXT_ICON);
229: }
230:
231: return textIcon;
232: }
233:
234: /**
235: * layout the children
236: */
237: public void layoutChildren() {
238: Rectangle boundingRect = new Rectangle(this .getBoundingRect());
239:
240: int minLayoutWidth = getMinimumWidth();
241: if (boundingRect.width < minLayoutWidth || !isExpandedState()) {
242: boundingRect.width = minLayoutWidth;
243: this .setBoundingRect(boundingRect);
244: }
245:
246: columnRect.setBoundingRect(boundingRect);
247:
248: int rectleft = getLeft();
249: int recttop = getTop();
250: int rectwidth = getWidth();
251: int rectheight = getHeight();
252:
253: int left = rectleft + insets.left;
254: int top = recttop + insets.top;
255: int width = rectwidth - insets.left - insets.right;
256: int height = rectheight - insets.top - insets.bottom;
257:
258: titleArea.setBoundingRect(left, top, width, titleArea
259: .getMinimumHeight());
260:
261: int aggrHeight = top + titleArea.getMinimumHeight();
262:
263: for (int i = 0; i < fieldList.size(); i++) {
264: OperatorGraphFieldNode fieldNode = (OperatorGraphFieldNode) fieldList
265: .get(i);
266:
267: if (aggrHeight < top + height) {
268: fieldNode.setVisible(true);
269: fieldNode.setBoundingRect(left, aggrHeight, width,
270: fieldNode.getHeight() + verticalGap);
271: } else {
272: fieldNode.setVisible(false);
273: fieldNode.setBoundingRect(left, top, width, fieldNode
274: .getHeight()
275: + verticalGap);
276: }
277: aggrHeight += fieldNode.getHeight() + verticalGap;
278: }
279: }
280:
281: /**
282: * Handles change to a bound property as indicated by the given event.
283: *
284: * @param evt A PropertyChangeEvent object describing the event source and the
285: * property that has changed.
286: */
287: public void propertyChange(PropertyChangeEvent evt) {
288: if (evt.getPropertyName().equals(BasicCellArea.TEXT)) {
289: VisibleSQLLiteral lit = (VisibleSQLLiteral) this
290: .getDataObject();
291: String newValue = (String) evt.getNewValue();
292:
293: if (lit != null) {
294: final String errorMsg = LiteralDialog
295: .evaluateIfLiteralValid(newValue, lit
296: .getJdbcType());
297: if (errorMsg == null) {
298: lit.setValue((String) evt.getNewValue());
299: } else {
300: valueNode.setText((String) evt.getOldValue());
301: SwingUtilities.invokeLater(new Runnable() {
302: public void run() {
303: LiteralDialog.showMessage(errorMsg);
304: }
305: });
306: }
307:
308: setSize(getMaximumWidth(), getMaximumHeight());
309: } else {
310: valueNode.setText((String) evt.getOldValue());
311: }
312: }
313: }
314:
315: class BasicTypeArea extends CanvasArea {
316: private BasicCellArea typeArea1;
317: private BasicCellArea typeValueArea;
318: private JGoRectangle rect;
319:
320: BasicTypeArea(String type, String typeValue) {
321: super ();
322: this .setSelectable(false);
323: this .setResizable(false);
324:
325: rect = new JGoRectangle();
326: rect.setPen(SQLOperatorGraphNode.PEN_DEFAULT);
327: rect.setBrush(SQLOperatorGraphNode.BRUSH_INPUT_REGULAR);
328: rect.setSelectable(false);
329: rect.setResizable(false);
330: addObjectAtHead(rect);
331:
332: typeArea1 = new BasicCellArea(type);
333: typeArea1.setTextAlignment(JGoText.ALIGN_CENTER);
334: typeArea1.setLinePen(SQLOperatorGraphNode.PEN_DEFAULT);
335: typeArea1
336: .setBrush(SQLOperatorGraphNode.BRUSH_INPUT_REGULAR);
337: typeArea1.setTextColor(TEXT_COLOR_INPUT);
338: this .addObjectAtTail(typeArea1);
339:
340: typeValueArea = new BasicCellArea(typeValue);
341: typeValueArea.setTextAlignment(JGoText.ALIGN_CENTER);
342: typeValueArea.setLinePen(SQLOperatorGraphNode.PEN_DEFAULT);
343: typeValueArea
344: .setBrush(SQLOperatorGraphNode.BRUSH_INPUT_REGULAR);
345: typeValueArea.setTextColor(TEXT_COLOR_INPUT);
346: this .addObjectAtTail(typeValueArea);
347:
348: this .setSize(getMaximumWidth(), getMaximumHeight());
349: }
350:
351: public int getMaximumHeight() {
352: int h = this .insets.top + this .insets.bottom;
353:
354: if (typeArea1 != null) {
355: h += typeArea1.getMaximumHeight();
356: }
357:
358: if (typeValueArea != null) {
359: h += typeValueArea.getHeight();
360: }
361:
362: return h;
363: }
364:
365: public int getMaximumWidth() {
366: int w = this .insets.left + this .insets.right;
367:
368: int width = 0;
369:
370: if (typeArea1 != null) {
371: width = typeArea1.getMaximumWidth();
372: }
373:
374: if (typeValueArea != null
375: && typeValueArea.getWidth() > width) {
376: width = typeValueArea.getWidth();
377: }
378:
379: w += width;
380:
381: return w;
382: }
383:
384: public void layoutChildren() {
385: rect.setBoundingRect(this .getBoundingRect());
386:
387: int rectleft = getLeft();
388: int recttop = getTop();
389: int rectwidth = getWidth();
390:
391: int left = rectleft + insets.left;
392: int top = recttop + insets.top;
393: int width = rectwidth - insets.left - insets.right;
394:
395: typeArea1.setBoundingRect(left, top, width, typeArea1
396: .getHeight());
397:
398: typeValueArea.setBoundingRect(left, top
399: + typeArea1.getHeight(), width, typeValueArea
400: .getHeight());
401:
402: }
403:
404: public void setValueType(String vType) {
405: typeValueArea.setText(vType);
406: }
407: }
408: }
|