PageRenderTime 50ms CodeModel.GetById 39ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/browser/VFSFileNameField.java

#
Java | 286 lines | 214 code | 28 blank | 44 comment | 46 complexity | 66fa7a24c00de2c8c08a0246bb31d383 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, 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
 23package org.gjt.sp.jedit.browser;
 24
 25//{{{ Imports
 26import java.util.HashSet;
 27import java.awt.event.*;
 28import java.awt.*;
 29import org.gjt.sp.jedit.gui.HistoryTextField;
 30import org.gjt.sp.jedit.io.*;
 31import org.gjt.sp.jedit.MiscUtilities;
 32
 33import org.gjt.sp.util.Log;
 34//}}}
 35
 36/**
 37 * @author Slava Pestov
 38 * @version $Id: VFSFileNameField.java 19318 2011-02-01 04:22:08Z vanza $
 39 * @since jEdit 4.2pre1 (public since 4.5pre1)
 40 */
 41public class VFSFileNameField extends HistoryTextField
 42{
 43	//{{{ VFSFileNameField constructor
 44	public 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		// Enable TAB pressed for completion instead of
 56		// focas traversal.
 57		final int FORWARD = KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS;
 58		HashSet<AWTKeyStroke> keys = new HashSet<AWTKeyStroke>(
 59				getFocusTraversalKeys(FORWARD));
 60		keys.remove(AWTKeyStroke.getAWTKeyStroke("pressed TAB"));
 61		setFocusTraversalKeys(FORWARD, keys);
 62	} //}}}
 63
 64	//{{{ processKeyEvent() method
 65	public void processKeyEvent(KeyEvent evt)
 66	{
 67		if(evt.getID() == KeyEvent.KEY_PRESSED)
 68		{
 69			String path = getText();
 70
 71			switch(evt.getKeyCode())
 72			{
 73			case KeyEvent.VK_TAB:
 74				doComplete(path);
 75				break;
 76			case KeyEvent.VK_LEFT:
 77				if ((evt.getModifiers() & KeyEvent.ALT_MASK) > 0)
 78				{
 79					browser.previousDirectory();
 80					evt.consume();
 81				}
 82				else
 83				{
 84					// 				browser.getBrowserView().getTable().processKeyEvent(evt);
 85					super.processKeyEvent(evt);
 86				}
 87				break;
 88			case KeyEvent.VK_UP:
 89				if ((evt.getModifiers() & KeyEvent.ALT_MASK)>0)
 90				{
 91					String p = browser.getDirectory();
 92					browser.setDirectory(MiscUtilities.getParentOfPath(p));
 93					evt.consume();
 94				}
 95				else
 96				{
 97					browser.getBrowserView().getTable()
 98					.processKeyEvent(evt);
 99				}
100				break;
101			case KeyEvent.VK_RIGHT:
102				if ((evt.getModifiers() & KeyEvent.ALT_MASK)>0)
103				{
104					evt.consume();
105					browser.nextDirectory();
106				}
107				else
108					super.processKeyEvent(evt);
109				break;
110			case KeyEvent.VK_DOWN:
111			case KeyEvent.VK_PAGE_UP:
112			case KeyEvent.VK_PAGE_DOWN:
113				browser.getBrowserView().getTable()
114					.processKeyEvent(evt);
115				break;
116			case KeyEvent.VK_ENTER:
117				browser.filesActivated(
118					(evt.isShiftDown()
119					? VFSBrowser.M_OPEN_NEW_VIEW
120					: VFSBrowser.M_OPEN),false);
121				setText(null);
122				evt.consume();
123				break;
124			default:
125				super.processKeyEvent(evt);
126				break;
127			}
128		}
129		else if(evt.getID() == KeyEvent.KEY_TYPED)
130		{
131			char ch = evt.getKeyChar();
132			if(ch > 0x20 && ch != 0x7f && ch != 0xff)
133			{
134				super.processKeyEvent(evt);
135				String path = getText();
136				BrowserView view = browser.getBrowserView();
137				view.selectNone();
138
139				if(MiscUtilities.getLastSeparatorIndex(path) == -1)
140				{
141					int mode = browser.getMode();
142					// fix for bug #765507
143					// we don't type complete in save dialog
144					// boxes. Press TAB to do an explicit
145					// complete
146					view.getTable().doTypeSelect(path,
147						mode == VFSBrowser
148						.CHOOSE_DIRECTORY_DIALOG
149						||
150						mode == VFSBrowser
151						.SAVE_DIALOG);
152				}
153			}
154			else
155				super.processKeyEvent(evt);
156		}
157	} //}}}
158
159	//{{{ Private members
160	private VFSBrowser browser;
161
162	//{{{ doComplete() method
163	public String doComplete(String path, String complete, boolean dirsOnly)
164	{
165		Log.log(Log.DEBUG,VFSFileNameField.class,
166			"doComplete(" + path + "," + complete
167			+ "," + dirsOnly);
168
169		for(;;)
170		{
171			if(complete.length() == 0)
172				return path;
173			int index = MiscUtilities.getFirstSeparatorIndex(complete);
174			if(index == -1)
175				return path;
176
177			/* Until the very last path component, we only complete on
178			directories */
179			String newPath = VFSFile.findCompletion(path,
180				complete.substring(0,index),browser,true);
181			if(newPath == null)
182				return null;
183			path = newPath;
184			complete = complete.substring(index + 1);
185		}
186	} //}}}
187
188	//{{{ doComplete() method
189	private void doComplete(String currentText)
190	{
191		int index = MiscUtilities.getLastSeparatorIndex(currentText);
192		String dir;
193		if(index != -1)
194			dir = currentText.substring(0,index + 1);
195		else
196			dir = "";
197
198		if(MiscUtilities.isAbsolutePath(currentText))
199		{
200			if(dir.startsWith("/"))
201				dir = dir.substring(1);
202			dir = doComplete(VFSBrowser.getRootDirectory(),dir,false);
203			if(dir == null)
204				return;
205
206			browser.setDirectory(dir);
207			VFSManager.waitForRequests();
208
209			if(index == -1)
210			{
211				if(currentText.startsWith("/"))
212					currentText = currentText.substring(1);
213			}
214			else
215				currentText = currentText.substring(index + 1);
216		}
217		else
218		{
219			if(dir.length() != 0)
220			{
221				dir = doComplete(browser.getDirectory(),dir,false);
222				if(dir == null)
223					return;
224
225				browser.setDirectory(dir);
226				VFSManager.waitForRequests();
227
228				currentText = currentText.substring(index + 1);
229			}
230		}
231
232		BrowserView view = browser.getBrowserView();
233		view.selectNone();
234		view.getTable().doTypeSelect(currentText,
235			browser.getMode() == VFSBrowser
236			.CHOOSE_DIRECTORY_DIALOG);
237
238		String newText;
239
240		VFSFile[] files = view.getSelectedFiles();
241		if(files.length == 0)
242			newText = currentText;
243		else
244		{
245			String path = files[0].getPath();
246			String name = files[0].getName();
247			String parent = MiscUtilities.getParentOfPath(path);
248
249			if(MiscUtilities.isAbsolutePath(currentText)
250				&& !currentText.startsWith(browser.getDirectory()))
251			{
252				newText = path;
253			}
254			else
255			{
256				if(MiscUtilities.pathsEqual(parent,browser.getDirectory()))
257					newText = name;
258				else
259					newText = path;
260			}
261		}
262
263		setText(newText);
264	} //}}}
265
266	//{{{ goToParent() method
267	private void goToParent()
268	{
269		String name = MiscUtilities.getFileName(browser.getDirectory());
270		String parent = MiscUtilities.getParentOfPath(
271			browser.getDirectory());
272		browser.setDirectory(parent);
273
274		VFS vfs = VFSManager.getVFSForPath(parent);
275		if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) != 0)
276		{
277			VFSManager.waitForRequests();
278			setText(name);
279			browser.getBrowserView().getTable().doTypeSelect(
280				name,browser.getMode() == VFSBrowser
281				.CHOOSE_DIRECTORY_DIALOG);
282		}
283	} //}}}
284
285	//}}}
286}