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.mashup.db.ui;
042:
043: import java.beans.IntrospectionException;
044:
045: import org.netbeans.modules.mashup.db.ui.model.FlatfileColumn;
046: import org.netbeans.modules.mashup.db.ui.model.FlatfileDatabase;
047: import org.netbeans.modules.mashup.db.ui.model.FlatfileTable;
048: import org.openide.nodes.BeanNode;
049: import org.openide.nodes.Children;
050: import org.openide.nodes.Node;
051: import org.openide.util.HelpCtx;
052: import org.openide.util.actions.SystemAction;
053: import net.java.hulp.i18n.Logger;
054: import org.netbeans.modules.etl.logger.Localizer;
055: import org.netbeans.modules.etl.logger.LogUtil;
056:
057: /**
058: * NetBeans node extension that represents a flatfile or field for purposes of configuring
059: * its properties in the Flatfile Database wizard.
060: *
061: * @author Jonathan Giron
062: * @author Ahimanikya Satapathy
063: * @version $Revision$
064: */
065: public class FlatfileNode extends BeanNode implements Comparable {
066:
067: private static transient final Logger mLogger = LogUtil
068: .getLogger(FlatfileNode.class.getName());
069: private static transient final Localizer mLoc = Localizer.get();
070:
071: /**
072: * Extends FlatfileNode to represent a FlatfileNode with no children.
073: */
074: public static final class Leaf extends FlatfileNode {
075:
076: /**
077: * Constructs a default instance of FlatfileNode.
078: *
079: * @throws FlatfileDBException if error occurs during instantiation
080: */
081: public Leaf() throws IntrospectionException {
082: super (Children.LEAF);
083: }
084:
085: /**
086: * Creates an instance of Leaf associated with the given Object.
087: *
088: * @param model Object associated with this new instance.
089: * @throws FlatfileDBException if error occurs during instantiation
090: */
091: public Leaf(Object model) throws IntrospectionException {
092: super (model, Children.LEAF);
093: }
094: }
095:
096: /* Constant: indicates table node type */
097: private static final int COLUMN = 102;
098:
099: /* Constant: indicates database model type */
100: private static final int DATABASE = 100;
101:
102: /* Log4J category string */
103: private static final String LOG_CATEGORY = FlatfileNode.class
104: .getName();
105:
106: /* Constant: indicates table node type */
107: private static final int TABLE = 101;
108:
109: /* node is enabled and eligible to be selected */
110: private boolean enabled = true;
111:
112: /* Array of available Actions for this node */
113: private SystemAction[] mActions;
114:
115: /* whether user has selected this node */
116: private boolean selected = true;
117:
118: /* node type */
119: private int type;
120:
121: /* associated user object */
122: private Object userObject;
123:
124: /**
125: * Creates a new instance of FlatfileNode with the associated Object.
126: *
127: * @param model Object associated with this instance.
128: * @throws IntrospectionException if error occurs during instantiation
129: */
130: public FlatfileNode(Object model) throws IntrospectionException {
131: this (model, new FlatfileChildren());
132: }
133:
134: /**
135: * Creates a new instance of FlatfileNode with the given child nodes and associated
136: * Object.
137: *
138: * @param children Children instance representing this instances's child nodes
139: * @param model Object associated with this instance.
140: * @throws IntrospectionException if error occurs during instantiation
141: */
142: public FlatfileNode(Object model, Children children)
143: throws IntrospectionException {
144: super (model, children);
145:
146: if (model instanceof FlatfileDatabase) {
147: type = DATABASE;
148: super .setName(((FlatfileDatabase) model).getName());
149: setIconBaseWithExtension("org/netbeans/modules/mashup/db/ui/resource/images/root.png");
150: } else if (model instanceof FlatfileTable) {
151: type = TABLE;
152: super .setName(((FlatfileTable) model).getTableName());
153: setIconBaseWithExtension("org/netbeans/modules/mashup/db/ui/resource/images/Table.gif");
154: } else if (model instanceof FlatfileColumn) {
155: type = COLUMN;
156: FlatfileColumn column = (FlatfileColumn) model;
157: super .setName(column.getName());
158: if (column.isNullable()) {
159: setIconBaseWithExtension("org/netbeans/modules/mashup/db/ui/resource/images/Column.gif");
160: } else {
161: setIconBaseWithExtension("org/netbeans/modules/mashup/db/ui/resource/images/ColumnNotNull.gif");
162: }
163: } else {
164: throw new IllegalArgumentException(
165: "Unrecognized model type: must be FlatfileDatabase, FlatfileTable, or FlatfileColumn");
166: }
167:
168: super .setDisplayName(getName());
169:
170: userObject = model;
171: initializeActionsAndCookies();
172: }
173:
174: /**
175: * Indicates whether this node can be copied hence can be dragged.
176: *
177: * @return true if this node can be copied or dragged
178: */
179: public boolean canCopy() {
180: return false;
181: }
182:
183: /**
184: * Indicates whether this node can be copied hence can be dragged.
185: *
186: * @return true if node can be copied or dragged
187: */
188: public boolean canCut() {
189: return false;
190: }
191:
192: /**
193: * Indicates whether this node can be renamed.
194: *
195: * @return true if renameable via UI; false otherwise
196: */
197: public boolean canRename() {
198: return false;
199: }
200:
201: /**
202: * Compares this object with the specified object for order. Returns a negative
203: * integer, zero, or a positive integer as this object is less than, equal to, or
204: * greater than the specified object.
205: * <p>
206: * Note: this class has a natural ordering that is inconsistent with equals.
207: *
208: * @param o the Object to be compared.
209: * @return a negative integer, zero, or a positive integer as this object is less
210: * than, equal to, or greater than the specified object.
211: * @throws ClassCastException if the specified object's type prevents it from being
212: * compared to this Object.
213: */
214: public int compareTo(Object o) {
215: if (o == this ) {
216: return 0;
217: } else if (o == null) {
218: return -1;
219: }
220:
221: FlatfileNode aNode = (FlatfileNode) o;
222: switch (type) {
223: case DATABASE:
224: if (aNode.type == DATABASE) {
225: return compareDisplayNames(this , aNode);
226: } else if (aNode.type == TABLE) {
227: return -1;
228: } else if (aNode.type == COLUMN) {
229: return -1;
230: } else {
231: throw new ClassCastException(
232: "Cannot compare between unrecognized FlatfileNode types.");
233: }
234:
235: case TABLE:
236: if (aNode.type == TABLE) {
237: return compareDisplayNames(this , aNode);
238: } else if (aNode.type == COLUMN) {
239: return -1;
240: } else {
241: throw new ClassCastException(
242: "Cannot compare between unrecognized FlatfileNode types.");
243: }
244:
245: case COLUMN:
246: if (aNode.type == COLUMN) {
247: return compareDisplayNames(this , aNode);
248: } else if (aNode.type == TABLE) {
249: return 1;
250: } else {
251: throw new ClassCastException(
252: "Cannot compare between unrecognized FlatfileNode types.");
253: }
254:
255: default:
256: throw new ClassCastException(
257: "Cannot compare between unrecognized FlatfileNode types.");
258: }
259: }
260:
261: /**
262: * Gets all available system actions for this node.
263: *
264: * @return array of system actions
265: */
266: public SystemAction[] getActions() {
267: return mActions;
268: }
269:
270: /**
271: * Overrides default implementation to return null for this node. This prevents the
272: * default NetBeans property panel from appearing.
273: *
274: * @return null; no actions are associated with flatfile nodes.
275: */
276: public SystemAction getDefaultAction() {
277: return null;
278: }
279:
280: /**
281: * @see org.openide.nodes.AbstractNode#getHelpCtx
282: */
283: public HelpCtx getHelpCtx() {
284: return null;
285: }
286:
287: /**
288: * Gets user object associated with this node.
289: *
290: * @return user object
291: */
292: public Object getUserObject() {
293: return userObject;
294: }
295:
296: /**
297: * Indicates whether this node is enabled.
298: *
299: * @return true if enabled, false otherwise
300: */
301: public boolean isEnabled() {
302: return enabled;
303: }
304:
305: /**
306: * Indicates whether this node is selected.
307: *
308: * @return true if node is selected, false otherwise
309: */
310: public boolean isSelected() {
311: return selected;
312: }
313:
314: /**
315: * Overrides parent implementation.
316: *
317: * @param newName new display name.
318: */
319: public void setDisplayName(String newName) {
320: setName(newName);
321: }
322:
323: /**
324: * Sets whether this node is enabled.
325: *
326: * @param isEnabled sets to true if node is enabled, false otherwise
327: */
328: public void setEnabled(boolean isEnabled) {
329: enabled = isEnabled;
330: }
331:
332: /**
333: * Overrides parent implementation.
334: *
335: * @param newName new table name.
336: */
337: public void setName(String newName) {
338: super .setName(newName);
339: }
340:
341: /**
342: * Sets whether this node is selected.
343: *
344: * @param isSelected sets to true if node is selected, false otherwise
345: */
346: public void setSelected(boolean isSelected) {
347: selected = isSelected;
348: }
349:
350: /**
351: * Sets user object associated with this node.
352: *
353: * @param obj -
354: */
355: public void setUserObject(Object obj) {
356: userObject = obj;
357: }
358:
359: /**
360: * Updates underlying user object with properties of this node.
361: */
362: public void updateUserObject() {
363: // XXX Update user object.
364: mLogger.infoNoloc(mLoc
365: .t("PRSR083: Current state of user object:{0}",
366: userObject));
367: }
368:
369: /**
370: * Returns FlatfileChildren child nodes for this node.
371: *
372: * @return FlatfileChildren
373: */
374: protected FlatfileChildren getFlatfileChildren() {
375: return (FlatfileChildren) getChildren();
376: }
377:
378: private int compareDisplayNames(Node first, Node second) {
379: String firstDisplayName = (first.getDisplayName() != null) ? first
380: .getDisplayName()
381: : "";
382:
383: String secondDisplayName = (second.getDisplayName() != null) ? second
384: .getDisplayName()
385: : "";
386:
387: return firstDisplayName.compareTo(secondDisplayName);
388: }
389:
390: /*
391: * Initializes Action and Cookies @param nodeType nodeType
392: */
393: private void initializeActionsAndCookies() {
394: mActions = new SystemAction[] {};
395: }
396: }
|