Skip to content

Package: GridTableViewerComposite$1

GridTableViewerComposite$1

nameinstructionbranchcomplexitylinemethod
getHeaderHeight()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getHeaderVisible()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getItemCount()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getItemHeight()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isDisposed()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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: * Alexandra Buzila - initial API and implementation
13: * Johannes Faltermeier - initial API and implementation
14: * Lucas Koehler - adaptions to RAP limitations
15: ******************************************************************************/
16: package org.eclipse.emf.ecp.view.spi.table.nebula.grid.rap;
17:
18: import java.util.List;
19:
20: import org.eclipse.core.databinding.observable.value.IObservableValue;
21: import org.eclipse.emf.databinding.EMFDataBindingContext;
22: import org.eclipse.emf.ecp.view.spi.table.nebula.grid.rap.menu.GridColumnAction;
23: import org.eclipse.emf.ecp.view.spi.table.nebula.grid.rap.messages.Messages;
24: import org.eclipse.emfforms.spi.swt.table.AbstractTableViewerComposite;
25: import org.eclipse.emfforms.spi.swt.table.ColumnConfiguration;
26: import org.eclipse.emfforms.spi.swt.table.TableConfiguration;
27: import org.eclipse.emfforms.spi.swt.table.TableControl;
28: import org.eclipse.emfforms.spi.swt.table.TableViewerComparator;
29: import org.eclipse.emfforms.spi.swt.table.TableViewerSWTCustomization;
30: import org.eclipse.jface.action.IMenuListener;
31: import org.eclipse.jface.action.IMenuManager;
32: import org.eclipse.jface.action.MenuManager;
33: import org.eclipse.jface.layout.AbstractColumnLayout;
34: import org.eclipse.jface.viewers.ViewerColumn;
35: import org.eclipse.nebula.jface.gridviewer.GridColumnLayout;
36: import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
37: import org.eclipse.nebula.jface.gridviewer.GridViewerColumn;
38: import org.eclipse.nebula.widgets.grid.Grid;
39: import org.eclipse.nebula.widgets.grid.GridColumn;
40: import org.eclipse.swt.SWT;
41: import org.eclipse.swt.events.ControlListener;
42: import org.eclipse.swt.events.SelectionAdapter;
43: import org.eclipse.swt.events.SelectionEvent;
44: import org.eclipse.swt.graphics.Point;
45: import org.eclipse.swt.widgets.Composite;
46: import org.eclipse.swt.widgets.Menu;
47: import org.eclipse.swt.widgets.Widget;
48:
49: /**
50: * A {@link Composite} containing a {@link GridTableViewer}.
51: *
52: * @author Jonas Helming
53: *
54: */
55: public class GridTableViewerComposite extends AbstractTableViewerComposite<GridTableViewer> {
56:
57:         private static final long serialVersionUID = 2569328255801045004L;
58:         private GridTableViewer gridTableViewer;
59:         private TableViewerComparator comparator;
60:         private List<Integer> sortableColumns;
61:
62:         /**
63:          * Default constructor.
64:          *
65:          * @param parent the parent {@link Composite}
66:          * @param style the style bits
67:          * @param inputObject the input object
68:          * @param customization the {@link TableViewerSWTCustomization}
69:          * @param title the title
70:          * @param tooltip the tooltip
71:          */
72:         @SuppressWarnings("unchecked")
73:         public GridTableViewerComposite(Composite parent, int style, Object inputObject,
74:                 TableViewerSWTCustomization customization,
75:                 IObservableValue title, IObservableValue tooltip) {
76:                 super(parent, style, inputObject, customization, title, tooltip);
77:         }
78:
79:         @Override
80:         public GridTableViewer getTableViewer() {
81:                 return gridTableViewer;
82:         }
83:
84:         @Override
85:         protected GridTableViewer createTableViewer(TableViewerSWTCustomization<GridTableViewer> customization,
86:                 Composite viewerComposite) {
87:                 gridTableViewer = customization.createTableViewer(viewerComposite);
88:                 gridTableViewer.getGrid().setAutoHeight(true);
89:                 return gridTableViewer;
90:         }
91:
92:         @Override
93:         protected void configureContextMenu(GridTableViewer tableViewer) {
94:                 final MenuManager menuMgr = new MenuManager();
95:                 menuMgr.setRemoveAllWhenShown(true);
96:
97:                 if (getEnabledFeatures().contains(TableConfiguration.FEATURE_COLUMN_HIDE_SHOW)) {
98:                         menuMgr.addMenuListener(new ColumnHideShowMenuListener());
99:                 }
100:
101:                 // TODO usable when the nebula grid RAP implementation supports header controls
102:                 // if (getEnabledFeatures().contains(TableConfiguration.FEATURE_COLUMN_FILTER)) {
103:                 // menuMgr.addMenuListener(new ColumnFilterMenuListener());
104:                 // }
105:
106:                 final Menu menu = menuMgr.createContextMenu(tableViewer.getControl());
107:                 tableViewer.getControl().setMenu(menu);
108:         }
109:
110:         @Override
111:         protected void configureViewerFilters(GridTableViewer tableViewer) {
112:                 // TODO usable when the nebula grid RAP implementation supports header controls
113:                 // if (getEnabledFeatures().contains(TableConfiguration.FEATURE_COLUMN_FILTER)) {
114:                 // tableViewer.addFilter(new GridColumnFilterViewerFilter(this, tableViewer));
115:                 // }
116:         }
117:
118:         @Override
119:         protected AbstractColumnLayout createLayout(Composite viewerComposite) {
120:                 final GridColumnLayout layout = new GridColumnLayout();
121:                 viewerComposite.setLayout(layout);
122:                 return layout;
123:         }
124:
125:         @Override
126:         public Widget[] getColumns() {
127:                 return gridTableViewer.getGrid().getColumns();
128:         }
129:
130:         @Override
131:         public void addColumnListener(ControlListener columnlistener) {
132:                 for (int i = 0; i < gridTableViewer.getGrid().getColumns().length; i++) {
133:                         final GridColumn gridColumn = gridTableViewer.getGrid().getColumns()[i];
134:                         gridColumn.addControlListener(columnlistener);
135:                 }
136:         }
137:
138:         @Override
139:         public TableControl getTableControl() {
140:                 return new TableControl() {
141:
142:                         @Override
143:                         public boolean isDisposed() {
144:                                 return getTableViewer().getGrid().isDisposed();
145:                         }
146:
147:                         @Override
148:                         public int getItemHeight() {
149:                                 return getTableViewer().getGrid().getItemHeight();
150:                         }
151:
152:                         @Override
153:                         public boolean getHeaderVisible() {
154:                                 return getTableViewer().getGrid().getHeaderVisible();
155:                         }
156:
157:                         @Override
158:                         public int getHeaderHeight() {
159:                                 return getTableViewer().getGrid().getHeaderHeight();
160:                         }
161:
162:                         @Override
163:                         public int getItemCount() {
164:                                 return getTableViewer().getGrid().getItemCount();
165:                         }
166:                 };
167:         }
168:
169:         @Override
170:         protected ViewerColumn createColumn(final ColumnConfiguration config,
171:                 EMFDataBindingContext emfDataBindingContext, final GridTableViewer tableViewer) {
172:
173:                 final GridViewerColumn column = new GridViewerColumnBuilder(config)
174:                         .withDatabinding(emfDataBindingContext)
175:                         .build(tableViewer);
176:
177:                 column.getColumn().setWordWrap(true);
178:
179:                 return column;
180:         }
181:
182:         @Override
183:         public void setComparator(final TableViewerComparator comparator, List<Integer> sortableColumns) {
184:                 this.comparator = comparator;
185:                 this.sortableColumns = sortableColumns;
186:                 for (int i = 0; i < getTableViewer().getGrid().getColumns().length; i++) {
187:                         if (!sortableColumns.contains(i)) {
188:                                 continue;
189:                         }
190:                         final int j = i;
191:                         final GridColumn tableColumn = getTableViewer().getGrid().getColumns()[i];
192:                         final SelectionAdapter selectionAdapter = new SelectionAdapter() {
193:                                 @Override
194:                                 public void widgetSelected(SelectionEvent e) {
195:                                         setCompareColumn(j);
196:                                 }
197:                         };
198:                         tableColumn.addSelectionListener(selectionAdapter);
199:                 }
200:
201:         }
202:
203:         private ColumnConfiguration getCurrentColumnConfig() {
204:
205:                 final Grid grid = getTableViewer().getGrid();
206:                 final Point cursorLocation = grid.getDisplay().getCursorLocation();
207:                 final GridColumn column = grid.getColumn(grid.toControl(cursorLocation));
208:
209:                 if (column == null) {
210:                         return null;
211:                 }
212:                 return getColumnConfiguration(column);
213:         }
214:
215:         /**
216:          * Column hide/show menu listener.
217:          *
218:          * @author Mat Hansen
219:          *
220:          */
221:         private class ColumnHideShowMenuListener implements IMenuListener {
222:
223:                 @Override
224:                 public void menuAboutToShow(IMenuManager manager) {
225:                         final ColumnConfiguration columnConfiguration = getCurrentColumnConfig();
226:                         if (columnConfiguration == null) {
227:                                 return;
228:                         }
229:                         manager.add(new GridColumnAction(GridTableViewerComposite.this,
230:                                 Messages.GridTableViewerComposite_hideColumnAction) {
231:                                 @Override
232:                                 public void run() {
233:                                         columnConfiguration.visible().setValue(Boolean.FALSE);
234:                                 }
235:
236:                                 @Override
237:                                 public boolean isEnabled() {
238:                                         if (!super.isEnabled()) {
239:                                                 return false;
240:                                         }
241:                                         return columnConfiguration.getEnabledFeatures()
242:                                                 .contains(ColumnConfiguration.FEATURE_COLUMN_HIDE_SHOW);
243:                                 }
244:                         });
245:                         manager.add(new GridColumnAction(GridTableViewerComposite.this,
246:                                 Messages.GridTableViewerComposite_showAllColumnsAction) {
247:                                 @Override
248:                                 public void run() {
249:                                         for (final Widget widget : getColumns()) {
250:                                                 getGridTableViewer().getColumnConfiguration(widget).visible().setValue(Boolean.TRUE);
251:                                         }
252:                                 }
253:
254:                                 @Override
255:                                 public boolean isEnabled() {
256:                                         return getEnabledFeatures().contains(TableConfiguration.FEATURE_COLUMN_HIDE_SHOW)
257:                                                 && hasHiddenColumns();
258:                                 }
259:
260:                                 boolean hasHiddenColumns() {
261:                                         for (final Widget widget : getColumns()) {
262:                                                 if (!getGridTableViewer().getColumnConfiguration(widget).visible().getValue()) {
263:                                                         return true;
264:                                                 }
265:                                         }
266:                                         return false;
267:                                 }
268:                         });
269:                 }
270:
271:         }
272:
273:         @Override
274:         public void setCompareColumn(int columnIndex) {
275:                 // Reset other columns to avoid left over sort indicators
276:                 for (final int index : sortableColumns) {
277:                         final GridColumn column = getTableViewer().getGrid().getColumns()[index];
278:                         if (index != columnIndex && column.getSort() != SWT.NONE) {
279:                                 column.setSort(SWT.NONE);
280:                         }
281:                 }
282:                 comparator.setColumn(columnIndex);
283:                 final GridColumn tableColumn = getTableViewer().getGrid().getColumns()[columnIndex];
284:                 tableColumn.setSort(comparator.getDirection());
285:                 gridTableViewer.refresh();
286:         }
287:
288:         // TODO usable when the nebula grid RAP implementation supports header controls
289:         // /**
290:         // * Column hide/show menu listener.
291:         // *
292:         // * @author Mat Hansen
293:         // *
294:         // */
295:         // private class ColumnFilterMenuListener implements IMenuListener {
296:         //
297:         // @Override
298:         // public void menuAboutToShow(IMenuManager manager) {
299:         // final ColumnConfiguration columnConfiguration = getCurrentColumnConfig();
300:         // if (columnConfiguration == null) {
301:         // return;
302:         // }
303:         // manager.add(new GridColumnAction(GridTableViewerComposite.this,
304:         // Messages.GridTableViewerComposite_toggleFilterControlsAction) {
305:         // @Override
306:         // public void run() {
307:         // for (final Widget widget : getColumns()) {
308:         // PropertyHelper.toggle(
309:         // getGridTableViewer().getColumnConfiguration(widget).showFilterControl());
310:         // }
311:         //
312:         // // getGrid().recalculateHeader();
313:         // }
314:         //
315:         // @Override
316:         // public boolean isEnabled() {
317:         // if (!super.isEnabled()) {
318:         // return false;
319:         // }
320:         // return columnConfiguration.getEnabledFeatures()
321:         // .contains(ColumnConfiguration.FEATURE_COLUMN_FILTER);
322:         // }
323:         // });
324:         // }
325:         //
326:         // }
327:
328:         // TODO usable when the nebula grid RAP implementation supports header controls
329:         // /**
330:         // * Viewer filter for column filter support.
331:         // *
332:         // * @author Mat Hansen
333:         // *
334:         // */
335:         // private class GridColumnFilterViewerFilter extends ViewerFilter {
336:         //
337:         // private final GridTableViewerComposite tableViewerComposite;
338:         // private final GridTableViewer tableViewer;
339:         // private final Grid grid;
340:         //
341:         // /**
342:         // * The Constructor.
343:         // *
344:         // * @param tableViewerComposite the Grid table viewer composite.
345:         // * @param tableViewer the Grid table viewer.
346:         // */
347:         // GridColumnFilterViewerFilter(
348:         // GridTableViewerComposite tableViewerComposite,
349:         // GridTableViewer tableViewer) {
350:         // super();
351:         // this.tableViewerComposite = tableViewerComposite;
352:         // this.tableViewer = tableViewer;
353:         // grid = tableViewer.getGrid();
354:         // }
355:         //
356:         // @Override
357:         // public boolean select(Viewer viewer, Object parentElement, Object element) {
358:         //
359:         // if (grid.getItemCount() == 0) {
360:         // return true;
361:         // }
362:         //
363:         // grid.setRedraw(false);
364:         // final GridItem dummyItem = new GridItem(grid, SWT.NONE);
365:         //
366:         // try {
367:         //
368:         // dummyItem.setData(element);
369:         // final GridViewerRow viewerRow = (GridViewerRow) ((CustomGridTableViewer) tableViewer)
370:         // .getViewerRowFromItem(dummyItem);
371:         //
372:         // for (final Widget widget : getColumns()) {
373:         //
374:         // final ColumnConfiguration config = tableViewerComposite.getColumnConfiguration(widget);
375:         //
376:         // final Object filter = config.matchFilter().getValue();
377:         // if (filter == null || String.valueOf(filter).isEmpty()) {
378:         // continue;
379:         // }
380:         //
381:         // final GridColumn column = (GridColumn) widget;
382:         // final int columnIndex = tableViewer.getGrid().indexOf(column);
383:         //
384:         // final ViewerCell cell = viewerRow.getCell(columnIndex);
385:         // final CellLabelProvider labelProvider = tableViewer.getLabelProvider(columnIndex);
386:         // labelProvider.update(cell);
387:         //
388:         // if (!matchesColumnFilter(cell.getText(), filter)) {
389:         // return false;
390:         // }
391:         //
392:         // }
393:         //
394:         // } finally {
395:         // dummyItem.dispose();
396:         // grid.setRedraw(true);
397:         // }
398:         //
399:         // return true;
400:         // }
401:         //
402:         // /**
403:         // * Test whether the given value/filter combination matches.
404:         // *
405:         // * @param value the value to test
406:         // * @param filterValue the filter value
407:         // * @return true if the value matches the filter value
408:         // */
409:         // protected boolean matchesColumnFilter(Object value, Object filterValue) {
410:         //
411:         // if (filterValue == null) {
412:         // return false;
413:         // }
414:         //
415:         // return String.valueOf(value).toLowerCase()
416:         // .contains(String.valueOf(filterValue).toLowerCase());
417:         // }
418:         //
419:         // }
420:
421: }