KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > browser > BrowserCommandsMenu


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
24 package org.gjt.sp.jedit.browser;
25
26 //{{{ Imports
27
import java.awt.event.*;
28 import java.util.*;
29 import javax.swing.*;
30
31 import org.gjt.sp.jedit.io.*;
32 import org.gjt.sp.jedit.*;
33 //}}}
34

35 /**
36  * @version $Id: BrowserCommandsMenu.java 5533 2006-07-05 19:05:44Z vampire0 $
37  * @author Slava Pestov and Jason Ginchereau
38  */

39 public class BrowserCommandsMenu extends JPopupMenu
40 {
41     //{{{ BrowserCommandsMenu constructor
42
public BrowserCommandsMenu(VFSBrowser browser, VFSFile[] files)
43     {
44         this.browser = browser;
45
46         if(files != null)
47         {
48             VFS vfs = VFSManager.getVFSForPath(
49                 files[0].getDeletePath());
50             int type = files[0].getType();
51
52             boolean fileOpen = (jEdit.getBuffer(files[0].getPath()) != null);
53
54             /* We check this flag separately so that we can
55             delete open files from the favorites. */

56             boolean deletePathOpen = (jEdit.getBuffer(files[0].getDeletePath()) != null);
57
58             boolean delete = !deletePathOpen
59                 && (vfs.getCapabilities()
60                 & VFS.DELETE_CAP) != 0;
61             boolean rename = !fileOpen
62                 && (vfs.getCapabilities()
63                 & VFS.RENAME_CAP) != 0;
64
65             for(int i = 1; i < files.length; i++)
66             {
67                 VFSFile file = files[i];
68
69                 VFS _vfs = VFSManager.getVFSForPath(file.getDeletePath());
70                 delete &= (vfs == _vfs) && (_vfs.getCapabilities()
71                     & VFS.DELETE_CAP) != 0;
72
73                 if(type == file.getType())
74                     /* all good */;
75                 else
76                 {
77                     // this will disable most operations if
78
// files of multiple types are selected
79
type = -1;
80                 }
81
82                 // set rename to false if > 1 file selected
83
rename = false;
84
85                 // show 'close' item if at least one selected
86
// file is currently open
87
if(jEdit.getBuffer(file.getPath()) != null)
88                     fileOpen = true;
89             }
90
91             if(type == VFSFile.DIRECTORY
92                 || type == VFSFile.FILESYSTEM)
93             {
94                 if(files.length == 1)
95                     add(createMenuItem("browse"));
96                 if(browser.getMode() == VFSBrowser.BROWSER)
97                     add(createMenuItem("browse-window"));
98             }
99             else if(type == VFSFile.FILE
100                 && (browser.getMode() == VFSBrowser.BROWSER
101                 || browser.getMode() == VFSBrowser.BROWSER_DIALOG))
102             {
103                 add(createMenuItem("open"));
104                 add(GUIUtilities.loadMenu(
105                     VFSBrowser.getActionContext(),
106                     "vfs.browser.open-in"));
107                 add(createMenuItem("insert"));
108
109                 if(fileOpen)
110                     add(createMenuItem("close"));
111             }
112             else if(type != -1)
113                 add(createMenuItem("open"));
114
115             if(rename)
116                 add(createMenuItem("rename"));
117
118             if(delete)
119                 add(createMenuItem("delete"));
120
121             add(createMenuItem("copy-path"));
122             addSeparator();
123         }
124
125         add(createMenuItem("up"));
126         add(createMenuItem("reload"));
127         add(createMenuItem("roots"));
128         add(createMenuItem("home"));
129         add(createMenuItem("synchronize"));
130         addSeparator();
131
132         if(browser.getMode() == VFSBrowser.BROWSER)
133             add(createMenuItem("new-file"));
134
135         add(createMenuItem("new-directory"));
136
137         if(browser.getMode() == VFSBrowser.BROWSER)
138         {
139             addSeparator();
140             add(createMenuItem("search-directory"));
141         }
142
143         addSeparator();
144
145         add(createMenuItem("show-hidden-files"));
146
147         if(browser.getMode() == VFSBrowser.BROWSER
148             || browser.getMode() == VFSBrowser.BROWSER_DIALOG)
149         {
150             addSeparator();
151             add(createEncodingMenu());
152         }
153         addSeparator();
154         add(createPluginMenu(browser));
155         update();
156     } //}}}
157

158     //{{{ update() method
159
public void update()
160     {
161         if(encodingMenuItems != null)
162         {
163             JRadioButtonMenuItem mi = (JRadioButtonMenuItem)
164                 encodingMenuItems.get(browser.currentEncoding);
165             if(mi != null)
166             {
167                 mi.setSelected(true);
168                 otherEncoding.setText(jEdit.getProperty(
169                     "vfs.browser.other-encoding.label"));
170             }
171             else
172             {
173                 otherEncoding.setSelected(true);
174                 otherEncoding.setText(jEdit.getProperty(
175                     "vfs.browser.other-encoding-2.label",
176                     new String JavaDoc[] { browser.currentEncoding }));
177             }
178         }
179     } //}}}
180

181     //{{{ Private members
182
private VFSBrowser browser;
183     private HashMap encodingMenuItems;
184     private JCheckBoxMenuItem autoDetect;
185     private JRadioButtonMenuItem otherEncoding;
186
187     //{{{ createMenuItem() method
188
private JMenuItem createMenuItem(String JavaDoc name)
189     {
190         return GUIUtilities.loadMenuItem(VFSBrowser.getActionContext(),
191             "vfs.browser." + name,false);
192     } //}}}
193

194     //{{{ createEncodingMenu() method
195
private JMenu createEncodingMenu()
196     {
197         ActionHandler actionHandler = new ActionHandler();
198
199         encodingMenuItems = new HashMap();
200         JMenu encodingMenu = new JMenu(jEdit.getProperty(
201             "vfs.browser.commands.encoding.label"));
202
203         JMenu menu = encodingMenu;
204
205         autoDetect = new JCheckBoxMenuItem(
206             jEdit.getProperty(
207             "vfs.browser.commands.encoding.auto-detect"));
208         autoDetect.setSelected(browser.autoDetectEncoding);
209         autoDetect.setActionCommand("auto-detect");
210         autoDetect.addActionListener(actionHandler);
211         menu.add(autoDetect);
212         menu.addSeparator();
213
214         ButtonGroup grp = new ButtonGroup();
215
216         List encodingMenuItemList = new ArrayList();
217         String JavaDoc[] encodings = MiscUtilities.getEncodings(true);
218         for(int i = 0; i < encodings.length; i++)
219         {
220             String JavaDoc encoding = encodings[i];
221             JRadioButtonMenuItem mi = new JRadioButtonMenuItem(encoding);
222             mi.setActionCommand("encoding@" + encoding);
223             mi.addActionListener(actionHandler);
224             grp.add(mi);
225             encodingMenuItems.put(encoding,mi);
226             encodingMenuItemList.add(mi);
227         }
228
229         String JavaDoc systemEncoding = System.getProperty("file.encoding");
230         if(encodingMenuItems.get(systemEncoding) == null)
231         {
232             JRadioButtonMenuItem mi = new JRadioButtonMenuItem(
233                 systemEncoding);
234             mi.setActionCommand("encoding@" + systemEncoding);
235             mi.addActionListener(actionHandler);
236             grp.add(mi);
237             encodingMenuItems.put(systemEncoding,mi);
238             encodingMenuItemList.add(mi);
239         }
240
241         Collections.sort(encodingMenuItemList,
242             new MiscUtilities.MenuItemCompare());
243
244         Iterator iter = encodingMenuItemList.iterator();
245         while(iter.hasNext())
246         {
247             JRadioButtonMenuItem mi = (JRadioButtonMenuItem)
248                 iter.next();
249
250             if(menu.getMenuComponentCount() > 20)
251             {
252                 JMenu newMenu = new JMenu(
253                     jEdit.getProperty("common.more"));
254                 menu.add(newMenu);
255                 menu = newMenu;
256             }
257
258             menu.add(mi);
259         }
260         menu.addSeparator();
261
262         otherEncoding = new JRadioButtonMenuItem();
263         otherEncoding.setActionCommand("other-encoding");
264         otherEncoding.addActionListener(actionHandler);
265         grp.add(otherEncoding);
266         menu.add(otherEncoding);
267
268         return encodingMenu;
269     } //}}}
270

271     //{{{ createPluginsMenu() method
272
private JMenu createPluginMenu(VFSBrowser browser)
273     {
274         JMenu pluginMenu = new JMenu(jEdit.getProperty(
275             "vfs.browser.plugins.label"));
276         return (JMenu)browser.createPluginsMenu(pluginMenu,false);
277         
278     } //}}}
279

280     
281     //}}}
282

283     //{{{ ActionHandler class
284
class ActionHandler implements ActionListener
285     {
286         public void actionPerformed(ActionEvent evt)
287         {
288             String JavaDoc actionCommand = evt.getActionCommand();
289
290             if(actionCommand.equals("auto-detect"))
291             {
292                 browser.autoDetectEncoding
293                     = autoDetect.isSelected();
294             }
295             else if(actionCommand.equals("other-encoding"))
296             {
297                 String JavaDoc encoding = GUIUtilities.input(browser,
298                     "encoding-prompt",null,
299                     jEdit.getProperty("buffer.encoding",
300                     System.getProperty("file.encoding")));
301                 if(encoding == null)
302                     return;
303                 browser.currentEncoding = encoding;
304             }
305             else if(actionCommand.startsWith("encoding@"))
306             {
307                 browser.currentEncoding = actionCommand.substring(9);
308             }
309         }
310     } //}}}
311
}
312
Popular Tags