Skip to content

Package: ECPE4Editor$1$1

ECPE4Editor$1$1

nameinstructionbranchcomplexitylinemethod
run()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 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-2013 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: *******************************************************************************/
15: package org.eclipse.emf.ecp.ui.e4.editor;
16:
17: import java.net.URL;
18:
19: import javax.annotation.PreDestroy;
20: import javax.inject.Inject;
21: import javax.inject.Named;
22:
23: import org.eclipse.e4.core.di.annotations.Optional;
24: import org.eclipse.e4.ui.di.Focus;
25: import org.eclipse.e4.ui.model.application.ui.basic.MPart;
26: import org.eclipse.emf.common.notify.Adapter;
27: import org.eclipse.emf.common.notify.Notification;
28: import org.eclipse.emf.common.notify.impl.AdapterImpl;
29: import org.eclipse.emf.common.util.URI;
30: import org.eclipse.emf.ecore.EObject;
31: import org.eclipse.emf.ecp.core.ECPProject;
32: import org.eclipse.emf.ecp.edit.spi.DeleteService;
33: import org.eclipse.emf.ecp.edit.spi.EMFDeleteServiceImpl;
34: import org.eclipse.emf.ecp.spi.core.InternalProvider;
35: import org.eclipse.emf.ecp.spi.ui.ECPDeleteServiceImpl;
36: import org.eclipse.emf.ecp.spi.ui.ECPReferenceServiceImpl;
37: import org.eclipse.emf.ecp.ui.internal.e4.Activator;
38: import org.eclipse.emf.ecp.ui.view.ECPRendererException;
39: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTView;
40: import org.eclipse.emf.ecp.ui.view.swt.ECPSWTViewRenderer;
41: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
42: import org.eclipse.emf.ecp.view.spi.context.ViewModelContextFactory;
43: import org.eclipse.emf.ecp.view.spi.model.VView;
44: import org.eclipse.emf.ecp.view.spi.provider.ViewProviderHelper;
45: import org.eclipse.emf.ecp.view.spi.swt.reporting.RenderingFailedReport;
46: import org.eclipse.emf.edit.provider.ComposedImage;
47: import org.eclipse.emf.edit.provider.IItemLabelProvider;
48: import org.eclipse.swt.SWT;
49: import org.eclipse.swt.custom.ScrolledComposite;
50: import org.eclipse.swt.widgets.Composite;
51: import org.eclipse.swt.widgets.Display;
52: import org.eclipse.swt.widgets.Shell;
53:
54: /**
55: * Editor displaying one {@link EObject}.
56: *
57: * @author Jonas
58: *
59: */
60: public class ECPE4Editor {
61:         /**
62:          * Key to set the input of the editor into the {@link org.eclipse.e4.core.contexts.IEclipseContext}.
63:          */
64:         public static final java.lang.String INPUT = "ecpEditorInput"; //$NON-NLS-1$
65:         private MPart part;
66:         private EObject modelElement;
67:         private Adapter adapter;
68:         private final ScrolledComposite parent;
69:
70:         /**
71:          * Default constructor.
72:          *
73:          * @param composite the parent composite.
74:          * @param shell to retrieve the display from. Used to retrieve the system colors.
75:          */
76:         @Inject
77:         public ECPE4Editor(Composite composite, Shell shell) {
78:
79:                 parent = new ScrolledComposite(composite, SWT.V_SCROLL
80:                         | SWT.H_SCROLL);
81:                 parent.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
82:                 parent.setBackgroundMode(SWT.INHERIT_FORCE);
83:         }
84:
85:         /**
86:          * Sets the input of the editor part.
87:          *
88:          * @param modelElement the {@link EObject} to be opened
89:          * @param part the corresponding {@link MPart}
90:          * @param ecpProject The {@link ECPProject} (optional parameter)
91:          */
92:         @Inject
93:         public void setInput(@Optional @Named(INPUT) EObject modelElement, MPart part, @Optional ECPProject ecpProject) {
94:                 if (modelElement == null) {
95:                         return;
96:                 }
97:                 this.part = part;
98:                 this.modelElement = modelElement;
99:                 ECPSWTView render = null;
100:                 DeleteService deleteService;
101:                 if (ecpProject != null) {
102:                         deleteService = new ECPDeleteServiceImpl();
103:                 } else {
104:                         deleteService = new EMFDeleteServiceImpl();
105:                 }
106:                 try {
107:                         // render = ECPSWTViewRenderer.INSTANCE.render(parent, modelElement);
108:                         final VView view = ViewProviderHelper.getView(modelElement, null);
109:                         final ViewModelContext vmc = ViewModelContextFactory.INSTANCE.createViewModelContext(view, modelElement,
110:                                 new ECPReferenceServiceImpl(), deleteService);
111:
112:                         render = ECPSWTViewRenderer.INSTANCE.render(parent, vmc);
113:
114:                         parent.setExpandHorizontal(true);
115:                         parent.setExpandVertical(true);
116:                         parent.setContent(render.getSWTControl());
117:                         parent.setMinSize(render.getSWTControl().computeSize(SWT.DEFAULT, SWT.DEFAULT));
118:                 } catch (final ECPRendererException ex) {
119:                         Activator.getReportService().report(new RenderingFailedReport(ex));
120:                         // MessageDialog.openError(parent.getShell(), ex.getClass().getName(), ex.getMessage());
121:                         // logger.log(LogService.LOG_ERROR, ex.getMessage(), ex);
122:                 }
123:
124:                 updateImageAndText();
125:                 adapter = new AdapterImpl() {
126:
127:                         /*
128:                          * (non-Javadoc)
129:                          * @see
130:                          * org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
131:                          */
132:                         @Override
133:                         public void notifyChanged(Notification msg) {
134:                                 Display.getDefault().asyncExec(new Runnable() {
135:                                         @Override
136:                                         public void run() {
137:                                                 updateImageAndText();
138:                                         }
139:                                 });
140:                         }
141:
142:                 };
143:                 modelElement.eAdapters().add(adapter);
144:         }
145:
146:         /**
147:          * removes listener.
148:          */
149:         @PreDestroy
150:         void dispose() {
151:                 modelElement.eAdapters().remove(adapter);
152:         }
153:
154:         private void updateImageAndText() {
155:
156:                 final IItemLabelProvider itemLabelProvider = (IItemLabelProvider) InternalProvider.EMF_ADAPTER_FACTORY.adapt(
157:                         modelElement, IItemLabelProvider.class);
158:
159:                 part.setLabel(itemLabelProvider.getText(modelElement));
160:                 part.setTooltip(itemLabelProvider.getText(modelElement));
161:
162:                 Object image = itemLabelProvider.getImage(modelElement);
163:                 String iconUri = null;
164:                 if (ComposedImage.class.isInstance(image)) {
165:                         final ComposedImage composedImage = (ComposedImage) image;
166:                         image = composedImage.getImages().get(0);
167:                 }
168:                 if (URI.class.isInstance(image)) {
169:                         final URI uri = (URI) image;
170:                         iconUri = uri.toString();
171:                 }
172:                 if (URL.class.isInstance(image)) {
173:                         final URL uri = (URL) image;
174:                         iconUri = uri.toString();
175:                 }
176:
177:                 part.setIconURI(iconUri);
178:         }
179:
180:         /**
181:          * Sets the focus to the parent composite.
182:          */
183:         @Focus
184:         void setFocus() {
185:                 if (parent != null) {
186:                         parent.setFocus();
187:                 }
188:         }
189: }