/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/browser/BrowserCommandsMenu.java
Java | 254 lines | 178 code | 36 blank | 40 comment | 55 complexity | 351a4eef281d4938cd58b808f8b11581 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
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 4725 2003-05-25 21:58:28Z 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 JRadioButtonMenuItem defaultEncoding;
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 ButtonGroup grp = new ButtonGroup();
191
192 StringTokenizer st = new StringTokenizer(
193 jEdit.getProperty("encodings"));
194 while(st.hasMoreTokens())
195 {
196 String encoding = st.nextToken();
197 JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
198 mi.setActionCommand("encoding@" + encoding);
199 mi.addActionListener(actionHandler);
200 grp.add(mi);
201 encodingMenuItems.put(encoding,mi);
202 encodingMenu.add(mi);
203 }
204
205 String systemEncoding = System.getProperty("file.encoding");
206 if(encodingMenuItems.get(systemEncoding) == null)
207 {
208 JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
209 systemEncoding);
210 mi.setActionCommand("encoding@" + systemEncoding);
211 mi.addActionListener(actionHandler);
212 grp.add(mi);
213 encodingMenuItems.put(systemEncoding,mi);
214 encodingMenu.add(mi);
215 }
216
217 encodingMenu.addSeparator();
218
219 otherEncoding = new JRadioButtonMenuItem();
220 otherEncoding.setActionCommand("other-encoding");
221 otherEncoding.addActionListener(actionHandler);
222 grp.add(otherEncoding);
223 encodingMenu.add(otherEncoding);
224
225 return encodingMenu;
226 } //}}}
227
228 //}}}
229
230 //{{{ ActionHandler class
231 class ActionHandler implements ActionListener
232 {
233 public void actionPerformed(ActionEvent evt)
234 {
235 View view = browser.getView();
236 String actionCommand = evt.getActionCommand();
237
238 if(actionCommand.equals("other-encoding"))
239 {
240 String encoding = GUIUtilities.input(browser,
241 "encoding-prompt",null,
242 jEdit.getProperty("buffer.encoding",
243 System.getProperty("file.encoding")));
244 if(encoding == null)
245 return;
246 browser.currentEncoding = encoding;
247 }
248 else if(actionCommand.startsWith("encoding@"))
249 {
250 browser.currentEncoding = actionCommand.substring(9);
251 }
252 }
253 } //}}}
254}