1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.experiment.gui.views.helpers;
20
21 import java.awt.Color;
22 import java.awt.Font;
23 import java.awt.GridBagConstraints;
24 import java.awt.Insets;
25
26 import javax.swing.BorderFactory;
27 import javax.swing.JLabel;
28 import javax.swing.JPanel;
29 import javax.swing.JScrollPane;
30 import javax.swing.JTextArea;
31 import javax.swing.ScrollPaneConstants;
32
33 import jgroup.relacs.config.ExperimentConfig;
34
35 import org.w3c.dom.CDATASection;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39
40
41
42
43 public class ScriptPanelManager implements PanelManager
44 {
45
46
47
48
49
50 private JPanel scriptPanel = null;
51
52 private JLabel scriptLabel;
53 private JTextArea scriptArea;
54 private JScrollPane scriptPane;
55
56
57
58
59
60
61 private String script;
62
63
64
65
66
67
68 public ScriptPanelManager(JPanel parentPanel)
69 {
70 scriptPanel = parentPanel;
71 }
72
73
74
75
76
77
78
79
80
81 public void makePanel()
82 {
83 initComponents();
84 addComponents();
85 }
86
87
88
89
90
91 public void updatePanel(ExperimentConfig expConfig)
92 {
93 script = expConfig.getRepeatScript();
94 cleanScript();
95 scriptArea.setText(script);
96 }
97
98
99
100
101
102 public void setParentPanel(JPanel panel)
103 {
104 if(scriptPanel == null)
105 throw new IllegalArgumentException("JPanel expPanel has already been set.");
106
107 scriptPanel = panel;
108 }
109
110
111
112
113
114 public void clear()
115 {
116 scriptArea.setText("");
117 }
118
119
120 public void save(Element parentElement, Document document)
121 {
122 Element repeatScriptElement = document.createElement("RepeatScript");
123 CDATASection cdata = document.createCDATASection("\n" + scriptArea.getText());
124 repeatScriptElement.appendChild(cdata);
125 parentElement.appendChild(repeatScriptElement);
126 }
127
128
129
130
131
132
133 private void initComponents() {
134 scriptPanel.setBorder(BorderFactory.createCompoundBorder(
135 BorderFactory.createEtchedBorder(),
136 BorderFactory.createEmptyBorder(5,5,5,5)));
137
138 scriptLabel = new JLabel("Repeat script:");
139 scriptLabel.setBorder(BorderFactory.createEmptyBorder(5,20,5,20));
140
141 scriptArea = new JTextArea();
142 scriptArea.setFont(new Font("Monospace", Font.BOLD, 12));
143
144 scriptPane = new JScrollPane(scriptArea);
145 scriptPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
146 scriptPane.setBorder(BorderFactory.createCompoundBorder(
147 BorderFactory.createLineBorder(Color.BLACK,2),
148 BorderFactory.createEtchedBorder()));
149 }
150
151
152 private void addComponents() {
153 GridBagConstraints gbc = new GridBagConstraints();
154
155 gbc.anchor = GridBagConstraints.FIRST_LINE_START;
156 scriptPanel.add(scriptLabel, gbc);
157
158 gbc.gridy = 1;
159 gbc.anchor = GridBagConstraints.CENTER;
160 gbc.fill = GridBagConstraints.BOTH;
161 gbc.weightx = 0.1;
162 gbc.weighty = 0.1;
163 gbc.insets = new Insets(5,20,20,20);
164 scriptPanel.add(scriptPane, gbc);
165 }
166
167
168 private void cleanScript()
169 {
170
171 script = script.replaceAll("\t", " ");
172
173 String[] scriptLines = script.split("\n");
174 char[] chars = null;
175
176
177
178 int lineIndex = -1;
179 int characterIndex = -1;
180 for (int i = 0; i < scriptLines.length; i++) {
181 chars = scriptLines[i].toCharArray();
182 for (int j = 0; j < chars.length; j++) {
183 if(Character.isWhitespace(chars[j]) == false) {
184 lineIndex = i;
185 characterIndex = j;
186 break;
187 }
188 }
189
190 if(lineIndex > -1)
191 break;
192 }
193
194
195
196
197 StringBuilder temp = new StringBuilder();
198 for (int i = lineIndex; i < scriptLines.length; i++) {
199 if(scriptLines[i].length() > characterIndex)
200 temp.append(scriptLines[i].substring(characterIndex) + '\n');
201 else
202 temp.append(scriptLines[i]);
203 }
204
205
206
207 script = new String(temp);
208 }
209 }