1 19 20 package org.netbeans.core.ui.sysopen; 21 22 import java.awt.event.ActionEvent ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import javax.swing.AbstractAction ; 32 import javax.swing.Action ; 33 import javax.swing.JComponent ; 34 import javax.swing.JMenuItem ; 35 import org.openide.awt.DynamicMenuContent; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.loaders.DataObject; 38 import org.openide.util.ContextAwareAction; 39 import org.openide.util.Lookup; 40 import org.openide.util.NbBundle; 41 import org.openide.util.Utilities; 42 import org.openide.util.actions.Presenter; 43 44 49 public final class SystemOpenAction extends AbstractAction implements ContextAwareAction { 50 51 public SystemOpenAction() { 52 super(NbBundle.getMessage(SystemOpenAction.class, "CTL_SystemOpenAction")); 53 } 54 55 public void actionPerformed(ActionEvent e) { 56 new ContextAction(Utilities.actionsGlobalContext()).actionPerformed(e); 57 } 58 59 public Action createContextAwareInstance(Lookup context) { 60 return new ContextAction(context); 61 } 62 63 private static final class ContextAction extends AbstractAction implements Presenter.Popup { 64 65 private static interface Performer { 66 void open(File f) throws IOException ; 67 } 68 private static final Performer performer; 69 static { 70 Performer _performer = null; 71 try { 72 Class desktop = Class.forName("java.awt.Desktop"); 73 if ((Boolean ) desktop.getMethod("isDesktopSupported").invoke(null)) { 74 final Object desktopInstance = desktop.getMethod("getDesktop").invoke(null); 75 Class action = Class.forName("java.awt.Desktop$Action"); 76 if ((Boolean ) desktop.getMethod("isSupported", action). 77 invoke(desktopInstance, action.getField("OPEN").get(null))) { 78 final Method open = desktop.getMethod("open", File .class); 79 _performer = new Performer() { 80 public void open(File f) throws IOException { 81 try { 83 open.invoke(desktopInstance, f); 84 } catch (InvocationTargetException x) { 85 throw (IOException ) x.getTargetException(); 86 } catch (Exception x) { 87 throw (IOException ) new IOException (x.toString()).initCause(x); 88 } 89 } 90 }; 91 } 92 } 93 } catch (ClassNotFoundException x) { 94 } catch (Exception x) { 96 Logger.getLogger(SystemOpenAction.class.getName()).log(Level.WARNING, null, x); 97 } 98 performer = _performer; 99 } 100 101 private final Set <File > files; 102 103 public ContextAction(Lookup context) { 104 super(NbBundle.getMessage(SystemOpenAction.class, "CTL_SystemOpenAction")); 105 files = new HashSet <File >(); 106 for (DataObject d : context.lookupAll(DataObject.class)) { 107 File f = FileUtil.toFile(d.getPrimaryFile()); 108 if (f == null) { 109 files.clear(); 110 break; 111 } 112 files.add(f); 113 } 114 } 115 116 public void actionPerformed(ActionEvent e) { 117 if (performer == null) { 118 return; 119 } 120 for (File f : files) { 121 try { 122 performer.open(f); 123 } catch (IOException x) { 124 Logger.getLogger(SystemOpenAction.class.getName()).log(Level.INFO, null, x); 125 } 127 } 128 } 129 130 public JMenuItem getPopupPresenter() { 131 class Menu extends JMenuItem implements DynamicMenuContent { 132 public Menu() { 133 super(ContextAction.this); 134 } 135 public JComponent [] getMenuPresenters() { 136 if (performer != null && !files.isEmpty()) { 137 return new JComponent [] {this}; 138 } else { 139 return new JComponent [0]; 140 } 141 } 142 public JComponent [] synchMenuPresenters(JComponent [] items) { 143 return getMenuPresenters(); 144 } 145 } 146 return new Menu(); 147 } 148 149 } 150 151 } 152 153 | Popular Tags |