1 23 24 package org.gjt.sp.jedit.browser; 25 26 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 35 39 public class BrowserCommandsMenu extends JPopupMenu 40 { 41 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 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 ; 75 else 76 { 77 type = -1; 80 } 81 82 rename = false; 84 85 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 } 158 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 [] { browser.currentEncoding })); 177 } 178 } 179 } 181 private VFSBrowser browser; 183 private HashMap encodingMenuItems; 184 private JCheckBoxMenuItem autoDetect; 185 private JRadioButtonMenuItem otherEncoding; 186 187 private JMenuItem createMenuItem(String name) 189 { 190 return GUIUtilities.loadMenuItem(VFSBrowser.getActionContext(), 191 "vfs.browser." + name,false); 192 } 194 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 [] encodings = MiscUtilities.getEncodings(true); 218 for(int i = 0; i < encodings.length; i++) 219 { 220 String 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 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 } 271 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 } 280 281 283 class ActionHandler implements ActionListener 285 { 286 public void actionPerformed(ActionEvent evt) 287 { 288 String 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 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 } } 312 | Popular Tags |