1 |
package appl.parallel.gui; |
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.awt.event.ActionListener; |
5 |
import java.util.Vector; |
6 |
|
7 |
import javax.swing.JOptionPane; |
8 |
import javax.swing.JTable; |
9 |
import javax.swing.event.ListSelectionEvent; |
10 |
import javax.swing.event.ListSelectionListener; |
11 |
import javax.swing.event.TableModelEvent; |
12 |
import javax.swing.event.TableModelListener; |
13 |
import javax.swing.table.DefaultTableModel; |
14 |
|
15 |
import org.apache.log4j.LogManager; |
16 |
import org.apache.log4j.Logger; |
17 |
|
18 |
import appl.ext.XuluConfig; |
19 |
import appl.parallel.ComputingResourceContainer; |
20 |
import appl.parallel.ComputingResourceProperties; |
21 |
import appl.parallel.client.RemoteExecutionController; |
22 |
import appl.parallel.client.ResourceChangeListener; |
23 |
import appl.util.NonEditableTableModel; |
24 |
import edu.bonn.xulu.XuluModellingPlatform; |
25 |
|
26 |
/** |
27 |
* This class controls the {@link ParallelControlPanel}. |
28 |
* |
29 |
* @author Dominik Appl |
30 |
*/ |
31 |
public class ParallelControlPanelEngine implements ActionListener, |
32 |
ListSelectionListener, TableModelListener, ResourceChangeListener { |
33 |
|
34 |
ParallelControlPanel panel; |
35 |
|
36 |
XuluModellingPlatform xulu; |
37 |
|
38 |
// get logger for this class |
39 |
protected final Logger LOG = LogManager |
40 |
.getLogger(this.getClass().getName()); |
41 |
|
42 |
private DefaultTableModel resTableModel; |
43 |
|
44 |
private JTable resTable; |
45 |
|
46 |
private Vector<ComputingResourceContainer> resources; |
47 |
|
48 |
private RemoteExecutionController remoteExecutionController; |
49 |
|
50 |
/** |
51 |
* @param xulu |
52 |
* the active modeling platform |
53 |
*/ |
54 |
public ParallelControlPanelEngine(XuluModellingPlatform xulu) { |
55 |
this.xulu = xulu; |
56 |
this.panel = new ParallelControlPanel(); |
57 |
init(); |
58 |
} |
59 |
|
60 |
/** |
61 |
* @param xulu |
62 |
* the active modeling platform |
63 |
* @param panel |
64 |
* the panel to integrate |
65 |
*/ |
66 |
public ParallelControlPanelEngine(XuluModellingPlatform xulu, |
67 |
ParallelControlPanel panel) { |
68 |
this.xulu = xulu; |
69 |
this.panel = panel; |
70 |
init(); |
71 |
} |
72 |
|
73 |
/** |
74 |
* The init method must be called after all Components of the GUI are |
75 |
* initialized. |
76 |
*/ |
77 |
void init() { |
78 |
remoteExecutionController = RemoteExecutionController |
79 |
.getRemoteExecutionController(xulu); |
80 |
if (remoteExecutionController != null) |
81 |
remoteExecutionController.addResourceChangeListener(this); |
82 |
resTable = panel.computingResourcesList; |
83 |
|
84 |
// add listeners |
85 |
resTable.getSelectionModel().addListSelectionListener(this); |
86 |
panel.refreshButton.addActionListener(this); |
87 |
panel.selectAllButton.addActionListener(this); |
88 |
|
89 |
resTableModel = new NonEditableTableModel(new Object[] { "Name", |
90 |
"Rating" }, new boolean[] { false, true }, 1); |
91 |
resTable.setModel(resTableModel); |
92 |
resTable.getModel().addTableModelListener(this); |
93 |
selectAvailableResources(); |
94 |
} |
95 |
|
96 |
/* |
97 |
* (non-Javadoc) |
98 |
* |
99 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) |
100 |
*/ |
101 |
public void actionPerformed(ActionEvent e) { |
102 |
// the source of the event. |
103 |
Object src = e.getSource(); |
104 |
if (src == panel.refreshButton) { |
105 |
handleRefresh(); |
106 |
} else if (src == panel.selectAllButton) { |
107 |
selectAvailableResources(); |
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
/** |
113 |
* handles the refresh button |
114 |
*/ |
115 |
private void handleRefresh() { |
116 |
if (remoteExecutionController != null) |
117 |
remoteExecutionController.refreshResources(); |
118 |
else |
119 |
JOptionPane |
120 |
.showMessageDialog( |
121 |
getPanel(), |
122 |
"Error: no discovery possible. The RemoteExecutionController-Plugin can " + |
123 |
"not be found. The plugin is required for parallel " + |
124 |
"computation","Error",JOptionPane.ERROR_MESSAGE); |
125 |
} |
126 |
|
127 |
private Vector<String> getVectorOfSelectedNames() { |
128 |
|
129 |
Object[] selectedComputingResources = getSelectedResourceContainers(); |
130 |
|
131 |
// write selected names in vector |
132 |
Vector<String> names = new Vector<String>(); |
133 |
for (int i = 0; i < selectedComputingResources.length; i++) { |
134 |
names |
135 |
.add(((ComputingResourceContainer) selectedComputingResources[i]) |
136 |
.getInformation().getName()); |
137 |
} |
138 |
return names; |
139 |
} |
140 |
|
141 |
private void setSelectedElements(Vector<String> names) { |
142 |
for (int i = 0; i < resTableModel.getRowCount(); i++) { |
143 |
ComputingResourceContainer container = (ComputingResourceContainer) resTableModel |
144 |
.getValueAt(i, 0); |
145 |
if (container == null) |
146 |
return; |
147 |
String name = ((ComputingResourceContainer) resTableModel |
148 |
.getValueAt(i, 0)).getInformation().getName(); |
149 |
if (names.contains(name)) |
150 |
resTable.getSelectionModel().addSelectionInterval(i, i); |
151 |
} |
152 |
} |
153 |
|
154 |
private void selectAvailableResources() { |
155 |
// write available selected names in vector |
156 |
Vector<String> names = new Vector<String>(); |
157 |
for (int i = 0; i < resTableModel.getRowCount(); i++) { |
158 |
ComputingResourceContainer container = ((ComputingResourceContainer) resTableModel |
159 |
.getValueAt(i, 0)); |
160 |
if (container == null) |
161 |
return; |
162 |
ComputingResourceProperties info = container.getInformation(); |
163 |
if (info.isAvailable()) |
164 |
names.add(info.getName()); |
165 |
|
166 |
} |
167 |
setSelectedElements(names); |
168 |
} |
169 |
|
170 |
/** |
171 |
* @return the first current selected ComputingResourceContainer from the |
172 |
* resouce list |
173 |
*/ |
174 |
private ComputingResourceContainer getSelectedResourceContainer() { |
175 |
int selectedRow = resTable.getSelectedRow(); |
176 |
if (selectedRow == -1) |
177 |
return null; |
178 |
return (ComputingResourceContainer) resTableModel.getValueAt( |
179 |
selectedRow, 0); |
180 |
} |
181 |
|
182 |
/** |
183 |
* @return all current selected ComputingResourceContainer from the resouce |
184 |
* list |
185 |
*/ |
186 |
ComputingResourceContainer[] getSelectedResourceContainers() { |
187 |
int[] selRows = panel.computingResourcesList.getSelectedRows(); |
188 |
Object[] selectedValues = new Object[selRows.length]; |
189 |
for (int i = 0; i < selRows.length; i++) { |
190 |
selectedValues[i] = resTableModel.getValueAt(selRows[i], 0); |
191 |
if (selectedValues[i] == null) |
192 |
return new ComputingResourceContainer[0]; |
193 |
} |
194 |
ComputingResourceContainer[] selectedContainers = new ComputingResourceContainer[selectedValues.length]; |
195 |
for (int i = 0; i < selectedValues.length; i++) |
196 |
selectedContainers[i] = (ComputingResourceContainer) selectedValues[i]; |
197 |
return selectedContainers; |
198 |
} |
199 |
|
200 |
/* |
201 |
* (non-Javadoc) |
202 |
* |
203 |
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent) |
204 |
*/ |
205 |
public void valueChanged(ListSelectionEvent e) { |
206 |
Object src = e.getSource(); |
207 |
if (src == resTable.getSelectionModel()) { |
208 |
ComputingResourceContainer selValue = getSelectedResourceContainer(); |
209 |
if (selValue == null) |
210 |
return; |
211 |
panel.propertyTable.setPropertyData(selValue.getInformation() |
212 |
.getProperties()); |
213 |
} |
214 |
} |
215 |
|
216 |
/** |
217 |
* Listens for user changes of the rating |
218 |
* |
219 |
* @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent) |
220 |
*/ |
221 |
public void tableChanged(TableModelEvent e) { |
222 |
// enter the new rating information to the configuration |
223 |
XuluConfig config = XuluConfig.getXuluConfig(); |
224 |
if (e.getType() == TableModelEvent.UPDATE) { |
225 |
int changedRow = e.getFirstRow(); |
226 |
// only the 2nd col is editable |
227 |
String newRating = resTableModel.getValueAt(changedRow, 1) |
228 |
.toString(); |
229 |
try { |
230 |
int rating = Integer.valueOf(newRating); |
231 |
if (rating < 0) { |
232 |
resTableModel.setValueAt(0, changedRow, 1); |
233 |
throw new UnsupportedOperationException( |
234 |
"Rating must be > 0"); |
235 |
} |
236 |
|
237 |
} catch (Exception f) { |
238 |
resTableModel.setValueAt(0, changedRow, 1); |
239 |
throw new UnsupportedOperationException( |
240 |
"Rating must be an integer!"); |
241 |
} |
242 |
String computerName = resTableModel.getValueAt(changedRow, 0) |
243 |
.toString(); |
244 |
String prefix = "PerformanceRating"; |
245 |
String key = prefix + "." + computerName; |
246 |
config.setProperty(key, newRating); |
247 |
} |
248 |
} |
249 |
|
250 |
/** |
251 |
* @return the panel |
252 |
*/ |
253 |
public ParallelControlPanel getPanel() { |
254 |
return this.panel; |
255 |
} |
256 |
|
257 |
/* |
258 |
* (non-Javadoc) |
259 |
* |
260 |
* @see appl.parallel.client.ResourceChangeListener#updateResources(java.util.Vector) |
261 |
*/ |
262 |
public void updateResources( |
263 |
Vector<ComputingResourceContainer> currentResources) { |
264 |
// get the selected values (and restore the selection after list |
265 |
// initializing) |
266 |
Vector names = getVectorOfSelectedNames(); |
267 |
|
268 |
// clear list |
269 |
resTableModel.setRowCount(0); |
270 |
for (ComputingResourceContainer res : currentResources) { |
271 |
// init the rating from the configuration |
272 |
XuluConfig config = XuluConfig.getXuluConfig(); |
273 |
String computerName = res.getInformation().getName(); |
274 |
String prefix = "PerformanceRating"; |
275 |
String key = prefix + "." + computerName; |
276 |
String rating = config.getProperty(key, false); |
277 |
// if no rating found use the selfrating of the machine |
278 |
if (rating == null || rating.equals("0")) |
279 |
rating = res.getInformation().getProperty("Rating"); |
280 |
if (rating == null) |
281 |
rating = "0"; // '0' means average |
282 |
resTableModel.addRow(new Object[] { res, rating }); |
283 |
} |
284 |
|
285 |
// restore selection |
286 |
this.setSelectedElements(names); |
287 |
} |
288 |
} |