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

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/browser/BrowserCommandsMenu.java

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