PageRenderTime 70ms CodeModel.GetById 30ms app.highlight 35ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/browser/VFSFileChooserDialog.java

#
Java | 520 lines | 413 code | 51 blank | 56 comment | 76 complexity | c15c7b35d16d427ea70f89d24d17d713 MD5 | raw file
  1/*
  2 * VFSFileChooserDialog.java - VFS file chooser
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2000, 2003 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
 23package org.gjt.sp.jedit.browser;
 24
 25//{{{ Imports
 26import javax.swing.border.EmptyBorder;
 27import javax.swing.*;
 28import java.awt.event.*;
 29import java.awt.*;
 30import java.io.File;
 31import java.io.IOException;
 32import java.util.*;
 33import org.gjt.sp.jedit.gui.EnhancedDialog;
 34import org.gjt.sp.jedit.io.*;
 35import org.gjt.sp.jedit.*;
 36import org.gjt.sp.util.*;
 37//}}}
 38
 39/**
 40 * Wraps the VFS browser in a modal dialog.
 41 * @author Slava Pestov
 42 * @version $Id: VFSFileChooserDialog.java 4808 2003-06-22 21:23:33Z spestov $
 43 */
 44public class VFSFileChooserDialog extends EnhancedDialog
 45{
 46	//{{{ VFSFileChooserDialog constructor
 47	public VFSFileChooserDialog(View view, String path,
 48		int mode, boolean multipleSelection)
 49	{
 50		super(view,jEdit.getProperty("vfs.browser.title"),true);
 51
 52		JPanel content = new JPanel(new BorderLayout());
 53		content.setBorder(new EmptyBorder(12,12,12,12));
 54		setContentPane(content);
 55
 56		String name;
 57		if(mode == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
 58			name = null;
 59		else if(path == null || path.endsWith(File.separator)
 60			|| path.endsWith("/"))
 61		{
 62			name = null;
 63		}
 64		else
 65		{
 66			VFS vfs = VFSManager.getVFSForPath(path);
 67			name = vfs.getFileName(path);
 68			path = vfs.getParentOfPath(path);
 69		}
 70
 71		browser = new VFSBrowser(view,path,mode,multipleSelection,null);
 72		browser.getBrowserView().getTable().setRequestFocusEnabled(false);
 73		browser.getBrowserView().getParentDirectoryList()
 74			.setRequestFocusEnabled(false);
 75		/* browser.getBrowserView().getTable().addKeyListener(new KeyHandler()); */
 76		browser.addBrowserListener(new BrowserHandler());
 77		content.add(BorderLayout.CENTER,browser);
 78
 79		JPanel panel = new JPanel();
 80		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
 81		panel.setBorder(new EmptyBorder(12,0,0,0));
 82
 83		filenameField = new VFSFileNameField(browser,null);
 84		filenameField.setText(name);
 85		filenameField.selectAll();
 86		Box box = new Box(BoxLayout.Y_AXIS);
 87		box.add(Box.createGlue());
 88		box.add(filenameField);
 89		box.add(Box.createGlue());
 90
 91		JLabel label = new JLabel(jEdit.getProperty("vfs.browser.dialog.filename"));
 92		label.setDisplayedMnemonic(jEdit.getProperty(
 93			"vfs.browser.dialog.filename.mnemonic").charAt(0));
 94		label.setLabelFor(filenameField);
 95		panel.add(label);
 96		panel.add(Box.createHorizontalStrut(12));
 97
 98		panel.add(box);
 99
100		panel.add(Box.createHorizontalStrut(12));
101
102		GUIUtilities.requestFocus(this,filenameField);
103
104		ok = new JButton();
105		getRootPane().setDefaultButton(ok);
106
107		switch(mode)
108		{
109		case VFSBrowser.OPEN_DIALOG:
110		case VFSBrowser.BROWSER_DIALOG:
111			ok.setText(jEdit.getProperty("vfs.browser.dialog.open"));
112			break;
113		case VFSBrowser.CHOOSE_DIRECTORY_DIALOG:
114			ok.setText(jEdit.getProperty("vfs.browser.dialog.choose-dir"));
115			// so that it doesn't resize...
116			Dimension dim = ok.getPreferredSize();
117			ok.setPreferredSize(dim);
118			break;
119		case VFSBrowser.SAVE_DIALOG:
120			ok.setText(jEdit.getProperty("vfs.browser.dialog.save"));
121			break;
122		}
123
124		ok.addActionListener(new ActionHandler());
125		panel.add(ok);
126		panel.add(Box.createHorizontalStrut(6));
127		cancel = new JButton(jEdit.getProperty("common.cancel"));
128		cancel.addActionListener(new ActionHandler());
129		panel.add(cancel);
130
131		content.add(BorderLayout.SOUTH,panel);
132
133		VFSManager.getIOThreadPool().addProgressListener(
134			workThreadHandler = new WorkThreadHandler());
135
136		pack();
137		GUIUtilities.loadGeometry(this,"vfs.browser.dialog");
138		show();
139	} //}}}
140
141	//{{{ dispose() method
142	public void dispose()
143	{
144		GUIUtilities.saveGeometry(this,"vfs.browser.dialog");
145		VFSManager.getIOThreadPool().removeProgressListener(workThreadHandler);
146		super.dispose();
147	} //}}}
148
149	//{{{ ok() method
150	public void ok()
151	{
152		VFS.DirectoryEntry[] files = browser.getSelectedFiles();
153
154		if(files.length != 0)
155		{
156			browser.filesActivated(VFSBrowser.M_OPEN,false);
157		}
158		else
159		{
160			if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
161			{
162				isOK = true;
163				dispose();
164				return;
165			}
166
167			String filename = filenameField.getText();
168			if(filename == null || filename.length() == 0)
169			{
170				getToolkit().beep();
171				return;
172			}
173
174			String bufferDir = browser.getView().getBuffer()
175				.getDirectory();
176			if(filename.equals("-"))
177				filename = bufferDir;
178			else if(filename.startsWith("-/")
179				|| filename.startsWith("-" + File.separator))
180			{
181				filename = MiscUtilities.constructPath(
182					bufferDir,filename.substring(2));
183			}
184
185			final int[] type = { -1 };
186			final String path = MiscUtilities.constructPath(
187				browser.getDirectory(),filename);
188			final VFS vfs = VFSManager.getVFSForPath(path);
189			Object session = vfs.createVFSSession(path,this);
190			if(session == null)
191				return;
192
193			VFSManager.runInWorkThread(new GetFileTypeRequest(
194				vfs,session,path,type));
195			VFSManager.runInAWTThread(new Runnable()
196			{
197				public void run()
198				{
199					switch(type[0])
200					{
201					case VFS.DirectoryEntry.FILE:
202						if(vfs instanceof FileVFS)
203						{
204							if(doFileExistsWarning(path))
205								break;
206						}
207						isOK = true;
208						if(browser.getMode() == VFSBrowser.BROWSER_DIALOG)
209						{
210							Hashtable props = new Hashtable();
211							props.put(Buffer.ENCODING,browser.currentEncoding);
212							jEdit.openFile(browser.getView(),
213								browser.getDirectory(),
214								path,false,props);
215						}
216						dispose();
217						break;
218					case VFS.DirectoryEntry.DIRECTORY:
219					case VFS.DirectoryEntry.FILESYSTEM:
220						browser.setDirectory(path);
221						break;
222					}
223				}
224			});
225		}
226	} //}}}
227
228	//{{{ cancel() method
229	public void cancel()
230	{
231		dispose();
232	} //}}}
233
234	//{{{ getSelectedFiles() method
235	public String[] getSelectedFiles()
236	{
237		if(!isOK)
238			return null;
239
240		String filename = filenameField.getText();
241
242		if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
243		{
244			return new String[] { browser.getDirectory() };
245		}
246		else if(filename != null && filename.length() != 0)
247		{
248			String path = browser.getDirectory();
249			return new String[] { MiscUtilities.constructPath(
250				path,filename) };
251		}
252		else
253		{
254			Vector vector = new Vector();
255			VFS.DirectoryEntry[] selectedFiles = browser.getSelectedFiles();
256			for(int i = 0; i < selectedFiles.length; i++)
257			{
258				VFS.DirectoryEntry file = selectedFiles[i];
259				if(file.type == VFS.DirectoryEntry.FILE)
260					vector.addElement(file.path);
261			}
262			String[] retVal = new String[vector.size()];
263			vector.copyInto(retVal);
264			return retVal;
265		}
266	} //}}}
267
268	//{{{ Private members
269
270	//{{{ Instance variables
271	private VFSBrowser browser;
272	private VFSFileNameField filenameField;
273	private JButton ok;
274	private JButton cancel;
275	private boolean isOK;
276	private WorkThreadHandler workThreadHandler;
277	//}}}
278
279	//{{{ doFileExistsWarning() method
280	private boolean doFileExistsWarning(String filename)
281	{
282		if(browser.getMode() == VFSBrowser.SAVE_DIALOG
283			&& new File(filename).exists())
284		{
285			String[] args = { MiscUtilities.getFileName(filename) };
286			int result = GUIUtilities.confirm(browser,
287				"fileexists",args,
288				JOptionPane.YES_NO_OPTION,
289				JOptionPane.WARNING_MESSAGE);
290			if(result != JOptionPane.YES_OPTION)
291				return true;
292		}
293
294		return false;
295	} //}}}
296
297	//}}}
298
299	//{{{ Inner classes
300
301	//{{{ ActionHandler class
302	class ActionHandler implements ActionListener
303	{
304		public void actionPerformed(ActionEvent evt)
305		{
306			if(evt.getSource() == ok)
307			{
308				if(!browser.getDirectory().equals(
309					browser.getDirectoryField().getText()))
310				{
311					browser.setDirectory(browser.getDirectoryField().getText());
312				}
313				else
314					ok();
315			}
316			else if(evt.getSource() == cancel)
317				cancel();
318		}
319	} //}}}
320
321	//{{{ BrowserHandler class
322	class BrowserHandler implements BrowserListener
323	{
324		//{{{ filesSelected() method
325		public void filesSelected(VFSBrowser browser, VFS.DirectoryEntry[] files)
326		{
327			if(files.length == 0)
328			{
329				if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
330				{
331					ok.setText(jEdit.getProperty(
332						"vfs.browser.dialog.choose-dir"));
333				}
334				return;
335			}
336			else if(files.length == 1)
337			{
338				if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
339				{
340					ok.setText(jEdit.getProperty(
341						"vfs.browser.dialog.open"));
342				}
343
344				VFS.DirectoryEntry file = files[0];
345				if(file.type == VFS.DirectoryEntry.FILE)
346				{
347					String path = file.path;
348					String directory = browser.getDirectory();
349					String parent = MiscUtilities
350						.getParentOfPath(path);
351					if(VFSBrowser.pathsEqual(parent,directory))
352						path = file.name;
353
354					filenameField.setText(path);
355					filenameField.selectAll();
356				}
357			}
358			else
359			{
360				if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
361				{
362					ok.setText(jEdit.getProperty(
363						"vfs.browser.dialog.open"));
364				}
365
366				filenameField.setText(null);
367			}
368		} //}}}
369
370		//{{{ filesActivated() method
371		public void filesActivated(VFSBrowser browser, VFS.DirectoryEntry[] files)
372		{
373			filenameField.selectAll();
374
375			if(files.length == 0)
376			{
377				// user pressed enter when the vfs table or
378				// file name field has focus, with nothing
379				// selected.
380				ok();
381				return;
382			}
383
384			for(int i = 0; i < files.length; i++)
385			{
386				if(files[i].type == VFS.DirectoryEntry.FILE)
387				{
388					String path = files[i].path;
389					VFS vfs = VFSManager.getVFSForPath(path);
390					if(browser.getMode() == VFSBrowser.SAVE_DIALOG
391						&& vfs instanceof FileVFS)
392					{
393						if(doFileExistsWarning(path))
394							return;
395					}
396
397					isOK = true;
398					filenameField.setText(null);
399					dispose();
400					return;
401				}
402				else
403					return;
404			}
405		} //}}}
406	} //}}}
407
408	//{{{ KeyHandler class
409	class KeyHandler extends KeyAdapter
410	{
411		public void keyTyped(KeyEvent evt)
412		{
413			switch(evt.getKeyChar())
414			{
415			case '/':
416			case '-':
417			case '~':
418				filenameField.processKeyEvent(evt);
419				filenameField.requestFocus();
420				break;
421			}
422		}
423	} //}}}
424
425	//{{{ WorkThreadListener class
426	class WorkThreadHandler implements WorkThreadProgressListener
427	{
428		//{{{ statusUpdate() method
429		public void statusUpdate(WorkThreadPool threadPool, int threadIndex)
430		{
431			// synchronize with hide/showWaitCursor()
432			synchronized(VFSFileChooserDialog.this)
433			{
434				int requestCount = threadPool.getRequestCount();
435				if(requestCount == 0)
436				{
437					getContentPane().setCursor(
438						Cursor.getDefaultCursor());
439				}
440				else if(requestCount >= 1)
441				{
442					getContentPane().setCursor(
443						Cursor.getPredefinedCursor(
444						Cursor.WAIT_CURSOR));
445				}
446			}
447		} //}}}
448
449		//{{{ progressUpdate() method
450		public void progressUpdate(WorkThreadPool threadPool, int threadIndex)
451		{
452		} //}}}
453	} //}}}
454
455	//{{{ GetFileTypeRequest class
456	class GetFileTypeRequest implements Runnable
457	{
458		VFS    vfs;
459		Object session;
460		String path;
461		int[]  type;
462
463		GetFileTypeRequest(VFS vfs, Object session,
464			String path, int[] type)
465		{
466			this.vfs     = vfs;
467			this.session = session;
468			this.path    = path;
469			this.type    = type;
470		}
471
472		public void run()
473		{
474			try
475			{
476				VFS.DirectoryEntry entry
477					= vfs._getDirectoryEntry(
478						session,
479						path,
480						browser);
481				if(entry == null)
482				{
483					// non-existent file
484					type[0] = VFS.DirectoryEntry.FILE;
485				}
486				else
487					type[0] = entry.type;
488			}
489			catch(IOException e)
490			{
491				Log.log(Log.ERROR,this,e);
492				VFSManager.error(browser,path,
493					"ioerror",
494					new String[]
495					{ e.toString() });
496				return;
497			}
498			finally
499			{
500				try
501				{
502					vfs._endVFSSession(
503						session,
504						browser);
505				}
506				catch(IOException e)
507				{
508					Log.log(Log.ERROR,this,e);
509					VFSManager.error(browser,path,
510						"ioerror",
511						new String[]
512						{ e.toString() });
513					return;
514				}
515			}
516		}
517	} //}}}
518
519	//}}}
520}