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