KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > browser > VFSFileChooserDialog


1 /*
2  * VFSFileChooserDialog.java - VFS file chooser
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2000, 2005 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.browser;
24
25 //{{{ Imports
26
import javax.swing.border.EmptyBorder JavaDoc;
27 import javax.swing.*;
28 import java.awt.event.*;
29 import java.awt.BorderLayout JavaDoc;
30 import java.awt.Cursor JavaDoc;
31 import java.awt.Dimension JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.*;
35 import org.gjt.sp.jedit.gui.EnhancedDialog;
36 import org.gjt.sp.jedit.io.*;
37 import org.gjt.sp.jedit.*;
38 import org.gjt.sp.util.*;
39 //}}}
40

41 /**
42  * Wraps the VFS browser in a modal dialog.
43  * @author Slava Pestov
44  * @version $Id: VFSFileChooserDialog.java 7183 2006-10-07 04:46:25Z ezust $
45  */

46 public class VFSFileChooserDialog extends EnhancedDialog
47 {
48
49     //{{{ VFSFileChooserDialog constructor
50
public VFSFileChooserDialog(View view, String JavaDoc path,
51         int mode, boolean multipleSelection)
52     {
53         this(view,path,mode,multipleSelection,true);
54     } //}}}
55

56     //{{{ VFSFileChooserDialog constructor
57
/**
58      * Constructs a new VFSFileChooserDialog. If <code>authoshow</code>
59      * is true, the dialog will be show automatically and the call
60      * will only return after the user disposes of the dialog.
61      *
62      * @since jEdit 4.3pre7
63      */

64     public VFSFileChooserDialog(View view, String JavaDoc path,
65         int mode, boolean multipleSelection, boolean autoshow)
66     {
67         super(view,jEdit.getProperty("vfs.browser.title"),true);
68
69         JPanel content = new JPanel(new BorderLayout JavaDoc());
70         content.setBorder(new EmptyBorder JavaDoc(12,12,12,12));
71         setContentPane(content);
72
73         String JavaDoc name;
74         if(mode == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
75             name = null;
76         else if(path == null || path.endsWith(File.separator)
77             || path.endsWith("/"))
78         {
79             name = null;
80         }
81         else
82         {
83             VFS vfs = VFSManager.getVFSForPath(path);
84             name = vfs.getFileName(path);
85             path = vfs.getParentOfPath(path);
86         }
87
88         browser = new VFSBrowser(view,path,mode,multipleSelection,null);
89         browser.getBrowserView().getTable().setRequestFocusEnabled(false);
90         browser.getBrowserView().getParentDirectoryList()
91             .setRequestFocusEnabled(false);
92         /* browser.getBrowserView().getTable().addKeyListener(new KeyHandler()); */
93         browser.addBrowserListener(new BrowserHandler());
94         content.add(BorderLayout.CENTER,browser);
95
96         JPanel panel = new JPanel();
97         panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
98         panel.setBorder(new EmptyBorder JavaDoc(12,0,0,0));
99
100         filenameField = new VFSFileNameField(browser,null);
101         filenameField.setText(name);
102         filenameField.selectAll();
103         Box box = new Box(BoxLayout.Y_AXIS);
104         box.add(Box.createGlue());
105         box.add(filenameField);
106         box.add(Box.createGlue());
107
108         JLabel label = new JLabel(jEdit.getProperty("vfs.browser.dialog.filename"));
109         label.setDisplayedMnemonic(jEdit.getProperty(
110             "vfs.browser.dialog.filename.mnemonic").charAt(0));
111         label.setLabelFor(filenameField);
112         panel.add(label);
113         panel.add(Box.createHorizontalStrut(12));
114
115         panel.add(box);
116
117         panel.add(Box.createHorizontalStrut(12));
118
119         ok = new JButton();
120         getRootPane().setDefaultButton(ok);
121
122         switch(mode)
123         {
124         case VFSBrowser.OPEN_DIALOG:
125         case VFSBrowser.BROWSER_DIALOG:
126             ok.setText(jEdit.getProperty("vfs.browser.dialog.open"));
127             break;
128         case VFSBrowser.CHOOSE_DIRECTORY_DIALOG:
129             ok.setText(jEdit.getProperty("vfs.browser.dialog.choose-dir"));
130             // so that it doesn't resize...
131
Dimension JavaDoc dim = ok.getPreferredSize();
132             ok.setPreferredSize(dim);
133             break;
134         case VFSBrowser.SAVE_DIALOG:
135             ok.setText(jEdit.getProperty("vfs.browser.dialog.save"));
136             break;
137         }
138
139         ok.addActionListener(new ActionHandler());
140         panel.add(ok);
141         panel.add(Box.createHorizontalStrut(6));
142         cancel = new JButton(jEdit.getProperty("common.cancel"));
143         cancel.addActionListener(new ActionHandler());
144         panel.add(cancel);
145
146         content.add(BorderLayout.SOUTH,panel);
147
148         VFSManager.getIOThreadPool().addProgressListener(
149             workThreadHandler = new WorkThreadHandler());
150
151         pack();
152         GUIUtilities.loadGeometry(this,"vfs.browser.dialog");
153         GUIUtilities.requestFocus(this,filenameField);
154         if (autoshow)
155             setVisible(true);
156     } //}}}
157

158     //{{{ getBrowser() method
159
/**
160      * Returns the VFSBrowser instance used internally.
161      *
162      * @since jEdit 4.3pre7
163      */

164     public VFSBrowser getBrowser()
165     {
166         return browser;
167     } //}}}
168

169     //{{{ dispose() method
170
public void dispose()
171     {
172         GUIUtilities.saveGeometry(this,"vfs.browser.dialog");
173         VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
174         super.dispose();
175     } //}}}
176

177     //{{{ ok() method
178
public void ok()
179     {
180         VFSFile[] files = browser.getSelectedFiles();
181         filename = filenameField.getText();
182         boolean choosingDir = (browser.getMode() ==
183             VFSBrowser.CHOOSE_DIRECTORY_DIALOG);
184
185         if(files.length != 0)
186         {
187             if(files.length > 1 && choosingDir)
188             {
189                 isOK = true;
190                 dispose();
191             }
192             else
193                 browser.filesActivated(VFSBrowser.M_OPEN,false);
194             return;
195         }
196         else if(choosingDir && (filename == null || filename.length() == 0))
197         {
198             isOK = true;
199             dispose();
200             return;
201         }
202         else if(filename == null || filename.length() == 0)
203         {
204             getToolkit().beep();
205             return;
206         }
207
208         String JavaDoc bufferDir = browser.getView().getBuffer()
209             .getDirectory();
210         if(filename.equals("-"))
211             filename = bufferDir;
212         else if(filename.startsWith("-/")
213             || filename.startsWith("-" + File.separator))
214         {
215             filename = MiscUtilities.constructPath(
216                 bufferDir,filename.substring(2));
217         }
218
219         final int[] type = { -1 };
220         filename = MiscUtilities.expandVariables(filename);
221         final String JavaDoc path = MiscUtilities.constructPath(
222             browser.getDirectory(),filename);
223         final VFS vfs = VFSManager.getVFSForPath(path);
224         Object JavaDoc session = vfs.createVFSSession(path,this);
225         if(session == null)
226             return;
227
228         VFSManager.runInWorkThread(new GetFileTypeRequest(
229             vfs,session,path,type));
230         VFSManager.runInAWTThread(new Runnable JavaDoc()
231         {
232             public void run()
233             {
234                 switch(type[0])
235                 {
236                 case VFSFile.FILE:
237                     if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
238                         break;
239
240                     if(vfs instanceof FileVFS)
241                     {
242                         if(doFileExistsWarning(path))
243                             break;
244                     }
245                     isOK = true;
246                     if(browser.getMode() == VFSBrowser.BROWSER_DIALOG)
247                     {
248                         Hashtable props = new Hashtable();
249                         props.put(Buffer.ENCODING,browser.currentEncoding);
250                         jEdit.openFile(browser.getView(),
251                             browser.getDirectory(),
252                             path,false,props);
253                     }
254                     dispose();
255                     break;
256                 case VFSFile.DIRECTORY:
257                 case VFSFile.FILESYSTEM:
258                     browser.setDirectory(path);
259                     break;
260                 }
261             }
262         });
263     } //}}}
264

265     //{{{ cancel() method
266
public void cancel()
267     {
268         dispose();
269     } //}}}
270

271     //{{{ getSelectedFiles() method
272
public String JavaDoc[] getSelectedFiles()
273     {
274         if(!isOK)
275             return null;
276
277         if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
278         {
279             if(browser.getSelectedFiles().length > 0)
280             {
281                 return getSelectedFiles(VFSFile.DIRECTORY,
282                     VFSFile.FILESYSTEM);
283             }
284             else
285                 return new String JavaDoc[] { browser.getDirectory() };
286         }
287         else if(filename != null && filename.length() != 0)
288         {
289             String JavaDoc path = browser.getDirectory();
290             return new String JavaDoc[] { MiscUtilities.constructPath(
291                 path,filename) };
292         }
293         else
294             return getSelectedFiles(VFSFile.FILE,VFSFile.FILE);
295     } //}}}
296

297     //{{{ Private members
298

299     //{{{ Instance variables
300
private VFSBrowser browser;
301     private VFSFileNameField filenameField;
302     private String JavaDoc filename;
303     private JButton ok;
304     private JButton cancel;
305     private boolean isOK;
306     private WorkThreadHandler workThreadHandler;
307     //}}}
308

309     //{{{ doFileExistsWarning() method
310
private boolean doFileExistsWarning(String JavaDoc filename)
311     {
312         if(browser.getMode() == VFSBrowser.SAVE_DIALOG
313             && new File JavaDoc(filename).exists())
314         {
315             String JavaDoc[] args = { MiscUtilities.getFileName(filename) };
316             int result = GUIUtilities.confirm(browser,
317                 "fileexists",args,
318                 JOptionPane.YES_NO_OPTION,
319                 JOptionPane.WARNING_MESSAGE);
320             if(result != JOptionPane.YES_OPTION)
321                 return true;
322         }
323
324         return false;
325     } //}}}
326

327     //{{{ getSelectedFiles() method
328
private String JavaDoc[] getSelectedFiles(int type1, int type2)
329     {
330         List l = new ArrayList();
331         VFSFile[] selectedFiles = browser.getSelectedFiles();
332         for(int i = 0; i < selectedFiles.length; i++)
333         {
334             VFSFile file = selectedFiles[i];
335             if(file.getType() == type1 || file.getType() == type2)
336                 l.add(file.getPath());
337         }
338         return (String JavaDoc[])l.toArray(new String JavaDoc[l.size()]);
339     } //}}}
340

341     //}}}
342

343     //{{{ Inner classes
344

345     //{{{ ActionHandler class
346
class ActionHandler implements ActionListener
347     {
348         public void actionPerformed(ActionEvent evt)
349         {
350             if(evt.getSource() == ok)
351             {
352                 if(!browser.getDirectory().equals(
353                     browser.getDirectoryField().getText()))
354                 {
355                     browser.setDirectory(browser.getDirectoryField().getText());
356                 }
357                 else
358                     ok();
359             }
360             else if(evt.getSource() == cancel)
361                 cancel();
362         }
363     } //}}}
364

365     //{{{ BrowserHandler class
366
class BrowserHandler implements BrowserListener
367     {
368         //{{{ filesSelected() method
369
public void filesSelected(VFSBrowser browser, VFSFile[] files)
370         {
371             boolean choosingDir = (browser.getMode()
372                 == VFSBrowser.CHOOSE_DIRECTORY_DIALOG);
373
374             if(files.length == 0)
375             {
376                 if(choosingDir)
377                 {
378                     ok.setText(jEdit.getProperty(
379                         "vfs.browser.dialog.choose-dir"));
380                 }
381                 return;
382             }
383             else if(files.length == 1)
384             {
385                 if(choosingDir)
386                 {
387                     ok.setText(jEdit.getProperty(
388                         "vfs.browser.dialog.open"));
389                 }
390
391                 VFSFile file = files[0];
392                 if(file.getType() == VFSFile.FILE)
393                 {
394                     String JavaDoc path = file.getPath();
395                     String JavaDoc directory = browser.getDirectory();
396                     String JavaDoc parent = MiscUtilities
397                         .getParentOfPath(path);
398                     if(MiscUtilities.pathsEqual(parent,directory))
399                         path = file.getName();
400
401                     filenameField.setText(path);
402                     filenameField.selectAll();
403                 }
404             }
405             else
406             {
407                 if(choosingDir)
408                 {
409                     ok.setText(jEdit.getProperty(
410                         "vfs.browser.dialog.choose-dir"));
411                 }
412
413                 filenameField.setText(null);
414             }
415         } //}}}
416

417         //{{{ filesActivated() method
418
public void filesActivated(VFSBrowser browser, VFSFile[] files)
419         {
420             filenameField.selectAll();
421
422             if(files.length == 0)
423             {
424                 // user pressed enter when the vfs table or
425
// file name field has focus, with nothing
426
// selected.
427
ok();
428                 return;
429             }
430
431             for(int i = 0; i < files.length; i++)
432             {
433                 if(files[i].getType() == VFSFile.FILE)
434                 {
435                     String JavaDoc path = files[i].getPath();
436                     VFS vfs = VFSManager.getVFSForPath(path);
437                     if(browser.getMode() == VFSBrowser.SAVE_DIALOG
438                         && vfs instanceof FileVFS)
439                     {
440                         if(doFileExistsWarning(path))
441                             return;
442                     }
443
444                     isOK = true;
445                     filenameField.setText(null);
446                     if(browser.getMode() != VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
447                     {
448                         dispose();
449                     }
450                     return;
451                 }
452                 else
453                     return;
454             }
455         } //}}}
456
} //}}}
457

458     //{{{ KeyHandler class
459
class KeyHandler extends KeyAdapter
460     {
461         public void keyTyped(KeyEvent evt)
462         {
463             switch(evt.getKeyChar())
464             {
465             case '/':
466             case '-':
467             case '~':
468                 filenameField.processKeyEvent(evt);
469                 filenameField.requestFocus();
470                 break;
471             }
472         }
473     } //}}}
474

475     //{{{ WorkThreadListener class
476
class WorkThreadHandler implements WorkThreadProgressListener
477     {
478         //{{{ statusUpdate() method
479
public void statusUpdate(final WorkThreadPool threadPool,
480             final int threadIndex)
481         {
482             SwingUtilities.invokeLater(new Runnable JavaDoc()
483             {
484                 public void run()
485                 {
486                     int requestCount = threadPool.getRequestCount();
487                     if(requestCount == 0)
488                     {
489                         getContentPane().setCursor(
490                             Cursor.getDefaultCursor());
491                     }
492                     else if(requestCount >= 1)
493                     {
494                         getContentPane().setCursor(
495                             Cursor.getPredefinedCursor(
496                             Cursor.WAIT_CURSOR));
497                     }
498                 }
499             });
500         } //}}}
501

502         //{{{ progressUpdate() method
503
public void progressUpdate(WorkThreadPool threadPool, int threadIndex)
504         {
505         } //}}}
506
} //}}}
507

508     //{{{ GetFileTypeRequest class
509
class GetFileTypeRequest implements Runnable JavaDoc
510     {
511         VFS vfs;
512         Object JavaDoc session;
513         String JavaDoc path;
514         int[] type;
515
516         GetFileTypeRequest(VFS vfs, Object JavaDoc session,
517             String JavaDoc path, int[] type)
518         {
519             this.vfs = vfs;
520             this.session = session;
521             this.path = path;
522             this.type = type;
523         }
524
525         public void run()
526         {
527             try
528             {
529                 VFSFile entry = vfs._getFile(
530                         session,
531                         path,
532                         browser);
533                 if(entry == null)
534                 {
535                     // non-existent file
536
type[0] = VFSFile.FILE;
537                 }
538                 else
539                     type[0] = entry.getType();
540             }
541             catch(IOException JavaDoc e)
542             {
543                 VFSManager.error(e,path,browser);
544                 return;
545             }
546             finally
547             {
548                 try
549                 {
550                     vfs._endVFSSession(
551                         session,
552                         browser);
553                 }
554                 catch(IOException JavaDoc e)
555                 {
556                     VFSManager.error(e,path,browser);
557                     return;
558                 }
559             }
560         }
561     } //}}}
562

563     //}}}
564
}
565
Popular Tags