001: /*
002: * VFSFileNameField.java - File name field with completion
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2003, 2005 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.browser;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import java.awt.event.*;
028: import java.awt.*;
029: import java.io.File;
030: import org.gjt.sp.jedit.gui.HistoryTextField;
031: import org.gjt.sp.jedit.io.*;
032: import org.gjt.sp.jedit.MiscUtilities;
033: import org.gjt.sp.jedit.OperatingSystem;
034: import org.gjt.sp.util.Log;
035:
036: //}}}
037:
038: /**
039: * @author Slava Pestov
040: * @version $Id: VFSFileNameField.java 5221 2005-05-21 20:51:43Z spestov $
041: * @since jEdit 4.2pre1
042: */
043: class VFSFileNameField extends HistoryTextField {
044: //{{{ VFSFileNameField constructor
045: VFSFileNameField(VFSBrowser browser, String model) {
046: super (model);
047: setEnterAddsToHistory(false);
048:
049: this .browser = browser;
050:
051: Dimension dim = getPreferredSize();
052: dim.width = Integer.MAX_VALUE;
053: setMaximumSize(dim);
054: } //}}}
055:
056: //{{{ isManagingFocus() method
057: public boolean isManagingFocus() {
058: return false;
059: } //}}}
060:
061: //{{{ getFocusTraversalKeysEnabled() method
062: public boolean getFocusTraversalKeysEnabled() {
063: return false;
064: } //}}}
065:
066: //{{{ processKeyEvent() method
067: public void processKeyEvent(KeyEvent evt) {
068: if (evt.getID() == KeyEvent.KEY_PRESSED) {
069: String path = getText();
070:
071: switch (evt.getKeyCode()) {
072: case KeyEvent.VK_TAB:
073: doComplete(path);
074: break;
075: case KeyEvent.VK_LEFT:
076: if (getCaretPosition() == 0)
077: browser.getBrowserView().getTable()
078: .processKeyEvent(evt);
079: else
080: super .processKeyEvent(evt);
081: break;
082: case KeyEvent.VK_RIGHT:
083: if (getCaretPosition() == getDocument().getLength())
084: browser.getBrowserView().getTable()
085: .processKeyEvent(evt);
086: else
087: super .processKeyEvent(evt);
088: break;
089: case KeyEvent.VK_UP:
090: case KeyEvent.VK_DOWN:
091: case KeyEvent.VK_PAGE_UP:
092: case KeyEvent.VK_PAGE_DOWN:
093: browser.getBrowserView().getTable()
094: .processKeyEvent(evt);
095: break;
096: case KeyEvent.VK_ENTER:
097: browser.filesActivated(
098: (evt.isShiftDown() ? VFSBrowser.M_OPEN_NEW_VIEW
099: : VFSBrowser.M_OPEN), false);
100: setText(null);
101: evt.consume();
102: break;
103: default:
104: super .processKeyEvent(evt);
105: break;
106: }
107: } else if (evt.getID() == KeyEvent.KEY_TYPED) {
108: char ch = evt.getKeyChar();
109: if (ch > 0x20 && ch != 0x7f && ch != 0xff) {
110: super .processKeyEvent(evt);
111: String path = getText();
112: BrowserView view = browser.getBrowserView();
113: view.selectNone();
114:
115: if (MiscUtilities.getLastSeparatorIndex(path) == -1) {
116: int mode = browser.getMode();
117: // fix for bug #765507
118: // we don't type complete in save dialog
119: // boxes. Press TAB to do an explicit
120: // complete
121: view.getTable().doTypeSelect(
122: path,
123: mode == VFSBrowser.CHOOSE_DIRECTORY_DIALOG
124: || mode == VFSBrowser.SAVE_DIALOG);
125: }
126: } else
127: super .processKeyEvent(evt);
128: }
129: } //}}}
130:
131: //{{{ Private members
132: private VFSBrowser browser;
133:
134: //{{{ doComplete() method
135: public String doComplete(String path, String complete,
136: boolean dirsOnly) {
137: Log.log(Log.DEBUG, VFSFileNameField.class, "doComplete(" + path
138: + "," + complete + "," + dirsOnly);
139:
140: for (;;) {
141: if (complete.length() == 0)
142: return path;
143: int index = MiscUtilities.getFirstSeparatorIndex(complete);
144: if (index == -1)
145: return path;
146:
147: /* Until the very last path component, we only complete on
148: directories */
149: String newPath = VFSFile.findCompletion(path, complete
150: .substring(0, index), browser, true);
151: if (newPath == null)
152: return null;
153: path = newPath;
154: complete = complete.substring(index + 1);
155: }
156: } //}}}
157:
158: //{{{ doComplete() method
159: private void doComplete(String currentText) {
160: int index = MiscUtilities.getLastSeparatorIndex(currentText);
161: String dir;
162: if (index != -1)
163: dir = currentText.substring(0, index + 1);
164: else
165: dir = "";
166:
167: if (MiscUtilities.isAbsolutePath(currentText)) {
168: if (dir.startsWith("/"))
169: dir = dir.substring(1);
170: dir = doComplete(VFSBrowser.getRootDirectory(), dir, false);
171: if (dir == null)
172: return;
173:
174: browser.setDirectory(dir);
175: VFSManager.waitForRequests();
176:
177: if (index == -1) {
178: if (currentText.startsWith("/"))
179: currentText = currentText.substring(1);
180: } else
181: currentText = currentText.substring(index + 1);
182: } else {
183: if (dir.length() != 0) {
184: dir = doComplete(browser.getDirectory(), dir, false);
185: if (dir == null)
186: return;
187:
188: browser.setDirectory(dir);
189: VFSManager.waitForRequests();
190:
191: currentText = currentText.substring(index + 1);
192: }
193: }
194:
195: BrowserView view = browser.getBrowserView();
196: view.selectNone();
197: view
198: .getTable()
199: .doTypeSelect(
200: currentText,
201: browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG);
202:
203: String newText;
204:
205: VFSFile[] files = view.getSelectedFiles();
206: if (files.length == 0)
207: newText = currentText;
208: else {
209: String path = files[0].getPath();
210: String name = files[0].getName();
211: String parent = MiscUtilities.getParentOfPath(path);
212:
213: if (MiscUtilities.isAbsolutePath(currentText)
214: && !currentText.startsWith(browser.getDirectory())) {
215: newText = path;
216: } else {
217: if (MiscUtilities.pathsEqual(parent, browser
218: .getDirectory()))
219: newText = name;
220: else
221: newText = path;
222: }
223: }
224:
225: setText(newText);
226: } //}}}
227:
228: //{{{ goToParent() method
229: private void goToParent() {
230: String name = MiscUtilities.getFileName(browser.getDirectory());
231: String parent = MiscUtilities.getParentOfPath(browser
232: .getDirectory());
233: browser.setDirectory(parent);
234:
235: VFS vfs = VFSManager.getVFSForPath(parent);
236: if ((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) != 0) {
237: VFSManager.waitForRequests();
238: setText(name);
239: browser
240: .getBrowserView()
241: .getTable()
242: .doTypeSelect(
243: name,
244: browser.getMode() == VFSBrowser.CHOOSE_DIRECTORY_DIALOG);
245: }
246: } //}}}
247:
248: //}}}
249: }
|