1 |
package appl.parallel.starter.client; |
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Component; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.rmi.RemoteException; |
8 |
import java.util.Vector; |
9 |
|
10 |
import javax.swing.DefaultListCellRenderer; |
11 |
import javax.swing.JList; |
12 |
|
13 |
import org.apache.log4j.LogManager; |
14 |
import org.apache.log4j.Logger; |
15 |
|
16 |
import appl.parallel.services.HostnameDiscoveryService; |
17 |
import appl.parallel.starter.Starter; |
18 |
import appl.parallel.starter.server.XuluServerStarter; |
19 |
|
20 |
/** |
21 |
* This is the Controller for the {@link XuluStarterControllerFrame}, which is |
22 |
* the user interface for the {@link XuluServerStarter}. All events are handled |
23 |
* here. <br> |
24 |
* |
25 |
* The controller updates a list of {@link Starter}s it finds in the network. |
26 |
* At the moment is uses the {@link HostnameDiscoveryService} to discover |
27 |
* starters. |
28 |
* |
29 |
* @see HostnameDiscoveryService#getStarterContainers() |
30 |
* @author Dominik Appl |
31 |
*/ |
32 |
public class XuluStarterController implements ActionListener { |
33 |
|
34 |
Logger LOG = LogManager.getLogger(this.getClass().getName()); |
35 |
|
36 |
private XuluStarterClientPanel GUI; |
37 |
|
38 |
private HostnameDiscoveryService discovery; |
39 |
|
40 |
private Vector<StarterContainer> currentData; |
41 |
|
42 |
/** |
43 |
* |
44 |
*/ |
45 |
public XuluStarterController() { |
46 |
GUI = new XuluStarterClientPanel(); |
47 |
// start simpleDiscovery service |
48 |
discovery = new HostnameDiscoveryService(); |
49 |
discovery.startService(); |
50 |
updateList(); |
51 |
initGUI(); |
52 |
} |
53 |
|
54 |
private void initGUI() { |
55 |
GUI.startButton.addActionListener(this); |
56 |
GUI.stopButton.addActionListener(this); |
57 |
GUI.refreshButton.addActionListener(this); |
58 |
GUI.restartButton.addActionListener(this); |
59 |
} |
60 |
|
61 |
public XuluStarterClientPanel getPanel() { |
62 |
return GUI; |
63 |
} |
64 |
|
65 |
/** |
66 |
* |
67 |
*/ |
68 |
private void updateList() { |
69 |
currentData = discovery.getStarterContainers(); |
70 |
GUI.serverList.setCellRenderer(new MyCellRenderer(currentData)); |
71 |
// DefaultListModel serverListModel = new DefaultListModel(); |
72 |
// GUI.serverList.setModel(serverListModel); |
73 |
GUI.serverList.setListData(currentData); |
74 |
} |
75 |
|
76 |
private Starter getSelectedStarter() { |
77 |
Object value = GUI.serverList.getSelectedValue(); |
78 |
if (value != null) { |
79 |
return ((StarterContainer) value).getStarter(); |
80 |
} |
81 |
return null; |
82 |
} |
83 |
|
84 |
public void actionPerformed(ActionEvent e) { |
85 |
if (e.getSource() == GUI.refreshButton) { |
86 |
updateList(); |
87 |
} |
88 |
Starter selectedStarter = getSelectedStarter(); |
89 |
// if nothing selected: return |
90 |
if (selectedStarter == null) |
91 |
return; |
92 |
try { |
93 |
if (e.getSource() == GUI.startButton) { |
94 |
selectedStarter.start(); |
95 |
updateList(); |
96 |
} |
97 |
if (e.getSource() == GUI.stopButton) { |
98 |
selectedStarter.stop(); |
99 |
updateList(); |
100 |
} |
101 |
if (e.getSource() == GUI.restartButton) { |
102 |
selectedStarter.restart(); |
103 |
updateList(); |
104 |
} |
105 |
} catch (RemoteException e1) { |
106 |
LOG |
107 |
.error( |
108 |
"Could not access selected Starter. Remote Exeception occured.", |
109 |
e1); |
110 |
} |
111 |
} |
112 |
|
113 |
class MyCellRenderer extends DefaultListCellRenderer { |
114 |
private final Vector<StarterContainer> containers; |
115 |
|
116 |
public MyCellRenderer(Vector<StarterContainer> containers) { |
117 |
this.containers = containers; |
118 |
// TODO Auto-generated constructor stub |
119 |
} |
120 |
|
121 |
@Override |
122 |
public Component getListCellRendererComponent(JList list, Object value, |
123 |
int index, boolean isSelected, boolean cellHasFocus) { |
124 |
// TODO Auto-generated method stub |
125 |
Component listCellRendererComponent = super |
126 |
.getListCellRendererComponent(list, value, index, |
127 |
isSelected, cellHasFocus); |
128 |
if (!isSelected) { |
129 |
if (containers.get(index).isServerRunning()) |
130 |
listCellRendererComponent.setBackground(Color.green); |
131 |
else |
132 |
listCellRendererComponent.setBackground(Color.red); |
133 |
} |
134 |
return listCellRendererComponent; |
135 |
} |
136 |
|
137 |
} |
138 |
} |