001: /*
002: *Copyright 2007 Wilken GmbH
003: */
004: package gui.section;
005:
006: import gui.actions.ActionUtil;
007:
008: import java.util.ArrayList;
009: import java.util.Arrays;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import newprocess.validation.EnvEntryValidatorImpl;
014:
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.emf.common.notify.AdapterFactory;
017: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
018: import org.eclipse.emf.edit.provider.IItemPropertySource;
019: import org.eclipse.emf.edit.ui.provider.PropertySource;
020: import org.eclipse.emf.transaction.TransactionalEditingDomain;
021: import org.eclipse.emf.transaction.util.TransactionUtil;
022: import org.eclipse.gef.EditPart;
023: import org.eclipse.gmf.runtime.diagram.ui.properties.sections.AbstractModelerPropertySection;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.jface.viewers.CellEditor;
026: import org.eclipse.jface.viewers.ICellEditorValidator;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.IStructuredContentProvider;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.jface.viewers.StructuredSelection;
031: import org.eclipse.jface.viewers.TableViewer;
032: import org.eclipse.jface.viewers.TextCellEditor;
033: import org.eclipse.jface.viewers.Viewer;
034: import org.eclipse.swt.SWT;
035: import org.eclipse.swt.events.SelectionAdapter;
036: import org.eclipse.swt.events.SelectionEvent;
037: import org.eclipse.swt.layout.FormAttachment;
038: import org.eclipse.swt.layout.FormData;
039: import org.eclipse.swt.widgets.Button;
040: import org.eclipse.swt.widgets.Composite;
041: import org.eclipse.swt.widgets.Table;
042: import org.eclipse.swt.widgets.TableColumn;
043: import org.eclipse.ui.IWorkbenchPart;
044: import org.eclipse.ui.views.properties.IPropertySource;
045: import org.eclipse.ui.views.properties.IPropertySourceProvider;
046: import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
047:
048: import diagram.section.EnvEntry;
049:
050: /**
051: * Creates the Layout of the EnvironmentPropertySection
052: * @generated NOT
053: */
054: public class EnvironmentPropertySection extends
055: AbstractModelerPropertySection implements
056: IPropertySourceProvider, IEnvEntryViewer {
057:
058: private Table table = null;
059: private TableViewer tableViewer = null;
060: private Button addButton = null;
061: private Button deleteButton = null;
062: private Button refactorButton = null;
063:
064: // Set column names
065: private String[] columnNames = new String[] { "Entry name",
066: "Entry type", "Entry value" };
067:
068: /*
069: public static final String[] TYPES = new String[] { "java.lang.Boolean",
070: "java.lang.Byte",
071: "java.lang.Character",
072: "java.lang.String",
073: "java.lang.Short",
074: "java.lang.Integer",
075: "java.lang.Long",
076: "java.lang.Float",
077: "java.lang.Double",
078: "java.math.BigInteger",
079: "java.math.BigDecimal",
080: "java.net.URL"};
081: */
082: public static final String[] TYPES = new String[] { "boolean",
083: "byte", "char", "String", "short", "int", "long", "float",
084: "double" };
085:
086: // Create an EnvEntryList
087: private EnvEntryList envEntryList = null;
088:
089: /**
090: * @generated
091: */
092: public IPropertySource getPropertySource(Object object) {
093: if (object instanceof IPropertySource) {
094: return (IPropertySource) object;
095: }
096: AdapterFactory af = getAdapterFactory(object);
097: if (af != null) {
098: IItemPropertySource ips = (IItemPropertySource) af.adapt(
099: object, IItemPropertySource.class);
100: if (ips != null) {
101: return new PropertySource(object, ips);
102: }
103: }
104: if (object instanceof IAdaptable) {
105: return (IPropertySource) ((IAdaptable) object)
106: .getAdapter(IPropertySource.class);
107: }
108: return null;
109: }
110:
111: /**
112: * Modify/unwrap selection.
113: * @generated
114: */
115: protected Object transformSelection(Object selected) {
116: return selected;
117: }
118:
119: /**
120: * @generated
121: */
122: protected IPropertySourceProvider getPropertySourceProvider() {
123: return this ;
124: }
125:
126: /**
127: * Sets all EMF EnvEntry model Objects as Input for this Property Section
128: * @generated NOT
129: */
130: public void setInput(IWorkbenchPart part, ISelection selection) {
131: if (selection.isEmpty()
132: || false == selection instanceof StructuredSelection) {
133: super .setInput(part, selection);
134: return;
135: }
136: final StructuredSelection structuredSelection = ((StructuredSelection) selection);
137: ArrayList transformedSelection = new ArrayList(
138: structuredSelection.size());
139: for (Iterator it = structuredSelection.iterator(); it.hasNext();) {
140: Object r = transformSelection(it.next());
141: if (r != null) {
142: transformedSelection.add(r);
143:
144: // set the EnvEntries as input data and refresh the table
145: if (r instanceof EditPart)
146: envEntryList = new EnvEntryList((EditPart) r);
147: envEntryList.addChangeListener(this );
148: tableViewer.setInput(envEntryList);
149: tableViewer.refresh();
150: }
151: }
152: super .setInput(part, new StructuredSelection(
153: transformedSelection));
154: }
155:
156: /**
157: * @generated
158: */
159: protected AdapterFactory getAdapterFactory(Object object) {
160: if (getEditingDomain() instanceof AdapterFactoryEditingDomain) {
161: return ((AdapterFactoryEditingDomain) getEditingDomain())
162: .getAdapterFactory();
163: }
164: TransactionalEditingDomain editingDomain = TransactionUtil
165: .getEditingDomain(object);
166: if (editingDomain != null) {
167: return ((AdapterFactoryEditingDomain) editingDomain)
168: .getAdapterFactory();
169: }
170: return null;
171: }
172:
173: @Override
174: public void createControls(Composite parent,
175: TabbedPropertySheetPage tabbedPropertySheetPage) {
176: super .createControls(parent, tabbedPropertySheetPage);
177:
178: Composite composite = getWidgetFactory()
179: .createFlatFormComposite(parent);
180:
181: // create Buttons
182: createButtons(composite);
183:
184: // create Table
185: int style = SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL
186: | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
187:
188: table = getWidgetFactory().createTable(composite, style);
189: FormData formData = new FormData();
190: formData.top = new FormAttachment(0, 5);
191: formData.bottom = new FormAttachment(deleteButton, -5);
192: formData.left = new FormAttachment(0, 5);
193: formData.right = new FormAttachment(100, -5);
194: formData.height = 150;
195: table.setLayoutData(formData);
196: table.setLinesVisible(true);
197: table.setHeaderVisible(true);
198: // create some Table columns
199: createTableColumns(composite);
200:
201: // Create and setup the TableViewer
202: createTableViewer();
203: tableViewer.setContentProvider(new EnvEntryContentProvider());
204: tableViewer.setLabelProvider(new EnvEntriesLabelProvider());
205: }
206:
207: /**
208: * Add the "Add","Delete" and "Refactor" buttons
209: * @param parent the parent composite
210: */
211: private void createButtons(Composite parent) {
212: // create "Add" Button
213: addButton = getWidgetFactory().createButton(parent, "Add",
214: SWT.PUSH | SWT.CENTER);
215: FormData formData = new FormData();
216: formData.left = new FormAttachment(0, 5);
217: formData.bottom = new FormAttachment(100, -5);
218: formData.width = 80;
219: addButton.setLayoutData(formData);
220: addButton.addSelectionListener(new SelectionAdapter() {
221: // Add an envEntry to the envEntryList and refresh the view
222: public void widgetSelected(SelectionEvent e) {
223: envEntryList.addEnvEntry();
224: }
225: });
226:
227: // create "Delete" Button
228: deleteButton = getWidgetFactory().createButton(parent,
229: "Delete", SWT.PUSH | SWT.CENTER);
230: formData = new FormData();
231: formData.left = new FormAttachment(addButton, -5);
232: formData.bottom = new FormAttachment(100, -5);
233: formData.width = 80;
234: deleteButton.setLayoutData(formData);
235: deleteButton.addSelectionListener(new SelectionAdapter() {
236: // Remove the selection and refresh the view
237: public void widgetSelected(SelectionEvent e) {
238: EnvEntry envEntry = (EnvEntry) ((IStructuredSelection) tableViewer
239: .getSelection()).getFirstElement();
240: if (envEntry != null) {
241: envEntryList.removeEnvEntry(envEntry);
242: }
243: }
244: });
245:
246: // create "Refactor" Button
247: refactorButton = getWidgetFactory().createButton(parent,
248: "Refactor", SWT.PUSH | SWT.CENTER);
249: formData = new FormData();
250: formData.left = new FormAttachment(deleteButton, -5);
251: formData.bottom = new FormAttachment(100, -5);
252: formData.width = 80;
253: refactorButton.setLayoutData(formData);
254: refactorButton.addSelectionListener(new SelectionAdapter() {
255: // Remove the selection and refresh the view
256: public void widgetSelected(SelectionEvent e) {
257: EnvEntry envEntry = (EnvEntry) ((IStructuredSelection) tableViewer
258: .getSelection()).getFirstElement();
259: if (envEntry == null) {
260: String title = "Environment Entry Refactoring";
261: String message = "This refactoring is not available for the selected Element.";
262: MessageDialog.openInformation(
263: ActionUtil.getShell(), title, message);
264: } else {
265: envEntryList.refactorEnvEntry(envEntry);
266: }
267: }
268: });
269: }
270:
271: /**
272: * Create the Table Columns
273: * @param parent
274: */
275: private void createTableColumns(Composite parent) {
276: // 1st column with Entry value
277: TableColumn column = new TableColumn(table, SWT.LEFT, 0);
278: column.setText("Entry value");
279: column.setWidth(100);
280: // Add listener to column so Entry values are sorted by the Entry values when clicked
281: column.addSelectionListener(new SelectionAdapter() {
282: public void widgetSelected(SelectionEvent e) {
283: //maybe we can set a column sorter here
284: //tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.PERCENT_COMPLETE));
285: }
286: });
287:
288: // 2nd column with Entry type
289: column = new TableColumn(table, SWT.LEFT, 0);
290: column.setText("Entry type");
291: column.setWidth(100);
292: // Add listener to column so Entry type are sorted by the Entry type when clicked
293: column.addSelectionListener(new SelectionAdapter() {
294: public void widgetSelected(SelectionEvent e) {
295: //maybe we can set a column sorter here
296: //tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.PERCENT_COMPLETE));
297: }
298: });
299:
300: // 3rd column with Entry name
301: column = new TableColumn(table, SWT.LEFT, 0);
302: column.setText("Entry name");
303: column.setWidth(100);
304: // Add listener to column so Entry names are sorted by the Entry names when clicked
305: column.addSelectionListener(new SelectionAdapter() {
306: public void widgetSelected(SelectionEvent e) {
307: //maybe we can set a column sorter here
308: //tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.PERCENT_COMPLETE));
309: }
310: });
311: }
312:
313: /**
314: * Create the TableViewer
315: */
316: private void createTableViewer() {
317: tableViewer = new TableViewer(table);
318: tableViewer.setUseHashlookup(true);
319: tableViewer.setColumnProperties(columnNames);
320:
321: // Create the cell editors
322: CellEditor[] editors = new CellEditor[columnNames.length];
323:
324: // Column 1 : Entry name (Free text)
325: editors[0] = new TextCellEditor(table);
326:
327: // Column 2 : Entry type (Combo Box selection)
328: editors[1] = new StringComboBoxCellEditor(table, TYPES,
329: SWT.SIMPLE);
330: editors[1].setValidator(new TypeValidator());
331:
332: // Column 3 : Entry value (Free text)
333: editors[2] = new TextCellEditor(table);
334:
335: // Assign the cell editors to the viewer
336: tableViewer.setCellEditors(editors);
337:
338: // Set the cell modifier for the viewer
339: tableViewer.setCellModifier(new EnvEntriesCellModifier(this ));
340:
341: // Set the default sorter for the viewer
342: //tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.DESCRIPTION));
343: }
344:
345: public void addEnvEntry(EnvEntry envEntry) {
346: tableViewer.refresh(true);
347: }
348:
349: public void removeEnvEntry(EnvEntry envEntry) {
350: tableViewer.refresh(true);
351: }
352:
353: public void updateEnvEntry(EnvEntry envEntry) {
354: tableViewer.refresh(true);
355: }
356:
357: /**
358: * InnerClass that acts as a proxy for the EnvEntryList
359: * providing content for the Table. It implements the IEnvEntriesViewer
360: * interface since it must register changeListeners with the
361: * EnvEntryList
362: */
363: class EnvEntryContentProvider implements IStructuredContentProvider {
364: public void inputChanged(Viewer viewer, Object oldInput,
365: Object newInput) {
366: tableViewer.refresh(true);
367: }
368:
369: public void dispose() {
370: }
371:
372: // Return the EnvEntries as an array of Objects
373: public Object[] getElements(Object parent) {
374: return envEntryList.getEnvEntries().toArray();
375: }
376: }
377:
378: /**
379: * Return the array of choices for the type multiple choice cell
380: */
381: public String[] getChoices(String property) {
382: return TYPES;
383: }
384:
385: /**
386: * Return the column names in a collection
387: * @return List containing column names
388: */
389: public List<String> getColumnNames() {
390: return Arrays.asList(columnNames);
391: }
392:
393: /**
394: * Return the ExampleAnnotationList
395: */
396: public EnvEntryList getEnvEntryList() {
397: return envEntryList;
398: }
399:
400: /**
401: * This class is used for the StringComBoxCellEditor
402: * @author sh
403: */
404: private class TypeValidator implements ICellEditorValidator {
405:
406: @Override
407: public String isValid(Object value) {
408: if (value instanceof String == false)
409: return null;
410: return EnvEntryValidatorImpl.INSTANCE.validateType(value
411: .toString());
412: }
413: }
414: }
|