001: /*
002: * BrowserCommandsMenu.java - provides various commands
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2000, 2003 Slava Pestov
007: * Portions copyright (C) 1999 Jason Ginchereau
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit.browser;
025:
026: //{{{ Imports
027: import java.awt.event.*;
028: import java.util.*;
029: import javax.swing.*;
030:
031: import org.gjt.sp.jedit.io.*;
032: import org.gjt.sp.jedit.*;
033:
034: //}}}
035:
036: /**
037: * @version $Id: BrowserCommandsMenu.java 5533 2006-07-05 19:05:44Z vampire0 $
038: * @author Slava Pestov and Jason Ginchereau
039: */
040: public class BrowserCommandsMenu extends JPopupMenu {
041: //{{{ BrowserCommandsMenu constructor
042: public BrowserCommandsMenu(VFSBrowser browser, VFSFile[] files) {
043: this .browser = browser;
044:
045: if (files != null) {
046: VFS vfs = VFSManager
047: .getVFSForPath(files[0].getDeletePath());
048: int type = files[0].getType();
049:
050: boolean fileOpen = (jEdit.getBuffer(files[0].getPath()) != null);
051:
052: /* We check this flag separately so that we can
053: delete open files from the favorites. */
054: boolean deletePathOpen = (jEdit.getBuffer(files[0]
055: .getDeletePath()) != null);
056:
057: boolean delete = !deletePathOpen
058: && (vfs.getCapabilities() & VFS.DELETE_CAP) != 0;
059: boolean rename = !fileOpen
060: && (vfs.getCapabilities() & VFS.RENAME_CAP) != 0;
061:
062: for (int i = 1; i < files.length; i++) {
063: VFSFile file = files[i];
064:
065: VFS _vfs = VFSManager.getVFSForPath(file
066: .getDeletePath());
067: delete &= (vfs == _vfs)
068: && (_vfs.getCapabilities() & VFS.DELETE_CAP) != 0;
069:
070: if (type == file.getType())
071: /* all good */;
072: else {
073: // this will disable most operations if
074: // files of multiple types are selected
075: type = -1;
076: }
077:
078: // set rename to false if > 1 file selected
079: rename = false;
080:
081: // show 'close' item if at least one selected
082: // file is currently open
083: if (jEdit.getBuffer(file.getPath()) != null)
084: fileOpen = true;
085: }
086:
087: if (type == VFSFile.DIRECTORY || type == VFSFile.FILESYSTEM) {
088: if (files.length == 1)
089: add(createMenuItem("browse"));
090: if (browser.getMode() == VFSBrowser.BROWSER)
091: add(createMenuItem("browse-window"));
092: } else if (type == VFSFile.FILE
093: && (browser.getMode() == VFSBrowser.BROWSER || browser
094: .getMode() == VFSBrowser.BROWSER_DIALOG)) {
095: add(createMenuItem("open"));
096: add(GUIUtilities.loadMenu(
097: VFSBrowser.getActionContext(),
098: "vfs.browser.open-in"));
099: add(createMenuItem("insert"));
100:
101: if (fileOpen)
102: add(createMenuItem("close"));
103: } else if (type != -1)
104: add(createMenuItem("open"));
105:
106: if (rename)
107: add(createMenuItem("rename"));
108:
109: if (delete)
110: add(createMenuItem("delete"));
111:
112: add(createMenuItem("copy-path"));
113: addSeparator();
114: }
115:
116: add(createMenuItem("up"));
117: add(createMenuItem("reload"));
118: add(createMenuItem("roots"));
119: add(createMenuItem("home"));
120: add(createMenuItem("synchronize"));
121: addSeparator();
122:
123: if (browser.getMode() == VFSBrowser.BROWSER)
124: add(createMenuItem("new-file"));
125:
126: add(createMenuItem("new-directory"));
127:
128: if (browser.getMode() == VFSBrowser.BROWSER) {
129: addSeparator();
130: add(createMenuItem("search-directory"));
131: }
132:
133: addSeparator();
134:
135: add(createMenuItem("show-hidden-files"));
136:
137: if (browser.getMode() == VFSBrowser.BROWSER
138: || browser.getMode() == VFSBrowser.BROWSER_DIALOG) {
139: addSeparator();
140: add(createEncodingMenu());
141: }
142: addSeparator();
143: add(createPluginMenu(browser));
144: update();
145: } //}}}
146:
147: //{{{ update() method
148: public void update() {
149: if (encodingMenuItems != null) {
150: JRadioButtonMenuItem mi = (JRadioButtonMenuItem) encodingMenuItems
151: .get(browser.currentEncoding);
152: if (mi != null) {
153: mi.setSelected(true);
154: otherEncoding
155: .setText(jEdit
156: .getProperty("vfs.browser.other-encoding.label"));
157: } else {
158: otherEncoding.setSelected(true);
159: otherEncoding.setText(jEdit.getProperty(
160: "vfs.browser.other-encoding-2.label",
161: new String[] { browser.currentEncoding }));
162: }
163: }
164: } //}}}
165:
166: //{{{ Private members
167: private VFSBrowser browser;
168: private HashMap encodingMenuItems;
169: private JCheckBoxMenuItem autoDetect;
170: private JRadioButtonMenuItem otherEncoding;
171:
172: //{{{ createMenuItem() method
173: private JMenuItem createMenuItem(String name) {
174: return GUIUtilities.loadMenuItem(VFSBrowser.getActionContext(),
175: "vfs.browser." + name, false);
176: } //}}}
177:
178: //{{{ createEncodingMenu() method
179: private JMenu createEncodingMenu() {
180: ActionHandler actionHandler = new ActionHandler();
181:
182: encodingMenuItems = new HashMap();
183: JMenu encodingMenu = new JMenu(jEdit
184: .getProperty("vfs.browser.commands.encoding.label"));
185:
186: JMenu menu = encodingMenu;
187:
188: autoDetect = new JCheckBoxMenuItem(
189: jEdit
190: .getProperty("vfs.browser.commands.encoding.auto-detect"));
191: autoDetect.setSelected(browser.autoDetectEncoding);
192: autoDetect.setActionCommand("auto-detect");
193: autoDetect.addActionListener(actionHandler);
194: menu.add(autoDetect);
195: menu.addSeparator();
196:
197: ButtonGroup grp = new ButtonGroup();
198:
199: List encodingMenuItemList = new ArrayList();
200: String[] encodings = MiscUtilities.getEncodings(true);
201: for (int i = 0; i < encodings.length; i++) {
202: String encoding = encodings[i];
203: JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
204: mi.setActionCommand("encoding@" + encoding);
205: mi.addActionListener(actionHandler);
206: grp.add(mi);
207: encodingMenuItems.put(encoding, mi);
208: encodingMenuItemList.add(mi);
209: }
210:
211: String systemEncoding = System.getProperty("file.encoding");
212: if (encodingMenuItems.get(systemEncoding) == null) {
213: JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
214: systemEncoding);
215: mi.setActionCommand("encoding@" + systemEncoding);
216: mi.addActionListener(actionHandler);
217: grp.add(mi);
218: encodingMenuItems.put(systemEncoding, mi);
219: encodingMenuItemList.add(mi);
220: }
221:
222: Collections.sort(encodingMenuItemList,
223: new MiscUtilities.MenuItemCompare());
224:
225: Iterator iter = encodingMenuItemList.iterator();
226: while (iter.hasNext()) {
227: JRadioButtonMenuItem mi = (JRadioButtonMenuItem) iter
228: .next();
229:
230: if (menu.getMenuComponentCount() > 20) {
231: JMenu newMenu = new JMenu(jEdit
232: .getProperty("common.more"));
233: menu.add(newMenu);
234: menu = newMenu;
235: }
236:
237: menu.add(mi);
238: }
239: menu.addSeparator();
240:
241: otherEncoding = new JRadioButtonMenuItem();
242: otherEncoding.setActionCommand("other-encoding");
243: otherEncoding.addActionListener(actionHandler);
244: grp.add(otherEncoding);
245: menu.add(otherEncoding);
246:
247: return encodingMenu;
248: } //}}}
249:
250: //{{{ createPluginsMenu() method
251: private JMenu createPluginMenu(VFSBrowser browser) {
252: JMenu pluginMenu = new JMenu(jEdit
253: .getProperty("vfs.browser.plugins.label"));
254: return (JMenu) browser.createPluginsMenu(pluginMenu, false);
255:
256: } //}}}
257:
258: //}}}
259:
260: //{{{ ActionHandler class
261: class ActionHandler implements ActionListener {
262: public void actionPerformed(ActionEvent evt) {
263: String actionCommand = evt.getActionCommand();
264:
265: if (actionCommand.equals("auto-detect")) {
266: browser.autoDetectEncoding = autoDetect.isSelected();
267: } else if (actionCommand.equals("other-encoding")) {
268: String encoding = GUIUtilities.input(browser,
269: "encoding-prompt", null, jEdit.getProperty(
270: "buffer.encoding", System
271: .getProperty("file.encoding")));
272: if (encoding == null)
273: return;
274: browser.currentEncoding = encoding;
275: } else if (actionCommand.startsWith("encoding@")) {
276: browser.currentEncoding = actionCommand.substring(9);
277: }
278: }
279: } //}}}
280: }
|