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.model;
042:
043: import java.beans.IntrospectionException;
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.Iterator;
048: import java.util.List;
049:
050: import org.netbeans.modules.mashup.db.common.PropertyKeys;
051: import org.netbeans.modules.mashup.db.model.FlatfileDBColumn;
052: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
053: import org.netbeans.modules.mashup.db.model.FlatfileDatabaseModel;
054: import org.netbeans.modules.mashup.db.ui.FlatfileNode;
055: import org.openide.explorer.view.NodeTreeModel;
056: import org.openide.nodes.Children;
057: import org.openide.nodes.Node;
058: import net.java.hulp.i18n.Logger;
059: import org.netbeans.modules.etl.logger.Localizer;
060: import org.netbeans.modules.etl.logger.LogUtil;
061:
062: /**
063: * @author Ritesh Adval, Jonathan Giron
064: * @version $Revision$
065: */
066: public class FlatfileTreeTableModel extends NodeTreeModel {
067:
068: private static transient final Logger mLogger = LogUtil
069: .getLogger(FlatfileTreeTableModel.class.getName());
070: private static transient final Localizer mLoc = Localizer.get();
071: /* Log4J category string */
072: private static final String LOG_CATEGORY = FlatfileTreeTableModel.class
073: .getName();
074: private FlatfileNode modelNode;
075:
076: /**
077: * Creates a new default instance of FlatfileTreeTableModel.
078: */
079: public FlatfileTreeTableModel() {
080: super ();
081: }
082:
083: /**
084: * Configures this model using the contents of the given FlatfileDatabaseModel
085: * instance.
086: *
087: * @param dbModel FlatfileDatabaseModel containing FlatfileDBTable instances to be
088: * represented
089: */
090: public void configureModel(FlatfileDatabaseModel dbModel) {
091: createNodes(dbModel);
092:
093: this .setNode(modelNode);
094: this .setAsksAllowsChildren(false);
095: }
096:
097: /**
098: * Gets FlatfileDatabaseModel with contents modified as a result of user's selection
099: * of columns
100: *
101: * @return model containing user-customized content
102: */
103: public FlatfileDatabaseModel getModel() {
104: updateUserObjects();
105: FlatfileDatabase dbModel = (FlatfileDatabase) modelNode
106: .getUserObject();
107: return dbModel.getDeligate();
108: }
109:
110: /**
111: * Gets the root node for this model
112: *
113: * @return FlatfileNode - the root node.
114: */
115: public FlatfileNode getRootNode() {
116: return modelNode;
117: }
118:
119: /**
120: * Returns whether the specified node is a leaf node. The way the test is performed
121: * depends on the askAllowsChildren setting.
122: *
123: * @param obj is the leaf object to check
124: * @return boolean true if the node is a leaf node; false otherwise
125: * @see javax.swing.tree.DefaultTreeModel#isLeaf
126: */
127: public boolean isLeaf(Object obj) {
128: boolean isLeaf = false;
129:
130: if (obj instanceof FlatfileNode) {
131: isLeaf = ((FlatfileNode) obj).isLeaf();
132: }
133:
134: return isLeaf;
135: }
136:
137: private void createNodes(FlatfileDatabaseModel dbModel) {
138: try {
139: FlatfileDatabase db = new FlatfileDatabase(dbModel);
140: modelNode = new FlatfileNode(db);
141:
142: // Make a copy of the List so that sorting will not affect
143: // the order of the source List.
144: List fileList = new ArrayList(dbModel.getTables());
145: Collections.sort(fileList);
146:
147: Iterator it = fileList.iterator();
148: while (it.hasNext()) {
149: FlatfileDBTable table = (FlatfileDBTable) it.next();
150: FlatfileTable ffTable = null;
151:
152: try {
153: String fileType = table
154: .getProperty(PropertyKeys.LOADTYPE);
155: if (PropertyKeys.DELIMITED
156: .equalsIgnoreCase(fileType)) {
157: ffTable = new DelimitedFlatfile(table);
158: } else if (PropertyKeys.FIXEDWIDTH
159: .equalsIgnoreCase(fileType)) {
160: ffTable = new FixedWidthFlatfile(table);
161: } else {
162: ffTable = new FlatfileTable(table) {
163: };
164: }
165:
166: if (ffTable != null) {
167: FlatfileNode fileNode = createFlatfileNode(ffTable);
168: modelNode.getChildren().add(
169: new Node[] { fileNode });
170: }
171: } catch (IntrospectionException ignore) {
172: mLogger
173: .errorNoloc(
174: mLoc
175: .t(
176: "PRSR071: Caught exception while building FlatfileNode for{0}",
177: table.getName()),
178: ignore);
179: }
180: }
181: } catch (IntrospectionException ignore) {
182: mLogger
183: .errorNoloc(
184: mLoc
185: .t(
186: "PRSR072: Caught exception while building FlatfileNode for{0}",
187: dbModel.getModelName()),
188: ignore);
189: }
190: }
191:
192: private FlatfileNode createFlatfileNode(FlatfileTable table)
193: throws IntrospectionException {
194: FlatfileNode fileNode = null;
195:
196: if (table.getColumnList().size() == 0) {
197: fileNode = new FlatfileNode.Leaf(table);
198: } else {
199: fileNode = new FlatfileNode(table);
200:
201: Collection fields = table.getColumnList();
202: Iterator iter = fields.iterator();
203:
204: List newNodes = new ArrayList(fields.size());
205: while (iter.hasNext()) {
206: FlatfileDBColumn column = (FlatfileDBColumn) iter
207: .next();
208: FlatfileColumn ffColumn = new FlatfileColumn(column);
209: newNodes.add(new FlatfileNode.Leaf(ffColumn));
210: }
211:
212: if (!newNodes.isEmpty()) {
213: fileNode.getChildren().add(
214: (Node[]) newNodes.toArray(new Node[newNodes
215: .size()]));
216: }
217: }
218:
219: return fileNode;
220: }
221:
222: private void updateUserObjects() {
223: if (modelNode == null) {
224: return;
225: }
226:
227: final Children children = modelNode.getChildren();
228: Children.MUTEX.postReadRequest(new Runnable() {
229:
230: public void run() {
231: Node[] nodes = children.getNodes(true);
232: for (int i = 0; i < nodes.length; i++) {
233: FlatfileNode aNode = (FlatfileNode) nodes[i];
234: aNode.updateUserObject();
235: }
236: }
237: });
238: }
239: }
|