PageRenderTime 69ms CodeModel.GetById 58ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 304 lines | 226 code | 30 blank | 48 comment | 61 complexity | 0c00cfc9b3f33c28dbc81412e1fae71f MD5 | raw file
  1/*
  2 * VFSFileNameField.java - File name field with completion
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 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.*;
 27import java.awt.event.*;
 28import java.awt.*;
 29import java.io.File;
 30import org.gjt.sp.jedit.gui.HistoryTextField;
 31import org.gjt.sp.jedit.io.*;
 32import org.gjt.sp.jedit.MiscUtilities;
 33import org.gjt.sp.jedit.OperatingSystem;
 34//}}}
 35
 36/**
 37 * @author Slava Pestov
 38 * @version $Id: VFSFileNameField.java 5020 2004-04-19 21:17:54Z spestov $
 39 * @since jEdit 4.2pre1
 40 */
 41class VFSFileNameField extends HistoryTextField
 42{
 43	//{{{ VFSFileNameField constructor
 44	VFSFileNameField(VFSBrowser browser, String model)
 45	{
 46		super(model);
 47		setEnterAddsToHistory(false);
 48
 49		this.browser = browser;
 50
 51		Dimension dim = getPreferredSize();
 52		dim.width = Integer.MAX_VALUE;
 53		setMaximumSize(dim);
 54
 55		ActionMap map = getActionMap();
 56		Action backspace = map.get("delete-previous");
 57		map.put("delete-previous",new BackspaceAction(backspace));
 58	} //}}}
 59
 60	//{{{ isManagingFocus() method
 61	public boolean isManagingFocus()
 62	{
 63		return false;
 64	} //}}}
 65
 66	//{{{ getFocusTraversalKeysEnabled() method
 67	public boolean getFocusTraversalKeysEnabled()
 68	{
 69		return false;
 70	} //}}}
 71
 72	//{{{ processKeyEvent() method
 73	public void processKeyEvent(KeyEvent evt)
 74	{
 75		if(evt.getID() == KeyEvent.KEY_PRESSED)
 76		{
 77			String path = getText();
 78
 79			switch(evt.getKeyCode())
 80			{
 81			case KeyEvent.VK_TAB:
 82				doComplete(path);
 83				break;
 84			case KeyEvent.VK_LEFT:
 85				if(getCaretPosition() == 0)
 86					browser.getBrowserView().getTable().processKeyEvent(evt);
 87				else
 88					super.processKeyEvent(evt);
 89				break;
 90			case KeyEvent.VK_RIGHT:
 91				if(getCaretPosition() == getDocument().getLength())
 92					browser.getBrowserView().getTable().processKeyEvent(evt);
 93				else
 94					super.processKeyEvent(evt);
 95				break;
 96			case KeyEvent.VK_UP:
 97			case KeyEvent.VK_DOWN:
 98			case KeyEvent.VK_PAGE_UP:
 99			case KeyEvent.VK_PAGE_DOWN:
100				browser.getBrowserView().getTable()
101					.processKeyEvent(evt);
102				break;
103			case KeyEvent.VK_ENTER:
104				browser.filesActivated(
105					(evt.isShiftDown()
106					? VFSBrowser.M_OPEN_NEW_VIEW
107					: VFSBrowser.M_OPEN),false);
108				setText(null);
109				evt.consume();
110				break;
111			default:
112				super.processKeyEvent(evt);
113				break;
114			}
115		}
116		else if(evt.getID() == KeyEvent.KEY_TYPED)
117		{
118			char ch = evt.getKeyChar();
119			if(ch == '/' || ch == File.separatorChar)
120			{
121				super.processKeyEvent(evt);
122				String path = getText();
123
124				if(path.length() == 2 && path.charAt(0) == '-')
125				{
126					path = browser.getView().getBuffer()
127						.getDirectory();
128				}
129				else if(path.length() == 3 && path.startsWith(".."))
130				{
131					path = MiscUtilities.getParentOfPath(
132						browser.getDirectory());
133					VFS vfs = VFSManager.getVFSForPath(path);
134					if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) != 0)
135					{
136						browser.setDirectory(path);
137						VFSManager.waitForRequests();
138						setText(null);
139					}
140				}
141				else if(!MiscUtilities.isAbsolutePath(path))
142				{
143					VFS.DirectoryEntry[] files = browser
144						.getBrowserView().getSelectedFiles();
145					if(files.length != 1
146						|| files[0].type ==
147						VFS.DirectoryEntry.FILE)
148					{
149						return;
150					}
151					path = files[0].path;
152				}
153				else if(OperatingSystem.isDOSDerived()
154					&& path.length() == 3
155					&& path.charAt(1) == ':')
156				{
157					browser.setDirectory(path);
158					VFSManager.waitForRequests();
159					setText(null);
160				}
161
162				VFS vfs = VFSManager.getVFSForPath(path);
163				if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) != 0)
164				{
165					setText(null);
166					browser.setDirectory(path);
167					VFSManager.waitForRequests();
168				}
169				else
170				{
171					if(path.endsWith("/") || path.endsWith(File.separator))
172						setText(path);
173					else
174						setText(path + vfs.getFileSeparator());
175				}
176			}
177			else if(ch > 0x20 && ch != 0x7f && ch != 0xff)
178			{
179				super.processKeyEvent(evt);
180				String path = getText();
181
182				BrowserView view = browser.getBrowserView();
183				view.selectNone();
184
185				int mode = browser.getMode();
186				// fix for bug #765507
187				// we don't type complete in save dialog
188				// boxes. Press TAB to do an explicit
189				// complete
190				view.getTable().doTypeSelect(path,
191					mode == VFSBrowser
192					.CHOOSE_DIRECTORY_DIALOG
193					||
194					mode == VFSBrowser
195					.SAVE_DIALOG);
196			}
197			else
198				super.processKeyEvent(evt);
199		}
200	} //}}}
201
202	//{{{ Private members
203	private VFSBrowser browser;
204
205	//{{{ doComplete() method
206	private void doComplete(String currentText)
207	{
208		BrowserView view = browser.getBrowserView();
209		view.selectNone();
210		view.getTable().doTypeSelect(currentText,
211			browser.getMode() == VFSBrowser
212			.CHOOSE_DIRECTORY_DIALOG);
213
214		VFS.DirectoryEntry[] files = view.getSelectedFiles();
215		if(files.length == 0)
216			return;
217
218		String path = files[0].path;
219		String name = files[0].name;
220		String parent = MiscUtilities.getParentOfPath(path);
221
222		String newText;
223		if(MiscUtilities.isAbsolutePath(currentText)
224			&& !currentText.startsWith(browser.getDirectory()))
225		{
226			newText = path;
227		}
228		else
229		{
230			if(VFSBrowser.pathsEqual(parent,browser.getDirectory()))
231				newText = name;
232			else
233				newText = path;
234		}
235
236		setText(newText);
237	} //}}}
238
239	//{{{ goToParent() method
240	private void goToParent()
241	{
242		String name = MiscUtilities.getFileName(browser.getDirectory());
243		String parent = MiscUtilities.getParentOfPath(
244			browser.getDirectory());
245		browser.setDirectory(parent);
246
247		VFS vfs = VFSManager.getVFSForPath(parent);
248		if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) != 0)
249		{
250			VFSManager.waitForRequests();
251			setText(name);
252			browser.getBrowserView().getTable().doTypeSelect(
253				name,browser.getMode() == VFSBrowser
254				.CHOOSE_DIRECTORY_DIALOG);
255		}
256	} //}}}
257
258	//}}}
259
260	//{{{ BackspaceAction class
261	/**
262	 * In 4.3, I need to change all the keystrokes to use actions.
263	 */
264	class BackspaceAction extends AbstractAction
265	{
266		private Action delegate;
267
268		BackspaceAction(Action delegate)
269		{
270			this.delegate = delegate;
271		}
272
273		public void actionPerformed(ActionEvent evt)
274		{
275			if(getSelectionStart() == 0
276				&& getSelectionEnd() == 0)
277			{
278				goToParent();
279			}
280			else
281			{
282				delegate.actionPerformed(evt);
283
284				String path = getText();
285
286				BrowserView view = browser.getBrowserView();
287
288				view.selectNone();
289
290				int mode = browser.getMode();
291				// fix for bug #765507
292				// we don't type complete in save dialog
293				// boxes. Press TAB to do an explicit
294				// complete
295				view.getTable().doTypeSelect(path,
296					mode == VFSBrowser
297					.CHOOSE_DIRECTORY_DIALOG
298					||
299					mode == VFSBrowser
300					.SAVE_DIALOG);
301			}
302		}
303	} //}}}
304}