| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.tools.debugger; |
| 8 |
|
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.PrintStream; |
| 11 |
|
| 12 |
import javax.swing.JFrame; |
| 13 |
|
| 14 |
import org.mozilla.javascript.*; |
| 15 |
import org.mozilla.javascript.tools.shell.Global; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public class Main { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private Dim dim; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private SwingGui debugGui; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public Main(String title) { |
| 37 |
dim = new Dim(); |
| 38 |
debugGui = new SwingGui(dim, title); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public JFrame getDebugFrame() { |
| 45 |
return debugGui; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public void doBreak() { |
| 52 |
dim.setBreak(); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public void setBreakOnExceptions(boolean value) { |
| 59 |
dim.setBreakOnExceptions(value); |
| 60 |
debugGui.getMenubar().getBreakOnExceptions().setSelected(value); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void setBreakOnEnter(boolean value) { |
| 67 |
dim.setBreakOnEnter(value); |
| 68 |
debugGui.getMenubar().getBreakOnEnter().setSelected(value); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public void setBreakOnReturn(boolean value) { |
| 75 |
dim.setBreakOnReturn(value); |
| 76 |
debugGui.getMenubar().getBreakOnReturn().setSelected(value); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void clearAllBreakpoints() { |
| 83 |
dim.clearAllBreakpoints(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public void go() { |
| 90 |
dim.go(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void setScope(Scriptable scope) { |
| 97 |
setScopeProvider(IProxy.newScopeProvider(scope)); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void setScopeProvider(ScopeProvider p) { |
| 105 |
dim.setScopeProvider(p); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void setSourceProvider(final SourceProvider sourceProvider) { |
| 113 |
dim.setSourceProvider(sourceProvider); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public void setExitAction(Runnable r) { |
| 121 |
debugGui.setExitAction(r); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public InputStream getIn() { |
| 129 |
return debugGui.getConsole().getIn(); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public PrintStream getOut() { |
| 137 |
return debugGui.getConsole().getOut(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public PrintStream getErr() { |
| 145 |
return debugGui.getConsole().getErr(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public void pack() { |
| 152 |
debugGui.pack(); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void setSize(int w, int h) { |
| 159 |
debugGui.setSize(w, h); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public void setVisible(boolean flag) { |
| 166 |
debugGui.setVisible(flag); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public boolean isVisible() { |
| 173 |
return debugGui.isVisible(); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public void dispose() { |
| 180 |
clearAllBreakpoints(); |
| 181 |
dim.go(); |
| 182 |
debugGui.dispose(); |
| 183 |
dim = null; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public void attachTo(ContextFactory factory) { |
| 190 |
dim.attachTo(factory); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public void detach() { |
| 197 |
dim.detach(); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static void main(String[] args) { |
| 205 |
Main main = new Main("Rhino JavaScript Debugger"); |
| 206 |
main.doBreak(); |
| 207 |
main.setExitAction(new IProxy(IProxy.EXIT_ACTION)); |
| 208 |
|
| 209 |
System.setIn(main.getIn()); |
| 210 |
System.setOut(main.getOut()); |
| 211 |
System.setErr(main.getErr()); |
| 212 |
|
| 213 |
Global global = org.mozilla.javascript.tools.shell.Main.getGlobal(); |
| 214 |
global.setIn(main.getIn()); |
| 215 |
global.setOut(main.getOut()); |
| 216 |
global.setErr(main.getErr()); |
| 217 |
|
| 218 |
main.attachTo( |
| 219 |
org.mozilla.javascript.tools.shell.Main.shellContextFactory); |
| 220 |
|
| 221 |
main.setScope(global); |
| 222 |
|
| 223 |
main.pack(); |
| 224 |
main.setSize(600, 460); |
| 225 |
main.setVisible(true); |
| 226 |
|
| 227 |
org.mozilla.javascript.tools.shell.Main.exec(args); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public static Main mainEmbedded(String title) { |
| 237 |
ContextFactory factory = ContextFactory.getGlobal(); |
| 238 |
Global global = new Global(); |
| 239 |
global.init(factory); |
| 240 |
return mainEmbedded(factory, global, title); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static Main mainEmbedded(ContextFactory factory, |
| 249 |
Scriptable scope, |
| 250 |
String title) { |
| 251 |
return mainEmbeddedImpl(factory, scope, title); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public static Main mainEmbedded(ContextFactory factory, |
| 260 |
ScopeProvider scopeProvider, |
| 261 |
String title) { |
| 262 |
return mainEmbeddedImpl(factory, scopeProvider, title); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
private static Main mainEmbeddedImpl(ContextFactory factory, |
| 269 |
Object scopeProvider, |
| 270 |
String title) { |
| 271 |
if (title == null) { |
| 272 |
title = "Rhino JavaScript Debugger (embedded usage)"; |
| 273 |
} |
| 274 |
Main main = new Main(title); |
| 275 |
main.doBreak(); |
| 276 |
main.setExitAction(new IProxy(IProxy.EXIT_ACTION)); |
| 277 |
|
| 278 |
main.attachTo(factory); |
| 279 |
if (scopeProvider instanceof ScopeProvider) { |
| 280 |
main.setScopeProvider((ScopeProvider)scopeProvider); |
| 281 |
} else { |
| 282 |
Scriptable scope = (Scriptable)scopeProvider; |
| 283 |
if (scope instanceof Global) { |
| 284 |
Global global = (Global)scope; |
| 285 |
global.setIn(main.getIn()); |
| 286 |
global.setOut(main.getOut()); |
| 287 |
global.setErr(main.getErr()); |
| 288 |
} |
| 289 |
main.setScope(scope); |
| 290 |
} |
| 291 |
|
| 292 |
main.pack(); |
| 293 |
main.setSize(600, 460); |
| 294 |
main.setVisible(true); |
| 295 |
return main; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
@Deprecated |
| 304 |
public void setSize(java.awt.Dimension dimension) { |
| 305 |
debugGui.setSize(dimension.width, dimension.height); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
@Deprecated |
| 313 |
public void setOptimizationLevel(int level) { |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
@Deprecated |
| 321 |
public void contextEntered(Context cx) { |
| 322 |
throw new IllegalStateException(); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
@Deprecated |
| 330 |
public void contextExited(Context cx) { |
| 331 |
throw new IllegalStateException(); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
@Deprecated |
| 339 |
public void contextCreated(Context cx) { |
| 340 |
throw new IllegalStateException(); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
@Deprecated |
| 348 |
public void contextReleased(Context cx) |
| 349 |
{ |
| 350 |
throw new IllegalStateException(); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
private static class IProxy implements Runnable, ScopeProvider { |
| 358 |
|
| 359 |
|
| 360 |
public static final int EXIT_ACTION = 1; |
| 361 |
public static final int SCOPE_PROVIDER = 2; |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
private final int type; |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
private Scriptable scope; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public IProxy(int type) { |
| 378 |
this.type = type; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public static ScopeProvider newScopeProvider(Scriptable scope) { |
| 385 |
IProxy scopeProvider = new IProxy(SCOPE_PROVIDER); |
| 386 |
scopeProvider.scope = scope; |
| 387 |
return scopeProvider; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public void run() { |
| 396 |
if (type != EXIT_ACTION) Kit.codeBug(); |
| 397 |
System.exit(0); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
public Scriptable getScope() { |
| 406 |
if (type != SCOPE_PROVIDER) Kit.codeBug(); |
| 407 |
if (scope == null) Kit.codeBug(); |
| 408 |
return scope; |
| 409 |
} |
| 410 |
} |
| 411 |
} |