Skip to content

Package: TableControlRendererSort_PTest$TableControlSWTRendererSortService

TableControlRendererSort_PTest$TableControlSWTRendererSortService

nameinstructionbranchcomplexitylinemethod
getRendererClass()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isApplicable(VElement, ViewModelContext)
M: 4 C: 18
82%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 6
75%
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: * eugen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.table.swt;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.fail;
18:
19: import java.util.Arrays;
20: import java.util.Dictionary;
21: import java.util.Hashtable;
22:
23: import javax.inject.Inject;
24:
25: import org.eclipse.emf.ecore.EAttribute;
26: import org.eclipse.emf.ecore.EClass;
27: import org.eclipse.emf.ecore.EReference;
28: import org.eclipse.emf.ecore.EcoreFactory;
29: import org.eclipse.emf.ecore.EcorePackage;
30: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
31: import org.eclipse.emf.ecp.view.spi.model.VElement;
32: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
33: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
34: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
35: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
36: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
37: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
38: import org.eclipse.emf.ecp.view.spi.util.swt.ImageRegistryService;
39: import org.eclipse.emf.ecp.view.table.test.common.TableTestUtil;
40: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
41: import org.eclipse.emf.ecp.view.test.common.swt.spi.DatabindingClassRunner;
42: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
43: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTViewTestHelper;
44: import org.eclipse.emfforms.spi.common.report.ReportService;
45: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
46: import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
47: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
48: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
49: import org.eclipse.emfforms.spi.swt.core.EMFFormsNoRendererException;
50: import org.eclipse.emfforms.spi.swt.core.di.EMFFormsDIRendererService;
51: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
52: import org.eclipse.jface.viewers.Viewer;
53: import org.eclipse.swt.SWT;
54: import org.eclipse.swt.widgets.Composite;
55: import org.eclipse.swt.widgets.Control;
56: import org.eclipse.swt.widgets.Shell;
57: import org.eclipse.swt.widgets.Table;
58: import org.junit.After;
59: import org.junit.AfterClass;
60: import org.junit.Before;
61: import org.junit.BeforeClass;
62: import org.junit.Test;
63: import org.junit.runner.RunWith;
64: import org.osgi.framework.Bundle;
65: import org.osgi.framework.BundleContext;
66: import org.osgi.framework.Constants;
67: import org.osgi.framework.FrameworkUtil;
68: import org.osgi.framework.ServiceRegistration;
69:
70: /**
71: * @author eugen
72: *
73: */
74: @RunWith(DatabindingClassRunner.class)
75: public class TableControlRendererSort_PTest {
76:
77:         private static class TableControlSWTRendererSort extends TableControlSWTRenderer {
78:
79:                 @Inject
80:                 // CHECKSTYLE.OFF: ParameterNumber
81:                 TableControlSWTRendererSort(VTableControl vElement, ViewModelContext viewContext,
82:                         ReportService reportService,
83:                         EMFFormsDatabindingEMF emfFormsDatabinding, EMFFormsLabelProvider emfFormsLabelProvider,
84:                         VTViewTemplateProvider vtViewTemplateProvider, ImageRegistryService imageRegistryService,
85:                         EMFFormsEditSupport emfFormsEditSupport) {
86:                         // CHECKSTYLE.ON: ParameterNumber
87:                         super(vElement, viewContext, reportService, emfFormsDatabinding, emfFormsLabelProvider,
88:                                 vtViewTemplateProvider,
89:                                 imageRegistryService, emfFormsEditSupport);
90:                 }
91:
92:                 @Override
93:                 protected TableControlComparator createTableViewerComparator() {
94:                         // CHECKSTYLE.OFF: AnonInnerLength
95:                         return new TableControlComparator() {
96:                                 private int propertyIndex;
97:                                 private int direction;
98:
99:                                 @Override
100:                                 public int getDirection() {
101:                                         switch (direction) {
102:                                         case 0:
103:                                                 return SWT.DOWN; // ascending
104:                                         case 1:
105:                                                 return SWT.UP; // descending
106:                                         default:
107:                                                 return SWT.DOWN;
108:                                         }
109:                                 }
110:
111:                                 @Override
112:                                 public void setColumn(int column) {
113:                                         if (column == propertyIndex) {
114:                                                 // Same column as last sort; toggle the direction
115:                                                 direction = (direction + 1) % 2;
116:                                         } else {
117:                                                 // New column; do an ascending sort
118:                                                 propertyIndex = column;
119:                                                 direction = 0;
120:                                         }
121:                                         // columnFeatures starts at index 0 with the first regular column
122:                                         setSortColumnFeature(getColumnFeature(propertyIndex));
123:                                 }
124:
125:                                 @Override
126:                                 public int compare(Viewer viewer, Object e1, Object e2) {
127:                                         return TableControlSWTRendererSort.this.compare(viewer, e1, e2, direction + 1, propertyIndex);
128:                                 }
129:                         };
130:                         // CHECKSTYLE.ON: AnonInnerLength
131:                 }
132:
133:                 @Override
134:                 protected Control renderTableControl(SWTGridCell gridCell, Composite parent)
135:                         throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
136:                         final Control result = super.renderTableControl(gridCell, parent);
137:                         getTableViewerComposite().setCompareColumn(1);
138:                         return result;
139:                 }
140:
141:         }
142:
143:         private static class TableControlSWTRendererSortService implements EMFFormsDIRendererService<VTableControl> {
144:
145:                 @Override
146:                 public double isApplicable(VElement vElement, ViewModelContext viewModelContext) {
147:•                        if (!VTableControl.class.isInstance(vElement)) {
148:                                 return NOT_APPLICABLE;
149:                         }
150:                         final VTableControl tableControl = (VTableControl) vElement;
151:                         final VTableDomainModelReference domainRef = (VTableDomainModelReference) tableControl
152:                                 .getDomainModelReference();
153:•                        if (domainRef.getDomainModelEFeature() == EcorePackage.eINSTANCE.getEClass_EStructuralFeatures()) {
154:                                 return Double.MAX_VALUE;
155:                         }
156:                         return NOT_APPLICABLE;
157:                 }
158:
159:                 @Override
160:                 public Class<? extends AbstractSWTRenderer<VTableControl>> getRendererClass() {
161:                         return TableControlSWTRendererSort.class;
162:                 }
163:
164:         }
165:
166:         private static ServiceRegistration<EMFFormsDIRendererService> serviceRegistration;
167:         private Shell shell;
168:
169:         @BeforeClass
170:         public static void beforeClass() {
171:                 final Bundle bundle = FrameworkUtil.getBundle(TableControlRendererSort_PTest.class);
172:                 final BundleContext bundleContext = bundle.getBundleContext();
173:                 final Dictionary<String, Object> properties = new Hashtable<String, Object>();
174:                 properties.put(Constants.SERVICE_RANKING, Double.MAX_VALUE);
175:                 serviceRegistration = bundleContext.registerService(EMFFormsDIRendererService.class,
176:                         new TableControlSWTRendererSortService(), properties);
177:         }
178:
179:         @AfterClass
180:         public static void afterClass() {
181:                 serviceRegistration.unregister();
182:         }
183:
184:         @Before
185:         public void init() {
186:                 shell = SWTViewTestHelper.createShell();
187:         }
188:
189:         @After
190:         public void after() {
191:                 if (shell != null && !shell.isDisposed()) {
192:                         shell.dispose();
193:                 }
194:         }
195:
196:         @Test
197:         public void tableCustomSort()
198:                 throws EMFFormsNoRendererException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
199:
200:                 // table control
201:                 final VTableControl tableControl = TableTestUtil.createTableControl();
202:                 final VTableDomainModelReference tableDMR = (VTableDomainModelReference) tableControl.getDomainModelReference();
203:                 tableDMR.setDomainModelEFeature(EcorePackage.eINSTANCE.getEClass_EStructuralFeatures());
204:                 tableDMR.getColumnDomainModelReferences().add(createDMR(EcorePackage.eINSTANCE.getENamedElement_Name()));
205:
206:                 // render
207:                 shell.open();
208:
209:                 final EClass domain = EcoreFactory.eINSTANCE.createEClass();
210:                 final EAttribute att1 = EcoreFactory.eINSTANCE.createEAttribute();
211:                 att1.setName("zzz");
212:                 final EAttribute att2 = EcoreFactory.eINSTANCE.createEAttribute();
213:                 att2.setName("aaa");
214:                 domain.getEStructuralFeatures().add(att1);
215:                 domain.getEStructuralFeatures().add(att2);
216:
217:                 Control control = null;
218:                 try {
219:                         control = SWTViewTestHelper.render(tableControl, domain, shell);
220:                 } catch (NoRendererFoundException | NoPropertyDescriptorFoundExeption | EMFFormsNoRendererException ex) {
221:                         fail("An exception occurred while rendering the table: " + ex.getMessage());
222:                 }
223:                 if (control == null) {
224:                         fail("No control was rendered");
225:                 }
226:                 final Table table = SWTTestUtil.findControl(control, 0, Table.class);
227:
228:                 SWTTestUtil.waitForUIThread();
229:                 assertEquals(SWT.DOWN, table.getSortDirection()); // SWT.DOWN := ascending sorting
230:
231:                 final EAttribute first = (EAttribute) table.getItem(0).getData();
232:                 assertEquals("aaa", first.getName());
233:
234:                 final EAttribute second = (EAttribute) table.getItem(1).getData();
235:                 assertEquals("zzz", second.getName());
236:         }
237:
238:         private static VFeaturePathDomainModelReference createDMR(EAttribute attribute, EReference... refs) {
239:                 final VFeaturePathDomainModelReference dmr = VViewFactory.eINSTANCE.createFeaturePathDomainModelReference();
240:                 dmr.setDomainModelEFeature(attribute);
241:                 dmr.getDomainModelEReferencePath().addAll(Arrays.asList(refs));
242:                 return dmr;
243:         }
244: }