KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > outline > TestOutline


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.outline;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.JFrame JavaDoc;
30 import javax.swing.JScrollPane JavaDoc;
31 import javax.swing.UIManager JavaDoc;
32 import javax.swing.table.TableModel JavaDoc;
33 import javax.swing.tree.DefaultTreeModel JavaDoc;
34 import javax.swing.tree.TreeModel JavaDoc;
35 import javax.swing.tree.TreeNode JavaDoc;
36 import org.openide.util.Utilities;
37
38 /** A simple test of the Outline (aka TreeTable) class which implements
39  * a filesystem browser.
40  *
41  * @author Tim Boudreau
42  */

43 public class TestOutline extends JFrame JavaDoc {
44     private Outline outline;
45     /** Creates a new instance of Test */
46     public TestOutline() {
47         setDefaultCloseOperation (EXIT_ON_CLOSE);
48         getContentPane().setLayout (new BorderLayout JavaDoc());
49         
50         //Use root 1 on windows to avoid making a tree of the floppy drive.
51
/*
52         TreeModel treeMdl = new DefaultTreeModel(
53             new FileTreeNode(File.listRoots()[Utilities.isWindows() ? 1 : 0]));
54          */

55             
56         TreeModel JavaDoc treeMdl = createModel();
57         
58         OutlineModel mdl = DefaultOutlineModel.createOutlineModel(treeMdl,
59             new FileRowModel(), true);
60         
61         outline = new Outline();
62         
63         outline.setRenderDataProvider(new RenderData());
64         
65         outline.setRootVisible (true);
66         
67         outline.setModel (mdl);
68         
69         
70         getContentPane().add(new JScrollPane JavaDoc(outline), BorderLayout.CENTER);
71         setBounds (20, 20, 700, 400);
72     }
73     
74     /** A handy method to create a model to install into a JTree to compare
75      * behavior of a real JTree's layout cache and ours */

76     public static TreeModel JavaDoc createModel() {
77 // TreeModel treeMdl = /*new DefaultTreeModel(
78
// new FileTreeNode(File.listRoots()[Utilities.isWindows() ? 1 : 0]));
79

80         TreeModel JavaDoc treeMdl = new FileTreeModel (
81             File.listRoots()[Utilities.isWindows() ? 1 : 0]);
82         return treeMdl;
83     }
84     
85     public static void main(String JavaDoc[] ignored) {
86         try {
87            //UIManager.setLookAndFeel (new javax.swing.plaf.metal.MetalLookAndFeel());
88
} catch (Exception JavaDoc e) {}
89         
90         new TestOutline().show();
91     }
92     
93     private class FileRowModel implements RowModel {
94         
95         public Class JavaDoc getColumnClass(int column) {
96             switch (column) {
97                 case 0 : return Date JavaDoc.class;
98                 case 1 : return Long JavaDoc.class;
99                 default : assert false;
100             }
101             return null;
102         }
103         
104         public int getColumnCount() {
105             return 2;
106         }
107         
108         public String JavaDoc getColumnName(int column) {
109             return column == 0 ? "Date" : "Size";
110         }
111         
112         public Object JavaDoc getValueFor(Object JavaDoc node, int column) {
113             File JavaDoc f = (File JavaDoc) node;
114             switch (column) {
115                 case 0 : return new Date JavaDoc (f.lastModified());
116                 case 1 : return new Long JavaDoc (f.length());
117                 default : assert false;
118             }
119             return null;
120         }
121         
122         public boolean isCellEditable(Object JavaDoc node, int column) {
123             return false;
124         }
125         
126         public void setValueFor(Object JavaDoc node, int column, Object JavaDoc value) {
127             //do nothing for now
128
}
129         
130     }
131     
132     
133     private class RenderData implements RenderDataProvider {
134         
135         public java.awt.Color JavaDoc getBackground(Object JavaDoc o) {
136             return null;
137         }
138         
139         public String JavaDoc getDisplayName(Object JavaDoc o) {
140             return ((File JavaDoc) o).getName();
141         }
142         
143         public java.awt.Color JavaDoc getForeground(Object JavaDoc o) {
144             File JavaDoc f = (File JavaDoc) o;
145             if (!f.isDirectory() && !f.canWrite()) {
146                 return UIManager.getColor ("controlShadow");
147             }
148             return null;
149         }
150         
151         public javax.swing.Icon JavaDoc getIcon(Object JavaDoc o) {
152             return null;
153         
154         }
155         
156         public String JavaDoc getTooltipText(Object JavaDoc o) {
157             File JavaDoc f = (File JavaDoc) o;
158             return f.getAbsolutePath();
159         }
160         
161         public boolean isHtmlDisplayName(Object JavaDoc o) {
162             return false;
163         }
164         
165     }
166     
167     private static class FileTreeModel implements TreeModel JavaDoc {
168         private File JavaDoc root;
169         public FileTreeModel (File JavaDoc root) {
170             this.root = root;
171         }
172         
173         public void addTreeModelListener(javax.swing.event.TreeModelListener JavaDoc l) {
174             //do nothing
175
}
176         
177         public Object JavaDoc getChild(Object JavaDoc parent, int index) {
178             File JavaDoc f = (File JavaDoc) parent;
179             return f.listFiles()[index];
180         }
181         
182         public int getChildCount(Object JavaDoc parent) {
183             File JavaDoc f = (File JavaDoc) parent;
184             if (!f.isDirectory()) {
185                 return 0;
186             } else {
187                 return f.list().length;
188             }
189         }
190         
191         public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
192             File JavaDoc par = (File JavaDoc) parent;
193             File JavaDoc ch = (File JavaDoc) child;
194             return Arrays.asList(par.listFiles()).indexOf(ch);
195         }
196         
197         public Object JavaDoc getRoot() {
198             return root;
199         }
200         
201         public boolean isLeaf(Object JavaDoc node) {
202             File JavaDoc f = (File JavaDoc) node;
203             return !f.isDirectory();
204         }
205         
206         public void removeTreeModelListener(javax.swing.event.TreeModelListener JavaDoc l) {
207             //do nothing
208
}
209         
210         public void valueForPathChanged(javax.swing.tree.TreePath JavaDoc path, Object JavaDoc newValue) {
211             //do nothing
212
}
213     }
214 }
215
Popular Tags