PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 340 lines | 263 code | 37 blank | 40 comment | 98 complexity | 8ae5b376dae9006cbf69817cebff2d6c 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) 2000, 2001, 2002 Slava Pestov
  7. * Portions copyright (C) 1999 Jason Ginchereau
  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 4304 2002-08-13 16:38:47Z 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[] files)
  41. {
  42. this.browser = browser;
  43. if(files != null)
  44. {
  45. this.files = files;
  46. VFS vfs = VFSManager.getVFSForPath(files[0].deletePath);
  47. int type = files[0].type;
  48. boolean delete = (vfs.getCapabilities() & VFS.DELETE_CAP) != 0;
  49. boolean rename = (vfs.getCapabilities() & VFS.RENAME_CAP) != 0;
  50. boolean canClose = (jEdit.getBuffer(files[0].path) != null);
  51. for(int i = 1; i < files.length; i++)
  52. {
  53. VFS.DirectoryEntry file = files[i];
  54. VFS _vfs = VFSManager.getVFSForPath(file.deletePath);
  55. delete &= (vfs == _vfs) && (_vfs.getCapabilities()
  56. & VFS.DELETE_CAP) != 0;
  57. if(type == file.type)
  58. /* all good */;
  59. else
  60. {
  61. // this will disable most operations if
  62. // files of multiple types are selected
  63. type = -1;
  64. }
  65. // set rename to false if > 1 file selected
  66. rename = false;
  67. // show 'close' item if at least one selected
  68. // file is currently open
  69. if(jEdit.getBuffer(file.path) != null)
  70. canClose = true;
  71. }
  72. if(type == VFS.DirectoryEntry.DIRECTORY
  73. || type == VFS.DirectoryEntry.FILESYSTEM)
  74. {
  75. if(files.length == 1)
  76. add(createMenuItem("browse"));
  77. if(browser.getMode() == VFSBrowser.BROWSER)
  78. add(createMenuItem("browse-window"));
  79. }
  80. else if(type == VFS.DirectoryEntry.FILE
  81. && (browser.getMode() == VFSBrowser.BROWSER
  82. || browser.getMode() == VFSBrowser.BROWSER_DIALOG))
  83. {
  84. add(createMenuItem("open"));
  85. JMenu openIn = new JMenu(jEdit.getProperty(
  86. "vfs.browser.commands.open-in.label"));
  87. openIn.add(createMenuItem("open-view"));
  88. openIn.add(createMenuItem("open-plain-view"));
  89. openIn.add(createMenuItem("open-split"));
  90. add(openIn);
  91. add(createMenuItem("insert"));
  92. if(canClose)
  93. add(createMenuItem("close"));
  94. }
  95. else if(type != -1)
  96. add(createMenuItem("choose"));
  97. if(rename)
  98. add(createMenuItem("rename"));
  99. if(delete)
  100. add(createMenuItem("delete"));
  101. addSeparator();
  102. }
  103. add(createMenuItem("up"));
  104. add(createMenuItem("reload"));
  105. add(createMenuItem("roots"));
  106. add(createMenuItem("home"));
  107. add(createMenuItem("synchronize"));
  108. addSeparator();
  109. if(browser.getMode() == VFSBrowser.BROWSER)
  110. add(createMenuItem("new-file"));
  111. add(createMenuItem("new-directory"));
  112. if(browser.getMode() == VFSBrowser.BROWSER)
  113. {
  114. addSeparator();
  115. add(createMenuItem("search-in-directory"));
  116. }
  117. addSeparator();
  118. showHiddenFiles = new JCheckBoxMenuItem(
  119. jEdit.getProperty("vfs.browser.commands.show-hidden-files.label"));
  120. showHiddenFiles.setActionCommand("show-hidden-files");
  121. showHiddenFiles.addActionListener(new ActionHandler());
  122. add(showHiddenFiles);
  123. if(browser.getMode() == VFSBrowser.BROWSER
  124. || browser.getMode() == VFSBrowser.BROWSER_DIALOG)
  125. {
  126. addSeparator();
  127. add(createEncodingMenu());
  128. }
  129. update();
  130. } //}}}
  131. //{{{ update() method
  132. public void update()
  133. {
  134. showHiddenFiles.setSelected(browser.getShowHiddenFiles());
  135. if(encodingMenuItems != null)
  136. {
  137. JRadioButtonMenuItem mi = (JRadioButtonMenuItem)
  138. encodingMenuItems.get(browser.currentEncoding);
  139. if(mi != null)
  140. {
  141. mi.setSelected(true);
  142. otherEncoding.setText(jEdit.getProperty(
  143. "vfs.browser.commands.other-encoding.label"));
  144. }
  145. else
  146. {
  147. otherEncoding.setSelected(true);
  148. otherEncoding.setText(jEdit.getProperty(
  149. "vfs.browser.commands.other-encoding-2.label",
  150. new String[] { browser.currentEncoding }));
  151. }
  152. }
  153. } //}}}
  154. //{{{ Private members
  155. private VFSBrowser browser;
  156. private VFS.DirectoryEntry[] files;
  157. private VFS vfs;
  158. private JCheckBoxMenuItem showHiddenFiles;
  159. private HashMap encodingMenuItems;
  160. private JRadioButtonMenuItem defaultEncoding;
  161. private JRadioButtonMenuItem otherEncoding;
  162. //{{{ createMenuItem() method
  163. private JMenuItem createMenuItem(String name)
  164. {
  165. String label = jEdit.getProperty("vfs.browser.commands." + name + ".label");
  166. JMenuItem mi = new JMenuItem(label);
  167. mi.setActionCommand(name);
  168. mi.addActionListener(new ActionHandler());
  169. return mi;
  170. } //}}}
  171. //{{{ createEncodingMenu() method
  172. private JMenu createEncodingMenu()
  173. {
  174. ActionHandler actionHandler = new ActionHandler();
  175. encodingMenuItems = new HashMap();
  176. JMenu encodingMenu = new JMenu(jEdit.getProperty(
  177. "vfs.browser.commands.encoding.label"));
  178. ButtonGroup grp = new ButtonGroup();
  179. StringTokenizer st = new StringTokenizer(
  180. jEdit.getProperty("encodings"));
  181. while(st.hasMoreTokens())
  182. {
  183. String encoding = st.nextToken();
  184. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
  185. mi.setActionCommand("encoding@" + encoding);
  186. mi.addActionListener(actionHandler);
  187. grp.add(mi);
  188. encodingMenuItems.put(encoding,mi);
  189. encodingMenu.add(mi);
  190. }
  191. String systemEncoding = System.getProperty("file.encoding");
  192. if(encodingMenuItems.get(systemEncoding) == null)
  193. {
  194. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
  195. systemEncoding);
  196. mi.setActionCommand("encoding@" + systemEncoding);
  197. mi.addActionListener(actionHandler);
  198. grp.add(mi);
  199. encodingMenuItems.put(systemEncoding,mi);
  200. encodingMenu.add(mi);
  201. }
  202. encodingMenu.addSeparator();
  203. otherEncoding = new JRadioButtonMenuItem();
  204. otherEncoding.setActionCommand("other-encoding");
  205. otherEncoding.addActionListener(actionHandler);
  206. grp.add(otherEncoding);
  207. encodingMenu.add(otherEncoding);
  208. return encodingMenu;
  209. } //}}}
  210. //}}}
  211. //{{{ ActionHandler class
  212. class ActionHandler implements ActionListener
  213. {
  214. public void actionPerformed(ActionEvent evt)
  215. {
  216. View view = browser.getView();
  217. String actionCommand = evt.getActionCommand();
  218. if(actionCommand.equals("other-encoding"))
  219. {
  220. String encoding = GUIUtilities.input(browser,
  221. "encoding-prompt",null,
  222. jEdit.getProperty("buffer.encoding",
  223. System.getProperty("file.encoding")));
  224. if(encoding == null)
  225. return;
  226. browser.currentEncoding = encoding;
  227. }
  228. else if(actionCommand.startsWith("encoding@"))
  229. {
  230. browser.currentEncoding = actionCommand.substring(9);
  231. }
  232. else if(actionCommand.equals("open"))
  233. browser.filesActivated(VFSBrowser.M_OPEN,false);
  234. else if(actionCommand.equals("open-view"))
  235. browser.filesActivated(VFSBrowser.M_OPEN_NEW_VIEW,false);
  236. else if(actionCommand.equals("open-plain-view"))
  237. browser.filesActivated(VFSBrowser.M_OPEN_NEW_PLAIN_VIEW,false);
  238. else if(actionCommand.equals("open-split"))
  239. browser.filesActivated(VFSBrowser.M_OPEN_NEW_SPLIT,false);
  240. else if(actionCommand.equals("insert"))
  241. {
  242. for(int i = 0; i < files.length; i++)
  243. {
  244. view.getBuffer().insertFile(view,files[i].path);
  245. }
  246. }
  247. else if(actionCommand.equals("choose"))
  248. browser.filesActivated(VFSBrowser.M_OPEN,false);
  249. else if(actionCommand.equals("close"))
  250. {
  251. for(int i = 0; i < files.length; i++)
  252. {
  253. Buffer buffer = jEdit.getBuffer(files[i].path);
  254. if(buffer != null)
  255. jEdit.closeBuffer(view,buffer);
  256. }
  257. }
  258. else if(actionCommand.equals("browse"))
  259. browser.setDirectory(files[0].path);
  260. else if(actionCommand.equals("browse-window"))
  261. {
  262. for(int i = 0; i < files.length; i++)
  263. {
  264. VFSBrowser.browseDirectoryInNewWindow(view,
  265. files[i].path);
  266. }
  267. }
  268. else if(actionCommand.equals("rename"))
  269. browser.rename(files[0].path);
  270. else if(actionCommand.equals("delete"))
  271. browser.delete(files);
  272. else if(actionCommand.equals("up"))
  273. {
  274. String path = browser.getDirectory();
  275. VFS vfs = VFSManager.getVFSForPath(path);
  276. browser.setDirectory(vfs.getParentOfPath(path));
  277. }
  278. else if(actionCommand.equals("reload"))
  279. browser.reloadDirectory();
  280. else if(actionCommand.equals("roots"))
  281. browser.rootDirectory();
  282. else if(actionCommand.equals("home"))
  283. browser.setDirectory(System.getProperty("user.home"));
  284. else if(actionCommand.equals("synchronize"))
  285. {
  286. Buffer buffer = browser.getView().getBuffer();
  287. browser.setDirectory(buffer.getVFS().getParentOfPath(
  288. buffer.getPath()));
  289. }
  290. else if(actionCommand.equals("new-file"))
  291. browser.newFile();
  292. else if(actionCommand.equals("new-directory"))
  293. browser.mkdir();
  294. else if(actionCommand.equals("search-in-directory"))
  295. browser.searchInDirectory();
  296. else if(actionCommand.equals("show-hidden-files"))
  297. {
  298. browser.setShowHiddenFiles(!browser.getShowHiddenFiles());
  299. browser.reloadDirectory();
  300. }
  301. }
  302. } //}}}
  303. }