KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > browser > VFSFileNameField


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
23 package org.gjt.sp.jedit.browser;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import java.awt.event.*;
28 import java.awt.*;
29 import java.io.File JavaDoc;
30 import org.gjt.sp.jedit.gui.HistoryTextField;
31 import org.gjt.sp.jedit.io.*;
32 import org.gjt.sp.jedit.MiscUtilities;
33 import org.gjt.sp.jedit.OperatingSystem;
34 import org.gjt.sp.util.Log;
35 //}}}
36

37 /**
38  * @author Slava Pestov
39  * @version $Id: VFSFileNameField.java 5221 2005-05-21 20:51:43Z spestov $
40  * @since jEdit 4.2pre1
41  */

42 class VFSFileNameField extends HistoryTextField
43 {
44     //{{{ VFSFileNameField constructor
45
VFSFileNameField(VFSBrowser browser, String JavaDoc model)
46     {
47         super(model);
48         setEnterAddsToHistory(false);
49
50         this.browser = browser;
51
52         Dimension dim = getPreferredSize();
53         dim.width = Integer.MAX_VALUE;
54         setMaximumSize(dim);
55     } //}}}
56

57     //{{{ isManagingFocus() method
58
public boolean isManagingFocus()
59     {
60         return false;
61     } //}}}
62

63     //{{{ getFocusTraversalKeysEnabled() method
64
public boolean getFocusTraversalKeysEnabled()
65     {
66         return false;
67     } //}}}
68

69     //{{{ processKeyEvent() method
70
public void processKeyEvent(KeyEvent evt)
71     {
72         if(evt.getID() == KeyEvent.KEY_PRESSED)
73         {
74             String JavaDoc path = getText();
75
76             switch(evt.getKeyCode())
77             {
78             case KeyEvent.VK_TAB:
79                 doComplete(path);
80                 break;
81             case KeyEvent.VK_LEFT:
82                 if(getCaretPosition() == 0)
83                     browser.getBrowserView().getTable().processKeyEvent(evt);
84                 else
85                     super.processKeyEvent(evt);
86                 break;
87             case KeyEvent.VK_RIGHT:
88                 if(getCaretPosition() == getDocument().getLength())
89                     browser.getBrowserView().getTable().processKeyEvent(evt);
90                 else
91                     super.processKeyEvent(evt);
92                 break;
93             case KeyEvent.VK_UP:
94             case KeyEvent.VK_DOWN:
95             case KeyEvent.VK_PAGE_UP:
96             case KeyEvent.VK_PAGE_DOWN:
97                 browser.getBrowserView().getTable()
98                     .processKeyEvent(evt);
99                 break;
100             case KeyEvent.VK_ENTER:
101                 browser.filesActivated(
102                     (evt.isShiftDown()
103                     ? VFSBrowser.M_OPEN_NEW_VIEW
104                     : VFSBrowser.M_OPEN),false);
105                 setText(null);
106                 evt.consume();
107                 break;
108             default:
109                 super.processKeyEvent(evt);
110                 break;
111             }
112         }
113         else if(evt.getID() == KeyEvent.KEY_TYPED)
114         {
115             char ch = evt.getKeyChar();
116             if(ch > 0x20 && ch != 0x7f && ch != 0xff)
117             {
118                 super.processKeyEvent(evt);
119                 String JavaDoc path = getText();
120                 BrowserView view = browser.getBrowserView();
121                 view.selectNone();
122
123                 if(MiscUtilities.getLastSeparatorIndex(path) == -1)
124                 {
125                     int mode = browser.getMode();
126                     // fix for bug #765507
127
// we don't type complete in save dialog
128
// boxes. Press TAB to do an explicit
129
// complete
130
view.getTable().doTypeSelect(path,
131                         mode == VFSBrowser
132                         .CHOOSE_DIRECTORY_DIALOG
133                         ||
134                         mode == VFSBrowser
135                         .SAVE_DIALOG);
136                 }
137             }
138             else
139                 super.processKeyEvent(evt);
140         }
141     } //}}}
142

143     //{{{ Private members
144
private VFSBrowser browser;
145
146     //{{{ doComplete() method
147
public String JavaDoc doComplete(String JavaDoc path, String JavaDoc complete, boolean dirsOnly)
148     {
149         Log.log(Log.DEBUG,VFSFileNameField.class,
150             "doComplete(" + path + "," + complete
151             + "," + dirsOnly);
152
153         for(;;)
154         {
155             if(complete.length() == 0)
156                 return path;
157             int index = MiscUtilities.getFirstSeparatorIndex(complete);
158             if(index == -1)
159                 return path;
160
161             /* Until the very last path component, we only complete on
162             directories */

163             String JavaDoc newPath = VFSFile.findCompletion(path,
164                 complete.substring(0,index),browser,true);
165             if(newPath == null)
166                 return null;
167             path = newPath;
168             complete = complete.substring(index + 1);
169         }
170     } //}}}
171

172     //{{{ doComplete() method
173
private void doComplete(String JavaDoc currentText)
174     {
175         int index = MiscUtilities.getLastSeparatorIndex(currentText);
176         String JavaDoc dir;
177         if(index != -1)
178             dir = currentText.substring(0,index + 1);
179         else
180             dir = "";
181
182         if(MiscUtilities.isAbsolutePath(currentText))
183         {
184             if(dir.startsWith("/"))
185                 dir = dir.substring(1);
186             dir = doComplete(VFSBrowser.getRootDirectory(),dir,false);
187             if(dir == null)
188                 return;
189     
190             browser.setDirectory(dir);
191             VFSManager.waitForRequests();
192
193             if(index == -1)
194             {
195                 if(currentText.startsWith("/"))
196                     currentText = currentText.substring(1);
197             }
198             else
199                 currentText = currentText.substring(index + 1);
200         }
201         else
202         {
203             if(dir.length() != 0)
204             {
205                 dir = doComplete(browser.getDirectory(),dir,false);
206                 if(dir == null)
207                     return;
208     
209                 browser.setDirectory(dir);
210                 VFSManager.waitForRequests();
211     
212                 currentText = currentText.substring(index + 1);
213             }
214         }
215
216         BrowserView view = browser.getBrowserView();
217         view.selectNone();
218         view.getTable().doTypeSelect(currentText,
219             browser.getMode() == VFSBrowser
220             .CHOOSE_DIRECTORY_DIALOG);
221
222         String JavaDoc newText;
223
224         VFSFile[] files = view.getSelectedFiles();
225         if(files.length == 0)
226             newText = currentText;
227         else
228         {
229             String JavaDoc path = files[0].getPath();
230             String JavaDoc name = files[0].getName();
231             String JavaDoc parent = MiscUtilities.getParentOfPath(path);
232
233             if(MiscUtilities.isAbsolutePath(currentText)
234                 && !currentText.startsWith(browser.getDirectory()))
235             {
236                 newText = path;
237             }
238             else
239             {
240                 if(MiscUtilities.pathsEqual(parent,browser.getDirectory()))
241                     newText = name;
242                 else
243                     newText = path;
244             }
245         }
246
247         setText(newText);
248     } //}}}
249

250     //{{{ goToParent() method
251
private void goToParent()
252     {
253         String JavaDoc name = MiscUtilities.getFileName(browser.getDirectory());
254         String JavaDoc parent = MiscUtilities.getParentOfPath(
255             browser.getDirectory());
256         browser.setDirectory(parent);
257
258         VFS vfs = VFSManager.getVFSForPath(parent);
259         if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) != 0)
260         {
261             VFSManager.waitForRequests();
262             setText(name);
263             browser.getBrowserView().getTable().doTypeSelect(
264                 name,browser.getMode() == VFSBrowser
265                 .CHOOSE_DIRECTORY_DIALOG);
266         }
267     } //}}}
268

269     //}}}
270
}
271
Popular Tags