AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
SingleStepper.java
Go to the documentation of this file.
1package aud.util;
2
3import java.awt.*;
4import java.awt.event.*;
5import javax.swing.*;
6import javax.swing.event.ChangeEvent;
7import javax.swing.event.ChangeListener;
8
9//
10// Font size selection by Martin Haase.
11//
12
44public class SingleStepper {
45
46 protected JFrame frame;
47 protected JTextArea history;
48 protected JButton next;
49 protected Object monitor = new Object();
50 protected int timeout = 0;
51
53 public SingleStepper(JFrame parent) {
55 parent.getContentPane().add(createComponents());
56 if (Sys.env("AUD_TIMEOUT")!=null) {
57 try {
58 timeout=Integer.parseInt(Sys.env("AUD_TIMEOUT"));
59 } catch (NumberFormatException e) {}
60 }
61 }
63 public SingleStepper(String caption) {
64 this(new JFrame(caption!=null ? caption : "aud.util.SingleStepper"));
65 JFrame f=parent();
66
67 // TODO: read geometry and font size from environment variable(s);
68 // and hide spinner (below) in case
69 f.setSize(400,400);
70 f.setVisible(true);
71
72 // exit on close
73 f.addWindowListener(new WindowAdapter() {
74 public void windowClosing(WindowEvent e) {
75 System.exit(0);
76 }
77 });
78 }
79
81 public JFrame parent() { return frame; }
82
83
84 protected JComponent createComponents() {
85 final JSpinner spinner=new JSpinner();
86 spinner.setValue(20);
87
88 final JPanel panel=new JPanel(new BorderLayout());
89
90 history=new JTextArea();
91 history.setFont(new Font("",Font.BOLD,(int) spinner.getValue()));
92 history.setEditable(false);
93 history.setLineWrap(false);
94
95 final JScrollPane historyPane = new JScrollPane(history);
96 historyPane.setPreferredSize(new Dimension(400, 300));
97
98 next=new JButton("continue");
99
100 next.addActionListener(new ActionListener() {
101 public void actionPerformed(ActionEvent ae) {
102 onNext();
103 }
104 });
105
106 spinner.addChangeListener(new ChangeListener(){
107 @Override
108 public void stateChanged(ChangeEvent arg0) {
109 // TODO Auto-generated method stub
110 if ((int) spinner.getValue()<10)
111 spinner.setValue(10);
112 if ((int) spinner.getValue()>40)
113 spinner.setValue(40);
114
115 history.setFont(new Font("",Font.BOLD,(int)spinner.getValue()));
116 }
117 });
118
119 panel.add("North",spinner);
120 panel.add("Center", historyPane);
121 panel.add("South", next);
122 return panel;
123 }
124
126 protected void onNext() {
127 try {
128 synchronized (monitor) {
129 monitor.notify();
130 }
131 } catch (IllegalMonitorStateException e) {
132 System.err.println("ERROR: "+e);
133 System.exit(-1);
134 }
135 }
136
138 public void halt(String text,int timeout) {
139 println(text);
140 try {
141 onHalt();
142 synchronized(monitor) {
143 monitor.wait(timeout>=0 ? timeout : 0);
144 }
145
146 } catch (InterruptedException e) {
147 System.err.println(e);
148 history.append("--- interrupted ---\n");
149 } catch (IllegalMonitorStateException e) {
150 System.err.println("ERROR: "+e);
151 System.exit(-1);
152 }
153 }
154
156 protected void println(String text) {
157 history.append(text+"\n");
158 System.out.println(text);
159 }
160
161 protected void onHalt() {}
162
167 public void setTimeout(int timeout) {
168 this.timeout=timeout;
169 }
170
173 println(Sys.whereAmI(1));
174 return this;
175 }
179 return this;
180 }
181
183 public void halt(String text) {
184 halt(text,timeout);
185 }
186
188 public void halt() { halt(Sys.whereAmI(1)); }
189
190 public static void main(String[] args) {
191 SingleStepper s=new SingleStepper("aud.util.SingleStepper");
192
193 for (int i=0;i<4;++i) {
194 System.out.println(i);
195 s.halt("some message "+i);
196 }
197 s.halt("QUIT");
198 System.exit(0);
199 }
200
201}
Simple framework for single stepping code.
static void main(String[] args)
JFrame parent()
get parent widget
SingleStepper(JFrame parent)
create new instance
void halt()
wait for user
void setTimeout(int timeout)
Set global timeout.
SingleStepper whereAmI()
print location of calling code
void onNext()
call on button pressed
SingleStepper showSource()
jmp to caller's location in editor (emacs only)
SingleStepper(String caption)
create new instance
void halt(String text)
display text and wait for user (or global timeout)
void halt(String text, int timeout)
display text and wait for user or timeout
void println(String text)
print to both, text area and stdout
JComponent createComponents()
System related utilities.
Definition: Sys.java:58
static synchronized String env(String varname)
Get environment variable varname.
Definition: Sys.java:176
static String whereAmI(int depth)
get code location (like __FILE__,__LINE__
Definition: Sys.java:260
static void openCallersSourceInEmacs(int depth)
open emacs whereAmI (Un*x only)
Definition: Sys.java:284