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