1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.ui;
20
21 import java.awt.Point;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import javax.swing.JButton;
28 import javax.swing.JDialog;
29 import javax.swing.JFrame;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JScrollPane;
33 import javax.swing.JTable;
34 import javax.swing.SpringLayout;
35 import javax.swing.table.DefaultTableModel;
36
37 public abstract class DebugDialog extends JDialog{
38
39 private TestCaseResultsPane resultsPane;
40 private boolean step;
41
42 public DebugDialog(Object source, JFrame rootFrame, TestCaseResultsPane tcrf, String title){
43 super(rootFrame);
44 setBreakPointSource(source);
45 resultsPane = tcrf;
46 Point rootLocation = rootFrame.getLocation();
47 int height = (int)(rootFrame.getHeight()/2);
48 int width = (int)(rootFrame.getWidth()/2);
49 int x = (int)(rootLocation.getX() + width/2);
50 int y = (int)(rootLocation.getY() + height/4 + 20);
51 setBounds(x, y, width, height);
52 setTitle(title);
53 setModal(true);
54 init();
55 setVisible(true);
56 }
57
58 /***
59 * Sets the source where the data is pulled and where the data
60 * needs to be changed if any values are changed in the table.
61 * @param source - The tag that represents this debug dialog
62 */
63 protected abstract void setBreakPointSource(Object source);
64 /***
65 * Gets the key-value pairs that are set via this tag
66 * @return the key-value pairs that are set via this tag
67 */
68 protected abstract Map getAttributes();
69
70 /***
71 * Gets the description that shows up in the dialog box
72 * @return the description that shows up in the dialog box
73 */
74 protected abstract String getTagDescription();
75
76 /***
77 * Changes the value of the given attribute's name real-time
78 * @param attributeName - The name of the attribute to change
79 * @param newValue - The new value to set the attribute to
80 */
81 protected abstract void setNewAttributeValue(String attributeName, String newValue);
82
83 private void init(){
84 final DefaultTableModel attributesM = new DefaultTableModel(new Object[]{"Variable", "Value"}, 0);
85 Map attributes = getAttributes();
86 Iterator it = attributes.keySet().iterator();
87 while (it.hasNext()) {
88 Object[] values = new Object[2];
89 values[0] = it.next();
90 values[1] = attributes.get(values[0]);
91 attributesM.addRow(values);
92 }
93 JTable attTable = new JTable(attributesM);
94 JPanel attPanel = new JPanel(new SpringLayout());
95 JPanel bttnPanel = new JPanel(new SpringLayout());
96
97 attPanel.setPreferredSize(getSize());
98 attPanel.add(new JLabel("Tag: "+getTagDescription()));
99 attPanel.add(new JScrollPane(attTable));
100 JButton goButton = new JButton("Go");
101 goButton.addActionListener(new ActionListener(){
102 public void actionPerformed(ActionEvent e) {
103 registerNewAttributes(attributesM,false);
104 }
105 });
106
107 bttnPanel.add(goButton);
108 JButton stepButton = new JButton("Step");
109 stepButton.addActionListener(new ActionListener(){
110 public void actionPerformed(ActionEvent e) {
111 registerNewAttributes(attributesM,true);
112 }
113 });
114
115 bttnPanel.add(stepButton);
116 SpringUtilities.makeCompactGrid(bttnPanel,
117 1, 2,
118 6, 6,
119 6, 6);
120 attPanel.add(bttnPanel);
121 SpringUtilities.makeCompactGrid(attPanel,
122 3, 1,
123 6, 6,
124 6, 6);
125
126 getContentPane().add(attPanel);
127
128 pack();
129 }
130
131 private void registerNewAttributes(DefaultTableModel attributesM, boolean stepNextTag){
132 resultsPane.setStepNextTag(stepNextTag);
133 while (attributesM.getRowCount() > 0) {
134 String name = (String) attributesM.getValueAt(0,0);
135 Object val = attributesM.getValueAt(0,1);
136 if (val instanceof String) {
137 setNewAttributeValue(name, (String)val);
138 }
139 attributesM.removeRow(0);
140 }
141 dispose();
142 }
143
144 public boolean isStep(){
145 return step;
146 }
147
148 }