PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/browser/VFSFileNameField.java

#
Java | 270 lines | 199 code | 28 blank | 43 comment | 47 complexity | 8b8c1a44c596da74d42c18d29ac5f61c 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 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. import org.gjt.sp.util.Log;
  33. //}}}
  34. /**
  35. * @author Slava Pestov
  36. * @version $Id: VFSFileNameField.java 5221 2005-05-21 20:51:43Z spestov $
  37. * @since jEdit 4.2pre1
  38. */
  39. class VFSFileNameField extends HistoryTextField
  40. {
  41. //{{{ VFSFileNameField constructor
  42. VFSFileNameField(VFSBrowser browser, String model)
  43. {
  44. super(model);
  45. setEnterAddsToHistory(false);
  46. this.browser = browser;
  47. Dimension dim = getPreferredSize();
  48. dim.width = Integer.MAX_VALUE;
  49. setMaximumSize(dim);
  50. } //}}}
  51. //{{{ isManagingFocus() method
  52. public boolean isManagingFocus()
  53. {
  54. return false;
  55. } //}}}
  56. //{{{ getFocusTraversalKeysEnabled() method
  57. public boolean getFocusTraversalKeysEnabled()
  58. {
  59. return false;
  60. } //}}}
  61. //{{{ processKeyEvent() method
  62. public void processKeyEvent(KeyEvent evt)
  63. {
  64. if(evt.getID() == KeyEvent.KEY_PRESSED)
  65. {
  66. String path = getText();
  67. switch(evt.getKeyCode())
  68. {
  69. case KeyEvent.VK_TAB:
  70. doComplete(path);
  71. break;
  72. case KeyEvent.VK_LEFT:
  73. if(getCaretPosition() == 0)
  74. browser.getBrowserView().getTable().processKeyEvent(evt);
  75. else
  76. super.processKeyEvent(evt);
  77. break;
  78. case KeyEvent.VK_RIGHT:
  79. if(getCaretPosition() == getDocument().getLength())
  80. browser.getBrowserView().getTable().processKeyEvent(evt);
  81. else
  82. super.processKeyEvent(evt);
  83. break;
  84. case KeyEvent.VK_UP:
  85. case KeyEvent.VK_DOWN:
  86. case KeyEvent.VK_PAGE_UP:
  87. case KeyEvent.VK_PAGE_DOWN:
  88. browser.getBrowserView().getTable()
  89. .processKeyEvent(evt);
  90. break;
  91. case KeyEvent.VK_ENTER:
  92. browser.filesActivated(
  93. (evt.isShiftDown()
  94. ? VFSBrowser.M_OPEN_NEW_VIEW
  95. : VFSBrowser.M_OPEN),false);
  96. setText(null);
  97. evt.consume();
  98. break;
  99. default:
  100. super.processKeyEvent(evt);
  101. break;
  102. }
  103. }
  104. else if(evt.getID() == KeyEvent.KEY_TYPED)
  105. {
  106. char ch = evt.getKeyChar();
  107. if(ch > 0x20 && ch != 0x7f && ch != 0xff)
  108. {
  109. super.processKeyEvent(evt);
  110. String path = getText();
  111. BrowserView view = browser.getBrowserView();
  112. view.selectNone();
  113. if(MiscUtilities.getLastSeparatorIndex(path) == -1)
  114. {
  115. int mode = browser.getMode();
  116. // fix for bug #765507
  117. // we don't type complete in save dialog
  118. // boxes. Press TAB to do an explicit
  119. // complete
  120. view.getTable().doTypeSelect(path,
  121. mode == VFSBrowser
  122. .CHOOSE_DIRECTORY_DIALOG
  123. ||
  124. mode == VFSBrowser
  125. .SAVE_DIALOG);
  126. }
  127. }
  128. else
  129. super.processKeyEvent(evt);
  130. }
  131. } //}}}
  132. //{{{ Private members
  133. private VFSBrowser browser;
  134. //{{{ doComplete() method
  135. public String doComplete(String path, String complete, boolean dirsOnly)
  136. {
  137. Log.log(Log.DEBUG,VFSFileNameField.class,
  138. "doComplete(" + path + "," + complete
  139. + "," + dirsOnly);
  140. for(;;)
  141. {
  142. if(complete.length() == 0)
  143. return path;
  144. int index = MiscUtilities.getFirstSeparatorIndex(complete);
  145. if(index == -1)
  146. return path;
  147. /* Until the very last path component, we only complete on
  148. directories */
  149. String newPath = VFSFile.findCompletion(path,
  150. complete.substring(0,index),browser,true);
  151. if(newPath == null)
  152. return null;
  153. path = newPath;
  154. complete = complete.substring(index + 1);
  155. }
  156. } //}}}
  157. //{{{ doComplete() method
  158. private void doComplete(String currentText)
  159. {
  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. if(MiscUtilities.isAbsolutePath(currentText))
  167. {
  168. if(dir.startsWith("/"))
  169. dir = dir.substring(1);
  170. dir = doComplete(VFSBrowser.getRootDirectory(),dir,false);
  171. if(dir == null)
  172. return;
  173. browser.setDirectory(dir);
  174. VFSManager.waitForRequests();
  175. if(index == -1)
  176. {
  177. if(currentText.startsWith("/"))
  178. currentText = currentText.substring(1);
  179. }
  180. else
  181. currentText = currentText.substring(index + 1);
  182. }
  183. else
  184. {
  185. if(dir.length() != 0)
  186. {
  187. dir = doComplete(browser.getDirectory(),dir,false);
  188. if(dir == null)
  189. return;
  190. browser.setDirectory(dir);
  191. VFSManager.waitForRequests();
  192. currentText = currentText.substring(index + 1);
  193. }
  194. }
  195. BrowserView view = browser.getBrowserView();
  196. view.selectNone();
  197. view.getTable().doTypeSelect(currentText,
  198. browser.getMode() == VFSBrowser
  199. .CHOOSE_DIRECTORY_DIALOG);
  200. String newText;
  201. VFSFile[] files = view.getSelectedFiles();
  202. if(files.length == 0)
  203. newText = currentText;
  204. else
  205. {
  206. String path = files[0].getPath();
  207. String name = files[0].getName();
  208. String parent = MiscUtilities.getParentOfPath(path);
  209. if(MiscUtilities.isAbsolutePath(currentText)
  210. && !currentText.startsWith(browser.getDirectory()))
  211. {
  212. newText = path;
  213. }
  214. else
  215. {
  216. if(MiscUtilities.pathsEqual(parent,browser.getDirectory()))
  217. newText = name;
  218. else
  219. newText = path;
  220. }
  221. }
  222. setText(newText);
  223. } //}}}
  224. //{{{ goToParent() method
  225. private void goToParent()
  226. {
  227. String name = MiscUtilities.getFileName(browser.getDirectory());
  228. String parent = MiscUtilities.getParentOfPath(
  229. browser.getDirectory());
  230. browser.setDirectory(parent);
  231. VFS vfs = VFSManager.getVFSForPath(parent);
  232. if((vfs.getCapabilities() & VFS.LOW_LATENCY_CAP) != 0)
  233. {
  234. VFSManager.waitForRequests();
  235. setText(name);
  236. browser.getBrowserView().getTable().doTypeSelect(
  237. name,browser.getMode() == VFSBrowser
  238. .CHOOSE_DIRECTORY_DIALOG);
  239. }
  240. } //}}}
  241. //}}}
  242. }