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.visualweb.faces.dt.binding;
042:
043: import java.awt.*;
044: import java.awt.event.*;
045: import java.beans.*;
046: import java.sql.*;
047: import javax.faces.model.*;
048: import javax.swing.*;
049: import javax.swing.tree.*;
050: import com.sun.rave.designtime.*;
051: import org.netbeans.modules.visualweb.faces.dt.util.ComponentBundle;
052:
053: public class ResultSetTargetNodeFactory implements TargetNodeFactory {
054:
055: private static final ComponentBundle bundle = ComponentBundle
056: .getBundle(ResultSetTargetNodeFactory.class);
057:
058: public boolean supportsTargetClass(Class targetClass) {
059: return ResultSet.class.isAssignableFrom(targetClass);
060: }
061:
062: public BindingTargetNode createTargetNode(
063: DefaultTreeModel treeModel, DesignBean bean,
064: PropertyDescriptor[] propPath, Object propInstance) {
065: return new ResultSetTargetNode(treeModel, bean, propPath,
066: propInstance);
067: }
068:
069: public class ResultSetTargetNode extends
070: BindingTargetNode.PropertyTargetNode {
071: protected ResultSet resultSet;
072:
073: public ResultSetTargetNode(DefaultTreeModel treeModel,
074: DesignBean bean, PropertyDescriptor[] propPath,
075: Object propInstance) {
076: super (treeModel, bean, propPath, propInstance);
077: if (propInstance == null) {
078: propInstance = getPropInstance(bean, propPath);
079: }
080: if (propInstance instanceof ResultSet) {
081: resultSet = (ResultSet) propInstance;
082: }
083: }
084:
085: public void lazyLoadCustomTargetNodes() {
086: if (resultSet != null) {
087: try {
088: ResultSetMetaData rsmd = resultSet.getMetaData();
089: int cols = rsmd.getColumnCount();
090: if (cols > 0) {
091: for (int i = 0; i < cols; i++) {
092: //System.out.println("*** Adding column : " + rsmd.getColumnName(i + 1)); //NOI18N
093: super .add(new ColumnNode(treeModel, rsmd
094: .getColumnName(i + 1), rsmd
095: .getColumnType(i + 1)));
096: }
097: }
098: super .add(new SelectItemsNode(treeModel, rsmd));
099: } catch (Exception x) {
100: // x.printStackTrace();
101: }
102: }
103: }
104:
105: public class ColumnNode extends BindingTargetNode {
106: protected String columnName;
107: protected int columnType;
108:
109: public ColumnNode(DefaultTreeModel treeModel,
110: String columnName, int columnType) {
111: super (treeModel);
112: this .columnName = columnName;
113: this .columnType = columnType;
114: }
115:
116: public int getChildCount() {
117: return 0;
118: }
119:
120: public boolean lazyLoad() {
121: return true;
122: }
123:
124: public boolean isValidBindingTarget() {
125: return true;
126: }
127:
128: public String getBindingExpressionPart() {
129: return "currentRow['" + columnName + "']"; //NOI81N
130: }
131:
132: public Class getTargetTypeClass() {
133: return getJavaType(columnType);
134: }
135:
136: public String getDisplayText(boolean enableNode) {
137: String tn = getTypeName(columnType);
138: StringBuffer sb = new StringBuffer();
139: sb.append("<html>"); //NOI18N
140: if (!enableNode) {
141: sb.append("<font color=\"gray\">"); //NOI18N
142: }
143: sb.append(bundle.getMessage("column")); //NOI18N
144: sb.append(" "); //NOI18N
145: if (enableNode) {
146: sb.append("<b>"); //NOI18N
147: }
148: sb.append(columnName);
149: if (enableNode) {
150: sb.append("</b>"); //NOI18N
151: }
152: sb.append(" <font size=\"-1\"><i>"); //NOI18N
153: sb.append(tn);
154: sb.append("</i></font>"); //NOI18N
155: if (!enableNode) {
156: sb.append("</font>"); //NOI18N
157: }
158: sb.append("</html>"); //NOI18N
159: return sb.toString();
160: }
161: }
162:
163: public class SelectItemsNode extends BindingTargetNode {
164: protected ResultSetMetaData metaData;
165:
166: public SelectItemsNode(DefaultTreeModel treeModel,
167: ResultSetMetaData metaData) {
168: super (treeModel);
169: this .metaData = metaData;
170: initCustomPanel();
171: displayTextEnabled = getDisplayText(true);
172: displayTextDisabled = getDisplayText(false);
173: }
174:
175: protected String displayTextEnabled = null;
176: protected String displayTextDisabled = null;
177:
178: public String getDisplayText(boolean enableNode) {
179: if (enableNode && displayTextEnabled != null) {
180: return displayTextEnabled;
181: } else if (!enableNode && displayTextDisabled != null) {
182: return displayTextDisabled;
183: }
184: StringBuffer sb = new StringBuffer();
185: sb.append("<html>"); //NOI18N
186: if (!enableNode) {
187: sb.append("<font color=\"gray\">"); //NOI18N
188: }
189: if (enableNode) {
190: sb.append("<b>"); //NOI18N
191: }
192: sb.append(bundle.getMessage("selectItems")); //NOI81N
193: if (enableNode) {
194: sb.append("</b>"); //NOI18N
195: }
196: sb.append(" <font size=\"-1\"><i>"); //NOI18N
197: sb.append(bundle.getMessage("parenItemsForListBoxOr")); //NOI18N
198: sb.append("</i></font>"); //NOI18N
199: if (!enableNode) {
200: sb.append("</font>"); //NOI18N
201: }
202: sb.append("</html>"); //NOI18N
203: return sb.toString();
204: }
205:
206: public int getChildCount() {
207: return 0;
208: }
209:
210: public boolean lazyLoad() {
211: return true;
212: }
213:
214: public Class getTargetTypeClass() {
215: return SelectItem[].class;
216: }
217:
218: public boolean isValidBindingTarget() {
219: return true;
220: }
221:
222: public String getBindingExpressionPart() {
223: return "selectItems['" + getColumnPicks() + "']"; //NOI18N
224: }
225:
226: String getColumnPicks() {
227: StringBuffer sb = new StringBuffer();
228: Object o = valueCombo.getSelectedItem();
229: if (o instanceof ComboDisplayColumn) {
230: sb.append(((ComboDisplayColumn) o).columnName);
231: }
232: o = labelCombo.getSelectedItem();
233: if (o instanceof ComboDisplayColumn) {
234: sb.append(","); //NOI18N
235: sb.append(((ComboDisplayColumn) o).columnName);
236: }
237: o = descrCombo.getSelectedItem();
238: if (o instanceof ComboDisplayColumn) {
239: sb.append(","); //NOI18N
240: sb.append(((ComboDisplayColumn) o).columnName);
241: }
242: return sb.toString();
243: }
244:
245: JPanel pickerPanel = new JPanel();
246: JLabel valueLabel = new JLabel(bundle
247: .getMessage("valueField")); //NOI18N
248: JLabel labelLabel = new JLabel(bundle
249: .getMessage("displayField")); //NOI18N
250: JLabel descrLabel = new JLabel(bundle
251: .getMessage("tooltipField")); //NOI18N
252: JComboBox valueCombo = new JComboBox();
253: JComboBox labelCombo = new JComboBox();
254: JComboBox descrCombo = new JComboBox();
255:
256: void initCustomPanel() {
257: ComboDisplayColumnRenderer cdcr = new ComboDisplayColumnRenderer();
258: valueCombo.setRenderer(cdcr);
259: labelCombo.setRenderer(cdcr);
260: descrCombo.setRenderer(cdcr);
261: labelCombo.addItem(bundle.getMessage("noneBrackets")); //NOI18N
262: descrCombo.addItem(bundle.getMessage("noneBrackets")); //NOI18N
263: try {
264: int cols = metaData.getColumnCount();
265: if (cols > 0) {
266: for (int i = 0; i < cols; i++) {
267: ComboDisplayColumn col = new ComboDisplayColumn(
268: metaData.getColumnName(i + 1),
269: metaData.getColumnType(i + 1));
270: valueCombo.addItem(col);
271: labelCombo.addItem(col);
272: descrCombo.addItem(col);
273: }
274: }
275: } catch (Exception x) {
276: x.printStackTrace();
277: }
278: pickerPanel.setLayout(new GridBagLayout());
279: pickerPanel.add(valueLabel, new GridBagConstraints(0,
280: 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
281: GridBagConstraints.HORIZONTAL, new Insets(0, 0,
282: 2, 4), 0, 0));
283: pickerPanel.add(labelLabel, new GridBagConstraints(1,
284: 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
285: GridBagConstraints.HORIZONTAL, new Insets(0, 0,
286: 2, 4), 0, 0));
287: pickerPanel.add(descrLabel, new GridBagConstraints(2,
288: 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
289: GridBagConstraints.HORIZONTAL, new Insets(0, 0,
290: 2, 0), 0, 0));
291: pickerPanel.add(valueCombo, new GridBagConstraints(0,
292: 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
293: GridBagConstraints.HORIZONTAL, new Insets(0, 0,
294: 0, 4), 0, 0));
295: pickerPanel.add(labelCombo, new GridBagConstraints(1,
296: 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
297: GridBagConstraints.HORIZONTAL, new Insets(0, 0,
298: 0, 4), 0, 0));
299: pickerPanel.add(descrCombo, new GridBagConstraints(2,
300: 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
301: GridBagConstraints.HORIZONTAL, new Insets(0, 0,
302: 0, 0), 0, 0));
303: valueCombo.addActionListener(updateAdapter);
304: labelCombo.addActionListener(updateAdapter);
305: descrCombo.addActionListener(updateAdapter);
306: }
307:
308: public JComponent getCustomDisplayPanel(
309: ActionListener updateCallback) {
310: this .updateCallback = updateCallback;
311: return pickerPanel;
312: }
313:
314: ActionListener updateCallback = null;
315: ActionListener updateAdapter = new ActionListener() {
316: public void actionPerformed(ActionEvent e) {
317: if (updateCallback != null) {
318: updateCallback.actionPerformed(e);
319: }
320: }
321: };
322: }
323: }
324:
325: public class ComboDisplayColumn {
326: public String columnName;
327: public int columnType;
328:
329: public ComboDisplayColumn(String columnName, int columnType) {
330: this .columnName = columnName;
331: this .columnType = columnType;
332: }
333: }
334:
335: public class ComboDisplayColumnRenderer extends
336: DefaultListCellRenderer {
337: public Component getListCellRendererComponent(JList list,
338: Object value, int index, boolean isSelected,
339: boolean cellHasFocus) {
340: super .getListCellRendererComponent(list, value, index,
341: isSelected, cellHasFocus);
342: if (value instanceof ComboDisplayColumn) {
343: ComboDisplayColumn cdc = (ComboDisplayColumn) value;
344: String tn = getTypeName(cdc.columnType);
345: StringBuffer sb = new StringBuffer();
346: sb.append("<html><b>"); //NOI18N
347: sb.append(cdc.columnName);
348: sb.append("</b> <font size=\"-1\"><i>"); //NOI18N
349: sb.append(tn);
350: sb.append("</i></font></html>"); //NOI18N
351: this .setText(sb.toString());
352: } else {
353: this .setText(bundle.getMessage("noneBrackets")); //NOI18N
354: }
355: return this ;
356: }
357: }
358:
359: public static String getTypeName(int sqlType) {
360: switch (sqlType) {
361: case Types.SMALLINT:
362: return "SMALLINT"; //NOI18N
363: case Types.INTEGER:
364: return "INTEGER"; //NOI18N
365: case Types.TINYINT:
366: return "TINYINT"; //NOI18N
367: case Types.BIGINT:
368: return "BIGINT"; //NOI18N
369: case Types.BIT:
370: return "BIT"; //NOI18N
371: case Types.BOOLEAN:
372: return "BOOLEAN"; //NOI18N
373: case Types.DATE:
374: return "DATE"; //NOI18N
375: case Types.TIME:
376: return "TIME"; //NOI18N
377: case Types.DECIMAL:
378: return "DECIMAL"; //NOI18N
379: case Types.NUMERIC:
380: return "NUMERIC"; //NOI18N
381: case Types.DOUBLE:
382: return "DOUBLE"; //NOI18N
383: case Types.REAL:
384: return "REAL"; //NOI18N
385: case Types.FLOAT:
386: return "FLOAT"; //NOI18N
387: case Types.BINARY:
388: return "BINARY"; //NOI18N
389: case Types.CHAR:
390: return "CHAR"; //NOI18N
391: case Types.LONGVARCHAR:
392: return "LONGVARCHAR"; //NOI18N
393: case Types.VARCHAR:
394: return "VARCHAR"; //NOI18N
395: case Types.BLOB:
396: return "BLOB"; //NOI18N
397: case Types.CLOB:
398: return "CLOB"; //NOI18N
399: case Types.DATALINK:
400: return "DATALINK"; //NOI18N
401: case Types.DISTINCT:
402: return "DISTINCT"; //NOI18N
403: case Types.JAVA_OBJECT:
404: return "JAVA_OBJECT"; //NOI18N
405: case Types.LONGVARBINARY:
406: return "LONGVARBINARY"; //NOI18N
407: case Types.NULL:
408: return "NULL"; //NOI18N
409: case Types.OTHER:
410: return "OTHER"; //NOI18N
411: case Types.REF:
412: return "REF"; //NOI18N
413: case Types.STRUCT:
414: return "STRUCT"; //NOI18N
415: case Types.TIMESTAMP:
416: return "TIMESTAMP"; //NOI18N
417: case Types.VARBINARY:
418: return "VARBINARY"; //NOI18N
419: }
420: return null;
421: }
422:
423: public static Class getJavaType(int sqlType) {
424: switch (sqlType) {
425: case Types.SMALLINT:
426: return Short.class;
427: case Types.INTEGER:
428: return Integer.class;
429: case Types.TINYINT:
430: return Byte.class;
431: case Types.BIGINT:
432: return Long.class;
433: case Types.BIT:
434: case Types.BOOLEAN:
435: return Boolean.class;
436: case Types.DATE:
437: case Types.TIME:
438: return Timestamp.class;
439: case Types.DECIMAL:
440: case Types.NUMERIC:
441: return Number.class;
442: case Types.DOUBLE:
443: case Types.REAL:
444: return Double.class;
445: case Types.FLOAT:
446: return Float.class;
447: case Types.BINARY:
448: case Types.CHAR:
449: case Types.LONGVARCHAR:
450: case Types.VARCHAR:
451: case Types.BLOB:
452: case Types.CLOB:
453: case Types.DATALINK:
454: case Types.DISTINCT:
455: case Types.JAVA_OBJECT:
456: case Types.LONGVARBINARY:
457: case Types.NULL:
458: case Types.OTHER:
459: case Types.REF:
460: case Types.STRUCT:
461: case Types.TIMESTAMP:
462: case Types.VARBINARY:
463: return null;
464: }
465: return null;
466: }
467: }
|