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;
042:
043: import java.awt.Image;
044: import java.net.URL;
045: import java.util.Enumeration;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import javax.swing.ImageIcon;
050: import javax.swing.tree.DefaultMutableTreeNode;
051:
052: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
053: import org.netbeans.modules.sql.framework.model.SQLDBModel;
054: import org.netbeans.modules.sql.framework.model.SQLDBTable;
055: import org.netbeans.modules.sql.framework.model.SQLObject;
056: import org.netbeans.modules.sql.framework.ui.utils.UIUtil;
057:
058: /**
059: * Extension of DefaultMutableTreeNode which represents a Database, table or column for
060: * purposes of configuring its properties in the Flatfile Database wizard.
061: *
062: * @author Jonathan Giron
063: * @version $Revision$
064: */
065: public class TableColumnNode extends DefaultMutableTreeNode implements
066: Comparable {
067:
068: /**
069: * Extends TableColumnNode to represent a TableColumnNode with no children.
070: */
071: public static final class Leaf extends TableColumnNode {
072: /**
073: * Constructs a default instance of TableColumnNode.
074: */
075: public Leaf(Object model) {
076: super (model);
077: this .setAllowsChildren(false);
078: }
079: }
080:
081: /* Constant: indicates field node type */
082: private static final int COLUMN = 1;
083:
084: /* Constant: indicates folder node type */
085: private static final int DB = -1;
086:
087: /* Constant: indicates flatfile node type */
088: private static final int TABLE = 0;
089:
090: public static boolean isColumnVisible(SQLDBColumn column,
091: List tableNodes) {
092: Iterator it = tableNodes.iterator();
093: while (it.hasNext()) {
094: TableColumnNode tNode = (TableColumnNode) it.next();
095: Enumeration enu = tNode.children();
096: while (enu.hasMoreElements()) {
097: TableColumnNode cNode = (TableColumnNode) enu
098: .nextElement();
099: SQLDBColumn tColumn = (SQLDBColumn) cNode
100: .getUserObject();
101: if (column.equals(tColumn)) {
102: return cNode.isSelected();
103: }
104: }
105: }
106: return true;
107: }
108:
109: /* node is enabled and eligible to be selected */
110: private boolean enabled = true;
111:
112: /* whether user has selected this node */
113: private boolean selected = true;
114:
115: private String toolTip;
116:
117: /* node type */
118: private int type;
119:
120: /* associated user object */
121: private Object userObject;
122:
123: /**
124: * Creates a new instance of FlatfileNode with the associated Object.
125: *
126: * @param model Object associated with this instance.
127: * @throws Introspection Exception if error occurs during instantiation
128: */
129: public TableColumnNode(Object model) {
130: super (model);
131:
132: if (model instanceof SQLDBModel) {
133: type = DB;
134: toolTip = ((SQLDBModel) model).getDisplayName();
135: } else if (model instanceof SQLDBTable) {
136: type = TABLE;
137: toolTip = UIUtil.getTableToolTip((SQLDBTable) model);
138: } else if (model instanceof SQLDBColumn) {
139: SQLDBColumn field = (SQLDBColumn) model;
140: type = COLUMN;
141: selected = field.isVisible();
142: toolTip = UIUtil.getColumnToolTip(field);
143: } else {
144: throw new IllegalArgumentException(
145: "Unrecognized model type: must be SQLDBModel, SourceTable, or SourceColumn");
146: }
147:
148: userObject = model;
149: }
150:
151: /**
152: * Compares this object with the specified object for order. Returns a negative
153: * integer, zero, or a positive integer as this object is less than, equal to, or
154: * greater than the specified object.
155: * <p>
156: * Note: this class has a natural ordering that is inconsistent with equals.
157: *
158: * @param o the Object to be compared.
159: * @return a negative integer, zero, or a positive integer as this object is less
160: * than, equal to, or greater than the specified object.
161: * @throws ClassCastException if the specified object's type prevents it from being
162: * compared to this Object.
163: */
164: public int compareTo(Object o) {
165: if (o == this ) {
166: return 0;
167: } else if (o == null) {
168: return -1;
169: }
170:
171: TableColumnNode aNode = (TableColumnNode) o;
172: switch (type) {
173: case DB:
174: if (aNode.type == DB) {
175: return compareDisplayNames(this , aNode);
176: } else if (aNode.type == TABLE) {
177: return -1;
178: } else if (aNode.type == COLUMN) {
179: return -1;
180: }
181:
182: case TABLE:
183: if (aNode.type == TABLE) {
184: return compareDisplayNames(this , aNode);
185: } else if (aNode.type == DB) {
186: return 1;
187: } else if (aNode.type == COLUMN) {
188: return -1;
189: }
190:
191: case COLUMN:
192: if (aNode.type == COLUMN) {
193: return compareDisplayNames(this , aNode);
194: } else if (aNode.type == DB) {
195: return 1;
196: } else if (aNode.type == TABLE) {
197: return 1;
198: }
199:
200: default:
201: throw new ClassCastException(
202: "Cannot compare between unrecognized TableColumnNode types.");
203: }
204: }
205:
206: /**
207: * Overrides NetBeans implementation to directly return resolved image icons. We
208: * generally use PNG images, while the NetBeans implementation (which sets base icon
209: * names using setIconBase) can only handle GIFs.
210: */
211: public Image getIcon(int imgType) {
212: String imgPath = null;
213: Image myImage = null;
214:
215: switch (type) {
216: case DB:
217: imgPath = "/org/netbeans/modules/sql/framework/ui/resources/images/root.png";
218: break;
219:
220: case TABLE:
221: imgPath = "/org/netbeans/modules/sql/framework/ui/resources/images/SourceTable.png";
222: break;
223:
224: case COLUMN:
225: imgPath = "/org/netbeans/modules/sql/framework/ui/resources/images/column.png";
226: break;
227:
228: default:
229: imgPath = null;
230: }
231:
232: if (imgPath != null) {
233: URL url = getClass().getResource(imgPath);
234: if (url != null) {
235: myImage = new ImageIcon(url).getImage();
236: }
237: }
238:
239: return myImage;
240: }
241:
242: public String getName() {
243: return (userObject != null) ? ((SQLObject) userObject)
244: .getDisplayName() : "<< Unknown >>";
245: }
246:
247: public int getNodeType() {
248: return this .type;
249: }
250:
251: /**
252: * @return Returns the toolTip.
253: */
254: public String getToolTip() {
255: return toolTip;
256: }
257:
258: /**
259: * Indicates whether this node is enabled.
260: *
261: * @return true if enabled, false otherwise
262: */
263: public boolean isEnabled() {
264: return enabled;
265: }
266:
267: /**
268: * Indicates whether this node is selected.
269: *
270: * @return true if node is selected, false otherwise
271: */
272: public boolean isSelected() {
273: return selected;
274: }
275:
276: /**
277: * Sets whether this node is enabled.
278: *
279: * @param isEnabled sets to true if node is enabled, false otherwise
280: */
281: public void setEnabled(boolean isEnabled) {
282: enabled = isEnabled;
283: }
284:
285: /**
286: * Sets whether this node is selected.
287: *
288: * @param isSelected sets to true if node is selected, false otherwise
289: */
290: public void setSelected(boolean isSelected) {
291: selected = isSelected;
292: }
293:
294: public void setSelectedBasedOnChildren() {
295: if (type == TABLE && this .getChildCount() != 0) {
296: TableColumnNode child = (TableColumnNode) this
297: .getFirstChild();
298: boolean newValue = true;
299:
300: while (child != null) {
301: if (!child.isSelected()) {
302: newValue = false;
303: break;
304: }
305: child = (TableColumnNode) this .getChildAfter(child);
306: }
307:
308: setSelected(newValue);
309: }
310: }
311:
312: /**
313: * @param toolTip The toolTip to set.
314: */
315: public void setToolTip(String toolTip) {
316: this .toolTip = toolTip;
317: }
318:
319: private int compareDisplayNames(DefaultMutableTreeNode first,
320: DefaultMutableTreeNode second) {
321: String firstDisplayName = (first.getUserObject() != null) ? ((SQLObject) first
322: .getUserObject()).getDisplayName()
323: : "";
324:
325: String secondDisplayName = (second.getUserObject() != null) ? ((SQLObject) second
326: .getUserObject()).getDisplayName()
327: : "";
328:
329: return firstDisplayName.compareTo(secondDisplayName);
330: }
331: }
|