PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 254 lines | 178 code | 36 blank | 40 comment | 55 complexity | 351a4eef281d4938cd58b808f8b11581 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 4725 2003-05-25 21:58:28Z 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 JRadioButtonMenuItem defaultEncoding;
  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. ButtonGroup grp = new ButtonGroup();
  163. StringTokenizer st = new StringTokenizer(
  164. jEdit.getProperty("encodings"));
  165. while(st.hasMoreTokens())
  166. {
  167. String encoding = st.nextToken();
  168. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
  169. mi.setActionCommand("encoding@" + encoding);
  170. mi.addActionListener(actionHandler);
  171. grp.add(mi);
  172. encodingMenuItems.put(encoding,mi);
  173. encodingMenu.add(mi);
  174. }
  175. String systemEncoding = System.getProperty("file.encoding");
  176. if(encodingMenuItems.get(systemEncoding) == null)
  177. {
  178. JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
  179. systemEncoding);
  180. mi.setActionCommand("encoding@" + systemEncoding);
  181. mi.addActionListener(actionHandler);
  182. grp.add(mi);
  183. encodingMenuItems.put(systemEncoding,mi);
  184. encodingMenu.add(mi);
  185. }
  186. encodingMenu.addSeparator();
  187. otherEncoding = new JRadioButtonMenuItem();
  188. otherEncoding.setActionCommand("other-encoding");
  189. otherEncoding.addActionListener(actionHandler);
  190. grp.add(otherEncoding);
  191. encodingMenu.add(otherEncoding);
  192. return encodingMenu;
  193. } //}}}
  194. //}}}
  195. //{{{ ActionHandler class
  196. class ActionHandler implements ActionListener
  197. {
  198. public void actionPerformed(ActionEvent evt)
  199. {
  200. View view = browser.getView();
  201. String actionCommand = evt.getActionCommand();
  202. if(actionCommand.equals("other-encoding"))
  203. {
  204. String encoding = GUIUtilities.input(browser,
  205. "encoding-prompt",null,
  206. jEdit.getProperty("buffer.encoding",
  207. System.getProperty("file.encoding")));
  208. if(encoding == null)
  209. return;
  210. browser.currentEncoding = encoding;
  211. }
  212. else if(actionCommand.startsWith("encoding@"))
  213. {
  214. browser.currentEncoding = actionCommand.substring(9);
  215. }
  216. }
  217. } //}}}
  218. }