PageRenderTime 132ms CodeModel.GetById 111ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

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

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