KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > awt > AWTFileSelector


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.awt;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Checkbox JavaDoc;
24 import java.awt.Choice JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Dimension JavaDoc;
27 import java.awt.FlowLayout JavaDoc;
28 import java.awt.GridBagConstraints JavaDoc;
29 import java.awt.GridBagLayout JavaDoc;
30 import java.awt.Insets JavaDoc;
31 import java.awt.Label JavaDoc;
32 import java.awt.Panel JavaDoc;
33 import java.awt.TextField JavaDoc;
34 import java.awt.Toolkit JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.awt.event.ItemEvent JavaDoc;
38 import java.awt.event.ItemListener JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.text.MessageFormat JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.Vector JavaDoc;
44
45 import com.sshtools.ui.FileFilter;
46 import com.sshtools.ui.FileSelector;
47 import com.sshtools.ui.StockIcons;
48 import com.sshtools.ui.awt.options.Option;
49 import com.sshtools.ui.awt.options.OptionDialog;
50 import com.sshtools.util.StringComparator;
51 import com.sshtools.util.Util;
52
53 /**
54  *
55  */

56 public class AWTFileSelector extends Panel JavaDoc implements FileSelector, ActionListener JavaDoc, ItemListener JavaDoc {
57
58
59     private java.awt.List JavaDoc files;
60     private File JavaDoc cwd;
61     private TextField JavaDoc path;
62     // private Label cwdLabel;
63
private Choice JavaDoc lookIn;
64     private ImageButton go;
65     private ImageButton remove;
66     private ImageButton newFolder;
67     private ImageButton home;
68     private ImageButton parent;
69     private int type;
70     private Checkbox JavaDoc showHiddenFiles;
71     private Choice JavaDoc filterSelect;
72     private Vector JavaDoc filters;
73     
74     private FileFilter acceptAllFilter = new FileFilter() {
75
76         public String JavaDoc getDescription() {
77             return Messages.getString("AWTFileSelector.allFiles"); //$NON-NLS-1$
78
}
79
80         public boolean accept(File JavaDoc f) {
81             return true;
82         }
83         
84     };
85     
86     public AWTFileSelector() {
87         super(new BorderLayout JavaDoc());
88     }
89
90     public void init(int type, File JavaDoc cwd, boolean showButtons, boolean showHiddenFilesSwitch, boolean showButtonImages, boolean showButtonText) {
91
92         // Initialise
93
this.cwd = cwd;
94         this.type = type;
95         filters = new Vector JavaDoc();
96         filters.addElement(acceptAllFilter);
97         if (cwd == null) {
98             cwd = new File JavaDoc(System.getProperty("user.dir")); //$NON-NLS-1$
99
}
100
101         // Create the
102
files = new SelectList() {
103
104             public Dimension JavaDoc getPreferredSize() {
105                 return new Dimension JavaDoc(400, 260);
106             }
107
108             public void selected() {
109                 String JavaDoc item = files.getSelectedItem();
110                 if (item != null) {
111                     path.setText(item);
112                     selectFile();
113                 }
114             }
115
116         };
117         files.addItemListener(this);
118
119         // Create the 'Look In' component
120
lookIn = new Choice JavaDoc();
121         lookIn.addItemListener(new ItemListener JavaDoc() {
122
123             public void itemStateChanged(ItemEvent JavaDoc e) {
124                 AWTFileSelector.this.cwd = new File JavaDoc(lookIn.getSelectedItem());
125                 refresh();
126             }
127             
128         });
129         rebuildLookIn();
130
131         // Create the tool bar
132
Panel JavaDoc z = new Panel JavaDoc(new FlowLayout JavaDoc());
133         if (showButtons) {
134             home = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_HOME, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.home") : null, "home"); //$NON-NLS-1$ //$NON-NLS-2$
135
home.setHoverButton(true);
136             home.addActionListener(this);
137             home.setToolTipText(Messages.getString("AWTFileSelector.navigateToYourHomeDirectory")); //$NON-NLS-1$
138
z.add(home);
139             parent = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_UP_FOLDER, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.home") : null, "home"); //$NON-NLS-1$ //$NON-NLS-2$
140
parent.setHoverButton(true);
141             parent.addActionListener(this);
142             parent.setToolTipText(Messages.getString("AWTFileSelector.navigateToParent")); //$NON-NLS-1$
143
z.add(parent);
144             newFolder = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_NEW_FOLDER, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.new") : null, "newFolder"); //$NON-NLS-1$ //$NON-NLS-2$
145
newFolder.setHoverButton(true);
146             newFolder.addActionListener(this);
147             newFolder.setToolTipText(Messages.getString("AWTFileSelector.createFolder")); //$NON-NLS-1$
148
z.add(newFolder);
149             remove = new ImageButton(showButtonImages ? UIUtil.getStockImage(StockIcons.STOCK_DELETE, AWTFileSelector.class) : null, showButtonText ? Messages.getString("AWTFileSelector.delete") : null, "delete"); //$NON-NLS-1$ //$NON-NLS-2$
150
remove.setHoverButton(true);
151             remove.addActionListener(this);
152             remove.setToolTipText(Messages.getString("AWTFileSelector.removeSelected")); //$NON-NLS-1$
153
z.add(remove);
154         }
155         if (showHiddenFilesSwitch) {
156             showHiddenFiles = new Checkbox JavaDoc(Messages.getString("AWTFileSelector.hiddentFiles")); //$NON-NLS-1$
157
showHiddenFiles.addItemListener(new ItemListener JavaDoc() {
158
159                 public void itemStateChanged(ItemEvent JavaDoc e) {
160                     refresh();
161                 }
162             });
163         }
164
165         // Create the top bar
166
Panel JavaDoc top = new Panel JavaDoc(new GridBagLayout JavaDoc());
167         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
168         gbc.fill = GridBagConstraints.HORIZONTAL;
169         gbc.anchor = GridBagConstraints.WEST;
170         gbc.weightx = 1.0;
171         UIUtil.gridBagAdd(top, lookIn, gbc, GridBagConstraints.RELATIVE);
172         gbc.weightx = 0.0;
173         UIUtil.gridBagAdd(top, z, gbc, GridBagConstraints.REMAINDER);
174         
175         // Create the path panel
176
Panel JavaDoc pathPanel = new Panel JavaDoc(new GridBagLayout JavaDoc());
177         GridBagConstraints JavaDoc gbc1 = new GridBagConstraints JavaDoc();
178         path = new TextField JavaDoc(""); //$NON-NLS-1$
179
path.addActionListener(this);
180         gbc1.fill = GridBagConstraints.HORIZONTAL;
181         gbc1.anchor = GridBagConstraints.WEST;
182         gbc1.weightx = 0.0;
183         gbc1.insets = new Insets JavaDoc(2, 2, 2, 2);
184         UIUtil.gridBagAdd(pathPanel, new Label JavaDoc(Messages.getString("AWTFileSelector.fileName")), gbc1, showButtons ? 1 : GridBagConstraints.RELATIVE); //$NON-NLS-1$
185
gbc1.weightx = 1.0;
186         UIUtil.gridBagAdd(pathPanel, path, gbc1, showButtons ? GridBagConstraints.RELATIVE : GridBagConstraints.REMAINDER);
187         gbc1.weightx = 0.0;
188         if(showButtons) {
189             go = new ImageButton(null, Messages.getString("AWTFileSelector.go"), "go"); //$NON-NLS-1$ //$NON-NLS-2$
190
go.setHoverButton(true);
191             go.addActionListener(this);
192             go.setToolTipText(Messages.getString("AWTFileSelector.navigateToSelectedFolder")); //$NON-NLS-1$
193
UIUtil.gridBagAdd(pathPanel, go, gbc1, GridBagConstraints.REMAINDER);
194         }
195         UIUtil.gridBagAdd(pathPanel, new Label JavaDoc(Messages.getString("AWTFileSelector.filesOfType")), gbc1, showButtons ? 1 : GridBagConstraints.RELATIVE); //$NON-NLS-1$
196
gbc1.weightx = 1.0;
197         filterSelect = new Choice JavaDoc();
198         rebuildFilterSelect();
199         filterSelect.addItemListener(new ItemListener JavaDoc() {
200             public void itemStateChanged(ItemEvent JavaDoc e) {
201                 refresh();
202             }
203         });
204         UIUtil.gridBagAdd(pathPanel, filterSelect, gbc1, showButtons ? GridBagConstraints.RELATIVE : GridBagConstraints.REMAINDER);
205         gbc1.weightx = 0.0;
206         if(showButtons) {
207             UIUtil.gridBagAdd(pathPanel, new Label JavaDoc(), gbc1, GridBagConstraints.REMAINDER);
208         }
209
210         // Build the main component
211
add(top, "North"); //$NON-NLS-1$
212
add(files, "Center"); //$NON-NLS-1$
213
add(pathPanel, "South"); //$NON-NLS-1$
214
refresh();
215     }
216     
217     public void setUseAcceptAllFilter(boolean useAcceptAllFilter) {
218         if(useAcceptAllFilter && !filters.contains(acceptAllFilter)) {
219             filters.insertElementAt(acceptAllFilter, 0);
220         }
221         else if(!useAcceptAllFilter && filters.contains(acceptAllFilter)) {
222             filters.removeElement(acceptAllFilter);
223         }
224         rebuildFilterSelect();
225     }
226     
227     public void addFileFilter(FileFilter filter) {
228         filters.addElement(filter);
229         rebuildFilterSelect();
230     }
231     
232     private void rebuildFilterSelect() {
233         filterSelect.removeAll();
234         for(Enumeration JavaDoc e = filters.elements(); e.hasMoreElements(); ) {
235             FileFilter f = (FileFilter)e.nextElement();
236             filterSelect.add(f.getDescription());
237         }
238     }
239
240     private void rebuildLookIn() {
241         File JavaDoc dir = cwd;
242         String JavaDoc lastParentPath = null;
243         lookIn.removeAll();
244         while (dir != null && dir.exists()) {
245             lookIn.add(dir.getAbsolutePath());
246             String JavaDoc parentPath = dir.getParent();
247             System.out.println("Parent = " + parentPath); //$NON-NLS-1$
248
dir = parentPath == null ? null : new File JavaDoc(parentPath);
249         }
250     }
251     
252     private void gotoParent() {
253
254         String JavaDoc newPath = cwd.getAbsolutePath();
255         if (newPath.endsWith(File.separator)) {
256             newPath = newPath.substring(0, newPath.length() - 1);
257         }
258         int idx = newPath.lastIndexOf(File.separator);
259         if (idx != -1) {
260             newPath = newPath.substring(0, idx + 1);
261             cwd = new File JavaDoc(newPath);
262             path.setText(""); //$NON-NLS-1$
263
refresh();
264         }
265     }
266
267     private void selectFile() {
268         if (path.getText().equals("..")) { //$NON-NLS-1$
269
gotoParent();
270         } else {
271             File JavaDoc f = new File JavaDoc(path.getText());
272             if (!f.isAbsolute())
273                 f = new File JavaDoc(cwd, path.getText());
274             if (f.exists()) {
275                 if (f.isFile()) {
276                     cwd = new File JavaDoc(f.getParent());
277                     path.setText(f.getName());
278                 } else {
279                     cwd = f;
280                     path.setText(""); //$NON-NLS-1$
281
}
282                 refresh();
283             } else {
284                 Toolkit.getDefaultToolkit().beep();
285             }
286         }
287     }
288
289     public File JavaDoc[] getSelectedFiles() {
290         int[] sel = files.getSelectedIndexes();
291         File JavaDoc[] f = new File JavaDoc[sel.length];
292         for (int i = 0; i < sel.length; i++) {
293             f[i] = new File JavaDoc(cwd, files.getItem(sel[i]).toString());
294         }
295         return f;
296     }
297
298     public File JavaDoc getSelectedFile() {
299         File JavaDoc f = new File JavaDoc(path.getText());
300         if (f.isAbsolute())
301             if (type == 1 && !f.isDirectory())
302                 return cwd;
303             else
304                 return f;
305         if (type == 1 && !f.isDirectory())
306             return cwd;
307         else
308             return new File JavaDoc(cwd, path.getText());
309     }
310
311     public void refresh() {
312         String JavaDoc l[] = cwd.list();
313         rebuildLookIn();
314         files.removeAll();
315         Vector JavaDoc v = new Vector JavaDoc();
316         if (cwd.getParent() != null)
317             files.add(".."); //$NON-NLS-1$
318
for (int i = 0; l != null && i < l.length; i++) {
319             if (showHiddenFiles == null || showHiddenFiles.getState() && l[i].startsWith(".") || !l[i].startsWith(".")) { //$NON-NLS-1$ //$NON-NLS-2$
320
if(filters.size() == 0) {
321                     v.addElement(l[i]);
322                 }
323                 else {
324                     for(Enumeration JavaDoc e = filters.elements(); e.hasMoreElements(); ) {
325                         FileFilter filter = (FileFilter)e.nextElement();
326                         if(filter.getDescription().equals(filterSelect.getSelectedItem())) {
327                             File JavaDoc f = new File JavaDoc(cwd, l[i]);
328                             if(f.isDirectory() || filter.accept(f)) {
329                                 v.addElement(l[i]);
330                                 break;
331                             }
332                         }
333                     }
334                 }
335             }
336         }
337         Util.sort(v, StringComparator.getDefaultInstance());
338         for (Enumeration JavaDoc e = v.elements(); e.hasMoreElements();) {
339             files.addItem(e.nextElement().toString());
340         }
341         files.deselect(files.getSelectedIndex());
342         doLayout();
343     }
344
345     public void actionPerformed(ActionEvent JavaDoc e) {
346         if (e.getSource() == home) {
347             cwd = new File JavaDoc(System.getProperty("user.home")); //$NON-NLS-1$
348
refresh();
349         }
350         else if (e.getSource() == parent) {
351             gotoParent();
352         }
353         else if (e.getSource() == remove) {
354             File JavaDoc f = getSelectedFile();
355             Option choice = OptionDialog.prompt(this, OptionDialog.WARNING, Messages.getString("AWTFileSelector.confirmRemove"), MessageFormat.format(Messages.getString("AWTFileSelector.confirmRemoveText"), //$NON-NLS-1$ //$NON-NLS-2$
356
new Object JavaDoc[] { f.getPath() } ), OptionDialog.CHOICES_YES_NO);
357             if (choice == OptionDialog.CHOICE_YES)
358                 if (!f.delete()) {
359                     OptionDialog.error(this, Messages.getString("AWTFileSelector.error"), Messages.getString("AWTFileSelector.failedToRemove")); //$NON-NLS-1$ //$NON-NLS-2$
360
} else {
361                     refresh();
362                     path.setText(""); //$NON-NLS-1$
363
}
364         } else if (e.getSource() == newFolder)
365             newFolder();
366         else
367             selectFile();
368     }
369
370     private void newFolder() {
371         String JavaDoc name = OptionDialog.promptForText(this, Messages.getString("AWTFileSelector.newFolder"), "", null, ' ', Messages.getString("AWTFileSelector.name")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
372
if (name != null) {
373             File JavaDoc f = new File JavaDoc(cwd, name);
374             try {
375                 if (!f.mkdir())
376                     throw new IOException JavaDoc(Messages.getString("AWTFileSelector.couldNotCreateDirectory")); //$NON-NLS-1$
377
refresh();
378             } catch (IOException JavaDoc ioe) {
379                 OptionDialog.error(this, Messages.getString("AWTFileSelector.error"), ioe.getMessage()); //$NON-NLS-1$
380
}
381         }
382     }
383
384     public void setAllowMultipleSelection(boolean allowMultipleSelection) {
385         files.setMultipleMode(allowMultipleSelection);
386     }
387
388     public void itemStateChanged(ItemEvent JavaDoc e) {
389         String JavaDoc sel = files.getSelectedItem();
390         if (sel != null && sel.equals("..")) { //$NON-NLS-1$
391
String JavaDoc parent = getWorkingDirectory().getParent();
392             path.setText(parent);
393         }
394         path.setText(sel != null ? sel : ""); //$NON-NLS-1$
395
}
396
397     public Option showDialog(Component JavaDoc parent, String JavaDoc title) {
398         Option choice = OptionDialog.prompt(parent, OptionDialog.UNCATEGORISED, title, this, OptionDialog.CHOICES_OK_CANCEL, null, showHiddenFiles);
399         return choice;
400     }
401
402     public File JavaDoc getWorkingDirectory() {
403         return cwd;
404     }
405
406     /**
407      * @param filter
408      */

409     public void setSelectedFileFilter(FileFilter filter) {
410         int idx = filters.indexOf(filter);
411         if(idx != -1) {
412             filterSelect.select(idx);
413         }
414     }
415
416     /**
417      * @param cwd2
418      */

419     public void setWorkingDirectory(File JavaDoc cwd) {
420         this.cwd = cwd;
421         refresh();
422     }
423
424     public static void main(String JavaDoc[] args) {
425     }
426 }
Popular Tags