PageRenderTime 79ms CodeModel.GetById 16ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 267 lines | 199 code | 31 blank | 37 comment | 64 complexity | 2c61a275b5def79b0594fe5676f83687 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) 1999 Jason Ginchereau
  7 * Portions copyright (C) 2000, 2001 Slava Pestov
  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 3999 2002-01-28 11:40:33Z 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 file)
 45	{
 46		this.browser = browser;
 47
 48		if(file != null)
 49		{
 50			this.file = file;
 51
 52			VFS vfs = VFSManager.getVFSForPath(file.deletePath);
 53
 54			boolean delete = (vfs.getCapabilities() & VFS.DELETE_CAP) != 0;
 55			boolean rename = (vfs.getCapabilities() & VFS.RENAME_CAP) != 0;
 56
 57			if(jEdit.getBuffer(file.path) != null)
 58			{
 59				if(browser.getMode() == VFSBrowser.BROWSER)
 60				{
 61					add(createMenuItem("open"));
 62					add(createMenuItem("open-view"));
 63					add(createMenuItem("insert"));
 64					add(createMenuItem("close"));
 65				}
 66				else
 67					add(createMenuItem("choose"));
 68			}
 69			else
 70			{
 71				if(file.type == VFS.DirectoryEntry.DIRECTORY
 72					|| file.type == VFS.DirectoryEntry.FILESYSTEM)
 73				{
 74					add(createMenuItem("browse"));
 75				}
 76				else if(browser.getMode() != VFSBrowser.BROWSER)
 77				{
 78					add(createMenuItem("choose"));
 79				}
 80				// else if in browser mode
 81				else
 82				{
 83					add(createMenuItem("open"));
 84					add(createMenuItem("open-view"));
 85					add(createOpenEncodingMenu());
 86					add(createMenuItem("insert"));
 87				}
 88
 89				if(rename)
 90					add(createMenuItem("rename"));
 91				if(delete)
 92					add(createMenuItem("delete"));
 93			}
 94
 95			addSeparator();
 96		}
 97
 98		add(createMenuItem("up"));
 99		add(createMenuItem("reload"));
100		add(createMenuItem("roots"));
101		add(createMenuItem("home"));
102		add(createMenuItem("synchronize"));
103		addSeparator();
104
105		if(browser.getMode() == VFSBrowser.BROWSER)
106			add(createMenuItem("new-file"));
107
108		add(createMenuItem("new-directory"));
109
110		if(browser.getMode() == VFSBrowser.BROWSER)
111		{
112			addSeparator();
113			add(createMenuItem("search-in-directory"));
114		}
115
116		addSeparator();
117
118		JCheckBoxMenuItem showHiddenFiles = new JCheckBoxMenuItem(
119			jEdit.getProperty("vfs.browser.commands.show-hidden-files.label"));
120		showHiddenFiles.setActionCommand("show-hidden-files");
121		showHiddenFiles.setSelected(browser.getShowHiddenFiles());
122		showHiddenFiles.addActionListener(new ActionHandler());
123		add(showHiddenFiles);
124	} //}}}
125
126	//{{{ Private members
127	private VFSBrowser browser;
128	private VFS.DirectoryEntry file;
129	private VFS vfs;
130
131	//{{{ createMenuItem() method
132	private JMenuItem createMenuItem(String name)
133	{
134		String label = jEdit.getProperty("vfs.browser.commands." + name + ".label");
135		JMenuItem mi = new JMenuItem(label);
136		mi.setActionCommand(name);
137		mi.addActionListener(new ActionHandler());
138		return mi;
139	} //}}}
140
141	//{{{ createOpenEncodingMenu() method
142	private JMenu createOpenEncodingMenu()
143	{
144		ActionListener listener = new ActionHandler();
145
146		JMenu openEncoding = new JMenu(jEdit.getProperty("open-encoding.label"));
147
148		// used twice...
149		String systemEncoding = System.getProperty("file.encoding");
150
151		JMenuItem mi = new JMenuItem(jEdit.getProperty("os-encoding"));
152		mi.setActionCommand("open@" + systemEncoding);
153		mi.addActionListener(listener);
154		openEncoding.add(mi);
155
156		mi = new JMenuItem(jEdit.getProperty("jedit-encoding"));
157		mi.setActionCommand("open@" + jEdit.getProperty("buffer.encoding",systemEncoding));
158		mi.addActionListener(listener);
159		openEncoding.add(mi);
160
161		openEncoding.addSeparator();
162
163		StringTokenizer st = new StringTokenizer(jEdit.getProperty("encodings"));
164		while(st.hasMoreTokens())
165		{
166			String encoding = st.nextToken();
167			mi = new JMenuItem(encoding);
168			mi.setActionCommand("open@" + encoding);
169			mi.addActionListener(listener);
170			openEncoding.add(mi);
171		}
172
173		openEncoding.addSeparator();
174
175		mi = new JMenuItem(jEdit.getProperty("other-encoding.label"));
176		mi.setActionCommand("other-encoding");
177		mi.addActionListener(listener);
178		openEncoding.add(mi);
179
180		return openEncoding;
181	} //}}}
182
183	//}}}
184
185	//{{{ ActionHandler class
186	class ActionHandler implements ActionListener
187	{
188		public void actionPerformed(ActionEvent evt)
189		{
190			View view = browser.getView();
191			String actionCommand = evt.getActionCommand();
192
193			if(actionCommand.startsWith("open@"))
194			{
195				// a bit of a hack to support 'Open With Encoding' menu
196				Hashtable props = new Hashtable();
197				props.put(Buffer.ENCODING,actionCommand.substring(5));
198				jEdit.openFile(view,null,file.path,false,props);
199			}
200			else if(actionCommand.equals("other-encoding"))
201			{
202				String encoding = GUIUtilities.input(browser,
203					"encoding-prompt",null,
204					jEdit.getProperty("buffer.encoding",
205					System.getProperty("file.encoding")));
206				if(encoding == null)
207					return;
208				Hashtable props = new Hashtable();
209				props.put(Buffer.ENCODING,encoding);
210				jEdit.openFile(view,null,file.path,false,props);
211			}
212			else if(actionCommand.equals("open"))
213				jEdit.openFile(view,file.path);
214			else if(actionCommand.equals("open-view"))
215			{
216				Buffer buffer = jEdit.openFile(null,file.path);
217				if(buffer != null)
218					jEdit.newView(view,buffer);
219			}
220			else if(actionCommand.equals("insert"))
221				view.getBuffer().insertFile(view,file.path);
222			else if(actionCommand.equals("choose"))
223				browser.filesActivated(false,false);
224			else if(actionCommand.equals("close"))
225			{
226				Buffer buffer = jEdit.getBuffer(file.path);
227				if(buffer != null)
228					jEdit.closeBuffer(view,buffer);
229			}
230			else if(actionCommand.equals("browse"))
231				browser.setDirectory(file.path);
232			else if(actionCommand.equals("rename"))
233				browser.rename(file.path);
234			else if(actionCommand.equals("delete"))
235				browser.delete(file.deletePath);
236			else if(actionCommand.equals("up"))
237			{
238				String path = browser.getDirectory();
239				VFS vfs = VFSManager.getVFSForPath(path);
240				browser.setDirectory(vfs.getParentOfPath(path));
241			}
242			else if(actionCommand.equals("reload"))
243				browser.reloadDirectory();
244			else if(actionCommand.equals("roots"))
245				browser.rootDirectory();
246			else if(actionCommand.equals("home"))
247				browser.setDirectory(System.getProperty("user.home"));
248			else if(actionCommand.equals("synchronize"))
249			{
250				Buffer buffer = browser.getView().getBuffer();
251				browser.setDirectory(buffer.getVFS().getParentOfPath(
252					buffer.getPath()));
253			}
254			else if(actionCommand.equals("new-file"))
255				browser.newFile();
256			else if(actionCommand.equals("new-directory"))
257				browser.mkdir();
258			else if(actionCommand.equals("search-in-directory"))
259				browser.searchInDirectory();
260			else if(actionCommand.equals("show-hidden-files"))
261			{
262				browser.setShowHiddenFiles(!browser.getShowHiddenFiles());
263				browser.reloadDirectory();
264			}
265		}
266	} //}}}
267}