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