PageRenderTime 253ms CodeModel.GetById 233ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 528 lines | 421 code | 51 blank | 56 comment | 86 complexity | e1df42666f7896d3fa8f7b0f923a49a6 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 4957 2004-01-18 19:01:02Z 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		filename = filenameField.getText();
154
155		if(files.length != 0)
156		{
157			browser.filesActivated(VFSBrowser.M_OPEN,false);
158			return;
159		}
160		else if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG
161			&& (filename == null || filename.length() == 0))
162		{
163			isOK = true;
164			dispose();
165			return;
166		}
167		else if(filename == null || filename.length() == 0)
168		{
169			getToolkit().beep();
170			return;
171		}
172
173		String bufferDir = browser.getView().getBuffer()
174			.getDirectory();
175		if(filename.equals("-"))
176			filename = bufferDir;
177		else if(filename.startsWith("-/")
178			|| filename.startsWith("-" + File.separator))
179		{
180			filename = MiscUtilities.constructPath(
181				bufferDir,filename.substring(2));
182		}
183
184		final int[] type = { -1 };
185		final String path = MiscUtilities.constructPath(
186			browser.getDirectory(),filename);
187		final VFS vfs = VFSManager.getVFSForPath(path);
188		Object session = vfs.createVFSSession(path,this);
189		if(session == null)
190			return;
191
192		VFSManager.runInWorkThread(new GetFileTypeRequest(
193			vfs,session,path,type));
194		VFSManager.runInAWTThread(new Runnable()
195		{
196			public void run()
197			{
198				switch(type[0])
199				{
200				case VFS.DirectoryEntry.FILE:
201					if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
202						break;
203
204					if(vfs instanceof FileVFS)
205					{
206						if(doFileExistsWarning(path))
207							break;
208					}
209					isOK = true;
210					if(browser.getMode() == VFSBrowser.BROWSER_DIALOG)
211					{
212						Hashtable props = new Hashtable();
213						props.put(Buffer.ENCODING,browser.currentEncoding);
214						jEdit.openFile(browser.getView(),
215							browser.getDirectory(),
216							path,false,props);
217					}
218					dispose();
219					break;
220				case VFS.DirectoryEntry.DIRECTORY:
221				case VFS.DirectoryEntry.FILESYSTEM:
222					browser.setDirectory(path);
223					break;
224				}
225			}
226		});
227	} //}}}
228
229	//{{{ cancel() method
230	public void cancel()
231	{
232		dispose();
233	} //}}}
234
235	//{{{ getSelectedFiles() method
236	public String[] getSelectedFiles()
237	{
238		if(!isOK)
239			return null;
240
241		//String filename = filenameField.getText();
242
243		if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
244		{
245			return new String[] { browser.getDirectory() };
246		}
247		else if(filename != null && filename.length() != 0)
248		{
249			String path = browser.getDirectory();
250			return new String[] { MiscUtilities.constructPath(
251				path,filename) };
252		}
253		else
254		{
255			Vector vector = new Vector();
256			VFS.DirectoryEntry[] selectedFiles = browser.getSelectedFiles();
257			for(int i = 0; i < selectedFiles.length; i++)
258			{
259				VFS.DirectoryEntry file = selectedFiles[i];
260				if(file.type == VFS.DirectoryEntry.FILE)
261					vector.addElement(file.path);
262			}
263			String[] retVal = new String[vector.size()];
264			vector.copyInto(retVal);
265			return retVal;
266		}
267	} //}}}
268
269	//{{{ Private members
270
271	//{{{ Instance variables
272	private VFSBrowser browser;
273	private VFSFileNameField filenameField;
274	private String filename;
275	private JButton ok;
276	private JButton cancel;
277	private boolean isOK;
278	private WorkThreadHandler workThreadHandler;
279	//}}}
280
281	//{{{ doFileExistsWarning() method
282	private boolean doFileExistsWarning(String filename)
283	{
284		if(browser.getMode() == VFSBrowser.SAVE_DIALOG
285			&& new File(filename).exists())
286		{
287			String[] args = { MiscUtilities.getFileName(filename) };
288			int result = GUIUtilities.confirm(browser,
289				"fileexists",args,
290				JOptionPane.YES_NO_OPTION,
291				JOptionPane.WARNING_MESSAGE);
292			if(result != JOptionPane.YES_OPTION)
293				return true;
294		}
295
296		return false;
297	} //}}}
298
299	//}}}
300
301	//{{{ Inner classes
302
303	//{{{ ActionHandler class
304	class ActionHandler implements ActionListener
305	{
306		public void actionPerformed(ActionEvent evt)
307		{
308			if(evt.getSource() == ok)
309			{
310				if(!browser.getDirectory().equals(
311					browser.getDirectoryField().getText()))
312				{
313					browser.setDirectory(browser.getDirectoryField().getText());
314				}
315				else
316					ok();
317			}
318			else if(evt.getSource() == cancel)
319				cancel();
320		}
321	} //}}}
322
323	//{{{ BrowserHandler class
324	class BrowserHandler implements BrowserListener
325	{
326		//{{{ filesSelected() method
327		public void filesSelected(VFSBrowser browser, VFS.DirectoryEntry[] files)
328		{
329			if(files.length == 0)
330			{
331				if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
332				{
333					ok.setText(jEdit.getProperty(
334						"vfs.browser.dialog.choose-dir"));
335				}
336				return;
337			}
338			else if(files.length == 1)
339			{
340				if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
341				{
342					ok.setText(jEdit.getProperty(
343						"vfs.browser.dialog.open"));
344				}
345
346				VFS.DirectoryEntry file = files[0];
347				if(file.type == VFS.DirectoryEntry.FILE)
348				{
349					String path = file.path;
350					String directory = browser.getDirectory();
351					String parent = MiscUtilities
352						.getParentOfPath(path);
353					if(VFSBrowser.pathsEqual(parent,directory))
354						path = file.name;
355
356					filenameField.setText(path);
357					filenameField.selectAll();
358				}
359			}
360			else
361			{
362				if(browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
363				{
364					ok.setText(jEdit.getProperty(
365						"vfs.browser.dialog.open"));
366				}
367
368				filenameField.setText(null);
369			}
370		} //}}}
371
372		//{{{ filesActivated() method
373		public void filesActivated(VFSBrowser browser, VFS.DirectoryEntry[] files)
374		{
375			filenameField.selectAll();
376
377			if(files.length == 0)
378			{
379				// user pressed enter when the vfs table or
380				// file name field has focus, with nothing
381				// selected.
382				ok();
383				return;
384			}
385
386			for(int i = 0; i < files.length; i++)
387			{
388				if(files[i].type == VFS.DirectoryEntry.FILE)
389				{
390					String path = files[i].path;
391					VFS vfs = VFSManager.getVFSForPath(path);
392					if(browser.getMode() == VFSBrowser.SAVE_DIALOG
393						&& vfs instanceof FileVFS)
394					{
395						if(doFileExistsWarning(path))
396							return;
397					}
398
399					isOK = true;
400					filenameField.setText(null);
401					if(browser.getMode() != VFSBrowser.CHOOSE_DIRECTORY_DIALOG)
402					{
403						dispose();
404					}
405					return;
406				}
407				else
408					return;
409			}
410		} //}}}
411	} //}}}
412
413	//{{{ KeyHandler class
414	class KeyHandler extends KeyAdapter
415	{
416		public void keyTyped(KeyEvent evt)
417		{
418			switch(evt.getKeyChar())
419			{
420			case '/':
421			case '-':
422			case '~':
423				filenameField.processKeyEvent(evt);
424				filenameField.requestFocus();
425				break;
426			}
427		}
428	} //}}}
429
430	//{{{ WorkThreadListener class
431	class WorkThreadHandler implements WorkThreadProgressListener
432	{
433		//{{{ statusUpdate() method
434		public void statusUpdate(final WorkThreadPool threadPool,
435			final int threadIndex)
436		{
437			SwingUtilities.invokeLater(new Runnable()
438			{
439				public void run()
440				{
441					int requestCount = threadPool.getRequestCount();
442					if(requestCount == 0)
443					{
444						getContentPane().setCursor(
445							Cursor.getDefaultCursor());
446					}
447					else if(requestCount >= 1)
448					{
449						getContentPane().setCursor(
450							Cursor.getPredefinedCursor(
451							Cursor.WAIT_CURSOR));
452					}
453				}
454			});
455		} //}}}
456
457		//{{{ progressUpdate() method
458		public void progressUpdate(WorkThreadPool threadPool, int threadIndex)
459		{
460		} //}}}
461	} //}}}
462
463	//{{{ GetFileTypeRequest class
464	class GetFileTypeRequest implements Runnable
465	{
466		VFS    vfs;
467		Object session;
468		String path;
469		int[]  type;
470
471		GetFileTypeRequest(VFS vfs, Object session,
472			String path, int[] type)
473		{
474			this.vfs     = vfs;
475			this.session = session;
476			this.path    = path;
477			this.type    = type;
478		}
479
480		public void run()
481		{
482			try
483			{
484				VFS.DirectoryEntry entry
485					= vfs._getDirectoryEntry(
486						session,
487						path,
488						browser);
489				if(entry == null)
490				{
491					// non-existent file
492					type[0] = VFS.DirectoryEntry.FILE;
493				}
494				else
495					type[0] = entry.type;
496			}
497			catch(IOException e)
498			{
499				Log.log(Log.ERROR,this,e);
500				VFSManager.error(browser,path,
501					"ioerror",
502					new String[]
503					{ e.toString() });
504				return;
505			}
506			finally
507			{
508				try
509				{
510					vfs._endVFSSession(
511						session,
512						browser);
513				}
514				catch(IOException e)
515				{
516					Log.log(Log.ERROR,this,e);
517					VFSManager.error(browser,path,
518						"ioerror",
519						new String[]
520						{ e.toString() });
521					return;
522				}
523			}
524		}
525	} //}}}
526
527	//}}}
528}