/Users/lyon/j4p/src/gui/dialogs/DirectoryChooser.java

1    package gui.dialogs; 
2     
3    import gui.run.RunButton; 
4    import utils.SystemUtils; 
5     
6    import javax.swing.*; 
7    import javax.swing.event.TreeExpansionEvent; 
8    import javax.swing.event.TreeExpansionListener; 
9    import javax.swing.tree.DefaultMutableTreeNode; 
10   import javax.swing.tree.DefaultTreeCellRenderer; 
11   import javax.swing.tree.DefaultTreeModel; 
12   import javax.swing.tree.TreePath; 
13   import java.awt.*; 
14   import java.io.File; 
15   import java.io.Serializable; 
16    
17   public class DirectoryChooser extends JDialog 
18           implements TreeExpansionListener,Serializable { 
19    
20       public static final int WIDTH = 400; 
21       public static final int HEIGHT = 340; 
22       DefaultMutableTreeNode root; 
23       JTree directoryJTree; 
24       private String selectedDirectory; 
25       String fileSeparator = SystemUtils.getDirectorySeparator(); 
26    
27    
28       public static void main(String[] args) { 
29           String directory = getDirectory("select a directory"); 
30           System.out.println("you selected:" + directory); 
31    
32       } 
33    
34       public static String getDirectory(String prompt) { 
35           DirectoryChooser dc = new DirectoryChooser(null, true); 
36           dc.setTitle(prompt); 
37           dc.show(); 
38           String directory = dc.getSelectedDirectory(); 
39           return directory; 
40       } 
41    
42       class TreeCellRenderer extends DefaultTreeCellRenderer { 
43    
44    
45           public Component getTreeCellRendererComponent(JTree tree, 
46                                                         Object value, 
47                                                         boolean selected, 
48                                                         boolean expanded, 
49                                                         boolean leaf, 
50                                                         int row, 
51                                                         boolean hasFocus) { 
52               if (leaf) 
53                   return super.getTreeCellRendererComponent(tree, 
54                           value, 
55                           selected, 
56                           expanded, 
57                           false, 
58                           row, 
59                           hasFocus); 
60               else 
61                   return super.getTreeCellRendererComponent(tree, 
62                           value, 
63                           selected, 
64                           expanded, 
65                           leaf, 
66                           row, 
67                           hasFocus); 
68           } 
69    
70           TreeCellRenderer() { 
71           } 
72       } 
73    
74    
75       public DirectoryChooser(Frame owner, boolean allowCreate) { 
76           super(owner, "Directory Chooser", true); 
77    
78           root = new DefaultMutableTreeNode("Directory"); 
79           directoryJTree = new JTree(root); 
80           Container container = getContentPane(); 
81           container.setLayout(new BorderLayout()); 
82           JLabel label = new JLabel("Please select a directory"); 
83           JPanel buttonPanel = new JPanel(); 
84           buttonPanel.add(new RunButton("ok") { 
85               public void run() { 
86                   doOk(); 
87               } 
88           }); 
89           if (allowCreate) 
90               buttonPanel.add(new RunButton("create") { 
91                   public void run() { 
92                       doCreate(); 
93                   } 
94               }); 
95           directoryJTree.setCellRenderer(new TreeCellRenderer()); 
96           directoryJTree.addTreeExpansionListener(this); 
97           constructTree(); 
98           container.add(label, "North"); 
99           container.add(new JScrollPane(directoryJTree), "Center"); 
100          container.add(buttonPanel, "South"); 
101          setSize(400, 340); 
102      } 
103   
104      private void constructTree() { 
105          File directoryRoot[] = File.listRoots(); 
106          for (int i = 0; i < directoryRoot.length; i++) { 
107              DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode( 
108                      directoryRoot[i]); 
109              foundSubDirectory(tempNode, directoryRoot[i]); 
110              root.add(tempNode); 
111          } 
112   
113          directoryJTree.setModel(new DefaultTreeModel(root)); 
114   
115   
116          SwingUtilities.updateComponentTreeUI(directoryJTree); 
117   
118   
119      } 
120   
121   
122      private void restoreTreeToLastKnownState(String lastSelectedDirectory) { 
123          TreeExpansionEvent event = new TreeExpansionEvent(this, 
124                  new TreePath(lastSelectedDirectory)); 
125          treeExpanded(event); 
126      } 
127   
128      public void treeExpanded(TreeExpansionEvent event) { 
129          setCursor(new Cursor(3)); 
130          DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) event.getPath() 
131                  .getLastPathComponent(); 
132          for (int i = 0; i < selectedNode.getChildCount(); i++) { 
133              DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) selectedNode.getChildAt( 
134                      i); 
135              if (childNode.isLeaf()) 
136                  foundSubDirectory(childNode, 
137                          (File) childNode.getUserObject()); 
138          } 
139   
140          setCursor(new Cursor(0)); 
141      } 
142   
143      private void foundSubDirectory(DefaultMutableTreeNode currentNode, 
144                                     File directory) { 
145          File fileList[] = directory.listFiles(); 
146          if (fileList == null) 
147              return; 
148          for (int i = 0; i < fileList.length; i++) 
149              if (fileList[i].isDirectory()) 
150                  currentNode.add(new DefaultMutableTreeNode(fileList[i]) { 
151   
152                      public String toString() { 
153                          return ((File) getUserObject()).getName(); 
154                      } 
155   
156                  }); 
157   
158      } 
159   
160      public void treeCollapsed(TreeExpansionEvent treeexpansionevent) { 
161      } 
162   
163   
164      private void doCreate() { 
165          Object selectedObject = directoryJTree.getLastSelectedPathComponent(); 
166          if (selectedObject == null || selectedObject == root) { 
167              JOptionPane.showMessageDialog(this, 
168                      "Please select a parent for the new directory", 
169                      "Error", 
170                      1); 
171              return; 
172          } 
173          String nameOfCreation = JOptionPane.showInputDialog(this, 
174                  "Please enter the name of the new directory", 
175                  ""); 
176          if (nameOfCreation == null || 
177                  nameOfCreation.trim().equals("")) { 
178              JOptionPane.showMessageDialog(this, 
179                      "Please enter the name of the new directory", 
180                      "Error", 
181                      1); 
182              return; 
183          } 
184          File selectDirectory = (File) ((DefaultMutableTreeNode) selectedObject).getUserObject(); 
185          if (JOptionPane.showConfirmDialog(this, 
186                  "Create directory " + 
187                  nameOfCreation + 
188                  " to " + 
189                  selectDirectory.getAbsolutePath() + 
190                  "?", 
191                  "Confirmation", 
192                  0) == 
193                  0) { 
194              File newDirectory = new File(selectDirectory.getAbsolutePath() + 
195                      fileSeparator + 
196                      nameOfCreation); 
197              if (newDirectory.mkdir()) { 
198                  DefaultMutableTreeNode newLead = new DefaultMutableTreeNode( 
199                          newDirectory); 
200                  ((DefaultMutableTreeNode) selectedObject).add(newLead); 
201                  TreePath path = directoryJTree.getLeadSelectionPath(); 
202                  directoryJTree.setModel(new DefaultTreeModel(root)); 
203                  directoryJTree.expandPath(path); 
204                  path = path.pathByAddingChild(newLead); 
205                  directoryJTree.setSelectionPath(path); 
206                  SwingUtilities.updateComponentTreeUI(directoryJTree); 
207              } else { 
208                  JOptionPane.showMessageDialog(this, 
209                          "Unexpected error", 
210                          "Error", 
211                          0); 
212              } 
213          } 
214      } 
215   
216      private void doOk() { 
217          Object selectedDirectory = directoryJTree.getLastSelectedPathComponent(); 
218   
219          if (selectedDirectory == null) 
220              return; 
221          this.selectedDirectory = 
222                  ((File) ((DefaultMutableTreeNode) selectedDirectory 
223                  ).getUserObject()).getAbsolutePath(); 
224          hide(); 
225          getParent().remove(this); 
226      } 
227   
228   
229   
230      public String getSelectedDirectory() { 
231          return selectedDirectory; 
232      } 
233   
234  } 
235