Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.openfile; |
43 |
|
44 |
import java.io.File; |
45 |
import java.lang.reflect.InvocationHandler; |
46 |
import java.lang.reflect.Method; |
47 |
import java.lang.reflect.Proxy; |
48 |
import java.util.logging.Level; |
49 |
import java.util.logging.Logger; |
50 |
|
51 |
/** |
52 |
* |
53 |
* @author jhavlin |
54 |
*/ |
55 |
public class OpenFileMac { |
56 |
|
57 |
private static Logger LOG = |
58 |
Logger.getLogger(OpenFileMac.class.getName()); |
59 |
|
60 |
public static void register() { |
61 |
LOG.info("Registering Mac OpenFileHandler"); //NOI18N |
62 |
try { |
63 |
Class applicationClass = Class.forName( |
64 |
"com.apple.eawt.Application"); //NOI18N |
65 |
Object app = null; |
66 |
try { |
67 |
Method getApplicationMthd = applicationClass.getDeclaredMethod( |
68 |
"getApplication"); //NOI18N |
69 |
app = getApplicationMthd.invoke(null); |
70 |
} catch (Exception e) { |
71 |
LOG.log(Level.INFO, "Singleton not obtained", e); //NOI18N |
72 |
} |
73 |
if (app == null) { |
74 |
app = applicationClass.getConstructor().newInstance(); |
75 |
} |
76 |
if (app == null) { |
77 |
LOG.info("Could not get nor create application"); //NOI18N |
78 |
return; |
79 |
} |
80 |
Class applicationListenerClass = Class.forName( |
81 |
"com.apple.eawt.ApplicationListener"); //NOI18N |
82 |
Method addListenerMethod = applicationClass.getDeclaredMethod( |
83 |
"addApplicationListener", //NOI18N |
84 |
new Class[]{applicationListenerClass}); |
85 |
Object handlerProxy = Proxy.newProxyInstance( |
86 |
OpenFileMac.class.getClassLoader(), |
87 |
new Class[]{applicationListenerClass}, new Handler()); |
88 |
addListenerMethod.invoke(app, new Object[]{handlerProxy}); |
89 |
|
90 |
} catch (ClassNotFoundException cnfe) { |
91 |
LOG.info("Not Apple JDK: " + cnfe + ""); //NOI18N |
92 |
} catch (Exception e) { |
93 |
LOG.log(Level.INFO, e.getMessage(), e); //NOI18N |
94 |
} |
95 |
} |
96 |
|
97 |
private static class Handler implements InvocationHandler { |
98 |
|
99 |
private void openFile(String path) { |
100 |
OpenFile.openFile(new File(path), -1); |
101 |
} |
102 |
|
103 |
@Override |
104 |
public Object invoke(Object proxy, Method method, Object[] args) |
105 |
throws Throwable { |
106 |
|
107 |
LOG.info("Mac application event: " + method.getName()); //NOI18N |
108 |
if ("handleOpenFile".equals(method.getName())) { //NOI18N |
109 |
if (args.length == 1 && args[0] != null) { |
110 |
String fileName = getFileName(args[0]); |
111 |
if (fileName != null) { |
112 |
LOG.info("Opening file path: " + fileName); //NOI18N |
113 |
openFile(args[0].toString()); |
114 |
} else { |
115 |
LOG.info("Cannot get file name."); //NOI18N |
116 |
} |
117 |
} else { |
118 |
LOG.info("Invalit number of arguments: " //NOI18N |
119 |
+ args.length); |
120 |
for (Object o : args) { |
121 |
LOG.info(o == null ? "null" : o.toString()); //NOI18N |
122 |
} |
123 |
} |
124 |
} |
125 |
return null; |
126 |
} |
127 |
|
128 |
private String getFileName(Object appleEvent) { |
129 |
if (appleEvent != null) { |
130 |
try { |
131 |
Method getFilenameMethod = |
132 |
appleEvent.getClass().getDeclaredMethod( |
133 |
"getFilename"); //NOI18N |
134 |
String filename = (String) getFilenameMethod.invoke( |
135 |
appleEvent); |
136 |
return filename; |
137 |
} catch (Exception ex) { |
138 |
LOG.log(Level.INFO, |
139 |
"Error getting name: " + appleEvent, ex); //NOI18N |
140 |
} |
141 |
} |
142 |
return null; |
143 |
} |
144 |
} |
145 |
} |