1 | package de.uka.ipd.sdq.pcmbench.tabs.generic; |
2 | |
3 | import java.util.Observable; |
4 | import java.util.Observer; |
5 | |
6 | import org.eclipse.core.runtime.Assert; |
7 | import org.eclipse.emf.ecore.EObject; |
8 | import org.eclipse.jface.viewers.CellEditor; |
9 | import org.eclipse.jface.viewers.IBaseLabelProvider; |
10 | import org.eclipse.jface.viewers.ICellModifier; |
11 | import org.eclipse.jface.viewers.IContentProvider; |
12 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
13 | import org.eclipse.jface.viewers.IStructuredSelection; |
14 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
15 | import org.eclipse.jface.viewers.TableViewer; |
16 | import org.eclipse.swt.SWT; |
17 | import org.eclipse.swt.events.SelectionListener; |
18 | import org.eclipse.swt.layout.FormAttachment; |
19 | import org.eclipse.swt.layout.FormData; |
20 | import org.eclipse.swt.layout.GridData; |
21 | import org.eclipse.swt.widgets.Composite; |
22 | import org.eclipse.swt.widgets.Table; |
23 | import org.eclipse.swt.widgets.ToolBar; |
24 | import org.eclipse.swt.widgets.ToolItem; |
25 | |
26 | import de.uka.ipd.sdq.pcmbench.tabs.PCMBenchTabsImages; |
27 | |
28 | /** |
29 | * @author Roman Andrej |
30 | */ |
31 | public abstract class EditorSection implements Observer { |
32 | |
33 | /** Define the value for TableSection. */ |
34 | private Composite composite; |
35 | private ToolItem addButton, deleteButton; |
36 | /** TableViewer */ |
37 | protected TableViewer viewer; |
38 | /** Define selected element in the section. */ |
39 | private EObject selectedObject = null; |
40 | |
41 | public EditorSection(Composite composite) { |
42 | composite.getParent().setLayoutData( |
43 | new GridData(SWT.FILL, SWT.FILL, true, true)); |
44 | this.composite = composite; |
45 | |
46 | createEditorSection(); |
47 | } |
48 | |
49 | private void createEditorSection() { |
50 | createTableSection(createToolBar()); |
51 | } |
52 | |
53 | |
54 | protected ToolBar createToolBar() { |
55 | |
56 | ToolBar toolBar = new ToolBar(composite, SWT.VERTICAL | SWT.FLAT |
57 | | SWT.RIGHT); |
58 | FormData formData = new FormData(); |
59 | formData.left = new FormAttachment(96, 0); |
60 | formData.top = new FormAttachment(0, 0); |
61 | formData.right = new FormAttachment(100, 0); |
62 | formData.bottom = new FormAttachment(100, 0); |
63 | toolBar.setLayoutData(formData); |
64 | |
65 | /** Create Add-Button by ToolBar */ |
66 | if (canAddButonCreated()) { |
67 | addButton = new ToolItem(toolBar, SWT.PUSH); |
68 | addButton.setImage(PCMBenchTabsImages.imageRegistry |
69 | .get(PCMBenchTabsImages.ADD_SIGN)); |
70 | addButton.addSelectionListener(createAddButtonActionListener()); |
71 | } |
72 | |
73 | |
74 | /** Create Delete-Button by ToolBar */ |
75 | if (canDeleteButonCreated()) { |
76 | deleteButton = new ToolItem(toolBar, SWT.PUSH); |
77 | deleteButton.setImage(PCMBenchTabsImages.imageRegistry |
78 | .get(PCMBenchTabsImages.DELETE_SIGN)); |
79 | deleteButton.setEnabled(false); |
80 | } |
81 | |
82 | return toolBar; |
83 | } |
84 | |
85 | /** Create the section with Table and TableViewer. */ |
86 | protected void createTableSection(ToolBar toolBar){ |
87 | |
88 | // style the style of table to construct |
89 | int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL |
90 | | SWT.FULL_SELECTION | SWT.HIDE_SELECTION; |
91 | // Width board inside table and toolbar |
92 | int widthBoard = 6; |
93 | |
94 | Table table = new Table(composite, style); |
95 | |
96 | // create table layout |
97 | FormData data = new FormData(); |
98 | data.left = new FormAttachment(0, 0); |
99 | data.right = new FormAttachment(toolBar, widthBoard); |
100 | data.top = new FormAttachment(0, 0); |
101 | data.bottom = new FormAttachment(100, 0); |
102 | |
103 | table.setLayoutData(data); |
104 | table.setHeaderVisible(true); |
105 | table.setLinesVisible(true); |
106 | |
107 | |
108 | viewer = new TableViewer(table); |
109 | viewer.setUseHashlookup(true); |
110 | viewer.setColumnProperties(getTableColumnNames()); |
111 | viewer.setCellEditors(createViewerCellEditors(table)); |
112 | viewer.addSelectionChangedListener(new ISelectionChangedListener() { |
113 | |
114 | /* (non-Javadoc) |
115 | * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent) |
116 | */ |
117 | public void selectionChanged(SelectionChangedEvent event) { |
118 | Object input = ((IStructuredSelection) event.getSelection()) |
119 | .getFirstElement(); |
120 | |
121 | if (input instanceof EObject) { |
122 | selectedObject = (EObject) input; |
123 | setDeleteButtonEnabled(inputValidation(selectedObject)); |
124 | } else { |
125 | setDeleteButtonEnabled(false); |
126 | } |
127 | } |
128 | }); |
129 | |
130 | // define the listener for Delete-Button |
131 | setDeleteButtonListener(viewer); |
132 | // set CellModifier for Viewer |
133 | setViewerCellModifier(viewer); |
134 | // create column |
135 | createTableColumns(table); |
136 | |
137 | |
138 | } |
139 | |
140 | /** Create the table columns. */ |
141 | protected abstract void createTableColumns(Table table); |
142 | |
143 | protected abstract boolean inputValidation(EObject eObject); |
144 | |
145 | /** |
146 | * Create a SelectionListener for the Add-Button and set him to TableViewer |
147 | * as 'SelectionChangedListener'. Thus those becomes available element, |
148 | * which was selected in the TableViewer, for the DeleteAction |
149 | * |
150 | * @param viewer |
151 | * instance of TableViewer |
152 | */ |
153 | protected void setDeleteButtonListener(TableViewer viewer) { |
154 | SelectionListener listener = createDeleteButtonListener(); |
155 | viewer |
156 | .addSelectionChangedListener((ISelectionChangedListener) listener); |
157 | if (canDeleteButonCreated()) { |
158 | deleteButton.addSelectionListener(listener); |
159 | } |
160 | } |
161 | |
162 | /** |
163 | * Create a ICellModifier for TableViewer with 'EditorSection' as Observer for him. |
164 | * |
165 | * @param viewer |
166 | * instance of TableViewer |
167 | */ |
168 | protected void setViewerCellModifier(TableViewer viewer) { |
169 | ObservableCellModifier cellModifier = createViewerCellModifier(); |
170 | cellModifier.addObserver(this); |
171 | viewer.setCellModifier(cellModifier); |
172 | } |
173 | |
174 | /** Set a SelectionListener for the Delete-Button */ |
175 | protected abstract SelectionListener createDeleteButtonListener(); |
176 | |
177 | protected abstract String[] getTableColumnNames(); |
178 | |
179 | protected abstract boolean canAddButonCreated(); |
180 | protected abstract boolean canDeleteButonCreated(); |
181 | |
182 | /** Create a CellEditors for Viewer. */ |
183 | protected abstract CellEditor[] createViewerCellEditors(Table table); |
184 | |
185 | protected abstract ObservableCellModifier createViewerCellModifier(); |
186 | |
187 | /** Set a CellModifier for TabelViewer. */ |
188 | public void setViewerCellModifier(ICellModifier modifier){ |
189 | Assert.isNotNull(viewer); |
190 | viewer.setCellModifier(modifier); |
191 | } |
192 | |
193 | public void addViewerSelectionChangedListener(ISelectionChangedListener listener){ |
194 | Assert.isNotNull(viewer); |
195 | viewer.addSelectionChangedListener(listener); |
196 | } |
197 | |
198 | /** Create a SelectionListener for the Add-Button */ |
199 | protected abstract SelectionListener createAddButtonActionListener(); |
200 | |
201 | /** Set a input for the TableViewer */ |
202 | public void setViewerInput(Object input) { |
203 | Assert.isNotNull(viewer); |
204 | viewer.setInput(input); |
205 | } |
206 | |
207 | |
208 | /** Set a ContentProvider for the TableViewer */ |
209 | public void setViewerContentProvider(IContentProvider contentProvider) { |
210 | Assert.isNotNull(viewer); |
211 | viewer.setContentProvider(contentProvider); |
212 | } |
213 | |
214 | /** Set a LabelProvider for the TableViewer */ |
215 | public void setViewerLabelProvider(IBaseLabelProvider labelProvider) { |
216 | Assert.isNotNull(viewer); |
217 | viewer.setLabelProvider(labelProvider); |
218 | } |
219 | |
220 | public void refresh() { |
221 | Assert.isNotNull(viewer); |
222 | viewer.refresh(); |
223 | } |
224 | |
225 | protected void setDeleteButtonEnabled(boolean enabled) { |
226 | deleteButton.setEnabled(enabled); |
227 | } |
228 | |
229 | public EObject getSelectedObject() { |
230 | return selectedObject; |
231 | } |
232 | |
233 | public TableViewer getViewer(){ |
234 | return viewer; |
235 | } |
236 | |
237 | /* (non-Javadoc) |
238 | * @see java.util.Observer#update(java.util.Observable, java.lang.Object) |
239 | */ |
240 | public void update(Observable o, Object arg) { |
241 | if (viewer != null){ |
242 | viewer.refresh(); |
243 | } |
244 | } |
245 | } |