1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.awt.event.ActionEvent;
22
23 import javax.swing.WindowConstants;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.mozilla.javascript.ContextFactory;
28 import org.mozilla.javascript.Scriptable;
29 import org.mozilla.javascript.tools.debugger.Dim;
30 import org.mozilla.javascript.tools.debugger.ScopeProvider;
31 import org.mozilla.javascript.tools.debugger.SwingGui;
32 import org.mozilla.javascript.tools.shell.Global;
33
34
35
36
37
38
39
40 public class ScriptDebugger implements Runnable
41 {
42 private static final Log logger = LogFactory.getLog(ScriptDebugger.class);
43
44 private ContextFactory factory = null;
45 private SwingGui gui = null;
46 protected Dim dim = null;
47
48
49 protected void initDebugger()
50 {
51 dim = new Dim();
52 }
53
54
55
56
57 public void start()
58 {
59 if (logger.isDebugEnabled())
60 {
61 activate();
62 show();
63 }
64 }
65
66
67
68
69 public synchronized void activate()
70 {
71 factory = ContextFactory.getGlobal();
72 Global global = new Global();
73 global.init(factory);
74 global.setIn(System.in);
75 global.setOut(System.out);
76 global.setErr(System.err);
77 initDebugger();
78 ScopeProvider sp = new AlfrescoScopeProvider((Scriptable)global);
79 dim.setScopeProvider(sp);
80 gui = new AlfrescoGui(dim, getTitle(), this);
81 gui.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
82 gui.setExitAction(this);
83 }
84
85 protected String getTitle()
86 {
87 return "Alfresco web-tier JavaScript Debugger";
88 }
89
90
91
92
93
94 public synchronized void show()
95 {
96 if (!isActive())
97 {
98 activate();
99 }
100
101 dim.setBreakOnExceptions(true);
102 dim.setBreak();
103 dim.attachTo(factory);
104 gui.pack();
105 gui.setSize(600, 460);
106 gui.setVisible(true);
107 }
108
109
110
111
112
113 public synchronized void hide()
114 {
115 if (isVisible())
116 {
117 dim.detach();
118 gui.dispose();
119 }
120 }
121
122
123
124
125
126
127 public boolean isVisible()
128 {
129 return isActive() && gui.isVisible();
130 }
131
132
133
134
135
136
137 public boolean isActive()
138 {
139 return gui != null;
140 }
141
142
143
144
145 public void run()
146 {
147 dim.detach();
148 gui.dispose();
149 }
150
151
152 private static class AlfrescoGui extends SwingGui
153 {
154 private static final long serialVersionUID = 5053205080777378416L;
155 private ScriptDebugger debugger;
156
157 public AlfrescoGui(Dim dim, String title, ScriptDebugger debugger)
158 {
159 super(dim, title);
160 this.debugger = debugger;
161 }
162
163 public void actionPerformed(ActionEvent e)
164 {
165 String cmd = e.getActionCommand();
166 if (cmd.equals("Exit"))
167 {
168 debugger.hide();
169 }
170 else
171 {
172 super.actionPerformed(e);
173 }
174 }
175 }
176
177
178 public static class AlfrescoScopeProvider implements ScopeProvider
179 {
180 AlfrescoScopeProvider(Scriptable scope)
181 {
182 this.scope = scope;
183 }
184
185
186
187
188 private Scriptable scope;
189
190
191
192
193 public Scriptable getScope()
194 {
195 return scope;
196 }
197 }
198 }