Skip to content

Package: MEEditor$2

MEEditor$2

nameinstructionbranchcomplexitylinemethod
onChange(Notification)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 7 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) 2008-2011 Chair for Applied Software Engineering,
3: * Technische Universitaet Muenchen.
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: ******************************************************************************/
13: package org.eclipse.emf.ecp.editor.internal.e3;
14:
15: import java.util.ArrayList;
16: import java.util.Arrays;
17: import java.util.List;
18:
19: import org.eclipse.core.runtime.CoreException;
20: import org.eclipse.core.runtime.IConfigurationElement;
21: import org.eclipse.core.runtime.IProgressMonitor;
22: import org.eclipse.core.runtime.Platform;
23: import org.eclipse.emf.common.notify.AdapterFactory;
24: import org.eclipse.emf.common.notify.Notification;
25: import org.eclipse.emf.ecp.edit.spi.ECPContextDisposedListener;
26: import org.eclipse.emf.ecp.edit.spi.util.ECPModelElementChangeListener;
27: import org.eclipse.emf.ecp.editor.e3.AbstractMEEditorPage;
28: import org.eclipse.emf.ecp.editor.e3.ECPEditorContext;
29: import org.eclipse.emf.ecp.editor.e3.MEEditorInput;
30: import org.eclipse.emf.ecp.editor.e3.StatusMessageProvider;
31: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
32: import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
33: import org.eclipse.swt.SWTException;
34: import org.eclipse.swt.widgets.Display;
35: import org.eclipse.ui.IEditorInput;
36: import org.eclipse.ui.IEditorPart;
37: import org.eclipse.ui.IEditorSite;
38: import org.eclipse.ui.PartInitException;
39: import org.eclipse.ui.forms.editor.FormPage;
40: import org.eclipse.ui.forms.editor.SharedHeaderFormEditor;
41:
42: /**
43: * GUI view for editing MEs.
44: *
45: * @author helming
46: * @author naughton
47: */
48: public class MEEditor extends SharedHeaderFormEditor {
49:
50:         private static final String STATUS_EXTENSIONPOINT_ID = "org.eclipse.emf.ecp.editor.statusmessage"; //$NON-NLS-1$
51:
52:         private static final String DEFAULT_PAGE_ID = "Edit"; //$NON-NLS-1$
53:
54:         private static final String REPLACE_ATTRIBUTE = "replace"; //$NON-NLS-1$
55:
56:         private static final String EDITOR_PAGES_EXTENSIONPOINT_ID = "org.eclipse.emf.ecp.editor.pages"; //$NON-NLS-1$
57:
58:         /**
59:          * The Id for MEEditor. We need this to open a model element.
60:          */
61:         public static final String ID = "org.eclipse.emf.ecp.editor.internal.e3"; //$NON-NLS-1$
62:
63:         private MEEditorPage mePage;
64:
65:         private StatusMessageProvider statusMessageProvider;
66:
67:         private ECPModelElementChangeListener modelElementChangeListener;
68:
69:         private ECPEditorContext modelElementContext;
70:
71:         private ECPContextDisposedListener modelElementContextListener;
72:
73:         private MEEditorInput meInput;
74:
75:         private ShortLabelProvider shortLabelProvider;
76:
77:         private ComposedAdapterFactory composedAdapterFactory;
78:
79:         /**
80:          * Default constructor.
81:          */
82:         public MEEditor() {
83:         }
84:
85:         /**
86:          * {@inheritDoc}
87:          */
88:         @Override
89:         protected void addPages() {
90:                 final String editorID = DEFAULT_PAGE_ID;
91:                 final String editorDesc = Messages.MEEditor_Standard_View_Name;
92:                 final MEEditorInput editorInput = (MEEditorInput) getEditorInput();
93:
94:                 // add pages from the extension point
95:                 final IConfigurationElement[] configTemp = Platform.getExtensionRegistry().getConfigurationElementsFor(
96:                         EDITOR_PAGES_EXTENSIONPOINT_ID);
97:                 IConfigurationElement[] configIn = null;
98:
99:                 boolean replaceMEEditor = false;
100:                 int counter = 0;
101:
102:                 for (int i = 0; i < configTemp.length; i++) {
103:                         if (configTemp[i].getAttribute(REPLACE_ATTRIBUTE) != null
104:                                 && configTemp[i].getAttribute(REPLACE_ATTRIBUTE).equals(editorID)) {
105:                                 // if a replacement is found, create this page, so it becomes the first one
106:                                 replaceMEEditor = true;
107:                                 AbstractMEEditorPage newPage;
108:
109:                                 try {
110:                                         newPage = (AbstractMEEditorPage) configTemp[i].createExecutableExtension("class"); //$NON-NLS-1$
111:                                         final FormPage createPage = newPage.createPage(this, modelElementContext);
112:                                         if (createPage != null) {
113:                                                 addPage(createPage);
114:                                         }
115:                                 } catch (final CoreException e1) {
116:                                         Activator.logException(e1);
117:                                 }
118:
119:                                 // put remaining pages into the original configIn array
120:                                 configIn = new IConfigurationElement[configTemp.length - 1];
121:                                 for (int j = 0, k = 0; j < configTemp.length - 1; j++, k++) {
122:                                         if (counter == j) {
123:                                                 j--;
124:                                         } else {
125:                                                 configIn[j] = configTemp[k];
126:                                         }
127:                                 }
128:
129:                                 break;
130:                         }
131:                         counter++;
132:                 }
133:
134:                 // create original MEEditor standard view if no replacement exists
135:                 // and put remaining pages into the original configIn array
136:                 if (!replaceMEEditor) {
137:                         try {
138:                                 if (editorInput.getProblemFeature() != null) {
139:                                         mePage = new MEEditorPage(this, editorID, editorDesc, modelElementContext,
140:                                                 modelElementContext.getDomainObject(), editorInput.getProblemFeature());
141:                                 } else {
142:                                         mePage = new MEEditorPage(this, editorID, editorDesc, modelElementContext,
143:                                                 modelElementContext.getDomainObject());
144:                                 }
145:
146:                                 addPage(mePage);
147:                                 configIn = configTemp;
148:                         } catch (final PartInitException e) {
149:                                 // JH Auto-generated catch block
150:                                 Activator.logException(e);
151:                         }
152:                 }
153:
154:                 // Sort the pages by the "after" attribute and omit replaced pages
155:                 final List<IConfigurationElement> config = PageCandidate.getPages(configIn);
156:                 for (final IConfigurationElement e : config) {
157:                         try {
158:                                 final AbstractMEEditorPage newPage = (AbstractMEEditorPage) e.createExecutableExtension("class"); //$NON-NLS-1$
159:                                 final FormPage createPage = newPage.createPage(this, modelElementContext);
160:                                 if (createPage != null) {
161:                                         addPage(createPage);
162:                                 }
163:                         } catch (final CoreException e1) {
164:                                 Activator.logException(e1);
165:                         }
166:                 }
167:
168:         }
169:
170:         /**
171:          * {@inheritDoc}
172:          */
173:         @Override
174:         public void doSave(IProgressMonitor monitor) {
175:                 firePropertyChange(IEditorPart.PROP_DIRTY);
176:         }
177:
178:         /**
179:          * {@inheritDoc}
180:          */
181:         @Override
182:         public void doSaveAs() {
183:         }
184:
185:         /**
186:          * Save is not allowed as the editor can only modify model elements.
187:          *
188:          * @return false
189:          */
190:         @Override
191:         public boolean isSaveAsAllowed() {
192:                 return false;
193:         }
194:
195:         /**
196:          * {@inheritDoc}
197:          */
198:         @Override
199:         public void init(IEditorSite site, final IEditorInput input) throws PartInitException {
200:                 super.init(site, input);
201:                 if (input instanceof MEEditorInput) {
202:                         setInput(input);
203:                         meInput = (MEEditorInput) input;
204:
205:                         modelElementContext = meInput.getModelElementContext();
206:                         composedAdapterFactory = new ComposedAdapterFactory(new AdapterFactory[] {
207:                                 new ReflectiveItemProviderAdapterFactory(),
208:                                 new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE) });
209:                         shortLabelProvider = new ShortLabelProvider(composedAdapterFactory);
210:                         setPartName(shortLabelProvider.getText(modelElementContext.getDomainObject()));
211:                         setTitleImage(shortLabelProvider.getImage(modelElementContext.getDomainObject()));
212:
213:                         modelElementContextListener = new ECPContextDisposedListener() {
214:
215:                                 @Override
216:                                 public void contextDisposed() {
217:                                         close(false);
218:                                 }
219:
220:                         };
221:                         modelElementContext.addECPContextDisposeListener(modelElementContextListener);
222:                         modelElementChangeListener = new ECPModelElementChangeListener(modelElementContext.getDomainObject()) {
223:
224:                                 @Override
225:                                 public void onChange(Notification notification) {
226:                                         Display.getDefault().asyncExec(new Runnable() {
227:                                                 @Override
228:                                                 public void run() {
229:                                                         updateIcon();
230:                                                         setPartName(shortLabelProvider.getText(modelElementContext.getDomainObject()));
231:                                                         if (mePage != null) {
232:                                                                 mePage.updateSectionTitle();
233:                                                                 // mePage.updateLiveValidation();
234:                                                         }
235:                                                         updateStatusMessage();
236:                                                 }
237:                                         });
238:
239:                                 }
240:                         };
241:
242:                         initStatusProvider();
243:                         updateStatusMessage();
244:
245:                         // labelProviderListener = new ILabelProviderListener() {
246:                         // public void labelProviderChanged(LabelProviderChangedEvent event) {
247:                         // if(!titleImage.isDisposed())
248:                         // titleImage.dispose();
249:                         // titleImage=meInput.getImageDescriptor().createImage();
250:                         // updateIcon();
251:                         // }
252:                         // };
253:                         // meInput.getLabelProvider().addListener(labelProviderListener);
254:
255:                 } else {
256:                         throw new PartInitException("MEEditor is only appliable for MEEditorInputs"); //$NON-NLS-1$
257:                 }
258:         }
259:
260:         private void initStatusProvider() {
261:                 final IConfigurationElement[] configurationElements = Platform.getExtensionRegistry()
262:                         .getConfigurationElementsFor(
263:                                 STATUS_EXTENSIONPOINT_ID);
264:                 final ArrayList<IConfigurationElement> provider = new ArrayList<IConfigurationElement>();
265:                 provider.addAll(Arrays.asList(configurationElements));
266:                 int priority = 0;
267:                 for (final IConfigurationElement e : provider) {
268:                         try {
269:                                 final StatusMessageProvider statusMessageProvider = (StatusMessageProvider) e
270:                                         .createExecutableExtension("class"); //$NON-NLS-1$
271:                                 final int newpriority = statusMessageProvider.canRender(modelElementContext.getDomainObject());
272:                                 if (newpriority > priority) {
273:                                         priority = newpriority;
274:                                         this.statusMessageProvider = statusMessageProvider;
275:                                 }
276:                         } catch (final CoreException e1) {
277:                                 Activator.logException(e1);
278:                         }
279:                 }
280:         }
281:
282:         private void updateStatusMessage() {
283:                 if (statusMessageProvider != null) {
284:                         getEditorSite().getActionBars().getStatusLineManager()
285:                                 .setMessage(statusMessageProvider.getMessage(modelElementContext.getDomainObject()));
286:                 }
287:         }
288:
289:         /**
290:          * {@inheritDoc}
291:          */
292:         @Override
293:         public boolean isDirty() {
294:                 return false;
295:         }
296:
297:         /**
298:          * {@inheritDoc}
299:          */
300:         @Override
301:         public void setFocus() {
302:
303:                 super.setFocus();
304:                 if (mePage != null) {
305:                         mePage.setFocus();
306:                 }
307:                 updateStatusMessage();
308:
309:         }
310:
311:         /**
312:          * {@inheritDoc}
313:          */
314:         @Override
315:         public void dispose() {
316:                 modelElementChangeListener.remove();
317:                 modelElementContext.dispose();
318:                 meInput.dispose();
319:                 // ((MEEditorInput) getEditorInput()).getLabelProvider().removeListener(labelProviderListener);
320:                 composedAdapterFactory.dispose();
321:                 shortLabelProvider.dispose();
322:                 // meInput.dispose();
323:                 getSite().setSelectionProvider(null);
324:                 super.dispose();
325:
326:         }
327:
328:         private void updateIcon() {
329:
330:                 setTitleImage(shortLabelProvider.getImage(modelElementContext.getDomainObject()));
331:                 // TODO AS: Debug why sometimes the page is null - not disposed Adapter?
332:                 if (mePage != null) {
333:                         try {
334:                                 mePage.getManagedForm().getForm()
335:                                         .setImage(shortLabelProvider.getImage(modelElementContext.getDomainObject()));
336:                         } catch (final SWTException e) {
337:                                 // Catch in case Editor is directly closed after change.
338:                         }
339:                 }
340:         }
341: }