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.jdt.core.IMethod;
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.Event;
016: import org.eclipse.swt.widgets.Label;
017: import org.eclipse.swt.widgets.Listener;
018: import org.eclipse.swt.widgets.Table;
019: import org.eclipse.swt.widgets.TableColumn;
020: import org.eclipse.swt.widgets.TableItem;
021: import org.terracotta.dso.TcPlugin;
022: import org.terracotta.dso.editors.chooser.ExpressionChooser;
023: import org.terracotta.dso.editors.chooser.MethodBehavior;
024: import org.terracotta.dso.editors.chooser.NavigatorBehavior;
025: import org.terracotta.ui.util.SWTUtil;
026:
027: import com.tc.util.event.UpdateEvent;
028: import com.tc.util.event.UpdateEventListener;
029: import com.terracottatech.config.DistributedMethods;
030: import com.terracottatech.config.DsoApplication;
031: import com.terracottatech.config.DistributedMethods.MethodExpression;
032:
033: public class DistributedMethodsPanel extends ConfigurationEditorPanel {
034: private IProject m_project;
035: private DsoApplication m_dsoApp;
036: private DistributedMethods m_distributedMethods;
037:
038: private Layout m_layout;
039:
040: private AddHandler m_addHandler;
041: private RemoveHandler m_removeHandler;
042: private TableSelectionListener m_tableSelectionListener;
043: private TableDataListener m_tableDataListener;
044:
045: public DistributedMethodsPanel(Composite parent, int style) {
046: super (parent, style);
047: m_layout = new Layout(this );
048: m_addHandler = new AddHandler();
049: m_removeHandler = new RemoveHandler();
050: m_tableSelectionListener = new TableSelectionListener();
051: m_tableDataListener = new TableDataListener();
052: }
053:
054: public boolean hasAnySet() {
055: return m_distributedMethods != null
056: && m_distributedMethods.sizeOfMethodExpressionArray() > 0;
057: }
058:
059: private DistributedMethods ensureDistributedMethods() {
060: if (m_distributedMethods == null) {
061: ensureXmlObject();
062: }
063: return m_distributedMethods;
064: }
065:
066: public void ensureXmlObject() {
067: super .ensureXmlObject();
068:
069: if (m_distributedMethods == null) {
070: removeListeners();
071: m_distributedMethods = m_dsoApp.addNewDistributedMethods();
072: updateChildren();
073: addListeners();
074: }
075: }
076:
077: private void testRemoveDistributedMethods() {
078: if (!hasAnySet() && m_dsoApp.getDistributedMethods() != null) {
079: m_dsoApp.unsetDistributedMethods();
080: m_distributedMethods = null;
081: fireXmlObjectStructureChanged(m_dsoApp);
082: }
083: fireDistributedMethodsChanged();
084: testDisableRemoveButton();
085: }
086:
087: private void testDisableRemoveButton() {
088: m_layout.m_removeButton.setEnabled(m_layout.m_table
089: .getSelectionCount() > 0);
090: }
091:
092: private void addListeners() {
093: m_layout.m_addButton.addSelectionListener(m_addHandler);
094: m_layout.m_removeButton.addSelectionListener(m_removeHandler);
095: m_layout.m_table.addSelectionListener(m_tableSelectionListener);
096: m_layout.m_table.addListener(SWT.SetData, m_tableDataListener);
097: }
098:
099: private void removeListeners() {
100: m_layout.m_addButton.removeSelectionListener(m_addHandler);
101: m_layout.m_removeButton
102: .removeSelectionListener(m_removeHandler);
103: m_layout.m_table
104: .removeSelectionListener(m_tableSelectionListener);
105: m_layout.m_table.removeListener(SWT.SetData,
106: m_tableDataListener);
107: }
108:
109: public void updateChildren() {
110: initTableItems();
111: testDisableRemoveButton();
112: }
113:
114: public void updateModel() {
115: removeListeners();
116: updateChildren();
117: addListeners();
118: }
119:
120: public void setup(IProject project, DsoApplication dsoApp) {
121: setEnabled(true);
122: removeListeners();
123:
124: m_project = project;
125: m_dsoApp = dsoApp;
126: m_distributedMethods = m_dsoApp != null ? m_dsoApp
127: .getDistributedMethods() : null;
128:
129: updateChildren();
130: addListeners();
131: }
132:
133: public void tearDown() {
134: removeListeners();
135:
136: m_dsoApp = null;
137: m_distributedMethods = null;
138:
139: setEnabled(false);
140: }
141:
142: private void initTableItems() {
143: m_layout.m_table.removeAll();
144: if (m_distributedMethods == null)
145: return;
146: MethodExpression[] methods = m_distributedMethods
147: .getMethodExpressionArray();
148: for (int i = 0; i < methods.length; i++) {
149: createTableItem(methods[i]);
150: }
151: if (methods.length > 0) {
152: m_layout.m_table.setSelection(0);
153: }
154: }
155:
156: private void createTableItem(MethodExpression method) {
157: TableItem item = new TableItem(m_layout.m_table, SWT.NONE);
158: item.setText(method.getStringValue());
159: item.setData(method);
160: }
161:
162: public boolean isDistributed(IMethod method) {
163: return TcPlugin.getDefault().getConfigurationHelper(m_project)
164: .isDistributedMethod(method);
165: }
166:
167: private void internalAddDistributed(String expr) {
168: if (expr != null && expr.length() > 0) {
169: DistributedMethods dms = ensureDistributedMethods();
170: MethodExpression me = dms.addNewMethodExpression();
171: me.setStringValue(expr);
172: createTableItem(me);
173: }
174: }
175:
176: private static class Layout {
177: private static final String DISTRIBUTED_METHODS = "Distributed Methods";
178: private static final String ADD = "Add...";
179: private static final String REMOVE = "Remove";
180:
181: private Button m_addButton;
182: private Button m_removeButton;
183: private Table m_table;
184:
185: public void reset() {
186: m_removeButton.setEnabled(false);
187: m_table.removeAll();
188: }
189:
190: private Layout(Composite parent) {
191: Composite comp = new Composite(parent, SWT.NONE);
192: GridLayout gridLayout = new GridLayout(2, false);
193: gridLayout.marginWidth = gridLayout.marginHeight = 10;
194: comp.setLayout(gridLayout);
195:
196: Composite sidePanel = new Composite(comp, SWT.NONE);
197: gridLayout = new GridLayout();
198: gridLayout.marginWidth = gridLayout.marginHeight = 0;
199: sidePanel.setLayout(gridLayout);
200: sidePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
201:
202: Label label = new Label(sidePanel, SWT.NONE);
203: label.setText(DISTRIBUTED_METHODS);
204: label.setLayoutData(new GridData(
205: GridData.VERTICAL_ALIGN_BEGINNING));
206:
207: m_table = new Table(sidePanel, SWT.BORDER | SWT.MULTI
208: | SWT.FULL_SELECTION | SWT.V_SCROLL);
209: m_table.setHeaderVisible(true);
210: m_table.setLinesVisible(true);
211: SWTUtil.makeTableColumnsResizeWeightedWidth(sidePanel,
212: m_table, new int[] { 3, 1 });
213: SWTUtil.makeTableColumnsEditable(m_table, new int[] { 0 });
214: GridData gridData = new GridData(GridData.FILL_BOTH);
215: gridData.heightHint = SWTUtil.tableRowsToPixels(m_table, 3);
216: m_table.setLayoutData(gridData);
217:
218: TableColumn column = new TableColumn(m_table, SWT.NONE);
219: column.setText(DISTRIBUTED_METHODS);
220: column.pack();
221:
222: Composite buttonPanel = new Composite(comp, SWT.NONE);
223: gridLayout = new GridLayout();
224: gridLayout.marginWidth = gridLayout.marginHeight = 0;
225: buttonPanel.setLayout(gridLayout);
226: buttonPanel.setLayoutData(new GridData(
227: GridData.VERTICAL_ALIGN_BEGINNING));
228:
229: new Label(buttonPanel, SWT.NONE); // filler
230:
231: m_addButton = new Button(buttonPanel, SWT.PUSH);
232: m_addButton.setText(ADD);
233: m_addButton.setLayoutData(new GridData(
234: GridData.VERTICAL_ALIGN_END));
235: SWTUtil.applyDefaultButtonSize(m_addButton);
236:
237: m_removeButton = new Button(buttonPanel, SWT.PUSH);
238: m_removeButton.setText(REMOVE);
239: m_removeButton.setEnabled(false);
240: m_removeButton.setLayoutData(new GridData(
241: GridData.VERTICAL_ALIGN_BEGINNING));
242: SWTUtil.applyDefaultButtonSize(m_removeButton);
243: }
244: }
245:
246: class AddHandler extends SelectionAdapter {
247: public void widgetSelected(SelectionEvent e) {
248: m_layout.m_table.forceFocus();
249: NavigatorBehavior behavior = new MethodBehavior();
250: ExpressionChooser chooser = new ExpressionChooser(
251: getShell(), behavior.getTitle(),
252: MethodBehavior.ADD_MSG, m_project, behavior);
253: chooser.addValueListener(new UpdateEventListener() {
254: public void handleUpdate(UpdateEvent updateEvent) {
255: String[] items = (String[]) updateEvent.data;
256: for (int i = 0; i < items.length; i++) {
257: internalAddDistributed(items[i]);
258: }
259: fireDistributedMethodsChanged();
260: }
261: });
262: chooser.open();
263: }
264: }
265:
266: class RemoveHandler extends SelectionAdapter {
267: public void widgetSelected(SelectionEvent e) {
268: m_layout.m_table.forceFocus();
269: int[] selection = m_layout.m_table.getSelectionIndices();
270:
271: for (int i = selection.length - 1; i >= 0; i--) {
272: ensureDistributedMethods().removeMethodExpression(
273: selection[i]);
274: }
275: m_layout.m_table.remove(selection);
276: testRemoveDistributedMethods();
277: }
278: }
279:
280: class TableSelectionListener extends SelectionAdapter {
281: public void widgetSelected(SelectionEvent e) {
282: m_layout.m_removeButton.setEnabled(true);
283: }
284: }
285:
286: class TableDataListener implements Listener {
287: public void handleEvent(Event e) {
288: TableItem item = (TableItem) e.item;
289: MethodExpression expr = (MethodExpression) item.getData();
290: expr.setStringValue(item.getText(e.index));
291: fireDistributedMethodsChanged();
292: }
293: }
294: }
|