comparison visualizer/Filter/src/com/sun/hotspot/igv/filter/CustomFilter.java @ 4512:015fb895586b

Moved visualizer to new directory.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 07 Feb 2012 22:41:09 +0100
parents
children
comparison
equal deleted inserted replaced
4511:6cb549627941 4512:015fb895586b
1 /*
2 * Copyright (c) 1998, 2008, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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.logging.Level;
33 import java.util.logging.Logger;
34 import javax.script.*;
35 import org.openide.cookies.OpenCookie;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.util.Exceptions;
39
40 /**
41 *
42 * @author Thomas Wuerthinger
43 */
44 public class CustomFilter extends AbstractFilter {
45
46 public static final String JAVASCRIPT_HELPER_ID = "JavaScriptHelper";
47 private String code;
48 private String name;
49
50 public CustomFilter(String name, String code) {
51 this.name = name;
52 this.code = code;
53 getProperties().setProperty("name", name);
54 }
55
56 @Override
57 public String getName() {
58 return name;
59 }
60
61 public String getCode() {
62 return code;
63 }
64
65 public void setName(String s) {
66 name = s;
67 fireChangedEvent();
68 }
69
70 public void setCode(String s) {
71 code = s;
72 fireChangedEvent();
73 }
74
75 @Override
76 public OpenCookie getEditor() {
77 return new OpenCookie() {
78
79 @Override
80 public void open() {
81 openInEditor();
82 }
83 };
84 }
85
86 public boolean openInEditor() {
87 EditFilterDialog dialog = new EditFilterDialog(CustomFilter.this);
88 dialog.setVisible(true);
89 boolean result = dialog.wasAccepted();
90 this.getChangedEvent().fire();
91 return result;
92 }
93
94 @Override
95 public String toString() {
96 return getName();
97 }
98
99 private static String getJsHelperText() {
100 InputStream is = null;
101 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);");
102 try {
103 FileObject fo = FileUtil.getConfigRoot().getFileObject(JAVASCRIPT_HELPER_ID);
104 is = fo.getInputStream();
105 BufferedReader r = new BufferedReader(new InputStreamReader(is));
106 String s;
107 while ((s = r.readLine()) != null) {
108 sb.append(s);
109 sb.append("\n");
110 }
111
112 } catch (IOException ex) {
113 Logger.getLogger("global").log(Level.SEVERE, null, ex);
114 } finally {
115 try {
116 is.close();
117 } catch (IOException ex) {
118 Exceptions.printStackTrace(ex);
119 }
120 }
121 return sb.toString();
122 }
123
124 @Override
125 public void apply(Diagram d) {
126 try {
127 ScriptEngineManager sem = new ScriptEngineManager();
128 ScriptEngine e = sem.getEngineByName("ECMAScript");
129 e.eval(getJsHelperText());
130 Bindings b = e.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
131 b.put("graph", d);
132 b.put("IO", System.out);
133 e.eval(code, b);
134 } catch (ScriptException ex) {
135 Exceptions.printStackTrace(ex);
136 }
137 }
138 }