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 org.terracotta.dso.editors;
005:
006: import org.eclipse.core.resources.IProject;
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.events.SelectionAdapter;
009: import org.eclipse.swt.events.SelectionEvent;
010: import org.eclipse.swt.layout.GridData;
011: import org.eclipse.swt.layout.GridLayout;
012: import org.eclipse.swt.widgets.Button;
013: import org.eclipse.swt.widgets.Composite;
014: import org.eclipse.swt.widgets.Group;
015: import org.eclipse.swt.widgets.Table;
016: import org.eclipse.swt.widgets.TableColumn;
017: import org.eclipse.swt.widgets.TableItem;
018: import org.terracotta.dso.TcPlugin;
019: import org.terracotta.dso.editors.xmlbeans.XmlObjectHolderHelper;
020: import org.terracotta.ui.util.SWTLayout;
021: import org.terracotta.ui.util.SWTUtil;
022:
023: import com.terracottatech.config.Server;
024: import com.terracottatech.config.Servers;
025: import com.terracottatech.config.TcConfigDocument.TcConfig;
026:
027: public class ServersPanel extends ConfigurationEditorPanel implements
028: ConfigurationEditorRoot {
029: private IProject m_project;
030: private TcConfig m_config;
031: private Servers m_servers;
032:
033: private Layout m_layout;
034:
035: private TableSelectionHandler m_tableSelectionHandler;
036: private AddServerHandler m_addServerHandler;
037: private RemoveServerHandler m_removeServerHandler;
038:
039: private static final int NAME_INDEX = 0;
040: private static final int HOST_INDEX = 1;
041: private static final int DSO_PORT_INDEX = 2;
042: private static final int JMX_PORT_INDEX = 3;
043:
044: private static int DEFAULT_JMX_PORT;
045: private static int DEFAULT_DSO_PORT;
046:
047: static {
048: XmlObjectHolderHelper xmlHelper = new XmlObjectHolderHelper();
049: xmlHelper.init(Server.class, "dso-port");
050: DEFAULT_DSO_PORT = xmlHelper.defaultInteger();
051: xmlHelper.init(Server.class, "jmx-port");
052: DEFAULT_JMX_PORT = xmlHelper.defaultInteger();
053: }
054:
055: public ServersPanel(Composite parent, int style) {
056: super (parent, style);
057:
058: m_layout = new Layout(this );
059: m_tableSelectionHandler = new TableSelectionHandler();
060: m_addServerHandler = new AddServerHandler();
061: m_removeServerHandler = new RemoveServerHandler();
062:
063: SWTUtil.setBGColorRecurse(this .getDisplay().getSystemColor(
064: SWT.COLOR_WHITE), this );
065: }
066:
067: private void addListeners() {
068: m_layout.m_serverTable
069: .addSelectionListener(m_tableSelectionHandler);
070: m_layout.m_addServerButton
071: .addSelectionListener(m_addServerHandler);
072: m_layout.m_removeServerButton
073: .addSelectionListener(m_removeServerHandler);
074: }
075:
076: private void removeListeners() {
077: m_layout.m_serverTable
078: .removeSelectionListener(m_tableSelectionHandler);
079: m_layout.m_addServerButton
080: .removeSelectionListener(m_addServerHandler);
081: m_layout.m_removeServerButton
082: .removeSelectionListener(m_removeServerHandler);
083: }
084:
085: private void updateChildren() {
086: m_layout.m_serverTable.removeAll();
087: initTableItems();
088: testDisableRemoveButton();
089: m_layout.m_serverPanel.setVisible(haveAnyServers());
090: }
091:
092: public void setup(IProject project) {
093: TcPlugin plugin = TcPlugin.getDefault();
094:
095: setEnabled(true);
096: removeListeners();
097:
098: m_project = project;
099: m_config = plugin.getConfiguration(m_project);
100: m_servers = m_config != null ? m_config.getServers() : null;
101:
102: updateChildren();
103: addListeners();
104: handleTableSelection();
105: }
106:
107: public IProject getProject() {
108: return m_project;
109: }
110:
111: private Server getSelectedServer() {
112: int index = m_layout.m_serverTable.getSelectionIndex();
113: if (index != -1) {
114: TableItem item = m_layout.m_serverTable.getItem(index);
115: return (Server) item.getData();
116: } else {
117: return null;
118: }
119: }
120:
121: boolean haveAnyServers() {
122: return m_servers != null && m_servers.sizeOfServerArray() > 0;
123: }
124:
125: void testRemoveServers() {
126: if (m_servers != null && m_servers.sizeOfServerArray() == 0) {
127: m_config.unsetServers();
128: m_servers = null;
129: }
130: m_layout.m_serverPanel.setVisible(haveAnyServers());
131: fireServersChanged();
132: testDisableRemoveButton();
133: }
134:
135: private void testDisableRemoveButton() {
136: m_layout.m_removeServerButton.setEnabled(m_layout.m_serverTable
137: .getSelectionCount() > 0);
138: }
139:
140: public void tearDown() {
141: removeListeners();
142:
143: }
144:
145: private void initTableItems() {
146: m_layout.m_serverTable.removeAll();
147: if (m_servers == null)
148: return;
149: Server[] servers = m_servers.getServerArray();
150: for (int i = 0; i < servers.length; i++) {
151: createTableItem(servers[i]);
152: }
153: if (servers.length > 0) {
154: m_layout.m_serverTable.setSelection(0);
155: }
156: }
157:
158: private void initTableItem(TableItem item, Server server) {
159: item.setText(new String[] {
160: server.getName(),
161: server.getHost(),
162: Integer.toString(server.isSetDsoPort() ? server
163: .getDsoPort() : DEFAULT_DSO_PORT),
164: Integer.toString(server.isSetJmxPort() ? server
165: .getJmxPort() : DEFAULT_JMX_PORT) });
166: }
167:
168: private void updateTableItem(int index) {
169: TableItem item = m_layout.m_serverTable.getItem(index);
170: initTableItem(item, (Server) item.getData());
171: }
172:
173: private TableItem createTableItem(Server server) {
174: TableItem item = new TableItem(m_layout.m_serverTable, SWT.NONE);
175: initTableItem(item, server);
176: item.setData(server);
177: return item;
178: }
179:
180: private void handleTableSelection() {
181: Server server = getSelectedServer();
182: m_layout.m_removeServerButton.setEnabled(server != null);
183: m_layout.setServer(server);
184: }
185:
186: private static class Layout implements SWTLayout {
187: private static final String ADD = "Add...";
188: private static final String REMOVE = "Remove";
189: private static final String SERVERS = "Servers";
190: private static final String NAME = "Name";
191: private static final String HOST = "Host";
192: private static final String DSO_PORT = "DSO Port";
193: private static final String JMX_PORT = "JMX Port";
194:
195: private Table m_serverTable;
196: private Button m_addServerButton;
197: private Button m_removeServerButton;
198: private ServerPanel m_serverPanel;
199:
200: public void reset() {
201: m_serverTable.removeAll();
202: setServer(null);
203: }
204:
205: private void setServer(Server server) {
206: m_serverPanel.setVisible(server != null);
207: if (server != null) {
208: m_serverPanel.setup(server);
209: } else {
210: m_serverPanel.tearDown();
211: }
212: }
213:
214: private Layout(Composite parent) {
215: GridLayout gridLayout = new GridLayout();
216: gridLayout.marginWidth = gridLayout.marginHeight = 10;
217: parent.setLayout(gridLayout);
218:
219: createServersGroup(parent);
220: createServerGroup(parent);
221: }
222:
223: private void createServersGroup(Composite parent) {
224: Group serversGroup = new Group(parent, SWT.NONE);
225: serversGroup.setText(SERVERS);
226: GridLayout gridLayout = new GridLayout(2, false);
227: serversGroup.setLayout(gridLayout);
228: GridData gridData = new GridData(GridData.FILL_BOTH);
229: serversGroup.setLayoutData(gridData);
230:
231: m_serverTable = new Table(serversGroup, SWT.BORDER
232: | SWT.SINGLE | SWT.FULL_SELECTION | SWT.V_SCROLL);
233: m_serverTable.setHeaderVisible(true);
234: m_serverTable.setLinesVisible(true);
235: SWTUtil.makeTableColumnsResizeEqualWidth(serversGroup,
236: m_serverTable);
237: gridData = new GridData(GridData.FILL_BOTH);
238: gridData.heightHint = SWTUtil.tableRowsToPixels(
239: m_serverTable, 3);
240: m_serverTable.setLayoutData(gridData);
241:
242: TableColumn nameCol = new TableColumn(m_serverTable,
243: SWT.NONE, NAME_INDEX);
244: nameCol.setResizable(true);
245: nameCol.setText(NAME);
246: nameCol.pack();
247:
248: TableColumn hostCol = new TableColumn(m_serverTable,
249: SWT.NONE, HOST_INDEX);
250: hostCol.setResizable(true);
251: hostCol.setText(HOST);
252: hostCol.pack();
253:
254: TableColumn dsoPortCol = new TableColumn(m_serverTable,
255: SWT.NONE, DSO_PORT_INDEX);
256: dsoPortCol.setResizable(true);
257: dsoPortCol.setText(DSO_PORT);
258: dsoPortCol.pack();
259:
260: TableColumn jmxPortCol = new TableColumn(m_serverTable,
261: SWT.NONE, JMX_PORT_INDEX);
262: jmxPortCol.setResizable(true);
263: jmxPortCol.setText(JMX_PORT);
264: jmxPortCol.pack();
265:
266: Composite buttonPanel = new Composite(serversGroup,
267: SWT.NONE);
268: gridLayout = new GridLayout();
269: gridLayout.marginWidth = gridLayout.marginHeight = 0;
270: buttonPanel.setLayout(gridLayout);
271: buttonPanel.setLayoutData(new GridData(
272: GridData.VERTICAL_ALIGN_BEGINNING));
273:
274: m_addServerButton = new Button(buttonPanel, SWT.PUSH);
275: m_addServerButton.setText(ADD);
276: m_addServerButton.setLayoutData(new GridData(
277: GridData.VERTICAL_ALIGN_END));
278: SWTUtil.applyDefaultButtonSize(m_addServerButton);
279:
280: m_removeServerButton = new Button(buttonPanel, SWT.PUSH);
281: m_removeServerButton.setText(REMOVE);
282: m_removeServerButton.setLayoutData(new GridData(
283: GridData.VERTICAL_ALIGN_BEGINNING));
284: SWTUtil.applyDefaultButtonSize(m_removeServerButton);
285: }
286:
287: private void createServerGroup(Composite parent) {
288: m_serverPanel = new ServerPanel(parent, SWT.NONE);
289: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
290: m_serverPanel.setLayoutData(gridData);
291: }
292: }
293:
294: class TableSelectionHandler extends SelectionAdapter {
295: public void widgetSelected(SelectionEvent e) {
296: handleTableSelection();
297: }
298: }
299:
300: class AddServerHandler extends SelectionAdapter {
301: public void widgetSelected(SelectionEvent e) {
302: m_layout.m_serverTable.setRedraw(false);
303: if (m_servers == null) {
304: m_servers = m_config.addNewServers();
305: }
306:
307: Server server = m_servers.addNewServer();
308: server.setName("localhost");
309: server.setHost("localhost");
310: server.setDsoPort(9510);
311: server.setJmxPort(9520);
312:
313: createTableItem(server);
314: fireServersChanged();
315: m_layout.m_serverTable.setSelection(m_servers
316: .sizeOfServerArray() - 1);
317: m_layout.m_serverTable.forceFocus();
318: handleTableSelection();
319: m_layout.m_serverTable.setRedraw(true);
320: }
321: }
322:
323: class RemoveServerHandler extends SelectionAdapter {
324: public void widgetSelected(SelectionEvent e) {
325: m_layout.m_serverTable.setRedraw(false);
326: m_layout.m_serverTable.forceFocus();
327: int index = m_layout.m_serverTable.getSelectionIndex();
328: if (index != -1) {
329: m_servers.removeServer(index);
330: testRemoveServers();
331: int count = m_layout.m_serverTable.getItemCount();
332: if (count > 0) {
333: if (index >= count)
334: index = count - 1;
335: m_layout.m_serverTable.setSelection(index);
336: }
337: m_layout.m_serverTable.forceFocus();
338: handleTableSelection();
339: m_layout.m_serverTable.setRedraw(true);
340: }
341: }
342: }
343:
344: int indexOf(Server server) {
345: TableItem[] items = m_layout.m_serverTable.getItems();
346: for (int i = 0; i < items.length; i++) {
347: if (server == items[i].getData())
348: return i;
349: }
350: return -1;
351: }
352:
353: void fireServerChanged(Server server) {
354: fireServerChanged(indexOf(server));
355: }
356:
357: public void serverChanged(IProject project, int index) {
358: if (project.equals(getProject())) {
359: updateTableItem(index);
360: handleTableSelection();
361: }
362: }
363:
364: public void serversChanged(IProject project) {
365: if (project.equals(getProject())) {
366: initTableItems();
367: }
368: }
369: }
|