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.apache.xmlbeans.XmlObject;
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.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.FieldBehavior;
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.DsoApplication;
030: import com.terracottatech.config.QualifiedFieldName;
031: import com.terracottatech.config.TransientFields;
032:
033: public class TransientFieldsPanel extends ConfigurationEditorPanel {
034: private IProject m_project;
035: private DsoApplication m_dsoApp;
036: private TransientFields m_transientFields;
037:
038: private final 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 TransientFieldsPanel(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_transientFields != null
056: && m_transientFields.sizeOfFieldNameArray() > 0;
057: }
058:
059: private TransientFields ensureTransientFields() {
060: if (m_transientFields == null) {
061: ensureXmlObject();
062: }
063: return m_transientFields;
064: }
065:
066: public void ensureXmlObject() {
067: super .ensureXmlObject();
068:
069: if (m_transientFields == null) {
070: removeListeners();
071: m_transientFields = m_dsoApp.addNewTransientFields();
072: updateChildren();
073: addListeners();
074: }
075: }
076:
077: private void testRemoveTransientFields() {
078: if (!hasAnySet() && m_dsoApp.getTransientFields() != null) {
079: m_dsoApp.unsetTransientFields();
080: m_transientFields = null;
081: fireXmlObjectStructureChanged(m_dsoApp);
082: }
083: fireTransientFieldsChanged();
084: testDisableRemoveButton();
085: }
086:
087: private void addListeners() {
088: m_layout.m_addButton.addSelectionListener(m_addHandler);
089: m_layout.m_removeButton.addSelectionListener(m_removeHandler);
090: m_layout.m_table.addSelectionListener(m_tableSelectionListener);
091: m_layout.m_table.addListener(SWT.SetData, m_tableDataListener);
092: }
093:
094: private void removeListeners() {
095: m_layout.m_addButton.removeSelectionListener(m_addHandler);
096: m_layout.m_removeButton
097: .removeSelectionListener(m_removeHandler);
098: m_layout.m_table
099: .removeSelectionListener(m_tableSelectionListener);
100: m_layout.m_table.removeListener(SWT.SetData,
101: m_tableDataListener);
102: }
103:
104: public void updateChildren() {
105: initTableItems();
106: testDisableRemoveButton();
107: }
108:
109: public void updateModel() {
110: removeListeners();
111: updateChildren();
112: addListeners();
113: }
114:
115: public void setup(IProject project, DsoApplication dsoApp) {
116: setEnabled(true);
117: removeListeners();
118:
119: m_project = project;
120: m_dsoApp = dsoApp;
121: m_transientFields = m_dsoApp != null ? m_dsoApp
122: .getTransientFields() : null;
123:
124: updateChildren();
125: addListeners();
126: }
127:
128: public void tearDown() {
129: removeListeners();
130:
131: m_dsoApp = null;
132: m_transientFields = null;
133:
134: setEnabled(false);
135: }
136:
137: private void initTableItems() {
138: m_layout.m_table.removeAll();
139: if (m_transientFields == null)
140: return;
141: XmlObject[] fields = m_transientFields.selectPath("*");
142: for (int i = 0; i < fields.length; i++) {
143: createTableItem((QualifiedFieldName) fields[i]);
144: }
145: if (fields.length > 0) {
146: m_layout.m_table.setSelection(0);
147: }
148: }
149:
150: private void initTableItem(TableItem item, QualifiedFieldName qfn) {
151: item.setText(qfn.getStringValue());
152: }
153:
154: private void updateTableItem(int index) {
155: TableItem item = m_layout.m_table.getItem(index);
156: initTableItem(item, (QualifiedFieldName) item.getData());
157: }
158:
159: private void createTableItem(QualifiedFieldName qfn) {
160: TableItem item = new TableItem(m_layout.m_table, SWT.NONE);
161: initTableItem(item, qfn);
162: item.setData(qfn);
163: }
164:
165: private void internalAddTransient(String fieldName) {
166: TransientFields transientFields = ensureTransientFields();
167: transientFields.addFieldName(fieldName);
168: createTableItem(transientFields
169: .xgetFieldNameArray(transientFields
170: .sizeOfFieldNameArray() - 1));
171:
172: int row = m_layout.m_table.getItemCount() - 1;
173: m_layout.m_table.setSelection(row);
174: }
175:
176: public boolean isTransient(String fieldName) {
177: return TcPlugin.getDefault().getConfigurationHelper(m_project)
178: .isTransient(fieldName);
179: }
180:
181: private static class Layout {
182: private static final String TRANSIENT_FIELDS = "Transient Fields";
183: private static final String ADD = "Add...";
184: private static final String REMOVE = "Remove";
185:
186: private final Button m_addButton;
187: private final Button m_removeButton;
188: private final Table m_table;
189:
190: public void reset() {
191: m_removeButton.setEnabled(false);
192: m_table.removeAll();
193: }
194:
195: private Layout(Composite parent) {
196: Composite comp = new Composite(parent, SWT.NONE);
197: GridLayout gridLayout = new GridLayout(2, false);
198: gridLayout.marginWidth = gridLayout.marginHeight = 10;
199: comp.setLayout(gridLayout);
200:
201: Composite sidePanel = new Composite(comp, SWT.NONE);
202: gridLayout = new GridLayout();
203: gridLayout.marginWidth = gridLayout.marginHeight = 0;
204: sidePanel.setLayout(gridLayout);
205: sidePanel.setLayoutData(new GridData(GridData.FILL_BOTH));
206:
207: Label label = new Label(sidePanel, SWT.NONE);
208: label.setText(TRANSIENT_FIELDS);
209: label.setLayoutData(new GridData(
210: GridData.VERTICAL_ALIGN_BEGINNING));
211:
212: m_table = new Table(sidePanel, SWT.BORDER | SWT.MULTI
213: | SWT.FULL_SELECTION | SWT.V_SCROLL);
214: m_table.setHeaderVisible(true);
215: m_table.setLinesVisible(true);
216: SWTUtil.makeTableColumnsResizeWeightedWidth(sidePanel,
217: m_table, new int[] { 3, 1 });
218: SWTUtil.makeTableColumnsEditable(m_table, new int[] { 0 });
219: GridData gridData = new GridData(GridData.FILL_BOTH);
220: gridData.heightHint = SWTUtil.tableRowsToPixels(m_table, 3);
221: m_table.setLayoutData(gridData);
222:
223: TableColumn column = new TableColumn(m_table, SWT.NONE);
224: column.setText(TRANSIENT_FIELDS);
225: column.pack();
226:
227: Composite buttonPanel = new Composite(comp, SWT.NONE);
228: gridLayout = new GridLayout();
229: gridLayout.marginWidth = gridLayout.marginHeight = 0;
230: buttonPanel.setLayout(gridLayout);
231: buttonPanel.setLayoutData(new GridData(
232: GridData.VERTICAL_ALIGN_BEGINNING));
233:
234: new Label(buttonPanel, SWT.NONE); // filler
235:
236: m_addButton = new Button(buttonPanel, SWT.PUSH);
237: m_addButton.setText(ADD);
238: m_addButton.setLayoutData(new GridData(
239: GridData.VERTICAL_ALIGN_END));
240: SWTUtil.applyDefaultButtonSize(m_addButton);
241:
242: m_removeButton = new Button(buttonPanel, SWT.PUSH);
243: m_removeButton.setText(REMOVE);
244: m_removeButton.setEnabled(false);
245: m_removeButton.setLayoutData(new GridData(
246: GridData.VERTICAL_ALIGN_BEGINNING));
247: SWTUtil.applyDefaultButtonSize(m_removeButton);
248: }
249: }
250:
251: private void testDisableRemoveButton() {
252: m_layout.m_removeButton.setEnabled(m_layout.m_table
253: .getSelectionCount() > 0);
254: }
255:
256: private void handleTableSelection() {
257: m_layout.m_removeButton.setEnabled(true);
258: }
259:
260: class AddHandler extends SelectionAdapter {
261: public void widgetSelected(SelectionEvent e) {
262: m_layout.m_table.forceFocus();
263: NavigatorBehavior behavior = new FieldBehavior();
264: ExpressionChooser chooser = new ExpressionChooser(
265: getShell(), behavior.getTitle(),
266: FieldBehavior.ADD_MSG, m_project, behavior);
267: chooser.addValueListener(new UpdateEventListener() {
268: public void handleUpdate(UpdateEvent updateEvent) {
269: String[] items = (String[]) updateEvent.data;
270: for (int i = 0; i < items.length; i++) {
271: internalAddTransient(items[i]);
272: }
273: fireTransientFieldsChanged();
274: }
275: });
276: chooser.open();
277: }
278: }
279:
280: class RemoveHandler extends SelectionAdapter {
281: public void widgetSelected(SelectionEvent e) {
282: m_layout.m_table.setRedraw(false);
283: try {
284: m_layout.m_table.forceFocus();
285: int[] selection = m_layout.m_table
286: .getSelectionIndices();
287:
288: for (int i = selection.length - 1; i >= 0; i--) {
289: ensureTransientFields().removeFieldName(
290: selection[i]);
291: }
292: m_layout.m_table.remove(selection);
293: testRemoveTransientFields();
294: handleTableSelection();
295: } finally {
296: m_layout.m_table.setRedraw(true);
297: }
298: }
299: }
300:
301: class TableSelectionListener extends SelectionAdapter {
302: public void widgetSelected(SelectionEvent e) {
303: handleTableSelection();
304: }
305: }
306:
307: class TableDataListener implements Listener {
308: public void handleEvent(Event e) {
309: TableItem item = (TableItem) e.item;
310: int index = m_layout.m_table.getSelectionIndex();
311: QualifiedFieldName qfn = (QualifiedFieldName) item
312: .getData();
313: qfn.setStringValue(item.getText(e.index));
314: fireTransientFieldChanged(index);
315: }
316: }
317:
318: public void transientFieldChanged(IProject project, int index) {
319: if (project.equals(getProject())) {
320: int selIndex = m_layout.m_table.getSelectionIndex();
321: updateTableItem(index);
322: if (selIndex != -1) {
323: m_layout.m_table.setSelection(selIndex);
324: }
325: }
326: }
327: }
|