Skip to content

Package: AbstractTableViewerComposite

AbstractTableViewerComposite

nameinstructionbranchcomplexitylinemethod
AbstractTableViewerComposite(Composite, int, Object, TableViewerSWTCustomization, IObservableValue, IObservableValue)
M: 0 C: 28
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
addColumns(TableViewerSWTCustomization, AbstractTableViewer, EMFDataBindingContext)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
configureContextMenu(AbstractTableViewer)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
configureViewerFilters(AbstractTableViewer)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createColumnViewerActivationStrategy(AbstractTableViewer)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
determineEnabledFeatures(TableViewerSWTCustomization)
M: 0 C: 29
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
dispose()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
enableTooltipSupport(AbstractTableViewer)
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getActionBar()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getColumnConfiguration(Widget)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEnabledFeatures()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getValidationControls()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
initActionBar(Composite, ActionBarProvider, AbstractTableViewer)
M: 0 C: 13
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
initTitleLabel(Label, IObservableValue, IObservableValue, EMFDataBindingContext)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
renderControl(Composite, TableViewerSWTCustomization, Object, EMFDataBindingContext, IObservableValue, IObservableValue)
M: 0 C: 187
100%
M: 2 C: 14
88%
M: 2 C: 7
78%
M: 0 C: 45
100%
M: 0 C: 1
100%
setupDragAndDrop(TableViewerSWTCustomization, AbstractTableViewer)
M: 0 C: 22
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
viewerSelectionChanged(SelectionChangedEvent)
M: 0 C: 10
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * jonas - initial API and implementation
13: * Christian W. Damus - bug 547271
14: ******************************************************************************/
15: package org.eclipse.emfforms.spi.swt.table;
16:
17: import java.util.LinkedHashSet;
18: import java.util.List;
19: import java.util.Set;
20:
21: import org.eclipse.core.databinding.observable.value.IObservableValue;
22: import org.eclipse.emf.databinding.EMFDataBindingContext;
23: import org.eclipse.emfforms.common.Feature;
24: import org.eclipse.emfforms.common.Optional;
25: import org.eclipse.emfforms.internal.swt.table.action.KeyBindingManager;
26: import org.eclipse.emfforms.spi.swt.table.action.ActionBar;
27: import org.eclipse.emfforms.spi.swt.table.action.ActionBarProvider;
28: import org.eclipse.emfforms.spi.swt.table.action.ActionConfiguration;
29: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
30: import org.eclipse.jface.layout.AbstractColumnLayout;
31: import org.eclipse.jface.viewers.AbstractTableViewer;
32: import org.eclipse.jface.viewers.ColumnPixelData;
33: import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
34: import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
35: import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
36: import org.eclipse.jface.viewers.ColumnWeightData;
37: import org.eclipse.jface.viewers.ISelectionChangedListener;
38: import org.eclipse.jface.viewers.SelectionChangedEvent;
39: import org.eclipse.jface.viewers.TableViewer;
40: import org.eclipse.jface.viewers.ViewerColumn;
41: import org.eclipse.jface.viewers.ViewerComparator;
42: import org.eclipse.swt.SWT;
43: import org.eclipse.swt.events.ControlListener;
44: import org.eclipse.swt.widgets.Composite;
45: import org.eclipse.swt.widgets.Control;
46: import org.eclipse.swt.widgets.Label;
47: import org.eclipse.swt.widgets.Widget;
48:
49: /**
50: * @author Jonas Helming
51: *
52: * @param <V> the TableViewer implementation to use
53: */
54: public abstract class AbstractTableViewerComposite<V extends AbstractTableViewer> extends Composite {
55:
56:         private final EMFDataBindingContext emfDatabindingContext;
57:         private Optional<List<Control>> validationControls;
58:         private final Set<Feature> enabledFeatures;
59:         private final Optional<ActionBar<V>> actionBar;
60:
61:         /**
62:          * Default constructor.
63:          *
64:          * @param parent the parent {@link Composite}
65:          * @param style the style bits
66:          * @param inputObject the input object
67:          * @param customization the {@link TableViewerSWTCustomization}
68:          * @param title the title
69:          * @param tooltip the tooltip
70:          */
71:         protected AbstractTableViewerComposite(
72:                 Composite parent,
73:                 int style,
74:                 Object inputObject,
75:                 TableViewerSWTCustomization<V> customization,
76:                 IObservableValue title,
77:                 IObservableValue tooltip) {
78:                 super(parent, style);
79:
80:                 emfDatabindingContext = new EMFDataBindingContext();
81:                 enabledFeatures = determineEnabledFeatures(customization);
82:                 actionBar = customization.getActionBar();
83:
84:                 renderControl(this, customization, inputObject, emfDatabindingContext, title, tooltip);
85:         }
86:
87:         /**
88:          * Determine the list of enabled features (both for the table as well as the columns).
89:          *
90:          * @param customization the viewer customization
91:          * @return a set of enabled features
92:          */
93:         private Set<Feature> determineEnabledFeatures(TableViewerSWTCustomization<V> customization) {
94:
95:                 final Set<Feature> enabled = new LinkedHashSet<Feature>();
96:                 enabled.addAll(customization.getTableConfiguration().getEnabledFeatures());
97:
98:•                for (final ColumnConfiguration columnConfig : customization.getColumnConfigurations()) {
99:                         enabled.addAll(columnConfig.getEnabledFeatures());
100:                 }
101:
102:                 return enabled;
103:         }
104:
105:         /**
106:          * @return the enabledFeatures
107:          */
108:         public Set<Feature> getEnabledFeatures() {
109:                 return enabledFeatures;
110:         }
111:
112:         /**
113:          * If present, returns the {@link ActionBar} configuring actions and corresponding control for this composite's
114:          * viewer.
115:          *
116:          * @return The {@link ActionBar}, if any
117:          */
118:         public Optional<ActionBar<V>> getActionBar() {
119:                 return actionBar;
120:         }
121:
122:         /**
123:          * @return the {@link AbstractTableViewer}
124:          */
125:         public abstract V getTableViewer();
126:
127:         /**
128:          *
129:          * @return the validation controls, if present
130:          */
131:         public Optional<List<Control>> getValidationControls() {
132:                 return validationControls;
133:         }
134:
135:         /**
136:          * Configures the context menu for the given TableViewer instance.
137:          *
138:          * @param tableViewer the table viewer to configure
139:          */
140:         protected void configureContextMenu(V tableViewer) {
141:
142:         }
143:
144:         /**
145:          * Configures viewer filters for the given TableViewer instance.
146:          *
147:          * @param tableViewer the table viewer to configure
148:          */
149:         protected void configureViewerFilters(V tableViewer) {
150:
151:         }
152:
153:         private void renderControl(Composite parent, TableViewerSWTCustomization<V> customization,
154:                 Object inputObject, EMFDataBindingContext emfDataBindingContext, IObservableValue title,
155:                 IObservableValue tooltip) {
156:                 customization.createCompositeLayout(parent);
157:
158:                 final Optional<Label> titleLabel = customization.getTitleLabel();
159:•                if (titleLabel.isPresent()) {
160:                         initTitleLabel(titleLabel.get(), title, tooltip, emfDatabindingContext);
161:                 }
162:
163:                 validationControls = customization.getValidationControls();
164:
165:                 final Composite viewerComposite = customization.getViewerComposite();
166:
167:                 final V tableViewer = createTableViewer(customization, viewerComposite);
168:
169:                 final TableConfiguration configuration = customization.getTableConfiguration();
170:•                if (configuration != null) {
171:                         // Pump in the configuration data
172:                         configuration.getData().forEach(tableViewer::setData);
173:                 }
174:
175:                 // If an action configuration was configured, bind key bindings to the viewer
176:                 final Optional<ActionConfiguration> actionConfiguration = customization.getActionConfiguration();
177:•                if (actionConfiguration.isPresent()) {
178:                         final KeyBindingManager keyBindingManager = new KeyBindingManager();
179:                         keyBindingManager.applyActionConfiguration(actionConfiguration.get());
180:                         keyBindingManager.bindToViewer(tableViewer);
181:                 }
182:
183:                 final Optional<Composite> buttonComposite = customization.getButtonComposite();
184:•                if (buttonComposite.isPresent()) {
185:                         initActionBar(buttonComposite.get(), customization, tableViewer);
186:                 }
187:
188:                 enableTooltipSupport(tableViewer);
189:
190:                 final Optional<ViewerComparator> comparator = customization.getComparator();
191:•                if (comparator.isPresent()) {
192:                         tableViewer.setComparator(comparator.get());
193:                 }
194:
195:                 tableViewer.setContentProvider(customization.createContentProvider());
196:
197:                 addColumns(customization, tableViewer, emfDataBindingContext);
198:
199:                 setupDragAndDrop(customization, tableViewer);
200:                 configureContextMenu(tableViewer);
201:                 configureViewerFilters(tableViewer);
202:
203:                 tableViewer.setInput(inputObject);
204:
205:                 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
206:
207:                         @Override
208:                         public void selectionChanged(SelectionChangedEvent event) {
209:                                 viewerSelectionChanged(event);
210:                         }
211:                 });
212:
213:                 final AbstractColumnLayout layout = createLayout(viewerComposite);
214:                 final Widget[] columns = getColumns();
215:•                for (int i = 0; i < columns.length; i++) {
216:                         final Widget tableColumn = columns[i];
217:                         final boolean storedIsResizable = (Boolean) tableColumn.getData(ColumnConfiguration.RESIZABLE);
218:                         final Integer storedWeight = (Integer) tableColumn.getData(ColumnConfiguration.WEIGHT);
219:                         final Integer storedMinWidth = (Integer) tableColumn.getData(ColumnConfiguration.MIN_WIDTH);
220:•                        if (storedWeight == ColumnConfiguration.NO_WEIGHT) {
221:                                 layout.setColumnData(tableColumn, new ColumnPixelData(storedMinWidth, storedIsResizable));
222:•                        } else if (storedMinWidth > 0) {
223:                                 layout.setColumnData(tableColumn,
224:                                         new ColumnWeightData(storedWeight, storedMinWidth, storedIsResizable));
225:                         } else {
226:                                 layout.setColumnData(tableColumn,
227:                                         new ColumnWeightData(storedWeight, storedIsResizable));
228:                         }
229:                 }
230:         }
231:
232:         private void setupDragAndDrop(TableViewerSWTCustomization<V> customization, final V tableViewer) {
233:•                if (customization.hasDND()) {
234:                         tableViewer.addDragSupport(customization.getDragOperations(), customization.getDragTransferTypes(),
235:                                 customization.getDragListener(tableViewer));
236:                         tableViewer.addDropSupport(customization.getDropOperations(), customization.getDropTransferTypes(),
237:                                 customization.getDropListener(tableViewer));
238:                 }
239:         }
240:
241:         /**
242:          * Returns the list of columns of the table viewer.
243:          *
244:          * @return the list of columns
245:          */
246:         public abstract Widget[] getColumns();
247:
248:         /**
249:          * Sets the layout of the given {@link Composite}.
250:          *
251:          * @param viewerComposite the target composite
252:          *
253:          * @return the applied layout
254:          *
255:          */
256:         protected abstract AbstractColumnLayout createLayout(Composite viewerComposite);
257:
258:         /**
259:          * Creates the table viewer.
260:          *
261:          * @param customization the {@link TableViewerSWTCustomization} to use
262:          * @param viewerComposite the parent composite
263:          *
264:          * @return the table viewer
265:          */
266:         protected abstract V createTableViewer(TableViewerSWTCustomization<V> customization,
267:                 Composite viewerComposite);
268:
269:         private void addColumns(TableViewerSWTCustomization<V> customization, V tableViewer,
270:                 EMFDataBindingContext emfDataBindingContext) {
271:•                for (final ColumnConfiguration columnConfiguration : customization.getColumnConfigurations()) {
272:                         createColumn(columnConfiguration, emfDataBindingContext, tableViewer);
273:                 }
274:         }
275:
276:         /**
277:          * Creates a new {@link ViewerColumn}.
278:          *
279:          * @param columnDescription the column description to use
280:          * @param tableViewer the parent {@link AbstractTableViewer}
281:          * @param emfDataBindingContext the data binding context to use
282:          * @return the viewer column
283:          */
284:         protected abstract ViewerColumn createColumn(ColumnConfiguration columnDescription,
285:                 EMFDataBindingContext emfDataBindingContext, V tableViewer);
286:
287:         /**
288:          * Returns the {@link ColumnConfiguration} of the given widget instance.
289:          *
290:          * @param columnWidget the widget to fetch the column configuration for
291:          * @return the {@link ColumnConfigurationImpl}
292:          */
293:         public ColumnConfiguration getColumnConfiguration(Widget columnWidget) {
294:                 return (ColumnConfiguration) columnWidget.getData(ColumnConfiguration.ID);
295:         }
296:
297:         /**
298:          * This method gets called when the selection on the {@link TableViewer} (see {@link #getTableViewer()}) has
299:          * changed.
300:          * <p>
301:          * If you override this method make sure to call super.
302:          * </p>
303:          *
304:          * @param event the {@link SelectionChangedEvent}
305:          */
306:         protected void viewerSelectionChanged(SelectionChangedEvent event) {
307:•                if (actionBar.isPresent()) {
308:                         actionBar.get().updateActionBar();
309:                 }
310:         }
311:
312:         /**
313:          * Creates a new {@link ColumnViewerEditorActivationStrategy} for the given table viewer.
314:          *
315:          * @param tableViewer the target table viewer.
316:          *
317:          * @return the ColumnViewerEditorActivationStrategy
318:          *
319:          */
320:         protected static ColumnViewerEditorActivationStrategy createColumnViewerActivationStrategy(
321:                 final AbstractTableViewer tableViewer) {
322:                 return new ColumnViewerEditorActivationStrategy(tableViewer) {
323:                         @Override
324:                         protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
325:                                 return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
326:                                         || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_CLICK_SELECTION
327:                                         || event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR
328:                                         || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
329:                         }
330:                 };
331:         }
332:
333:         private static void enableTooltipSupport(AbstractTableViewer tableViewer) {
334:                 ColumnViewerToolTipSupport.enableFor(tableViewer);
335:         }
336:
337:         private static <T extends AbstractTableViewer> void initActionBar(
338:                 Composite composite, ActionBarProvider<T> actionBarProvider, T tableViewer) {
339:                 final Optional<ActionBar<T>> actionBar = actionBarProvider.getActionBar();
340:•                if (actionBar.isPresent()) {
341:                         actionBar.get().fillComposite(composite, tableViewer);
342:                 }
343:         }
344:
345:         private static void initTitleLabel(Label label, IObservableValue title, IObservableValue tooltip,
346:                 EMFDataBindingContext emfDatabindingContext) {
347:                 emfDatabindingContext.bindValue(
348:                         WidgetProperties.text().observe(label),
349:                         title);
350:                 emfDatabindingContext.bindValue(
351:                         WidgetProperties.tooltipText().observe(label),
352:                         tooltip);
353:         }
354:
355:         @Override
356:         public void dispose() {
357:                 emfDatabindingContext.dispose();
358:                 super.dispose();
359:         }
360:
361:         /**
362:          * Adds a new {@link ControlListener column listener}.
363:          *
364:          * @param columnlistener the listener to add
365:          */
366:         public abstract void addColumnListener(ControlListener columnlistener);
367:
368:         /**
369:          * Returns the {@link TableControl}.
370:          *
371:          * @return the table control
372:          */
373:         public abstract TableControl getTableControl();
374:
375:         /**
376:          * Sets the comparator of the table viewer.
377:          *
378:          * @param comparator the {@link TableViewerComparator} to set
379:          * @param sortableColumns the list of columns that can be sorted
380:          */
381:         public abstract void setComparator(TableViewerComparator comparator, List<Integer> sortableColumns);
382:
383:         /**
384:          * Allow to set an initial sort.
385:          *
386:          * @param column The column to sort
387:          * @since 1.22
388:          */
389:         public abstract void setCompareColumn(int column);
390: }