001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.editors;
006:
007: import org.eclipse.core.resources.IProject;
008: import org.eclipse.swt.SWT;
009: import org.eclipse.swt.events.SelectionAdapter;
010: import org.eclipse.swt.events.SelectionEvent;
011: import org.eclipse.swt.layout.GridData;
012: import org.eclipse.swt.layout.GridLayout;
013: import org.eclipse.swt.widgets.Button;
014: import org.eclipse.swt.widgets.Composite;
015: import org.eclipse.swt.widgets.Label;
016: import org.eclipse.swt.widgets.Text;
017: import org.terracotta.dso.TcPlugin;
018: import org.terracotta.dso.editors.chooser.FolderBehavior;
019: import org.terracotta.dso.editors.chooser.NavigatorBehavior;
020: import org.terracotta.dso.editors.chooser.PackageNavigator;
021: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureChangeEvent;
022: import org.terracotta.dso.editors.xmlbeans.XmlObjectStructureListener;
023: import org.terracotta.dso.editors.xmlbeans.XmlStringField;
024: import org.terracotta.ui.util.SWTUtil;
025:
026: import com.tc.util.event.UpdateEvent;
027: import com.tc.util.event.UpdateEventListener;
028: import com.terracottatech.config.Client;
029: import com.terracottatech.config.TcConfigDocument.TcConfig;
030:
031: public class ClientsPanel extends ConfigurationEditorPanel implements
032: ConfigurationEditorRoot, XmlObjectStructureListener {
033: private IProject m_project;
034: private TcConfig m_config;
035: private Client m_client;
036:
037: private Layout m_layout;
038:
039: private LogsBrowseSelectionHandler m_logsBrowseSelectionHandler;
040:
041: public ClientsPanel(Composite parent, int style) {
042: super (parent, style);
043: m_layout = new Layout(this );
044: m_logsBrowseSelectionHandler = new LogsBrowseSelectionHandler();
045: SWTUtil.setBGColorRecurse(this .getDisplay().getSystemColor(
046: SWT.COLOR_WHITE), this );
047: }
048:
049: public boolean hasAnySet() {
050: return m_client.isSetDso() || m_client.isSetLogs()
051: || m_layout.m_modulesPanel.hasAnySet();
052: }
053:
054: public void ensureXmlObject() {
055: super .ensureXmlObject();
056:
057: if (m_client == null) {
058: removeListeners();
059: m_client = m_config.addNewClients();
060: updateChildren();
061: addListeners();
062: }
063: }
064:
065: private void testUnsetClients() {
066: if (!hasAnySet() && m_config.getClients() != null) {
067: m_config.unsetClients();
068: m_client = null;
069: fireXmlObjectStructureChanged();
070: updateChildren();
071: }
072: fireClientChanged();
073: }
074:
075: private void fireXmlObjectStructureChanged() {
076: fireXmlObjectStructureChanged(m_config);
077: }
078:
079: public void structureChanged(XmlObjectStructureChangeEvent e) {
080: testUnsetClients();
081: }
082:
083: private void addListeners() {
084: m_layout.m_logsBrowse
085: .addSelectionListener(m_logsBrowseSelectionHandler);
086: ((XmlStringField) m_layout.m_logsLocation.getData())
087: .addXmlObjectStructureListener(this );
088: m_layout.m_dsoClientDataPanel
089: .addXmlObjectStructureListener(this );
090: m_layout.m_modulesPanel.addXmlObjectStructureListener(this );
091:
092: }
093:
094: private void removeListeners() {
095: m_layout.m_logsBrowse
096: .removeSelectionListener(m_logsBrowseSelectionHandler);
097: ((XmlStringField) m_layout.m_logsLocation.getData())
098: .removeXmlObjectStructureListener(this );
099: m_layout.m_dsoClientDataPanel
100: .removeXmlObjectStructureListener(this );
101: m_layout.m_modulesPanel.removeXmlObjectStructureListener(this );
102: }
103:
104: private void updateChildren() {
105: m_layout.setup(m_client);
106: }
107:
108: public void setup(IProject project) {
109: TcPlugin plugin = TcPlugin.getDefault();
110:
111: removeListeners();
112: setEnabled(true);
113:
114: m_project = project;
115: m_config = plugin.getConfiguration(project);
116: m_client = m_config != null ? m_config.getClients() : null;
117:
118: updateChildren();
119: addListeners();
120: }
121:
122: public IProject getProject() {
123: return m_project;
124: }
125:
126: public void tearDown() {
127: removeListeners();
128: m_layout.tearDown();
129: setEnabled(false);
130: }
131:
132: private class Layout {
133: private static final String LOGS_LOCATION = "Logs Location";
134: private static final String BROWSE = "Browse...";
135:
136: private Button m_logsBrowse;
137: private Text m_logsLocation;
138: private DsoClientDataPanel m_dsoClientDataPanel;
139: private ModulesPanel m_modulesPanel;
140:
141: void setup(Client client) {
142: ((XmlStringField) m_logsLocation.getData()).setup(client);
143: m_dsoClientDataPanel.setup(client);
144: m_modulesPanel.setup(client);
145: }
146:
147: public void reset() {
148: m_logsLocation.setText("");
149: m_logsLocation.setEnabled(false);
150: }
151:
152: void tearDown() {
153: ((XmlStringField) m_logsLocation.getData()).tearDown();
154: m_dsoClientDataPanel.tearDown();
155: m_modulesPanel.tearDown();
156: }
157:
158: private Layout(Composite parent) {
159: parent.setLayout(new GridLayout());
160:
161: Composite panel = new Composite(parent, SWT.NONE);
162: GridLayout gridLayout = new GridLayout();
163: gridLayout.marginWidth = gridLayout.marginHeight = 10;
164: panel.setLayout(gridLayout);
165: panel.setLayoutData(new GridData(GridData.FILL_BOTH));
166:
167: Composite comp = new Composite(panel, SWT.NONE);
168: gridLayout = new GridLayout(3, false);
169: gridLayout.marginWidth = gridLayout.marginHeight = 0;
170: comp.setLayout(gridLayout);
171: comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
172:
173: Label logsLabel = new Label(comp, SWT.NONE);
174: logsLabel.setText(LOGS_LOCATION);
175:
176: m_logsLocation = new Text(comp, SWT.BORDER);
177: m_logsLocation.setLayoutData(new GridData(
178: GridData.FILL_HORIZONTAL));
179: initStringField(m_logsLocation, Client.class, "logs");
180:
181: m_logsBrowse = new Button(comp, SWT.PUSH);
182: m_logsBrowse.setText(BROWSE);
183: SWTUtil.applyDefaultButtonSize(m_logsBrowse);
184:
185: m_dsoClientDataPanel = new DsoClientDataPanel(panel,
186: SWT.NONE);
187: m_dsoClientDataPanel.setLayout(new GridLayout());
188: m_dsoClientDataPanel.setLayoutData(new GridData(
189: GridData.FILL_HORIZONTAL));
190:
191: m_modulesPanel = new ModulesPanel(panel, SWT.NONE);
192: m_modulesPanel.setLayout(new GridLayout());
193: m_modulesPanel.setLayoutData(new GridData(
194: GridData.FILL_BOTH));
195: }
196: }
197:
198: class LogsBrowseSelectionHandler extends SelectionAdapter {
199: public void widgetSelected(SelectionEvent e) {
200: NavigatorBehavior behavior = new FolderBehavior();
201: PackageNavigator dialog = new PackageNavigator(getShell(),
202: behavior.getTitle(), m_project, behavior);
203: dialog.addValueListener(new UpdateEventListener() {
204: public void handleUpdate(UpdateEvent event) {
205: ensureXmlObject();
206: m_client.setLogs((String) event.data);
207: fireClientChanged();
208: }
209: });
210: dialog.open();
211: }
212: }
213: }
|