001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.servers;
005:
006: import org.dijon.Button;
007: import org.dijon.ComboBox;
008: import org.dijon.ComboModel;
009: import org.dijon.Dialog;
010: import org.dijon.DialogResource;
011: import org.dijon.Frame;
012: import org.dijon.Label;
013:
014: import com.tc.admin.common.XObjectTable;
015:
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import java.lang.reflect.Method;
019: import java.util.Properties;
020:
021: import javax.swing.ComboBoxModel;
022: import javax.swing.Icon;
023: import javax.swing.JButton;
024: import javax.swing.event.TableModelEvent;
025: import javax.swing.event.TableModelListener;
026: import javax.swing.table.TableCellEditor;
027:
028: public class ServersDialog extends Dialog {
029: private ServerInfo[] m_servers;
030: private ComboBox m_selector;
031: private ServerEnvTableModel m_envTableModel;
032: private XObjectTable m_envTable;
033: private Label m_errorLabel;
034: private Icon m_errorIcon;
035: private Button m_restoreButton;
036:
037: public ServersDialog(Frame frame) {
038: super (frame, true);
039: }
040:
041: public void load(DialogResource dialogRes) {
042: super .load(dialogRes);
043:
044: m_selector = (ComboBox) findComponent("ServerSelector");
045: m_selector.addActionListener(new ActionListener() {
046: public void actionPerformed(ActionEvent ae) {
047: finishEditing();
048: updateEnvironmentTable();
049: }
050: });
051:
052: m_envTable = (XObjectTable) findComponent("ServerEnvironment");
053: m_envTable
054: .setModel(m_envTableModel = new ServerEnvTableModel());
055: ((JButton) findComponent("CancelButton"))
056: .addActionListener(new ActionListener() {
057: public void actionPerformed(ActionEvent ae) {
058: setVisible(false);
059: }
060: });
061: m_envTableModel.addTableModelListener(new TableModelListener() {
062: public void tableChanged(TableModelEvent e) {
063: validateModel();
064: }
065: });
066:
067: m_errorLabel = (Label) findComponent("ErrorLabel");
068: m_errorIcon = m_errorLabel.getIcon();
069: m_errorLabel.setIcon(null);
070:
071: try {
072: String methodName = "setAlwaysOnTop";
073: Class[] argTypes = new Class[] { Boolean.class };
074: Method method = getClass().getMethod(methodName, argTypes);
075:
076: if (method != null) {
077: method.invoke(this , new Object[] { Boolean.TRUE });
078: }
079: } catch (Exception e) {/**/
080: }
081:
082: m_restoreButton = (Button) findComponent("RestoreButton");
083: m_restoreButton.addActionListener(new ActionListener() {
084: public void actionPerformed(ActionEvent ae) {
085: int index = getSelectedServerIndex();
086: ServerInfo server = getSelectedServer();
087: Properties props = ServerSelection.getInstance()
088: .getDefaultProperties(index);
089:
090: server.setProperties(props);
091: m_envTableModel.set(server.getProperties());
092: }
093: });
094: }
095:
096: public void addAcceptListener(ActionListener listener) {
097: ((JButton) findComponent("OKButton"))
098: .addActionListener(listener);
099: }
100:
101: public void setSelection(ServerSelection selection) {
102: setServers(selection.cloneServers());
103: m_selector.setSelectedIndex(selection.getSelectedServerIndex());
104: }
105:
106: private void updateEnvironmentTable() {
107: m_envTableModel.set(getSelectedServer().getProperties());
108: }
109:
110: private void setServers(ServerInfo[] servers) {
111: m_selector.setModel(new ComboModel(m_servers = servers));
112: updateEnvironmentTable();
113: }
114:
115: public ServerInfo[] getServers() {
116: return m_servers;
117: }
118:
119: public int getSelectedServerIndex() {
120: return m_selector.getSelectedIndex();
121: }
122:
123: public ServerInfo getSelectedServer() {
124: return (ServerInfo) m_selector.getSelectedItem();
125: }
126:
127: public ServerInfo getServer(String name) {
128: ComboBoxModel model = m_selector.getModel();
129: ServerInfo server;
130:
131: for (int i = 0; i < model.getSize(); i++) {
132: server = (ServerInfo) model.getElementAt(i);
133:
134: if (server.getName().equals(name)) {
135: return server;
136: }
137: }
138:
139: return null;
140: }
141:
142: public Properties getServerProperties(String name) {
143: ServerInfo server = getServer(name);
144: return server != null ? server.toProperties() : null;
145: }
146:
147: public void finishEditing() {
148: if (m_envTable.isEditing()) {
149: TableCellEditor editor = m_envTable.getCellEditor();
150:
151: if (!editor.stopCellEditing()) {
152: editor.cancelCellEditing();
153: }
154: }
155: }
156:
157: private void validateModel() {
158: ServerInfo server = getSelectedServer();
159: String[] messages = server.validateProperties();
160: String msg = null;
161: Icon icon = null;
162:
163: if (messages != null) {
164: msg = messages[0];
165: icon = m_errorIcon;
166: }
167:
168: m_errorLabel.setText(msg);
169: m_errorLabel.setIcon(icon);
170:
171: }
172: }
|