PageRenderTime 137ms CodeModel.GetById 108ms app.highlight 25ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/browser/BrowserCommandsMenu.java

#
Java | 340 lines | 263 code | 37 blank | 40 comment | 98 complexity | 8ae5b376dae9006cbf69817cebff2d6c 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, 2001, 2002 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 javax.swing.*;
 31import javax.swing.event.*;
 32
 33import org.gjt.sp.jedit.io.*;
 34import org.gjt.sp.jedit.*;
 35//}}}
 36
 37/**
 38 * @version $Id: BrowserCommandsMenu.java 4304 2002-08-13 16:38:47Z spestov $
 39 * @author Slava Pestov and Jason Ginchereau
 40 */
 41public class BrowserCommandsMenu extends JPopupMenu
 42{
 43	//{{{ BrowserCommandsMenu constructor
 44	public BrowserCommandsMenu(VFSBrowser browser, VFS.DirectoryEntry[] files)
 45	{
 46		this.browser = browser;
 47
 48		if(files != null)
 49		{
 50			this.files = files;
 51
 52			VFS vfs = VFSManager.getVFSForPath(files[0].deletePath);
 53			int type = files[0].type;
 54			boolean delete = (vfs.getCapabilities() & VFS.DELETE_CAP) != 0;
 55			boolean rename = (vfs.getCapabilities() & VFS.RENAME_CAP) != 0;
 56			boolean canClose = (jEdit.getBuffer(files[0].path) != null);
 57
 58			for(int i = 1; i < files.length; i++)
 59			{
 60				VFS.DirectoryEntry file = files[i];
 61
 62				VFS _vfs = VFSManager.getVFSForPath(file.deletePath);
 63				delete &= (vfs == _vfs) && (_vfs.getCapabilities()
 64					& VFS.DELETE_CAP) != 0;
 65
 66				if(type == file.type)
 67					/* all good */;
 68				else
 69				{
 70					// this will disable most operations if
 71					// files of multiple types are selected
 72					type = -1;
 73				}
 74
 75				// set rename to false if > 1 file selected
 76				rename = false;
 77
 78				// show 'close' item if at least one selected
 79				// file is currently open
 80				if(jEdit.getBuffer(file.path) != null)
 81					canClose = true;
 82			}
 83
 84			if(type == VFS.DirectoryEntry.DIRECTORY
 85				|| type == VFS.DirectoryEntry.FILESYSTEM)
 86			{
 87				if(files.length == 1)
 88					add(createMenuItem("browse"));
 89				if(browser.getMode() == VFSBrowser.BROWSER)
 90					add(createMenuItem("browse-window"));
 91			}
 92			else if(type == VFS.DirectoryEntry.FILE
 93				&& (browser.getMode() == VFSBrowser.BROWSER
 94				|| browser.getMode() == VFSBrowser.BROWSER_DIALOG))
 95			{
 96				add(createMenuItem("open"));
 97				JMenu openIn = new JMenu(jEdit.getProperty(
 98					"vfs.browser.commands.open-in.label"));
 99				openIn.add(createMenuItem("open-view"));
100				openIn.add(createMenuItem("open-plain-view"));
101				openIn.add(createMenuItem("open-split"));
102				add(openIn);
103				add(createMenuItem("insert"));
104
105				if(canClose)
106					add(createMenuItem("close"));
107			}
108			else if(type != -1)
109				add(createMenuItem("choose"));
110
111			if(rename)
112				add(createMenuItem("rename"));
113			if(delete)
114				add(createMenuItem("delete"));
115
116			addSeparator();
117		}
118
119		add(createMenuItem("up"));
120		add(createMenuItem("reload"));
121		add(createMenuItem("roots"));
122		add(createMenuItem("home"));
123		add(createMenuItem("synchronize"));
124		addSeparator();
125
126		if(browser.getMode() == VFSBrowser.BROWSER)
127			add(createMenuItem("new-file"));
128
129		add(createMenuItem("new-directory"));
130
131		if(browser.getMode() == VFSBrowser.BROWSER)
132		{
133			addSeparator();
134			add(createMenuItem("search-in-directory"));
135		}
136
137		addSeparator();
138
139		showHiddenFiles = new JCheckBoxMenuItem(
140			jEdit.getProperty("vfs.browser.commands.show-hidden-files.label"));
141		showHiddenFiles.setActionCommand("show-hidden-files");
142		showHiddenFiles.addActionListener(new ActionHandler());
143		add(showHiddenFiles);
144
145		if(browser.getMode() == VFSBrowser.BROWSER
146			|| browser.getMode() == VFSBrowser.BROWSER_DIALOG)
147		{
148			addSeparator();
149			add(createEncodingMenu());
150		}
151
152		update();
153	} //}}}
154
155	//{{{ update() method
156	public void update()
157	{
158		showHiddenFiles.setSelected(browser.getShowHiddenFiles());
159		if(encodingMenuItems != null)
160		{
161			JRadioButtonMenuItem mi = (JRadioButtonMenuItem)
162				encodingMenuItems.get(browser.currentEncoding);
163			if(mi != null)
164			{
165				mi.setSelected(true);
166				otherEncoding.setText(jEdit.getProperty(
167					"vfs.browser.commands.other-encoding.label"));
168			}
169			else
170			{
171				otherEncoding.setSelected(true);
172				otherEncoding.setText(jEdit.getProperty(
173					"vfs.browser.commands.other-encoding-2.label",
174					new String[] { browser.currentEncoding }));
175			}
176		}
177	} //}}}
178
179	//{{{ Private members
180	private VFSBrowser browser;
181	private VFS.DirectoryEntry[] files;
182	private VFS vfs;
183	private JCheckBoxMenuItem showHiddenFiles;
184	private HashMap encodingMenuItems;
185	private JRadioButtonMenuItem defaultEncoding;
186	private JRadioButtonMenuItem otherEncoding;
187
188	//{{{ createMenuItem() method
189	private JMenuItem createMenuItem(String name)
190	{
191		String label = jEdit.getProperty("vfs.browser.commands." + name + ".label");
192		JMenuItem mi = new JMenuItem(label);
193		mi.setActionCommand(name);
194		mi.addActionListener(new ActionHandler());
195		return mi;
196	} //}}}
197
198	//{{{ createEncodingMenu() method
199	private JMenu createEncodingMenu()
200	{
201		ActionHandler actionHandler = new ActionHandler();
202
203		encodingMenuItems = new HashMap();
204		JMenu encodingMenu = new JMenu(jEdit.getProperty(
205			"vfs.browser.commands.encoding.label"));
206
207		ButtonGroup grp = new ButtonGroup();
208
209		StringTokenizer st = new StringTokenizer(
210			jEdit.getProperty("encodings"));
211		while(st.hasMoreTokens())
212		{
213			String encoding = st.nextToken();
214			JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
215			mi.setActionCommand("encoding@" + encoding);
216			mi.addActionListener(actionHandler);
217			grp.add(mi);
218			encodingMenuItems.put(encoding,mi);
219			encodingMenu.add(mi);
220		}
221
222		String systemEncoding = System.getProperty("file.encoding");
223		if(encodingMenuItems.get(systemEncoding) == null)
224		{
225			JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
226				systemEncoding);
227			mi.setActionCommand("encoding@" + systemEncoding);
228			mi.addActionListener(actionHandler);
229			grp.add(mi);
230			encodingMenuItems.put(systemEncoding,mi);
231			encodingMenu.add(mi);
232		}
233
234		encodingMenu.addSeparator();
235
236		otherEncoding = new JRadioButtonMenuItem();
237		otherEncoding.setActionCommand("other-encoding");
238		otherEncoding.addActionListener(actionHandler);
239		grp.add(otherEncoding);
240		encodingMenu.add(otherEncoding);
241
242		return encodingMenu;
243	} //}}}
244
245	//}}}
246
247	//{{{ ActionHandler class
248	class ActionHandler implements ActionListener
249	{
250		public void actionPerformed(ActionEvent evt)
251		{
252			View view = browser.getView();
253			String actionCommand = evt.getActionCommand();
254
255			if(actionCommand.equals("other-encoding"))
256			{
257				String encoding = GUIUtilities.input(browser,
258					"encoding-prompt",null,
259					jEdit.getProperty("buffer.encoding",
260					System.getProperty("file.encoding")));
261				if(encoding == null)
262					return;
263				browser.currentEncoding = encoding;
264			}
265			else if(actionCommand.startsWith("encoding@"))
266			{
267				browser.currentEncoding = actionCommand.substring(9);
268			}
269			else if(actionCommand.equals("open"))
270				browser.filesActivated(VFSBrowser.M_OPEN,false);
271			else if(actionCommand.equals("open-view"))
272				browser.filesActivated(VFSBrowser.M_OPEN_NEW_VIEW,false);
273			else if(actionCommand.equals("open-plain-view"))
274				browser.filesActivated(VFSBrowser.M_OPEN_NEW_PLAIN_VIEW,false);
275			else if(actionCommand.equals("open-split"))
276				browser.filesActivated(VFSBrowser.M_OPEN_NEW_SPLIT,false);
277			else if(actionCommand.equals("insert"))
278			{
279				for(int i = 0; i < files.length; i++)
280				{
281					view.getBuffer().insertFile(view,files[i].path);
282				}
283			}
284			else if(actionCommand.equals("choose"))
285				browser.filesActivated(VFSBrowser.M_OPEN,false);
286			else if(actionCommand.equals("close"))
287			{
288				for(int i = 0; i < files.length; i++)
289				{
290					Buffer buffer = jEdit.getBuffer(files[i].path);
291					if(buffer != null)
292						jEdit.closeBuffer(view,buffer);
293				}
294			}
295			else if(actionCommand.equals("browse"))
296				browser.setDirectory(files[0].path);
297			else if(actionCommand.equals("browse-window"))
298			{
299				for(int i = 0; i < files.length; i++)
300				{
301					VFSBrowser.browseDirectoryInNewWindow(view,
302						files[i].path);
303				}
304			}
305			else if(actionCommand.equals("rename"))
306				browser.rename(files[0].path);
307			else if(actionCommand.equals("delete"))
308				browser.delete(files);
309			else if(actionCommand.equals("up"))
310			{
311				String path = browser.getDirectory();
312				VFS vfs = VFSManager.getVFSForPath(path);
313				browser.setDirectory(vfs.getParentOfPath(path));
314			}
315			else if(actionCommand.equals("reload"))
316				browser.reloadDirectory();
317			else if(actionCommand.equals("roots"))
318				browser.rootDirectory();
319			else if(actionCommand.equals("home"))
320				browser.setDirectory(System.getProperty("user.home"));
321			else if(actionCommand.equals("synchronize"))
322			{
323				Buffer buffer = browser.getView().getBuffer();
324				browser.setDirectory(buffer.getVFS().getParentOfPath(
325					buffer.getPath()));
326			}
327			else if(actionCommand.equals("new-file"))
328				browser.newFile();
329			else if(actionCommand.equals("new-directory"))
330				browser.mkdir();
331			else if(actionCommand.equals("search-in-directory"))
332				browser.searchInDirectory();
333			else if(actionCommand.equals("show-hidden-files"))
334			{
335				browser.setShowHiddenFiles(!browser.getShowHiddenFiles());
336				browser.reloadDirectory();
337			}
338		}
339	} //}}}
340}