PageRenderTime 93ms CodeModel.GetById 63ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

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