Skip to content

Package: EcorePathsRenderer

EcorePathsRenderer

nameinstructionbranchcomplexitylinemethod
EcorePathsRenderer(VControl, ViewModelContext, ReportService, EMFFormsDatabinding, EMFFormsLabelProvider, VTViewTemplateProvider, ImageRegistryService)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createUpDownButtons(Composite, IObservableList)
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%
finalizeRendering(Composite)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getValueForNewRow(EAttribute)
M: 12 C: 50
81%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 12
80%
M: 0 C: 1
100%
initButtons(IObservableList)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
postRemove(IStructuredSelection)
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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 Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.editor.controls;
15:
16: import java.io.IOException;
17: import java.text.MessageFormat;
18:
19: import javax.inject.Inject;
20:
21: import org.eclipse.core.databinding.observable.list.IObservableList;
22: import org.eclipse.core.resources.IFile;
23: import org.eclipse.core.resources.IResource;
24: import org.eclipse.core.resources.ResourcesPlugin;
25: import org.eclipse.core.runtime.IStatus;
26: import org.eclipse.core.runtime.Status;
27: import org.eclipse.emf.ecore.EAttribute;
28: import org.eclipse.emf.ecp.ide.spi.util.EcoreHelper;
29: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
30: import org.eclipse.emf.ecp.view.spi.model.VControl;
31: import org.eclipse.emf.ecp.view.spi.util.swt.ImageRegistryService;
32: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
33: import org.eclipse.emfforms.spi.common.report.AbstractReport;
34: import org.eclipse.emfforms.spi.common.report.ReportService;
35: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
36: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
37: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
38: import org.eclipse.emfforms.spi.view.control.multiattribute.MultiAttributeSWTRenderer;
39: import org.eclipse.jface.viewers.IStructuredSelection;
40: import org.eclipse.jface.window.Window;
41: import org.eclipse.swt.widgets.Composite;
42: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
43: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
44: import org.eclipse.ui.model.BaseWorkbenchContentProvider;
45: import org.eclipse.ui.model.WorkbenchLabelProvider;
46:
47: /**
48: * Custom Renderer for the EcorePath List of a View. This Renderer changes the behavior of the add and remove button so
49: * that a user gets a Dialog to select an Ecore from the Workspace in the case of an add. In the case of a remove the
50: * Ecore is deregistered.
51: *
52: * @author Eugen Neufeld
53: *
54: */
55: public class EcorePathsRenderer extends MultiAttributeSWTRenderer {
56:
57:         private Composite parent;
58:         private IObservableList list;
59:
60:         /**
61:          * Default constructor.
62:          *
63:          * @param vElement The {@link VControl} of the Renderer
64:          * @param viewContext The {@link ViewModelContext} of the Renderer
65:          * @param reportService The {@link ReportService} to use for logging
66:          * @param emfFormsDatabinding The {@link EMFFormsDatabinding} to use
67:          * @param emfFormsLabelProvider The {@link EMFFormsLabelProvider} to use
68:          * @param vtViewTemplateProvider The {@link VTViewTemplateProvider} to use
69:          * @param imageRegistryService The {@link ImageRegistryService} to use
70:          */
71:         @Inject
72:         public EcorePathsRenderer(VControl vElement, ViewModelContext viewContext, ReportService reportService,
73:                 EMFFormsDatabinding emfFormsDatabinding, EMFFormsLabelProvider emfFormsLabelProvider,
74:                 VTViewTemplateProvider vtViewTemplateProvider, ImageRegistryService imageRegistryService) {
75:                 super(vElement, viewContext, reportService, emfFormsDatabinding, emfFormsLabelProvider, vtViewTemplateProvider,
76:                         imageRegistryService);
77:         }
78:
79:         @Override
80:         public void finalizeRendering(Composite parent) {
81:                 super.finalizeRendering(parent);
82:                 this.parent = parent;
83:         }
84:
85:         @Override
86:         protected void initButtons(IObservableList list) {
87:                 this.list = list;
88:                 super.initButtons(list);
89:         }
90:
91:         @Override
92:         protected Object getValueForNewRow(EAttribute attribute) {
93:                 final ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(parent.getShell(),
94:                         new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
95:                 dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
96:                 dialog.setAllowMultiple(false);
97:                 dialog.setValidator(new ISelectionStatusValidator() {
98:
99:                         @Override
100:                         public IStatus validate(Object[] selection) {
101:                                 if (selection.length == 1 && selection[0] instanceof IFile) {
102:                                         final IFile file = (IFile) selection[0];
103:                                         if (file.getType() == IResource.FILE && "ecore".equalsIgnoreCase(file.getFileExtension())) { //$NON-NLS-1$
104:                                                 if (list.contains(file.getFullPath().toString())) {
105:                                                         return new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
106:                                                                 MessageFormat.format(LocalizationServiceHelper.getString(EcorePathsRenderer.class,
107:                                                                         "EcorePath_AddDialog_Status_AlreadyAdded"), file.getName()), //$NON-NLS-1$
108:                                                                 null);
109:                                                 }
110:                                                 return new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, null, null);
111:                                         }
112:                                 }
113:                                 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
114:                                         LocalizationServiceHelper.getString(EcorePathsRenderer.class,
115:                                                 "EcorePath_AddDialog_Status_NotEcore"), //$NON-NLS-1$
116:                                         null);
117:                         }
118:                 });
119:
120:                 dialog.setTitle(LocalizationServiceHelper.getString(EcorePathsRenderer.class, "EcorePath_AddDialog_Title")); //$NON-NLS-1$
121:                 dialog.setMessage(LocalizationServiceHelper.getString(EcorePathsRenderer.class, "EcorePath_AddDialog_Message")); //$NON-NLS-1$
122:
123:•                if (dialog.open() == Window.OK) {
124:                         final String path = ((IFile) dialog.getFirstResult()).getFullPath().toString();
125:                         try {
126:                                 EcoreHelper.registerEcore(path);
127:                         } catch (final IOException ex) {
128:                                 getReportService().report(new AbstractReport(ex));
129:                                 return null;
130:                         }
131:                         return path;
132:                 }
133:                 return null;
134:         }
135:
136:         @Override
137:         protected void postRemove(IStructuredSelection selection) {
138:•                for (final Object toDelete : selection.toList()) {
139:                         EcoreHelper.unregisterEcore(toDelete.toString());
140:                 }
141:         }
142:
143:         @Override
144:         protected void createUpDownButtons(Composite composite, IObservableList list) {
145:                 // do nothing
146:         }
147:
148: }