/jEdit/tags/jedit-4-3-pre16/org/gjt/sp/jedit/browser/BrowserCommandsMenu.java

# · Java · 314 lines · 224 code · 47 blank · 43 comment · 60 complexity · 9e687a5728992e8356916b4aae127e55 MD5 · raw file

  1. /*
  2. * BrowserCommandsMenu.java - provides various commands
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2003 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.event.*;
  26. import java.util.*;
  27. import javax.swing.*;
  28. import org.gjt.sp.jedit.io.*;
  29. import org.gjt.sp.jedit.*;
  30. //}}}
  31. /**
  32. * @version $Id: BrowserCommandsMenu.java 12787 2008-06-04 21:14:31Z ezust $
  33. * @author Slava Pestov and Jason Ginchereau
  34. */
  35. public class BrowserCommandsMenu extends JPopupMenu
  36. {
  37. //{{{ BrowserCommandsMenu constructor
  38. public BrowserCommandsMenu(VFSBrowser browser, VFSFile[] files)
  39. {
  40. this.browser = browser;
  41. if(files != null)
  42. {
  43. VFS vfs = VFSManager.getVFSForPath(
  44. files[0].getDeletePath());
  45. int type = files[0].getType();
  46. boolean fileOpen = (jEdit.getBuffer(files[0].getPath()) != null);
  47. /* We check this flag separately so that we can
  48. delete open files from the favorites. */
  49. boolean deletePathOpen = (jEdit.getBuffer(files[0].getDeletePath()) != null);
  50. boolean delete = !deletePathOpen
  51. && (vfs.getCapabilities()
  52. & VFS.DELETE_CAP) != 0;
  53. boolean rename = !fileOpen
  54. && (vfs.getCapabilities()
  55. & VFS.RENAME_CAP) != 0;
  56. for(int i = 1; i < files.length; i++)
  57. {
  58. VFSFile file = files[i];
  59. VFS _vfs = VFSManager.getVFSForPath(file.getDeletePath());
  60. delete &= (vfs == _vfs) && (_vfs.getCapabilities()
  61. & VFS.DELETE_CAP) != 0;
  62. if(type == file.getType())
  63. /* all good */;
  64. else
  65. {
  66. // this will disable most operations if
  67. // files of multiple types are selected
  68. type = -1;
  69. }
  70. // set rename to false if > 1 file selected
  71. rename = false;
  72. // show 'close' item if at least one selected
  73. // file is currently open
  74. if(jEdit.getBuffer(file.getPath()) != null)
  75. fileOpen = true;
  76. }
  77. if(type == VFSFile.DIRECTORY
  78. || type == VFSFile.FILESYSTEM)
  79. {
  80. if(files.length == 1)
  81. add(createMenuItem("browse"));
  82. if(browser.getMode() == VFSBrowser.BROWSER)
  83. add(createMenuItem("browse-window"));
  84. }
  85. else if(type == VFSFile.FILE
  86. && (browser.getMode() == VFSBrowser.BROWSER
  87. || browser.getMode() == VFSBrowser.BROWSER_DIALOG))
  88. {
  89. add(createMenuItem("open"));
  90. add(GUIUtilities.loadMenu(
  91. VFSBrowser.getActionContext(),
  92. "vfs.browser.open-in"));
  93. add(createMenuItem("insert"));
  94. if(fileOpen)
  95. add(createMenuItem("close"));
  96. }
  97. else if(type != -1)
  98. add(createMenuItem("open"));
  99. if(rename)
  100. add(createMenuItem("rename"));
  101. if(delete)
  102. add(createMenuItem("delete"));
  103. add(createMenuItem("copy-path"));
  104. add(createMenuItem("paste"));
  105. addSeparator();
  106. }
  107. add(createMenuItem("up"));
  108. add(createMenuItem("previous"));
  109. add(createMenuItem("next"));
  110. add(createMenuItem("reload"));
  111. add(createMenuItem("roots"));
  112. add(createMenuItem("home"));
  113. add(createMenuItem("synchronize"));
  114. addSeparator();
  115. if(browser.getMode() == VFSBrowser.BROWSER)
  116. add(createMenuItem("new-file"));
  117. add(createMenuItem("new-directory"));
  118. if(browser.getMode() == VFSBrowser.BROWSER)
  119. {
  120. addSeparator();
  121. add(createMenuItem("search-directory"));
  122. }
  123. addSeparator();
  124. add(createMenuItem("show-hidden-files"));
  125. if(browser.getMode() == VFSBrowser.BROWSER
  126. || browser.getMode() == VFSBrowser.BROWSER_DIALOG)
  127. {
  128. addSeparator();
  129. add(createEncodingMenu());
  130. }
  131. addSeparator();
  132. add(createPluginMenu(browser));
  133. update();
  134. } //}}}
  135. //{{{ update() method
  136. public void update()
  137. {
  138. if(encodingMenuItems != null)
  139. {
  140. JRadioButtonMenuItem mi = (JRadioButtonMenuItem)
  141. encodingMenuItems.get(browser.currentEncoding);
  142. if(mi != null)
  143. {
  144. mi.setSelected(true);
  145. otherEncoding.setText(jEdit.getProperty(
  146. "vfs.browser.other-encoding.label"));
  147. }
  148. else
  149. {
  150. otherEncoding.setSelected(true);
  151. otherEncoding.setText(jEdit.getProperty(
  152. "vfs.browser.other-encoding-2.label",
  153. new String[] { browser.currentEncoding }));
  154. }
  155. }
  156. } //}}}
  157. //{{{ Private members
  158. private VFSBrowser browser;
  159. private HashMap encodingMenuItems;
  160. private JCheckBoxMenuItem autoDetect;
  161. private JRadioButtonMenuItem otherEncoding;
  162. //{{{ createMenuItem() method
  163. private JMenuItem createMenuItem(String name)
  164. {
  165. return GUIUtilities.loadMenuItem(VFSBrowser.getActionContext(),
  166. "vfs.browser." + name,false);
  167. } //}}}
  168. //{{{ createEncodingMenu() method
  169. private JMenu createEncodingMenu()
  170. {
  171. ActionHandler actionHandler = new ActionHandler();
  172. encodingMenuItems = new HashMap();
  173. JMenu encodingMenu = new JMenu(jEdit.getProperty(
  174. "vfs.browser.commands.encoding.label"));
  175. JMenu menu = encodingMenu;
  176. autoDetect = new JCheckBoxMenuItem(
  177. jEdit.getProperty(
  178. "vfs.browser.commands.encoding.auto-detect"));
  179. autoDetect.setSelected(browser.autoDetectEncoding);
  180. autoDetect.setActionCommand("auto-detect");
  181. autoDetect.addActionListener(actionHandler);
  182. menu.add(autoDetect);
  183. menu.addSeparator();
  184. ButtonGroup grp = new ButtonGroup();
  185. List encodingMenuItemList = new ArrayList();
  186. String[] encodings = MiscUtilities.getEncodings(true);
  187. for(int i = 0; i < encodings.length; i++)
  188. {
  189. String encoding = encodings[i];
  190. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
  191. mi.setActionCommand("encoding@" + encoding);
  192. mi.addActionListener(actionHandler);
  193. grp.add(mi);
  194. encodingMenuItems.put(encoding,mi);
  195. encodingMenuItemList.add(mi);
  196. }
  197. String systemEncoding = System.getProperty("file.encoding");
  198. if(encodingMenuItems.get(systemEncoding) == null)
  199. {
  200. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
  201. systemEncoding);
  202. mi.setActionCommand("encoding@" + systemEncoding);
  203. mi.addActionListener(actionHandler);
  204. grp.add(mi);
  205. encodingMenuItems.put(systemEncoding,mi);
  206. encodingMenuItemList.add(mi);
  207. }
  208. Collections.sort(encodingMenuItemList,
  209. new MiscUtilities.MenuItemCompare());
  210. Iterator iter = encodingMenuItemList.iterator();
  211. while(iter.hasNext())
  212. {
  213. JRadioButtonMenuItem mi = (JRadioButtonMenuItem)
  214. iter.next();
  215. if(menu.getMenuComponentCount() > 20)
  216. {
  217. JMenu newMenu = new JMenu(
  218. jEdit.getProperty("common.more"));
  219. menu.add(newMenu);
  220. menu = newMenu;
  221. }
  222. menu.add(mi);
  223. }
  224. menu.addSeparator();
  225. otherEncoding = new JRadioButtonMenuItem();
  226. otherEncoding.setActionCommand("other-encoding");
  227. otherEncoding.addActionListener(actionHandler);
  228. grp.add(otherEncoding);
  229. menu.add(otherEncoding);
  230. return encodingMenu;
  231. } //}}}
  232. //{{{ createPluginsMenu() method
  233. private JMenu createPluginMenu(VFSBrowser browser)
  234. {
  235. JMenu pluginMenu = new JMenu(jEdit.getProperty(
  236. "vfs.browser.plugins.label"));
  237. return (JMenu)browser.createPluginsMenu(pluginMenu,false);
  238. } //}}}
  239. //}}}
  240. //{{{ ActionHandler class
  241. class ActionHandler implements ActionListener
  242. {
  243. public void actionPerformed(ActionEvent evt)
  244. {
  245. String actionCommand = evt.getActionCommand();
  246. if(actionCommand.equals("auto-detect"))
  247. {
  248. browser.autoDetectEncoding
  249. = autoDetect.isSelected();
  250. }
  251. else if(actionCommand.equals("other-encoding"))
  252. {
  253. String encoding = GUIUtilities.input(browser,
  254. "encoding-prompt",null,
  255. jEdit.getProperty("buffer.encoding",
  256. System.getProperty("file.encoding")));
  257. if(encoding == null)
  258. return;
  259. browser.currentEncoding = encoding;
  260. }
  261. else if(actionCommand.startsWith("encoding@"))
  262. {
  263. browser.currentEncoding = actionCommand.substring(9);
  264. }
  265. }
  266. } //}}}
  267. }