1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package org.libreoffice.example.java_scripts;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.text.Document;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.star.script.provider.XScriptContext;
import bsh.Interpreter;
public class OOBeanShellDebugger implements OOScriptDebugger, ActionListener,
DocumentListener {
private JFrame frame;
private JTextArea ta;
private GlyphGutter gg;
private XScriptContext context;
private int currentPosition = -1;
private int linecount;
private Interpreter sessionInterpreter;
private Thread execThread = null;
private String filename = null;
/* Entry point for script execution */
public void go(XScriptContext context, String filename) {
if (filename != null && filename != "") {
try {
FileInputStream fis = new FileInputStream(filename);
this.filename = filename;
go(context, fis);
} catch (IOException ioe) {
JOptionPane.showMessageDialog(frame,
"Error loading file: " + ioe.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
/* Entry point for script execution */
public void go(XScriptContext context, InputStream in) {
this.context = context;
initUI();
if (in != null) {
try {
loadFile(in);
} catch (IOException ioe) {
JOptionPane.showMessageDialog(frame,
"Error loading stream: " + ioe.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public void loadFile(InputStream in) throws IOException {
/* Remove ourselves as a DocumentListener while loading the file
so we don't get a storm of DocumentEvents during loading */
ta.getDocument().removeDocumentListener(this);
byte[] contents = new byte[1024];
int len = 0, pos = 0;
while ((len = in.read(contents, 0, 1024)) != -1) {
ta.insert(new String(contents, 0, len), pos);
pos += len;
}
try {
in.close();
} catch (IOException ignore) {
}
/* Update the GlyphGutter and add back the DocumentListener */
gg.update();
ta.getDocument().addDocumentListener(this);
}
private void initUI() {
frame = new JFrame("BeanShell Debug Window");
ta = new JTextArea();
ta.setRows(15);
ta.setColumns(40);
ta.setLineWrap(false);
linecount = ta.getLineCount();
gg = new GlyphGutter(this);
final JScrollPane sp = new JScrollPane();
sp.setViewportView(ta);
sp.setRowHeaderView(gg);
ta.getDocument().addDocumentListener(this);
String[] labels = {"Run", "Clear", "Save", "Close"};
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
for (int i = 0; i < labels.length; i++) {
JButton b = new JButton(labels[i]);
b.addActionListener(this);
p.add(b);
if (labels[i].equals("Save") && filename == null) {
b.setEnabled(false);
}
}
frame.getContentPane().add(sp, "Center");
frame.getContentPane().add(p, "South");
frame.pack();
frame.show();
}
/* Implementation of DocumentListener interface */
public void insertUpdate(DocumentEvent e) {
doChanged(e);
}
public void removeUpdate(DocumentEvent e) {
doChanged(e);
}
public void changedUpdate(DocumentEvent e) {
doChanged(e);
}
/* If the number of lines in the JTextArea has changed then update the
GlyphGutter */
public void doChanged(DocumentEvent e) {
if (linecount != ta.getLineCount()) {
gg.update();
linecount = ta.getLineCount();
}
}
private void startExecution() {
execThread = new Thread() {
public void run() {
Interpreter interpreter = new Interpreter();
interpreter.getNameSpace().clear();
// reset position and repaint gutter so no red arrow appears
currentPosition = -1;
gg.repaint();
try {
interpreter.set("context", context);
interpreter.eval(ta.getText());
} catch (bsh.EvalError err) {
currentPosition = err.getErrorLineNumber() - 1;
try {
// scroll to line of the error
int line = ta.getLineStartOffset(currentPosition);
Rectangle rect = ta.modelToView(line);
ta.scrollRectToVisible(rect);
} catch (Exception e) {
// couldn't scroll to line, do nothing
}
gg.repaint();
JOptionPane.showMessageDialog(frame, "Error at line " +
String.valueOf(err.getErrorLineNumber()) +
"\n\n: " + err.getErrorText(),
"Error", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(frame,
"Error: " + e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
};
execThread.start();
}
private void promptForSaveName() {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
if (f.isDirectory() || f.getName().endsWith(".bsh")) {
return true;
}
return false;
}
public String getDescription() {
return ("BeanShell files: *.bsh");
}
});
int ret = chooser.showSaveDialog(frame);
if (ret == JFileChooser.APPROVE_OPTION) {
filename = chooser.getSelectedFile().getAbsolutePath();
if (!filename.endsWith(".bsh")) {
filename += ".bsh";
}
}
}
private void saveTextArea() {
if (filename == null) {
promptForSaveName();
}
FileOutputStream fos = null;
if (filename != null) {
try {
File f = new File(filename);
fos = new FileOutputStream(f);
String s = ta.getText();
fos.write(s.getBytes(), 0, s.length());
} catch (IOException ioe) {
JOptionPane.showMessageDialog(frame,
"Error saving file: " + ioe.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ignore) {
}
}
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Run")) {
startExecution();
} else if (e.getActionCommand().equals("Close")) {
frame.dispose();
} else if (e.getActionCommand().equals("Save")) {
saveTextArea();
} else if (e.getActionCommand().equals("Clear")) {
ta.setText("");
}
}
public JTextArea getTextArea() {
return ta;
}
public int getCurrentPosition() {
return currentPosition;
}
}
class GlyphGutter extends JComponent {
private OOBeanShellDebugger debugger;
private final String DUMMY_STRING = "99";
GlyphGutter(OOBeanShellDebugger debugger) {
this.debugger = debugger;
update();
}
public void update() {
JTextArea textArea = debugger.getTextArea();
Font font = textArea.getFont();
setFont(font);
FontMetrics metrics = getFontMetrics(font);
int h = metrics.getHeight();
int lineCount = textArea.getLineCount() + 1;
String dummy = Integer.toString(lineCount);
if (dummy.length() < 2) {
dummy = DUMMY_STRING;
}
Dimension d = new Dimension();
d.width = metrics.stringWidth(dummy) + 16;
d.height = lineCount * h + 100;
setPreferredSize(d);
setSize(d);
}
public void paintComponent(Graphics g) {
JTextArea textArea = debugger.getTextArea();
Font font = textArea.getFont();
g.setFont(font);
FontMetrics metrics = getFontMetrics(font);
Rectangle clip = g.getClipBounds();
g.setColor(getBackground());
g.fillRect(clip.x, clip.y, clip.width, clip.height);
int ascent = metrics.getMaxAscent();
int h = metrics.getHeight();
int lineCount = textArea.getLineCount() + 1;
int startLine = clip.y / h;
int endLine = (clip.y + clip.height) / h + 1;
int width = getWidth();
if (endLine > lineCount) {
endLine = lineCount;
}
for (int i = startLine; i < endLine; i++) {
String text;
text = Integer.toString(i + 1) + " ";
int w = metrics.stringWidth(text);
int y = i * h;
g.setColor(Color.blue);
g.drawString(text, 0, y + ascent);
int x = width - ascent;
// if currentPosition is not -1 then a red arrow will be drawn
if (i == debugger.getCurrentPosition()) {
drawArrow(g, ascent, x, y);
}
}
}
private void drawArrow(Graphics g, int ascent, int x, int y) {
Polygon arrow = new Polygon();
int dx = x;
y += ascent - 10;
int dy = y;
arrow.addPoint(dx, dy + 3);
arrow.addPoint(dx + 5, dy + 3);
for (x = dx + 5; x <= dx + 10; x++, y++) {
arrow.addPoint(x, y);
}
for (x = dx + 9; x >= dx + 5; x--, y++) {
arrow.addPoint(x, y);
}
arrow.addPoint(dx + 5, dy + 7);
arrow.addPoint(dx, dy + 7);
g.setColor(Color.red);
g.fillPolygon(arrow);
g.setColor(Color.black);
g.drawPolygon(arrow);
}
};
|