PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/browser/BrowserCommandsMenu.java

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