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