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

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

#
Java | 311 lines | 221 code | 47 blank | 43 comment | 60 complexity | 105f1e964f3b26b747adbe62ca653073 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.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 5217 2005-04-23 20:29:12Z spestov $
  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. addSeparator();
  105. }
  106. add(createMenuItem("up"));
  107. add(createMenuItem("reload"));
  108. add(createMenuItem("roots"));
  109. add(createMenuItem("home"));
  110. add(createMenuItem("synchronize"));
  111. addSeparator();
  112. if(browser.getMode() == VFSBrowser.BROWSER)
  113. add(createMenuItem("new-file"));
  114. add(createMenuItem("new-directory"));
  115. if(browser.getMode() == VFSBrowser.BROWSER)
  116. {
  117. addSeparator();
  118. add(createMenuItem("search-directory"));
  119. }
  120. addSeparator();
  121. add(createMenuItem("show-hidden-files"));
  122. if(browser.getMode() == VFSBrowser.BROWSER
  123. || browser.getMode() == VFSBrowser.BROWSER_DIALOG)
  124. {
  125. addSeparator();
  126. add(createEncodingMenu());
  127. }
  128. addSeparator();
  129. add(createPluginMenu(browser));
  130. update();
  131. } //}}}
  132. //{{{ update() method
  133. public void update()
  134. {
  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.other-encoding.label"));
  144. }
  145. else
  146. {
  147. otherEncoding.setSelected(true);
  148. otherEncoding.setText(jEdit.getProperty(
  149. "vfs.browser.other-encoding-2.label",
  150. new String[] { browser.currentEncoding }));
  151. }
  152. }
  153. } //}}}
  154. //{{{ Private members
  155. private VFSBrowser browser;
  156. private HashMap encodingMenuItems;
  157. private JCheckBoxMenuItem autoDetect;
  158. private JRadioButtonMenuItem otherEncoding;
  159. //{{{ createMenuItem() method
  160. private JMenuItem createMenuItem(String name)
  161. {
  162. return GUIUtilities.loadMenuItem(VFSBrowser.getActionContext(),
  163. "vfs.browser." + name,false);
  164. } //}}}
  165. //{{{ createEncodingMenu() method
  166. private JMenu createEncodingMenu()
  167. {
  168. ActionHandler actionHandler = new ActionHandler();
  169. encodingMenuItems = new HashMap();
  170. JMenu encodingMenu = new JMenu(jEdit.getProperty(
  171. "vfs.browser.commands.encoding.label"));
  172. JMenu menu = encodingMenu;
  173. autoDetect = new JCheckBoxMenuItem(
  174. jEdit.getProperty(
  175. "vfs.browser.commands.encoding.auto-detect"));
  176. autoDetect.setSelected(browser.autoDetectEncoding);
  177. autoDetect.setActionCommand("auto-detect");
  178. autoDetect.addActionListener(actionHandler);
  179. menu.add(autoDetect);
  180. menu.addSeparator();
  181. ButtonGroup grp = new ButtonGroup();
  182. List encodingMenuItemList = new ArrayList();
  183. String[] encodings = MiscUtilities.getEncodings();
  184. for(int i = 0; i < encodings.length; i++)
  185. {
  186. String encoding = encodings[i];
  187. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
  188. mi.setActionCommand("encoding@" + encoding);
  189. mi.addActionListener(actionHandler);
  190. grp.add(mi);
  191. encodingMenuItems.put(encoding,mi);
  192. encodingMenuItemList.add(mi);
  193. }
  194. String systemEncoding = System.getProperty("file.encoding");
  195. if(encodingMenuItems.get(systemEncoding) == null)
  196. {
  197. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
  198. systemEncoding);
  199. mi.setActionCommand("encoding@" + systemEncoding);
  200. mi.addActionListener(actionHandler);
  201. grp.add(mi);
  202. encodingMenuItems.put(systemEncoding,mi);
  203. encodingMenuItemList.add(mi);
  204. }
  205. Collections.sort(encodingMenuItemList,
  206. new MiscUtilities.MenuItemCompare());
  207. Iterator iter = encodingMenuItemList.iterator();
  208. while(iter.hasNext())
  209. {
  210. JRadioButtonMenuItem mi = (JRadioButtonMenuItem)
  211. iter.next();
  212. if(menu.getMenuComponentCount() > 20)
  213. {
  214. JMenu newMenu = new JMenu(
  215. jEdit.getProperty("common.more"));
  216. menu.add(newMenu);
  217. menu = newMenu;
  218. }
  219. menu.add(mi);
  220. }
  221. menu.addSeparator();
  222. otherEncoding = new JRadioButtonMenuItem();
  223. otherEncoding.setActionCommand("other-encoding");
  224. otherEncoding.addActionListener(actionHandler);
  225. grp.add(otherEncoding);
  226. menu.add(otherEncoding);
  227. return encodingMenu;
  228. } //}}}
  229. //{{{ createPluginsMenu() method
  230. private JMenu createPluginMenu(VFSBrowser browser)
  231. {
  232. JMenu pluginMenu = new JMenu(jEdit.getProperty(
  233. "vfs.browser.plugins.label"));
  234. return (JMenu)browser.createPluginsMenu(pluginMenu,false);
  235. } //}}}
  236. //}}}
  237. //{{{ ActionHandler class
  238. class ActionHandler implements ActionListener
  239. {
  240. public void actionPerformed(ActionEvent evt)
  241. {
  242. String actionCommand = evt.getActionCommand();
  243. if(actionCommand.equals("auto-detect"))
  244. {
  245. browser.autoDetectEncoding
  246. = autoDetect.isSelected();
  247. }
  248. else if(actionCommand.equals("other-encoding"))
  249. {
  250. String encoding = GUIUtilities.input(browser,
  251. "encoding-prompt",null,
  252. jEdit.getProperty("buffer.encoding",
  253. System.getProperty("file.encoding")));
  254. if(encoding == null)
  255. return;
  256. browser.currentEncoding = encoding;
  257. }
  258. else if(actionCommand.startsWith("encoding@"))
  259. {
  260. browser.currentEncoding = actionCommand.substring(9);
  261. }
  262. }
  263. } //}}}
  264. }