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:
042: /*
043: * Test.java
044: *
045: * Created on January 28, 2004, 6:15 PM
046: */
047:
048: package org.netbeans.swing.outline;
049:
050: import java.awt.BorderLayout;
051: import java.io.File;
052: import java.util.Arrays;
053: import java.util.Date;
054: import javax.swing.JFrame;
055: import javax.swing.JScrollPane;
056: import javax.swing.UIManager;
057: import javax.swing.tree.TreeModel;
058: import org.openide.util.Utilities;
059:
060: /** A simple test of the Outline (aka TreeTable) class which implements
061: * a filesystem browser.
062: *
063: * @author Tim Boudreau
064: */
065: public class TestOutline extends JFrame {
066: private Outline outline;
067:
068: /** Creates a new instance of Test */
069: public TestOutline() {
070: setDefaultCloseOperation(EXIT_ON_CLOSE);
071: getContentPane().setLayout(new BorderLayout());
072:
073: //Use root 1 on windows to avoid making a tree of the floppy drive.
074: /*
075: TreeModel treeMdl = new DefaultTreeModel(
076: new FileTreeNode(File.listRoots()[Utilities.isWindows() ? 1 : 0]));
077: */
078:
079: TreeModel treeMdl = createModel();
080:
081: OutlineModel mdl = DefaultOutlineModel.createOutlineModel(
082: treeMdl, new FileRowModel(), true);
083:
084: outline = new Outline();
085:
086: outline.setRenderDataProvider(new RenderData());
087:
088: outline.setRootVisible(true);
089:
090: outline.setModel(mdl);
091:
092: getContentPane().add(new JScrollPane(outline),
093: BorderLayout.CENTER);
094: setBounds(20, 20, 700, 400);
095: }
096:
097: /** A handy method to create a model to install into a JTree to compare
098: * behavior of a real JTree's layout cache and ours */
099: public static TreeModel createModel() {
100: // TreeModel treeMdl = /*new DefaultTreeModel(
101: // new FileTreeNode(File.listRoots()[Utilities.isWindows() ? 1 : 0]));
102:
103: TreeModel treeMdl = new FileTreeModel(
104: File.listRoots()[Utilities.isWindows() ? 1 : 0]);
105: return treeMdl;
106: }
107:
108: public static void main(String[] ignored) {
109: try {
110: //UIManager.setLookAndFeel (new javax.swing.plaf.metal.MetalLookAndFeel());
111: } catch (Exception e) {
112: }
113:
114: new TestOutline().show();
115: }
116:
117: private class FileRowModel implements RowModel {
118:
119: public Class getColumnClass(int column) {
120: switch (column) {
121: case 0:
122: return Date.class;
123: case 1:
124: return Long.class;
125: default:
126: assert false;
127: }
128: return null;
129: }
130:
131: public int getColumnCount() {
132: return 2;
133: }
134:
135: public String getColumnName(int column) {
136: return column == 0 ? "Date" : "Size";
137: }
138:
139: public Object getValueFor(Object node, int column) {
140: File f = (File) node;
141: switch (column) {
142: case 0:
143: return new Date(f.lastModified());
144: case 1:
145: return new Long(f.length());
146: default:
147: assert false;
148: }
149: return null;
150: }
151:
152: public boolean isCellEditable(Object node, int column) {
153: return false;
154: }
155:
156: public void setValueFor(Object node, int column, Object value) {
157: //do nothing for now
158: }
159:
160: }
161:
162: private class RenderData implements RenderDataProvider {
163:
164: public java.awt.Color getBackground(Object o) {
165: return null;
166: }
167:
168: public String getDisplayName(Object o) {
169: return ((File) o).getName();
170: }
171:
172: public java.awt.Color getForeground(Object o) {
173: File f = (File) o;
174: if (!f.isDirectory() && !f.canWrite()) {
175: return UIManager.getColor("controlShadow");
176: }
177: return null;
178: }
179:
180: public javax.swing.Icon getIcon(Object o) {
181: return null;
182:
183: }
184:
185: public String getTooltipText(Object o) {
186: File f = (File) o;
187: return f.getAbsolutePath();
188: }
189:
190: public boolean isHtmlDisplayName(Object o) {
191: return false;
192: }
193:
194: }
195:
196: private static class FileTreeModel implements TreeModel {
197: private File root;
198:
199: public FileTreeModel(File root) {
200: this .root = root;
201: }
202:
203: public void addTreeModelListener(
204: javax.swing.event.TreeModelListener l) {
205: //do nothing
206: }
207:
208: public Object getChild(Object parent, int index) {
209: File f = (File) parent;
210: return f.listFiles()[index];
211: }
212:
213: public int getChildCount(Object parent) {
214: File f = (File) parent;
215: if (!f.isDirectory()) {
216: return 0;
217: } else {
218: return f.list().length;
219: }
220: }
221:
222: public int getIndexOfChild(Object parent, Object child) {
223: File par = (File) parent;
224: File ch = (File) child;
225: return Arrays.asList(par.listFiles()).indexOf(ch);
226: }
227:
228: public Object getRoot() {
229: return root;
230: }
231:
232: public boolean isLeaf(Object node) {
233: File f = (File) node;
234: return !f.isDirectory();
235: }
236:
237: public void removeTreeModelListener(
238: javax.swing.event.TreeModelListener l) {
239: //do nothing
240: }
241:
242: public void valueForPathChanged(javax.swing.tree.TreePath path,
243: Object newValue) {
244: //do nothing
245: }
246: }
247: }
|