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.BorderLayout;
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.Comparator;
047: import java.util.Iterator;
048: import java.util.List;
049:
050: import javax.swing.JLabel;
051: import javax.swing.JPanel;
052: import javax.swing.JScrollPane;
053: import javax.swing.JTree;
054: import javax.swing.ToolTipManager;
055: import javax.swing.tree.DefaultMutableTreeNode;
056: import javax.swing.tree.DefaultTreeModel;
057: import javax.swing.tree.TreeNode;
058: import javax.swing.tree.TreePath;
059: import org.netbeans.modules.sql.framework.model.DBColumn;
060:
061: import org.netbeans.modules.sql.framework.common.utils.NativeColumnOrderComparator;
062: import org.netbeans.modules.sql.framework.model.DBTable;
063: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
064: import org.netbeans.modules.sql.framework.model.SQLDBTable;
065:
066: /**
067: * @author Jonathan Giron
068: * @version $Revision$
069: */
070: public class TableColumnTreePanel extends JPanel {
071:
072: private Comparator<DBColumn> columnComparator = null;
073:
074: private DefaultMutableTreeNode rootNode;
075:
076: private List tables;
077:
078: private JTree tree;
079:
080: /**
081: * Creates a new instance of TableTreeView using the given List of Databases and showing
082: * all components (Databases, tables, and columns).
083: *
084: * @param dbList List containing Databases to display
085: */
086: public TableColumnTreePanel(List tableList) {
087: this (tableList, false);
088: }
089:
090: /**
091: * Creates a new instance of TableTreeView using the given List of Databases and showing
092: * all components (Db, tables, and columns).
093: *
094: * @param DbList List containing Databases to display
095: * @param useColumnOrdinalPosition true if columns should be sorted based on their
096: * ordinal position; false to sort by column name (ascending)
097: */
098: public TableColumnTreePanel(List tableList,
099: boolean useColumnOrdinalPosition) {
100: super ();
101:
102: if (useColumnOrdinalPosition) {
103: columnComparator = NativeColumnOrderComparator
104: .getInstance();
105: }
106: this .tables = tableList;
107: init();
108: }
109:
110: /**
111: * Gets List of selected items, if any, in this component.
112: *
113: * @return List (possibly empty) of user-selected items
114: */
115: public List<DBColumn> getSelectedItems() {
116: List<DBColumn> itemList = new ArrayList<DBColumn>();
117:
118: TreePath[] paths = tree.getSelectionPaths();
119: if (paths != null) {
120: for (int i = 0; i < paths.length; i++) {
121: Object obj = ((DefaultMutableTreeNode) paths[i]
122: .getLastPathComponent()).getUserObject();
123: if (obj instanceof SQLDBColumn) {
124: SQLDBColumn column = (SQLDBColumn) obj;
125: if (column.isVisible()) {
126: itemList.add((SQLDBColumn) obj);
127: }
128: }
129: }
130: }
131: return itemList;
132: }
133:
134: public List<TreeNode> getTableColumnNodes() {
135: List<TreeNode> tableNodes = new ArrayList<TreeNode>();
136: for (int i = 0; i < rootNode.getChildCount(); i++) {
137: tableNodes.add(rootNode.getChildAt(i));
138: }
139: return tableNodes;
140: }
141:
142: /**
143: * Associates the given JLabel with this component as its target whenever its
144: * associated keyboard accelerator is triggered.
145: *
146: * @param myLabel label to associate with this component
147: */
148: public void setAsLabel(JLabel myLabel) {
149: if (myLabel != null) {
150: myLabel.setLabelFor(tree);
151: }
152: }
153:
154: public void setTables(List<DBTable> tbls) {
155: this .tables = tbls;
156: DefaultTreeModel treeModel = createTreeModel();
157: tree.setModel(treeModel);
158: }
159:
160: private void createColumnNodes(DBTable table,
161: DefaultMutableTreeNode parentNode) {
162: List<DBColumn> columnList = new ArrayList<DBColumn>(table
163: .getColumnList());
164: Collections.sort(columnList, columnComparator);
165: Iterator it = columnList.iterator();
166: while (it.hasNext()) {
167: SQLDBColumn column = (SQLDBColumn) it.next();
168: TableColumnNode columnNode = new TableColumnNode.Leaf(
169: column);
170: if (column.isForeignKey() || column.isPrimaryKey()) {
171: columnNode.setEnabled(false);
172: } else {
173: columnNode.setEnabled(true);
174: }
175: parentNode.add(columnNode);
176: }
177: }
178:
179: private DefaultTreeModel createTreeModel() {
180: rootNode = new DefaultMutableTreeNode();
181: DefaultTreeModel model = new DefaultTreeModel(rootNode);
182:
183: Iterator it = tables.iterator();
184: while (it.hasNext()) {
185: SQLDBTable table = (SQLDBTable) it.next();
186: TableColumnNode columnAncestor = new TableColumnNode(table);
187: rootNode.add(columnAncestor);
188:
189: createColumnNodes(table, columnAncestor);
190: columnAncestor.setSelectedBasedOnChildren();
191: }
192:
193: return model;
194: }
195:
196: private void expandAllChildNodes(DefaultTreeModel model) {
197: if (tree != null && model != null) {
198: final int childCount = model.getChildCount(model.getRoot());
199: for (int i = 0; i < childCount; i++) {
200: DefaultMutableTreeNode node = (DefaultMutableTreeNode) model
201: .getChild(model.getRoot(), i);
202: tree.expandPath(new TreePath(node.getPath()));
203: }
204: }
205: }
206:
207: private void init() {
208: this .setLayout(new BorderLayout());
209:
210: tree = new JTree();
211: DefaultTreeModel treeModel = createTreeModel();
212:
213: tree.setModel(treeModel);
214: tree.setEditable(true);
215: tree.setRootVisible(false);
216: tree.setShowsRootHandles(true);
217: tree.setCellEditor(new TableColumnTreeCellEditor(this ));
218: tree.setCellRenderer(new TableColumnTreeCellRenderer());
219: tree.setSelectionModel(null);
220:
221: ToolTipManager.sharedInstance().registerComponent(tree);
222: expandAllChildNodes(treeModel);
223:
224: JScrollPane sPane = new JScrollPane(tree);
225: this.add(BorderLayout.CENTER, sPane);
226: }
227: }
|