// ********************************************************************** // // 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.AbstractAction; import javax.swing.Action; import javax.swing.DefaultComboBoxModel; import javax.swing.Icon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.JTree; import javax.swing.UIManager; import javax.swing.tree.DefaultTreeCellRenderer; import com.jgoodies.forms.builder.DefaultFormBuilder; import com.jgoodies.forms.factories.Borders; import com.jgoodies.forms.layout.FormLayout; import com.jgoodies.forms.layout.Sizes; import com.jgoodies.forms.util.LayoutStyle; import com.jgoodies.uif_lite.panel.SimpleInternalFrame; import IceGrid.IceBoxDescriptor; import IceGrid.Model; import IceGrid.TemplateDescriptor; import IceGrid.TreeModelI; import IceGrid.ServerDescriptor; import IceGrid.ServerDynamicInfo; import IceGrid.ServerInstanceDescriptor; import IceGrid.ServerState; import IceGrid.Utils; class ServerInstance extends Parent { static class Editor { JComponent getComponent() { if(_scrollPane == null) { // // Build everything using JGoodies's DefaultFormBuilder // FormLayout layout = new FormLayout( "right:pref, 3dlu, fill:pref:grow, 3dlu, pref", ""); DefaultFormBuilder builder = new DefaultFormBuilder(layout); builder.setBorder(Borders.DLU2_BORDER); builder.setRowGroupingEnabled(true); builder.setLineGapSize(LayoutStyle.getCurrent().getLinePad()); // // Type combox box // builder.append("Type"); builder.append(_type, 3); builder.nextLine(); // // Template combox box // builder.append(_templateLabel, _template, _templateButton); builder.nextLine(); // // Parameters // builder.append(_parameterValuesLabel, _parameterValues, _parameterValuesButton); builder.nextLine(); builder.setLineGapSize(LayoutStyle.getCurrent().getParagraphPad()); // // Node combo box // builder.append("Node"); builder.append(_node, _nodeButton); builder.nextLine(); // // Targets field // builder.append("Targets"); _targets.setToolTipText("XML-descriptor targets used to deploy this server"); builder.append(_targets, 3); _targets.setEditable(false); builder.nextLine(); _serverDescriptorEditor.build(builder); _scrollPane = new JScrollPane(builder.getPanel(), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); _scrollPane.setBorder(Borders.DIALOG_BORDER); } return _scrollPane; } void show(ServerInstance server) { _server = server; ServerInstanceDescriptor descriptor = server.getDescriptor(); _node.setModel(_server.getModel().getNodeViewRoot().getComboBoxModel()); _node.setSelectedItem(_server.getParent(TreeModelI.NODE_VIEW)); ServerDescriptor serverDescriptor = null; Ice.StringHolder toolTipHolder = new Ice.StringHolder(); java.util.Map[] variables = null; if(descriptor.template.length() == 0) { serverDescriptor = descriptor.descriptor; if(_server.getModel().substituteVariables()) { variables = new java.util.Map[] { server.getNodeVariables(), server.getApplication().getVariables()}; } if(serverDescriptor instanceof IceBoxDescriptor) { _type.setSelectedIndex(ICEBOX); } else { _type.setSelectedIndex(PLAIN_SERVER); } _templateLabel.setEnabled(false); _template.setEnabled(false); _template.setModel(new DefaultComboBoxModel()); _template.setSelectedItem(null); _templateButton.setEnabled(false); _parameterValuesLabel.setEnabled(false); _parameterValues.setEnabled(false); _parameterValues.setText(""); _parameterValues.setToolTipText(null); _parameterValuesButton.setEnabled(false); } else { _type.setSelectedIndex(TEMPLATE_INSTANCE); _templateLabel.setEnabled(true); _template.setEnabled(true); _templateButton.setEnabled(true); Application application = _server.getApplication(); TemplateDescriptor templateDescriptor = application.findServerTemplateDescriptor(descriptor.template); _template.setModel(application.getServerTemplates().getComboBoxModel()); _template.setSelectedItem(templateDescriptor); serverDescriptor = (ServerDescriptor)templateDescriptor.descriptor; _parameterValuesLabel.setEnabled(true); _parameterValues.setEnabled(true); _parameterValuesButton.setEnabled(true); String stringifiedParameterValues; if(_server.getModel().substituteVariables()) { java.util.Map substitutedParameters = Utils.substituteVariables( descriptor.parameterValues, _server.getNodeVariables(), application.getVariables()); stringifiedParameterValues = Utils.stringify(substitutedParameters, "=", ", ", toolTipHolder); _parameterValues.setEditable(false); variables = new java.util.Map[]{ substitutedParameters, server.getNodeVariables(), application.getVariables()}; } else { stringifiedParameterValues = Utils.stringify(descriptor.parameterValues, "=", ", ", toolTipHolder); _parameterValues.setEditable(true); } _parameterValues.setText(stringifiedParameterValues); _parameterValues.setToolTipText(toolTipHolder.value); } _targets.setText(Utils.stringify(descriptor.targets, ", ", toolTipHolder)); _targets.setToolTipText(toolTipHolder.value); _serverDescriptorEditor.show(serverDescriptor, variables); } Editor(boolean editDetails) { _serverDescriptorEditor = new ServerDescriptorEditor(editDetails); // // gotoTemplate action // AbstractAction gotoTemplate = new AbstractAction("->") { public void actionPerformed(ActionEvent e) { ServerTemplate template = (ServerTemplate) _template.getSelectedItem(); if(template != null && _server != null) { _server.getModel().getTreeNodeSelector(). selectNode( template.getPath( TreeModelI.APPLICATION_VIEW), TreeModelI.APPLICATION_VIEW); } } }; gotoTemplate.putValue(Action.SHORT_DESCRIPTION, "Goto this template"); _templateButton = new JButton(gotoTemplate); _parameterValuesButton = new JButton("..."); // // gotoNode action // AbstractAction gotoNode = new AbstractAction("->") { public void actionPerformed(ActionEvent e) { Node node = (Node)_node.getSelectedItem(); if(node != null) { // // I have the node but I really want to get to the NodeVar; // too bad node does not contain them // if(_server != null) { CommonBase parent = _server.getParent( TreeModelI.APPLICATION_VIEW); if(parent != null) { Application app = (Application)parent.getParent( TreeModelI.APPLICATION_VIEW); if(app != null) { NodeVar nodeVar = app.findNodeVar( node.getId()); // // TODO: offer to create a new nodeVar when // nodeVar == null // if(nodeVar != null) { _server.getModel().getTreeNodeSelector(). selectNode( nodeVar.getPath( TreeModelI.APPLICATION_VIEW), TreeModelI.APPLICATION_VIEW); } } } } } } }; gotoNode.putValue(Action.SHORT_DESCRIPTION, "Show the variables for this node"); _nodeButton = new JButton(gotoNode); } private final int PLAIN_SERVER = 0; private final int ICEBOX = 1; private final int TEMPLATE_INSTANCE = 2; private String[] _typeList = {"Plain Server", "IceBox Server", "Template Instance"}; private JComboBox _type = new JComboBox(_typeList); private JLabel _templateLabel = new JLabel("Template"); private JComboBox _template = new JComboBox(); private JButton _templateButton; private JLabel _parameterValuesLabel = new JLabel("Parameters"); private JTextField _parameterValues = new JTextField(20); private JButton _parameterValuesButton; private JComboBox _node = new JComboBox(); private JButton _nodeButton; private JTextField _targets = new JTextField(20); private ServerDescriptorEditor _serverDescriptorEditor; private ServerInstance _server; private JScrollPane _scrollPane; } 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 void displayProperties() { SimpleInternalFrame propertiesFrame = _model.getPropertiesFrame(); propertiesFrame.setTitle("Properties for " + _id); // // Pick the appropriate editor // Editor editor = null; if(_descriptor.template.length() > 0 || _model.substituteVariables()) { if(_editor == null) { _editor = new Editor(false); } editor = _editor; } else { if(_fullEditor == null) { _fullEditor = new Editor(true); } editor = _fullEditor; } editor.show(this); propertiesFrame.setContent(editor.getComponent()); propertiesFrame.validate(); propertiesFrame.repaint(); } 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(String serverName, ServerInstanceDescriptor descriptor, Application application, boolean fireNodeViewEvent) { super(serverName, application.getModel()); rebuild(application, 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(Application application, 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); java.util.Map appVariables = application.getVariables(); _nodeVariables = application.getNodeVariables(_descriptor.node); // can be null java.util.Map parameters = null; ServerDescriptor serverDescriptor = null; boolean editable = false; if(_descriptor.template.length() > 0) { TemplateDescriptor templateDescriptor = application.findServerTemplateDescriptor(_descriptor.template); serverDescriptor = (ServerDescriptor)templateDescriptor.descriptor; parameters = Utils.substituteVariables(_descriptor.parameterValues, _nodeVariables, appVariables); } else { serverDescriptor = _descriptor.descriptor; editable = true; } if(serverDescriptor instanceof IceBoxDescriptor) { IceBoxDescriptor iceBoxDescriptor = (IceBoxDescriptor)serverDescriptor; // // We need to pass the node to register the adapters // _serviceInstances = new ServiceInstances(iceBoxDescriptor.services, editable, application, _nodeVariables, parameters, node); addChild(_serviceInstances); _serviceInstances.addParent(this); // no-op when newNode == true } java.util.Map[] variables = new java.util.Map[3]; variables[0] = parameters; variables[1] = _nodeVariables; variables[2] = appVariables; _adapters = new Adapters(serverDescriptor.adapters, editable, variables, _model, node); addChild(_adapters); _adapters.addParent(this); // no-op when newNode == true _dbEnvs = new DbEnvs(serverDescriptor.dbEnvs, editable, variables, _model); 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; } ServerInstanceDescriptor getDescriptor() { return _descriptor; } Application getApplication() { CommonBase parent = getParent(TreeModelI.APPLICATION_VIEW); return (Application)parent.getParent(TreeModelI.APPLICATION_VIEW); } java.util.Map getNodeVariables() { return _nodeVariables; } 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 java.util.Map _nodeVariables; // // Children // private ServiceInstances _serviceInstances; private Adapters _adapters; private DbEnvs _dbEnvs; static private DefaultTreeCellRenderer _cellRenderer; static private Icon[] _icons; static private PopupMenu _popup; static private Editor _fullEditor; // writable server descriptor static private Editor _editor; // read-only server descriptor }