/Users/lyon/j4p/src/ip/raul/MyFileDialog.java

1    package ip.raul; 
2     
3    import java.awt.*; 
4    import java.awt.event.*; 
5    import java.io.InputStream; 
6    import java.util.NoSuchElementException; 
7    import java.util.StringTokenizer; 
8     
9    public class MyFileDialog extends Dialog implements 
10           ActionListener, ItemListener, WindowListener { 
11    
12       public String currentPath = new String(); 
13       public InputStream theFile = null; 
14       public String hostName = "vinny.bridgeport.edu"; 
15       public String userName = "anonymous"; 
16       public String password = "ip.raul@bridgeport.edu"; 
17    
18    
19       private static int colSpace = 5; 
20       private static int rowSpace = 5; 
21    
22       private Choice Dirs = new Choice(); 
23       private List fileList = new List(11, false); 
24       public Choice Ftypes = new Choice(); 
25       public Button openButton = new Button("Open"); 
26       public Button cancelButton = new Button("Cancel"); 
27       private TextField fName = new TextField(""); 
28    
29       public MyFileDialog(Frame parent) { 
30           super(parent, "Select File, please...", false); 
31    
32           init(); 
33       } 
34    
35       public static void main(String args[]) { 
36           MyFileDialog mf = new MyFileDialog(new Frame("d")); 
37           mf.show(); 
38       } 
39    
40       public void init() { 
41           Panel p0 = new Panel(); 
42           Panel p1 = new Panel(); 
43           Panel p2 = new Panel(); 
44           p0.setLayout(new GridLayout(1, 2, rowSpace, colSpace)); 
45           p1.setLayout(new GridLayout(1, 2, rowSpace, colSpace)); 
46           p2.setLayout(new GridLayout(2, 3, rowSpace, colSpace)); 
47           p0.add(new Label("Look in folder:")); 
48           p0.add(Dirs); 
49           p1.add(fileList); 
50           p2.add(new Label("File name:")); 
51           p2.add(fName); 
52           p2.add(openButton); 
53           p2.add(new Label("Files of type:")); 
54           p2.add(Ftypes); 
55           p2.add(cancelButton); 
56           add("North", p0); 
57           add("Center", p1); 
58           add("South", p2); 
59    
60           Ftypes.removeAll(); 
61           Dirs.removeAll(); 
62           currentPath = "/"; 
63           Dirs.add(currentPath); 
64           Ftypes.addItem("All"); 
65           Ftypes.addItem(".ip.gif"); 
66           Ftypes.addItem(".jpg"); 
67           Ftypes.addItem(".ppm"); 
68           Ftypes.addItem(".java"); 
69           Ftypes.addItem(".gui.html"); 
70           Ftypes.addItem(".zip"); 
71           Ftypes.select(Ftypes.getItem(0)); 
72    
73           pack(); 
74           setSize(400, 300); 
75           show(); 
76           cancelButton.addActionListener(this); 
77           openButton.addActionListener(this); 
78           addWindowListener(this); 
79           Dirs.addItemListener(this); 
80           fileList.addItemListener(this); 
81           Ftypes.addItemListener(this); 
82    
83       } 
84    
85    
86       public void UpdateDIRS() { 
87           String txt1 = new String(); 
88           Dirs.removeAll(); 
89           Dirs.add("/"); 
90           StringTokenizer parser = new StringTokenizer(currentPath, "/"); 
91           try { 
92               while (parser.hasMoreElements()) { 
93                   txt1 = (String) parser.nextElement(); 
94                   Dirs.addItem(txt1 + "/"); 
95               } 
96           } catch (NoSuchElementException e) { 
97           } 
98           Dirs.select(Dirs.getItem(Dirs.getItemCount() - 1)); 
99       } 
100   
101   
102      private void Dirs_Click() { 
103          try { 
104              String txt = new String(); 
105              int i1 = Dirs.getSelectedIndex(); 
106              txt = ""; 
107              for (int i = 0; i <= i1; i++) { 
108                  txt = txt + Dirs.getItem(i); 
109              } 
110              currentPath = txt; 
111              UpdateDIRS(); 
112          } catch (Exception ex) { 
113          } 
114      } 
115   
116   
117      private void fileList_Click() { 
118          String txt = new String(); 
119          try { 
120              txt = fileList.getItem(fileList.getSelectedIndex()); 
121              if (txt.endsWith("/")) { 
122                  Dirs.addItem(txt); 
123                  Dirs.select(Dirs.getItem(Dirs.getItemCount() - 1)); 
124                  currentPath = currentPath + txt; 
125   
126              } else { 
127                  fName.setText(txt); 
128              } 
129          } catch (Exception ex) { 
130          } 
131      } 
132   
133   
134      public void itemStateChanged(ItemEvent e) { 
135          if (e.getSource() == Dirs) { 
136              Dirs_Click(); 
137              return; 
138          } 
139          if (e.getSource() == fileList) { 
140              fileList_Click(); 
141              return; 
142          } 
143   
144      } 
145   
146   
147      public void actionPerformed(ActionEvent e) { 
148          Button b = (Button) e.getSource(); 
149          if (b == cancelButton) setVisible(false); 
150   
151      } 
152   
153      public void windowClosing(WindowEvent e) { 
154          dispose(); 
155      } 
156   
157      public void windowClosed(WindowEvent e) { 
158      }; 
159      public void windowDeiconified(WindowEvent e) { 
160      }; 
161      public void windowIconified(WindowEvent e) { 
162      }; 
163      public void windowActivated(WindowEvent e) { 
164      }; 
165      public void windowDeactivated(WindowEvent e) { 
166      }; 
167      public void windowOpened(WindowEvent e) { 
168      }; 
169   
170   
171  }