KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > ui > sysopen > SystemOpenAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.ui.sysopen;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.AbstractAction JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JMenuItem JavaDoc;
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 /**
45  * Open the selected file(s) with the system default tool.
46  * Available only on JDK 6+.
47  * @author Jesse Glick
48  */

49 public final class SystemOpenAction extends AbstractAction JavaDoc implements ContextAwareAction {
50     
51     public SystemOpenAction() {
52         super(NbBundle.getMessage(SystemOpenAction.class, "CTL_SystemOpenAction"));
53     }
54
55     public void actionPerformed(ActionEvent JavaDoc e) {
56         new ContextAction(Utilities.actionsGlobalContext()).actionPerformed(e);
57     }
58
59     public Action JavaDoc createContextAwareInstance(Lookup context) {
60         return new ContextAction(context);
61     }
62     
63     private static final class ContextAction extends AbstractAction JavaDoc implements Presenter.Popup {
64         
65         private static interface Performer {
66             void open(File JavaDoc f) throws IOException JavaDoc;
67         }
68         private static final Performer performer;
69         static {
70             Performer _performer = null;
71             try {
72                 Class JavaDoc desktop = Class.forName("java.awt.Desktop");
73                 if ((Boolean JavaDoc) desktop.getMethod("isDesktopSupported").invoke(null)) {
74                     final Object JavaDoc desktopInstance = desktop.getMethod("getDesktop").invoke(null);
75                     Class JavaDoc action = Class.forName("java.awt.Desktop$Action");
76                     if ((Boolean JavaDoc) desktop.getMethod("isSupported", action).
77                             invoke(desktopInstance, action.getField("OPEN").get(null))) {
78                         final Method JavaDoc open = desktop.getMethod("open", File JavaDoc.class);
79                         _performer = new Performer() {
80                             public void open(File JavaDoc f) throws IOException JavaDoc {
81                                 // XXX could try edit too?
82
try {
83                                     open.invoke(desktopInstance, f);
84                                 } catch (InvocationTargetException JavaDoc x) {
85                                     throw (IOException JavaDoc) x.getTargetException();
86                                 } catch (Exception JavaDoc x) {
87                                     throw (IOException JavaDoc) new IOException JavaDoc(x.toString()).initCause(x);
88                                 }
89                             }
90                         };
91                     }
92                 }
93             } catch (ClassNotFoundException JavaDoc x) {
94                 // OK, ignore
95
} catch (Exception JavaDoc x) {
96                 Logger.getLogger(SystemOpenAction.class.getName()).log(Level.WARNING, null, x);
97             }
98             performer = _performer;
99         }
100
101         private final Set JavaDoc<File JavaDoc> files;
102         
103         public ContextAction(Lookup context) {
104             super(NbBundle.getMessage(SystemOpenAction.class, "CTL_SystemOpenAction"));
105             files = new HashSet JavaDoc<File JavaDoc>();
106             for (DataObject d : context.lookupAll(DataObject.class)) {
107                 File JavaDoc 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 JavaDoc e) {
117             if (performer == null) {
118                 return;
119             }
120             for (File JavaDoc f : files) {
121                 try {
122                     performer.open(f);
123                 } catch (IOException JavaDoc x) {
124                     Logger.getLogger(SystemOpenAction.class.getName()).log(Level.INFO, null, x);
125                     // XXX or perhaps notify user of problem
126
}
127             }
128         }
129
130         public JMenuItem JavaDoc getPopupPresenter() {
131             class Menu extends JMenuItem JavaDoc implements DynamicMenuContent {
132                 public Menu() {
133                     super(ContextAction.this);
134                 }
135                 public JComponent JavaDoc[] getMenuPresenters() {
136                     if (performer != null && !files.isEmpty()) {
137                         return new JComponent JavaDoc[] {this};
138                     } else {
139                         return new JComponent JavaDoc[0];
140                     }
141                 }
142                 public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
143                     return getMenuPresenters();
144                 }
145             }
146             return new Menu();
147         }
148         
149     }
150     
151 }
152
153
Popular Tags