766
|
1 |
/*
|
|
2 |
* Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package com.sun.hotspot.igv.filter;
|
|
26 |
|
|
27 |
import com.sun.hotspot.igv.graph.Diagram;
|
|
28 |
import java.io.BufferedReader;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.InputStream;
|
|
31 |
import java.io.InputStreamReader;
|
|
32 |
import java.util.Collection;
|
|
33 |
import java.util.logging.Level;
|
|
34 |
import java.util.logging.Logger;
|
|
35 |
import org.openide.DialogDisplayer;
|
|
36 |
import org.openide.NotifyDescriptor;
|
|
37 |
import org.openide.cookies.OpenCookie;
|
|
38 |
import org.openide.filesystems.Repository;
|
|
39 |
import org.openide.filesystems.FileSystem;
|
|
40 |
import org.openide.filesystems.FileObject;
|
|
41 |
import org.openide.util.Exceptions;
|
|
42 |
import org.openide.util.Lookup;
|
|
43 |
|
|
44 |
/**
|
|
45 |
*
|
|
46 |
* @author Thomas Wuerthinger
|
|
47 |
*/
|
|
48 |
public class CustomFilter extends AbstractFilter {
|
|
49 |
|
|
50 |
public static final String JAVASCRIPT_HELPER_ID = "JavaScriptHelper";
|
|
51 |
private static ScriptEngineAbstraction engine;
|
|
52 |
private String code;
|
|
53 |
private String name;
|
|
54 |
|
|
55 |
public CustomFilter(String name, String code) {
|
|
56 |
this.name = name;
|
|
57 |
this.code = code;
|
1497
|
58 |
getProperties().setProperty("name", name);
|
766
|
59 |
}
|
|
60 |
|
|
61 |
public String getName() {
|
|
62 |
return name;
|
|
63 |
}
|
|
64 |
|
|
65 |
public String getCode() {
|
|
66 |
return code;
|
|
67 |
}
|
|
68 |
|
|
69 |
public void setName(String s) {
|
|
70 |
name = s;
|
|
71 |
fireChangedEvent();
|
|
72 |
}
|
|
73 |
|
|
74 |
public void setCode(String s) {
|
|
75 |
code = s;
|
|
76 |
fireChangedEvent();
|
|
77 |
}
|
|
78 |
|
|
79 |
@Override
|
|
80 |
public OpenCookie getEditor() {
|
|
81 |
return new OpenCookie() {
|
|
82 |
|
|
83 |
public void open() {
|
|
84 |
openInEditor();
|
|
85 |
}
|
|
86 |
};
|
|
87 |
}
|
|
88 |
|
|
89 |
public boolean openInEditor() {
|
|
90 |
EditFilterDialog dialog = new EditFilterDialog(CustomFilter.this);
|
|
91 |
dialog.setVisible(true);
|
|
92 |
return dialog.wasAccepted();
|
|
93 |
}
|
|
94 |
|
|
95 |
@Override
|
|
96 |
public String toString() {
|
|
97 |
return getName();
|
|
98 |
}
|
|
99 |
|
|
100 |
public static ScriptEngineAbstraction getEngine() {
|
|
101 |
if (engine == null) {
|
|
102 |
|
|
103 |
ScriptEngineAbstraction chosen = null;
|
|
104 |
try {
|
|
105 |
Collection<? extends ScriptEngineAbstraction> list = Lookup.getDefault().lookupAll(ScriptEngineAbstraction.class);
|
|
106 |
for (ScriptEngineAbstraction s : list) {
|
|
107 |
if (s.initialize(getJsHelperText())) {
|
|
108 |
if (chosen == null || !(chosen instanceof JavaSE6ScriptEngine)) {
|
|
109 |
chosen = s;
|
|
110 |
}
|
|
111 |
}
|
|
112 |
}
|
|
113 |
} catch (NoClassDefFoundError ncdfe) {
|
|
114 |
Logger.getLogger("global").log(Level.SEVERE, null, ncdfe);
|
|
115 |
}
|
|
116 |
|
|
117 |
if (chosen == null) {
|
|
118 |
NotifyDescriptor message = new NotifyDescriptor.Message("Could not find a scripting engine. Please make sure that the Rhino scripting engine is available. Otherwise filter cannot be used.", NotifyDescriptor.ERROR_MESSAGE);
|
|
119 |
DialogDisplayer.getDefault().notifyLater(message);
|
|
120 |
chosen = new NullScriptEngine();
|
|
121 |
}
|
|
122 |
|
|
123 |
engine = chosen;
|
|
124 |
}
|
|
125 |
|
|
126 |
return engine;
|
|
127 |
}
|
|
128 |
|
|
129 |
private static String getJsHelperText() {
|
|
130 |
InputStream is = null;
|
|
131 |
StringBuilder sb = new StringBuilder("importPackage(Packages.com.sun.hotspot.igv.filter);importPackage(Packages.com.sun.hotspot.igv.graph);importPackage(Packages.com.sun.hotspot.igv.data);importPackage(Packages.com.sun.hotspot.igv.util);importPackage(java.awt);");
|
|
132 |
try {
|
|
133 |
FileSystem fs = Repository.getDefault().getDefaultFileSystem();
|
|
134 |
FileObject fo = fs.getRoot().getFileObject(JAVASCRIPT_HELPER_ID);
|
|
135 |
is = fo.getInputStream();
|
|
136 |
BufferedReader r = new BufferedReader(new InputStreamReader(is));
|
|
137 |
String s;
|
|
138 |
while ((s = r.readLine()) != null) {
|
|
139 |
sb.append(s);
|
|
140 |
sb.append("\n");
|
|
141 |
}
|
|
142 |
|
|
143 |
} catch (IOException ex) {
|
|
144 |
Logger.getLogger("global").log(Level.SEVERE, null, ex);
|
|
145 |
} finally {
|
|
146 |
try {
|
|
147 |
is.close();
|
|
148 |
} catch (IOException ex) {
|
|
149 |
Exceptions.printStackTrace(ex);
|
|
150 |
}
|
|
151 |
}
|
|
152 |
return sb.toString();
|
|
153 |
}
|
|
154 |
|
|
155 |
public void apply(Diagram d) {
|
|
156 |
getEngine().execute(d, code);
|
|
157 |
}
|
|
158 |
}
|