PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/browser/BrowserCommandsMenu.java

#
Java | 267 lines | 199 code | 31 blank | 37 comment | 64 complexity | 2c61a275b5def79b0594fe5676f83687 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. * BrowserCommandsMenu.java - provides various commands
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999 Jason Ginchereau
  7. * Portions copyright (C) 2000, 2001 Slava Pestov
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit.browser;
  24. //{{{ Imports
  25. import java.awt.*;
  26. import java.awt.event.*;
  27. import java.util.*;
  28. import javax.swing.*;
  29. import javax.swing.event.*;
  30. import org.gjt.sp.jedit.io.*;
  31. import org.gjt.sp.jedit.*;
  32. //}}}
  33. /**
  34. * @version $Id: BrowserCommandsMenu.java 3999 2002-01-28 11:40:33Z spestov $
  35. * @author Slava Pestov and Jason Ginchereau
  36. */
  37. public class BrowserCommandsMenu extends JPopupMenu
  38. {
  39. //{{{ BrowserCommandsMenu constructor
  40. public BrowserCommandsMenu(VFSBrowser browser, VFS.DirectoryEntry file)
  41. {
  42. this.browser = browser;
  43. if(file != null)
  44. {
  45. this.file = file;
  46. VFS vfs = VFSManager.getVFSForPath(file.deletePath);
  47. boolean delete = (vfs.getCapabilities() & VFS.DELETE_CAP) != 0;
  48. boolean rename = (vfs.getCapabilities() & VFS.RENAME_CAP) != 0;
  49. if(jEdit.getBuffer(file.path) != null)
  50. {
  51. if(browser.getMode() == VFSBrowser.BROWSER)
  52. {
  53. add(createMenuItem("open"));
  54. add(createMenuItem("open-view"));
  55. add(createMenuItem("insert"));
  56. add(createMenuItem("close"));
  57. }
  58. else
  59. add(createMenuItem("choose"));
  60. }
  61. else
  62. {
  63. if(file.type == VFS.DirectoryEntry.DIRECTORY
  64. || file.type == VFS.DirectoryEntry.FILESYSTEM)
  65. {
  66. add(createMenuItem("browse"));
  67. }
  68. else if(browser.getMode() != VFSBrowser.BROWSER)
  69. {
  70. add(createMenuItem("choose"));
  71. }
  72. // else if in browser mode
  73. else
  74. {
  75. add(createMenuItem("open"));
  76. add(createMenuItem("open-view"));
  77. add(createOpenEncodingMenu());
  78. add(createMenuItem("insert"));
  79. }
  80. if(rename)
  81. add(createMenuItem("rename"));
  82. if(delete)
  83. add(createMenuItem("delete"));
  84. }
  85. addSeparator();
  86. }
  87. add(createMenuItem("up"));
  88. add(createMenuItem("reload"));
  89. add(createMenuItem("roots"));
  90. add(createMenuItem("home"));
  91. add(createMenuItem("synchronize"));
  92. addSeparator();
  93. if(browser.getMode() == VFSBrowser.BROWSER)
  94. add(createMenuItem("new-file"));
  95. add(createMenuItem("new-directory"));
  96. if(browser.getMode() == VFSBrowser.BROWSER)
  97. {
  98. addSeparator();
  99. add(createMenuItem("search-in-directory"));
  100. }
  101. addSeparator();
  102. JCheckBoxMenuItem showHiddenFiles = new JCheckBoxMenuItem(
  103. jEdit.getProperty("vfs.browser.commands.show-hidden-files.label"));
  104. showHiddenFiles.setActionCommand("show-hidden-files");
  105. showHiddenFiles.setSelected(browser.getShowHiddenFiles());
  106. showHiddenFiles.addActionListener(new ActionHandler());
  107. add(showHiddenFiles);
  108. } //}}}
  109. //{{{ Private members
  110. private VFSBrowser browser;
  111. private VFS.DirectoryEntry file;
  112. private VFS vfs;
  113. //{{{ createMenuItem() method
  114. private JMenuItem createMenuItem(String name)
  115. {
  116. String label = jEdit.getProperty("vfs.browser.commands." + name + ".label");
  117. JMenuItem mi = new JMenuItem(label);
  118. mi.setActionCommand(name);
  119. mi.addActionListener(new ActionHandler());
  120. return mi;
  121. } //}}}
  122. //{{{ createOpenEncodingMenu() method
  123. private JMenu createOpenEncodingMenu()
  124. {
  125. ActionListener listener = new ActionHandler();
  126. JMenu openEncoding = new JMenu(jEdit.getProperty("open-encoding.label"));
  127. // used twice...
  128. String systemEncoding = System.getProperty("file.encoding");
  129. JMenuItem mi = new JMenuItem(jEdit.getProperty("os-encoding"));
  130. mi.setActionCommand("open@" + systemEncoding);
  131. mi.addActionListener(listener);
  132. openEncoding.add(mi);
  133. mi = new JMenuItem(jEdit.getProperty("jedit-encoding"));
  134. mi.setActionCommand("open@" + jEdit.getProperty("buffer.encoding",systemEncoding));
  135. mi.addActionListener(listener);
  136. openEncoding.add(mi);
  137. openEncoding.addSeparator();
  138. StringTokenizer st = new StringTokenizer(jEdit.getProperty("encodings"));
  139. while(st.hasMoreTokens())
  140. {
  141. String encoding = st.nextToken();
  142. mi = new JMenuItem(encoding);
  143. mi.setActionCommand("open@" + encoding);
  144. mi.addActionListener(listener);
  145. openEncoding.add(mi);
  146. }
  147. openEncoding.addSeparator();
  148. mi = new JMenuItem(jEdit.getProperty("other-encoding.label"));
  149. mi.setActionCommand("other-encoding");
  150. mi.addActionListener(listener);
  151. openEncoding.add(mi);
  152. return openEncoding;
  153. } //}}}
  154. //}}}
  155. //{{{ ActionHandler class
  156. class ActionHandler implements ActionListener
  157. {
  158. public void actionPerformed(ActionEvent evt)
  159. {
  160. View view = browser.getView();
  161. String actionCommand = evt.getActionCommand();
  162. if(actionCommand.startsWith("open@"))
  163. {
  164. // a bit of a hack to support 'Open With Encoding' menu
  165. Hashtable props = new Hashtable();
  166. props.put(Buffer.ENCODING,actionCommand.substring(5));
  167. jEdit.openFile(view,null,file.path,false,props);
  168. }
  169. else if(actionCommand.equals("other-encoding"))
  170. {
  171. String encoding = GUIUtilities.input(browser,
  172. "encoding-prompt",null,
  173. jEdit.getProperty("buffer.encoding",
  174. System.getProperty("file.encoding")));
  175. if(encoding == null)
  176. return;
  177. Hashtable props = new Hashtable();
  178. props.put(Buffer.ENCODING,encoding);
  179. jEdit.openFile(view,null,file.path,false,props);
  180. }
  181. else if(actionCommand.equals("open"))
  182. jEdit.openFile(view,file.path);
  183. else if(actionCommand.equals("open-view"))
  184. {
  185. Buffer buffer = jEdit.openFile(null,file.path);
  186. if(buffer != null)
  187. jEdit.newView(view,buffer);
  188. }
  189. else if(actionCommand.equals("insert"))
  190. view.getBuffer().insertFile(view,file.path);
  191. else if(actionCommand.equals("choose"))
  192. browser.filesActivated(false,false);
  193. else if(actionCommand.equals("close"))
  194. {
  195. Buffer buffer = jEdit.getBuffer(file.path);
  196. if(buffer != null)
  197. jEdit.closeBuffer(view,buffer);
  198. }
  199. else if(actionCommand.equals("browse"))
  200. browser.setDirectory(file.path);
  201. else if(actionCommand.equals("rename"))
  202. browser.rename(file.path);
  203. else if(actionCommand.equals("delete"))
  204. browser.delete(file.deletePath);
  205. else if(actionCommand.equals("up"))
  206. {
  207. String path = browser.getDirectory();
  208. VFS vfs = VFSManager.getVFSForPath(path);
  209. browser.setDirectory(vfs.getParentOfPath(path));
  210. }
  211. else if(actionCommand.equals("reload"))
  212. browser.reloadDirectory();
  213. else if(actionCommand.equals("roots"))
  214. browser.rootDirectory();
  215. else if(actionCommand.equals("home"))
  216. browser.setDirectory(System.getProperty("user.home"));
  217. else if(actionCommand.equals("synchronize"))
  218. {
  219. Buffer buffer = browser.getView().getBuffer();
  220. browser.setDirectory(buffer.getVFS().getParentOfPath(
  221. buffer.getPath()));
  222. }
  223. else if(actionCommand.equals("new-file"))
  224. browser.newFile();
  225. else if(actionCommand.equals("new-directory"))
  226. browser.mkdir();
  227. else if(actionCommand.equals("search-in-directory"))
  228. browser.searchInDirectory();
  229. else if(actionCommand.equals("show-hidden-files"))
  230. {
  231. browser.setShowHiddenFiles(!browser.getShowHiddenFiles());
  232. browser.reloadDirectory();
  233. }
  234. }
  235. } //}}}
  236. }