-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugger.java
More file actions
187 lines (173 loc) · 5.88 KB
/
Copy pathDebugger.java
File metadata and controls
187 lines (173 loc) · 5.88 KB
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
package EzTree;
import java.awt.*;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.ArrayList;
import javax.swing.*;
/**
*
* @author Derek Smith
*/
public class Debugger extends JDialog{
private String Message;
private JPanel sizepanel;
private JTextArea textarea;
private JButton close_button, next_button, previous_button;
private Map<Integer, String> boxmap;
private int current_box = -1;
private View target;
private JTextArea target_textarea;
private ArrayList<Box> boxes;
public Debugger(String message)
{
super.setTitle("ERRORS");
super.setLocation(0, 0);
Message = message;
initcomponents();
super.pack();
boxmap = new HashMap<Integer, String>();
boxes = new ArrayList<Box>();
}
public void initcomponents(){
sizepanel = new JPanel();
sizepanel.setPreferredSize(new Dimension(530, 500));
sizepanel.setBorder(new MatteBorder(1, 1, 1, 1, Color.GRAY));
textarea = new JTextArea();
textarea.setPreferredSize(new Dimension(510, 400));
textarea.append(Message);
sizepanel.add(new JScrollPane(textarea));
close_button = new JButton("CLOSE");
close_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
close();
}});
sizepanel.add(close_button);
next_button = new JButton("HIGHLIGHT ERRORS");
next_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
highlight_errors();
}
});
sizepanel.add(next_button);
previous_button = new JButton("HIDE ERRORS");
previous_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
hide_errors();
}
});
sizepanel.add(previous_button);
add(sizepanel, BorderLayout.CENTER);
}
public void open(){
super.setVisible(true);
}
public boolean is_open(){
return (this.isVisible());
}
public void set_boxes(Map<Integer, String> m, View v){
try{
Iterator<Integer> it = m.keySet().iterator();
while (it.hasNext()){
int index = it.next();
String s = m.get(index);
boxmap.put(index, s);
}
target = v;
} catch (Exception exc){
JOptionPane.showMessageDialog(null, exc.getMessage());
}
}
public void highlight_errors(){
target_textarea = target.writingArea;
int first_box_position = 0;
for (int count = 0; count < boxmap.size(); ++count){
int current_box = count;
Iterator<Integer> it = boxmap.keySet().iterator();
int index = 0;
int iteration = 0;
while (it.hasNext()){
index = it.next();
++iteration;
if (iteration > current_box){
break;
}
}
int current_line_number = index;
int current_offset = -1;
try{
current_offset = target_textarea.getLineStartOffset(current_line_number);
String current_line = boxmap.get(index);
} catch (Exception exc){
JOptionPane.showMessageDialog(null, "error 1");
}
int original_caret_position = target_textarea.getCaretPosition();
int new_caret_position = current_offset;
if (count == 0){
first_box_position = new_caret_position;
}
target_textarea.setCaretPosition(new_caret_position);
Box borderbox = Box.createHorizontalBox();
borderbox.setFocusable(false);
borderbox.setBorder(new MatteBorder(1, 1, 1, 1, Color.RED));
borderbox.setSize(new Dimension(700, 17));
FontMetrics measure = target_textarea.getFontMetrics(target_textarea.getFont());
int fontheight = measure.getHeight();
try{
borderbox.setLocation(0, target_textarea.getLineOfOffset(new_caret_position) * fontheight);
} catch (Exception exc){
JOptionPane.showMessageDialog(null, "error: " + exc.getMessage());
}
borderbox.setVisible(true);
boxes.add(borderbox);
target_textarea.add(borderbox);
}
target_textarea.update(target.getGraphics());
target.update(target.getGraphics());
target_textarea.setCaretPosition(first_box_position);
}
public void hide_errors(){
remove_boxes();
target_textarea.update(target.getGraphics());
target.update(target.getGraphics());
}
public void remove_boxes(){
for (Box b : boxes){
target_textarea.remove(b);
}
boxes.clear();
}
public void add_message(String message){
Message += "\n";
Message += message;
textarea.append("\n");
textarea.append(message);
}
public void clear_message(){
Message = "";
textarea.setText("");
}
public boolean has_message(){
return (Message.equals("")? false : true);
}
public void close(){
try{
boxmap.clear();
boxes.clear();
this.setVisible(false);
this.dispose();
} catch (Exception exception){
JOptionPane.showMessageDialog(null, exception.getMessage());
}
}
}