1 22 23 package org.gjt.sp.jedit.browser; 24 25 import javax.swing.*; 27 import java.awt.event.*; 28 import java.awt.*; 29 import java.io.File ; 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 37 42 class VFSFileNameField extends HistoryTextField 43 { 44 VFSFileNameField(VFSBrowser browser, String 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 } 57 public boolean isManagingFocus() 59 { 60 return false; 61 } 63 public boolean getFocusTraversalKeysEnabled() 65 { 66 return false; 67 } 69 public void processKeyEvent(KeyEvent evt) 71 { 72 if(evt.getID() == KeyEvent.KEY_PRESSED) 73 { 74 String 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 path = getText(); 120 BrowserView view = browser.getBrowserView(); 121 view.selectNone(); 122 123 if(MiscUtilities.getLastSeparatorIndex(path) == -1) 124 { 125 int mode = browser.getMode(); 126 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 } 143 private VFSBrowser browser; 145 146 public String doComplete(String path, String 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 163 String 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 } 172 private void doComplete(String currentText) 174 { 175 int index = MiscUtilities.getLastSeparatorIndex(currentText); 176 String 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 newText; 223 224 VFSFile[] files = view.getSelectedFiles(); 225 if(files.length == 0) 226 newText = currentText; 227 else 228 { 229 String path = files[0].getPath(); 230 String name = files[0].getName(); 231 String 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 } 250 private void goToParent() 252 { 253 String name = MiscUtilities.getFileName(browser.getDirectory()); 254 String 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 } 269 } 271 | Popular Tags |