PageRenderTime 197ms CodeModel.GetById 158ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/browser/VFSFileChooserDialog.java

#
Java | 368 lines | 270 code | 45 blank | 53 comment | 60 complexity | c353b416b947913acdc1d30ccf2f1aad 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 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.util.Vector;
 32import org.gjt.sp.jedit.gui.EnhancedDialog;
 33import org.gjt.sp.jedit.io.*;
 34import org.gjt.sp.jedit.*;
 35import org.gjt.sp.util.*;
 36//}}}
 37
 38/**
 39 * Wraps the VFS browser in a modal dialog.
 40 * @author Slava Pestov
 41 * @version $Id: VFSFileChooserDialog.java 3925 2001-11-30 11:40:16Z spestov $
 42 */
 43public class VFSFileChooserDialog extends EnhancedDialog
 44implements WorkThreadProgressListener
 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(path == null || path.endsWith(File.separator) || path.endsWith("/"))
 58			name = null;
 59		else
 60		{
 61			VFS vfs = VFSManager.getVFSForPath(path);
 62			name = vfs.getFileName(path);
 63			path = vfs.getParentOfPath(path);
 64		}
 65
 66		browser = new VFSBrowser(view,path,mode,multipleSelection,true);
 67		browser.addBrowserListener(new BrowserHandler());
 68		content.add(BorderLayout.CENTER,browser);
 69
 70		JPanel bottomPanel = new JPanel(new BorderLayout());
 71
 72		JPanel panel = new JPanel();
 73		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
 74		panel.setBorder(new EmptyBorder(12,0,0,0));
 75
 76		JLabel label = new JLabel(jEdit.getProperty("vfs.browser.dialog.filename"));
 77		panel.add(label);
 78		panel.add(Box.createHorizontalStrut(12));
 79
 80		filenameField = new JTextField();
 81		filenameField.setText(name);
 82		filenameField.addKeyListener(new KeyHandler());
 83		filenameField.selectAll();
 84		label.setDisplayedMnemonic(jEdit.getProperty(
 85			"vfs.browser.dialog.filename.mnemonic").charAt(0));
 86		label.setLabelFor(filenameField);
 87		Dimension dim = filenameField.getPreferredSize();
 88		dim.width = Integer.MAX_VALUE;
 89		filenameField.setMaximumSize(dim);
 90		Box box = new Box(BoxLayout.Y_AXIS);
 91		box.add(Box.createGlue());
 92		box.add(filenameField);
 93		box.add(Box.createGlue());
 94		panel.add(box);
 95
 96		panel.add(Box.createHorizontalStrut(12));
 97
 98		if(mode == VFSBrowser.BROWSER || mode == VFSBrowser.OPEN_DIALOG)
 99		{
100			GUIUtilities.requestFocus(this,browser.getBrowserView()
101				.getDefaultFocusComponent());
102		}
103		else
104		{
105			GUIUtilities.requestFocus(this,filenameField);
106		}
107
108		ok = new JButton(jEdit.getProperty("vfs.browser.dialog."
109			+ (mode == VFSBrowser.OPEN_DIALOG ? "open" : "save")));
110		ok.addActionListener(new ActionHandler());
111		getRootPane().setDefaultButton(ok);
112		panel.add(ok);
113		panel.add(Box.createHorizontalStrut(6));
114		cancel = new JButton(jEdit.getProperty("common.cancel"));
115		cancel.addActionListener(new ActionHandler());
116		panel.add(cancel);
117
118		bottomPanel.add(BorderLayout.NORTH,panel);
119
120		status = new JLabel(" ");
121		status.setBorder(new EmptyBorder(12,0,0,0));
122		bottomPanel.add(BorderLayout.SOUTH,status);
123		content.add(BorderLayout.SOUTH,bottomPanel);
124
125		VFSManager.getIOThreadPool().addProgressListener(this);
126
127		pack();
128		GUIUtilities.loadGeometry(this,"vfs.browser.dialog");
129		show();
130	} //}}}
131
132	//{{{ dispose() method
133	public void dispose()
134	{
135		GUIUtilities.saveGeometry(this,"vfs.browser.dialog");
136		VFSManager.getIOThreadPool().removeProgressListener(this);
137		super.dispose();
138	} //}}}
139
140	//{{{ ok() method
141	public void ok()
142	{
143		VFS.DirectoryEntry[] files = browser.getSelectedFiles();
144
145		String directory = browser.getDirectory();
146
147		if(files.length == 0)
148		{
149			filename = filenameField.getText();
150
151			if(filename.length() == 0)
152			{
153				getToolkit().beep();
154				return;
155			}
156		}
157		else
158		{
159			for(int i = 0; i < files.length; i++)
160			{
161				VFS.DirectoryEntry file = files[i];
162				if(file.type == VFS.DirectoryEntry.FILESYSTEM
163					|| file.type == VFS.DirectoryEntry.DIRECTORY)
164				{
165					browser.setDirectory(file.path);
166					return;
167				}
168				else if(browser.getMode() == VFSBrowser.SAVE_DIALOG)
169					filename = file.path;
170			}
171		}
172
173		if(browser.getMode() == VFSBrowser.SAVE_DIALOG)
174		{
175			if(!MiscUtilities.isURL(directory)
176				&& !MiscUtilities.isURL(filename))
177			{
178				filename = MiscUtilities.constructPath(directory,
179					MiscUtilities.canonPath(filename));
180
181				if(doFileExistsWarning(filename))
182					return;
183			}
184		}
185
186		isOK = true;
187		dispose();
188	} //}}}
189
190	//{{{ cancel() method
191	public void cancel()
192	{
193		dispose();
194	} //}}}
195
196	//{{{ getSelectedFiles() method
197	public String[] getSelectedFiles()
198	{
199		if(!isOK)
200			return null;
201
202		if(filename != null)
203		{
204			String path = browser.getDirectory();
205			return new String[] { MiscUtilities.constructPath(
206				path,filename) };
207		}
208		else
209		{
210			Vector vector = new Vector();
211			VFS.DirectoryEntry[] selectedFiles = browser.getSelectedFiles();
212			for(int i = 0; i < selectedFiles.length; i++)
213			{
214				VFS.DirectoryEntry file =  selectedFiles[i];
215				if(file.type == VFS.DirectoryEntry.FILE)
216					vector.addElement(file.path);
217			}
218			String[] retVal = new String[vector.size()];
219			vector.copyInto(retVal);
220			return retVal;
221		}
222	} //}}}
223
224	//{{{ WorkThreadListener implementation
225
226	//{{{ statusUpdate() method
227	public void statusUpdate(final WorkThreadPool threadPool, int threadIndex)
228	{
229		SwingUtilities.invokeLater(new Runnable()
230		{
231			public void run()
232			{
233				int requestCount = threadPool.getRequestCount();
234				if(requestCount == 0)
235				{
236					status.setText(jEdit.getProperty(
237						"view.status.io.done"));
238				}
239				else if(requestCount == 1)
240				{
241					status.setText(jEdit.getProperty(
242						"view.status.io-1"));
243				}
244				else
245				{
246					Object[] args = { new Integer(requestCount) };
247					status.setText(jEdit.getProperty(
248						"view.status.io",args));
249				}
250			}
251		});
252	} //}}}
253
254	//{{{ progressUpdate() method
255	public void progressUpdate(WorkThreadPool threadPool, int threadIndex)
256	{
257	} //}}}
258
259	//}}}
260
261	//{{{ Private members
262
263	//{{{ Instance variables
264	private VFSBrowser browser;
265	private JTextField filenameField;
266	private JLabel status;
267	private String filename;
268	private JButton ok;
269	private JButton cancel;
270	private boolean isOK;
271	//}}}
272
273	//{{{ doFileExistsWarning() method
274	private boolean doFileExistsWarning(String filename)
275	{
276		if(new File(filename).exists())
277		{
278			String[] args = { MiscUtilities.getFileName(filename) };
279			int result = GUIUtilities.confirm(browser,
280				"fileexists",args,
281				JOptionPane.YES_NO_OPTION,
282				JOptionPane.WARNING_MESSAGE);
283			if(result != JOptionPane.YES_OPTION)
284				return true;
285		}
286
287		return false;
288	} //}}}
289
290	//}}}
291
292	//{{{ Inner classes
293
294	//{{{ ActionHandler class
295	class ActionHandler implements ActionListener
296	{
297		//{{{ actionPerformed() method
298		public void actionPerformed(ActionEvent evt)
299		{
300			if(evt.getSource() == ok)
301				ok();
302			else if(evt.getSource() == cancel)
303				cancel();
304		} //}}}
305	} //}}}
306
307	//{{{ BrowserHandler class
308	class BrowserHandler implements BrowserListener
309	{
310		//{{{ filesSelected() method
311		public void filesSelected(VFSBrowser browser, VFS.DirectoryEntry[] files)
312		{
313			if(files.length == 0)
314				return;
315			else if(files.length == 1)
316			{
317				VFS.DirectoryEntry file = files[0];
318				if(file.type == VFS.DirectoryEntry.FILE)
319				{
320					String path = file.path;
321					String directory = browser.getDirectory();
322					VFS vfs = VFSManager.getVFSForPath(directory);
323					String parent = vfs.getParentOfPath(path);
324					if(parent.endsWith("/") || parent.endsWith(File.separator))
325						parent = parent.substring(0,parent.length() - 1);
326					if(parent.equals(directory))
327						path = file.name;
328
329					filenameField.setText(path);
330				}
331			}
332			else
333			{
334				filenameField.setText(null);
335			}
336		} //}}}
337
338		//{{{ filesActivated() method
339		public void filesActivated(VFSBrowser browser, VFS.DirectoryEntry[] files)
340		{
341			for(int i = 0; i < files.length; i++)
342			{
343				VFS.DirectoryEntry file = files[i];
344				if(file.type == VFS.DirectoryEntry.FILESYSTEM
345					|| file.type == VFS.DirectoryEntry.DIRECTORY)
346				{
347					// the browser will list the directory
348					// in question, so just return
349					return;
350				}
351			}
352
353			ok();
354		} //}}}
355	} //}}}
356
357	//{{{ KeyHandler class
358	class KeyHandler extends KeyAdapter
359	{
360		//{{{ keyPressed() method
361		public void keyPressed(KeyEvent evt)
362		{
363			browser.getBrowserView().selectNone();
364		} //}}}
365	} //}}}
366
367	//}}}
368}