1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package IceGrid.TreeNode;
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.JPopupMenu;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.Icon;
import javax.swing.JMenuItem;
import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.tree.DefaultTreeCellRenderer;
import IceGrid.TreeModelI;
import IceGrid.IceBoxDescriptor;
import IceGrid.ServerInstanceDescriptor;
import IceGrid.ServerState;
import IceGrid.TemplateDescriptor;
import IceGrid.Utils;
import IceGrid.Model;
import IceGrid.ServerDynamicInfo;
class ServerInstance extends Parent
{
static class PopupMenu extends JPopupMenu
{
PopupMenu()
{
_startAction = new AbstractAction("Start")
{
public void actionPerformed(ActionEvent e)
{
_server.start();
}
};
_stopAction = new AbstractAction("Stop")
{
public void actionPerformed(ActionEvent e)
{
_server.stop();
}
};
add(_startAction);
add(_stopAction);
}
void setServer(ServerInstance server)
{
_server = server;
ServerState state = _server.getState();
boolean canStart = (_server.getState() == ServerState.Inactive);
_startAction.setEnabled(canStart);
_stopAction.setEnabled(!canStart);
}
private ServerInstance _server;
private Action _startAction;
private Action _stopAction;
}
public JPopupMenu getPopupMenu()
{
if(_popup == null)
{
_popup = new PopupMenu();
}
_popup.setServer(this);
return _popup;
}
public JPanel getProperties(int view)
{
return null;
}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus)
{
if(_cellRenderer == null)
{
//
// Initialization
//
_cellRenderer = new DefaultTreeCellRenderer();
_icons = new Icon[7];
_icons[0] = Utils.getIcon("/icons/unknown.png");
_icons[ServerState.Inactive.value() + 1] = Utils.getIcon("/icons/inactive.png");
_icons[ServerState.Activating.value() + 1] = Utils.getIcon("/icons/activating.png");
_icons[ServerState.Active.value() + 1] = Utils.getIcon("/icons/active.png");
_icons[ServerState.Deactivating.value() + 1] = Utils.getIcon("/icons/deactivating.png");
_icons[ServerState.Destroying.value() + 1] = Utils.getIcon("/icons/destroying.png");
_icons[ServerState.Destroyed.value() + 1] = Utils.getIcon("/icons/destroyed.png");
}
//
// TODO: separate icons for open and close
//
if(expanded)
{
_cellRenderer.setOpenIcon(_icons[_stateIconIndex]);
}
else
{
_cellRenderer.setClosedIcon(_icons[_stateIconIndex]);
}
_cellRenderer.setToolTipText(_toolTip);
return _cellRenderer.getTreeCellRendererComponent(
tree, value, sel, expanded, leaf, row, hasFocus);
}
//
// Builds the server instance and all its sub-tree
//
ServerInstance(ServerInstanceDescriptor descriptor,
Model model,
boolean fireNodeViewEvent)
{
super(descriptor.descriptor.name, model);
rebuild(descriptor, fireNodeViewEvent);
}
void removeFromNode()
{
removeFromNode(true);
}
private void removeFromNode(boolean fireNodeViewEvent)
{
if(_serviceInstances != null)
{
_serviceInstances.unregisterAdapters();
}
if(_adapters != null)
{
_adapters.unregisterAll();
}
Node node = (Node)getParent(TreeModelI.NODE_VIEW);
if(node != null)
{
node.removeChild(_id, fireNodeViewEvent);
}
}
//
// Update the server instance and all its subtree
//
void rebuild(ServerInstanceDescriptor newDescriptor,
boolean fireNodeViewEvent)
{
assert(newDescriptor != null);
removeFromNode(fireNodeViewEvent);
clearChildren();
//
// First check if my node changed [node view]
// For the application view, the only way a server instance can change application
// is by being removed and then re-added (i.e. not with an update)
//
boolean newNode = false;
if(_descriptor == null)
{
newNode = true;
}
else if(_descriptor.node != newDescriptor.node)
{
newNode = true;
//
// Remove from existing node
//
CommonBase parent = _parents[TreeModelI.NODE_VIEW];
assert(parent != null); // must be connected
removeParent(parent);
((Parent)parent).removeChild(this);
}
_descriptor = newDescriptor;
Node node = _model.getNodeViewRoot().findNode(_descriptor.node);
if(_descriptor.descriptor instanceof IceBoxDescriptor)
{
_iceBoxDescriptor = (IceBoxDescriptor)_descriptor.descriptor;
//
// We need to pass the node to register the adapters
//
_serviceInstances = new ServiceInstances(_iceBoxDescriptor.services,
_model, node);
addChild(_serviceInstances);
_serviceInstances.addParent(this); // no-op when newNode == true
}
else
{
_iceBoxDescriptor = null;
}
_adapters = new Adapters(_descriptor.descriptor.adapters, _model, node);
addChild(_adapters);
_adapters.addParent(this); // no-op when newNode == true
_dbEnvs = new DbEnvs(_descriptor.descriptor.dbEnvs, _model, false);
addChild(_dbEnvs);
_dbEnvs.addParent(this); // no-op when newNode == true
if(newNode)
{
updateDynamicInfo(node.getServerDynamicInfo(_id), false);
addParent(node); // propagates to children
node.addChild(this, fireNodeViewEvent);
}
}
void updateDynamicInfo(ServerDynamicInfo info)
{
updateDynamicInfo(info, true);
}
private void updateDynamicInfo(ServerDynamicInfo info, boolean fireEvent)
{
if(info.state != _state || info.pid != _pid)
{
_state = info.state;
_pid = info.pid;
_toolTip = toolTip(info.state, info.pid);
_stateIconIndex = (_state == null ? 0 : _state.value() + 1);
if(fireEvent)
{
//
// Change the node representation in all views
//
fireNodeChangedEvent(this);
}
}
}
void start()
{
//
// TODO: if this can take a long time, make the invocation in a separate thread
//
boolean started = false;
try
{
_model.getStatusBar().setText("Starting server '" + _id + "'...");
started = _model.getAdmin().startServer(_id);
}
catch(IceGrid.ServerNotExistException e)
{
_model.getStatusBar().setText("Server '" + _id + "' no longer exists.");
}
catch(IceGrid.NodeUnreachableException e)
{
_model.getStatusBar().setText("Could not reach the node for server '" + _id + "'.");
}
catch(Ice.LocalException e)
{
_model.getStatusBar().setText("Starting server '" + _id + "'... failed: " + e.toString());
}
if(started)
{
_model.getStatusBar().setText("Starting server '" + _id + "'... success!");
}
else
{
_model.getStatusBar().setText("Starting server '" + _id + "'... failed!");
}
}
void stop()
{
try
{
_model.getStatusBar().setText("Stopping server '" + _id + "'...");
_model.getAdmin().stopServer(_id);
}
catch(IceGrid.ServerNotExistException e)
{
_model.getStatusBar().setText("Server '" + _id + "' no longer exists.");
}
catch(IceGrid.NodeUnreachableException e)
{
_model.getStatusBar().setText("Could not reach the node for server '" + _id + "'.");
}
catch(Ice.LocalException e)
{
_model.getStatusBar().setText("Stopping server '" + _id + "'... failed: " + e.toString());
}
_model.getStatusBar().setText("Stopping server '" + _id + "'... done.");
}
ServerState getState()
{
return _state;
}
public String toString()
{
String result = _descriptor.descriptor.name;
if(!_descriptor.template.equals(""))
{
result += ": " + templateLabel(_descriptor.template,
_descriptor.parameterValues.values());
}
return result;
}
private static String toolTip(ServerState state, int pid)
{
String result = (state == null ? "Unknown" : state.toString());
if(pid != 0)
{
result += ", pid: " + pid;
}
return result;
}
private ServerState _state = null;
private int _stateIconIndex = 0;
private int _pid = 0;
private String _toolTip = toolTip(_state, _pid);
private ServerInstanceDescriptor _descriptor;
private TemplateDescriptor _templateDescriptor;
private IceBoxDescriptor _iceBoxDescriptor;
//
// Children
//
private ServiceInstances _serviceInstances;
private Adapters _adapters;
private DbEnvs _dbEnvs;
static private DefaultTreeCellRenderer _cellRenderer;
static private Icon[] _icons;
static private PopupMenu _popup;
}
|