1 19 20 package org.netbeans.swing.outline; 21 22 import java.awt.BorderLayout ; 23 import java.io.File ; 24 import java.util.Arrays ; 25 import java.util.Date ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import javax.swing.JFrame ; 30 import javax.swing.JScrollPane ; 31 import javax.swing.UIManager ; 32 import javax.swing.table.TableModel ; 33 import javax.swing.tree.DefaultTreeModel ; 34 import javax.swing.tree.TreeModel ; 35 import javax.swing.tree.TreeNode ; 36 import org.openide.util.Utilities; 37 38 43 public class TestOutline extends JFrame { 44 private Outline outline; 45 46 public TestOutline() { 47 setDefaultCloseOperation (EXIT_ON_CLOSE); 48 getContentPane().setLayout (new BorderLayout ()); 49 50 55 56 TreeModel 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 (outline), BorderLayout.CENTER); 71 setBounds (20, 20, 700, 400); 72 } 73 74 76 public static TreeModel createModel() { 77 80 TreeModel treeMdl = new FileTreeModel ( 81 File.listRoots()[Utilities.isWindows() ? 1 : 0]); 82 return treeMdl; 83 } 84 85 public static void main(String [] ignored) { 86 try { 87 } catch (Exception e) {} 89 90 new TestOutline().show(); 91 } 92 93 private class FileRowModel implements RowModel { 94 95 public Class getColumnClass(int column) { 96 switch (column) { 97 case 0 : return Date .class; 98 case 1 : return Long .class; 99 default : assert false; 100 } 101 return null; 102 } 103 104 public int getColumnCount() { 105 return 2; 106 } 107 108 public String getColumnName(int column) { 109 return column == 0 ? "Date" : "Size"; 110 } 111 112 public Object getValueFor(Object node, int column) { 113 File f = (File ) node; 114 switch (column) { 115 case 0 : return new Date (f.lastModified()); 116 case 1 : return new Long (f.length()); 117 default : assert false; 118 } 119 return null; 120 } 121 122 public boolean isCellEditable(Object node, int column) { 123 return false; 124 } 125 126 public void setValueFor(Object node, int column, Object value) { 127 } 129 130 } 131 132 133 private class RenderData implements RenderDataProvider { 134 135 public java.awt.Color getBackground(Object o) { 136 return null; 137 } 138 139 public String getDisplayName(Object o) { 140 return ((File ) o).getName(); 141 } 142 143 public java.awt.Color getForeground(Object o) { 144 File f = (File ) o; 145 if (!f.isDirectory() && !f.canWrite()) { 146 return UIManager.getColor ("controlShadow"); 147 } 148 return null; 149 } 150 151 public javax.swing.Icon getIcon(Object o) { 152 return null; 153 154 } 155 156 public String getTooltipText(Object o) { 157 File f = (File ) o; 158 return f.getAbsolutePath(); 159 } 160 161 public boolean isHtmlDisplayName(Object o) { 162 return false; 163 } 164 165 } 166 167 private static class FileTreeModel implements TreeModel { 168 private File root; 169 public FileTreeModel (File root) { 170 this.root = root; 171 } 172 173 public void addTreeModelListener(javax.swing.event.TreeModelListener l) { 174 } 176 177 public Object getChild(Object parent, int index) { 178 File f = (File ) parent; 179 return f.listFiles()[index]; 180 } 181 182 public int getChildCount(Object parent) { 183 File f = (File ) parent; 184 if (!f.isDirectory()) { 185 return 0; 186 } else { 187 return f.list().length; 188 } 189 } 190 191 public int getIndexOfChild(Object parent, Object child) { 192 File par = (File ) parent; 193 File ch = (File ) child; 194 return Arrays.asList(par.listFiles()).indexOf(ch); 195 } 196 197 public Object getRoot() { 198 return root; 199 } 200 201 public boolean isLeaf(Object node) { 202 File f = (File ) node; 203 return !f.isDirectory(); 204 } 205 206 public void removeTreeModelListener(javax.swing.event.TreeModelListener l) { 207 } 209 210 public void valueForPathChanged(javax.swing.tree.TreePath path, Object newValue) { 211 } 213 } 214 } 215 | Popular Tags |