-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.diagramEditor.e4;\r
-\r
-import java.lang.reflect.Constructor;\r
-\r
-import org.eclipse.core.runtime.IConfigurationElement;\r
-import org.eclipse.core.runtime.IExecutableExtension;\r
-import org.eclipse.core.runtime.Platform;\r
-import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
-import org.eclipse.e4.ui.workbench.modeling.EPartService;\r
-import org.eclipse.e4.ui.workbench.modeling.IPartListener;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.ui.PartInitException;\r
-import org.osgi.framework.Bundle;\r
-import org.simantics.db.Resource;\r
-import org.simantics.modeling.ui.diagramEditor.DisposingPolicy;\r
-import org.simantics.modeling.ui.diagramEditor.e4.DiagramViewer.DiagramViewerHost;\r
-import org.simantics.ui.workbench.e4.E4ResourceEditorBase;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-/**\r
- * A class for diagram editor parts that contains logic for destruction and\r
- * (re)initialization of the actual diagram viewer and its controls during the\r
- * life cycle of this editor part.\r
- * \r
- * <p>\r
- * To use this class in an editor part extension, define the following in the\r
- * <code>class</code> attribute of the extension:\r
- * \r
- * <pre>\r
- * class="org.simantics.modeling.ui.diagramEditor.DiagramEditor:viewer=%VIEWER%"\r
- * </pre>\r
- * \r
- * where <code>%VIEWER%</code> is the name of the class that either is or\r
- * extends {@link org.simantics.modeling.ui.diagramEditor.DiagramViewer}. The\r
- * <code>viewer</code> argument tells {@link DiagramEditor} where to find the\r
- * initializer for the diagram editor controls. The initializer must have a\r
- * default constructor.\r
- * \r
- * <p>\r
- * This class is not intended to be extended by clients. Customizations should\r
- * be performed through the viewer class.\r
- * \r
- * @author Tuukka Lehtonen\r
- * @author Antti Villberg\r
- */\r
-public class DiagramEditor extends E4ResourceEditorBase implements DiagramViewerHost, IExecutableExtension, IPartListener {\r
-\r
- /**\r
- * The {@value #ARG_VIEWER} argument for this editor part class tells the\r
- * name of the class to use for initializing the diagram viewer, i.e.\r
- * {@link #viewer}. The class is instantiated through reflection using the\r
- * class loader of the bundle named {@link #viewerContributor}.\r
- * \r
- * @see #setInitializationData(IConfigurationElement, String, Object)\r
- */\r
- public static final String ARG_VIEWER = "viewer";\r
-\r
- private Composite parent;\r
-\r
- private String viewerContributor;\r
- private String viewerClassName;\r
-\r
-// private ResourceEditorSupport support;\r
- private DiagramViewer viewer;\r
-\r
- /**\r
- * Reads the class arguments from the string in the data argument.\r
- * \r
- * @see org.eclipse.ui.part.EditorPart#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,\r
- * java.lang.String, java.lang.Object)\r
- * @see #createViewer()\r
- */\r
- @Override\r
- public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {\r
-// super.setInitializationData(cfig, propertyName, data);\r
-\r
- if (data instanceof String) {\r
- viewerContributor = cfig.getContributor().getName();\r
-\r
- String[] parameters = ((String) data).split(";");\r
-\r
- for (String parameter : parameters) {\r
- String[] keyValue = parameter.split("=");\r
- if (keyValue.length > 2) {\r
- ErrorLogger.defaultLogWarning("Invalid parameter '" + parameter + ". Complete view argument: " + data, null);\r
- continue;\r
- }\r
- String key = keyValue[0];\r
- String value = keyValue.length > 1 ? keyValue[1] : "";\r
-\r
- if (ARG_VIEWER.equals(key)) {\r
- viewerClassName = value;\r
- } \r
- }\r
- }\r
- }\r
-\r
- protected DiagramViewer createViewer() throws PartInitException {\r
- if (viewerClassName == null)\r
- throw new PartInitException(\r
- "DiagramViewer contributor class was not specified in editor extension's class attribute viewer-argument. contributor is '"\r
- + viewerContributor + "'");\r
-\r
- try {\r
- Bundle b = Platform.getBundle(viewerContributor);\r
- if (b == null)\r
- throw new PartInitException("DiagramViewer '" + viewerClassName + "' contributor bundle '"\r
- + viewerContributor + "' was not found in the platform.");\r
-\r
- Class<?> clazz = b.loadClass(viewerClassName);\r
- if (!DiagramViewer.class.isAssignableFrom(clazz))\r
- throw new PartInitException("DiagramViewer class '" + viewerClassName + "' is not assignable to "\r
- + DiagramViewer.class + ".");\r
-\r
- Constructor<?> ctor = clazz.getConstructor();\r
- return (DiagramViewer) ctor.newInstance();\r
- } catch (Exception e) {\r
- throw new PartInitException("Failed to instantiate DiagramViewer implementation '" + viewerClassName\r
- + "' from bundle '" + viewerContributor + "'. See exception for details.", e);\r
- }\r
- }\r
-\r
- public DiagramViewer getViewer() {\r
- return viewer;\r
- }\r
-\r
- public Resource getRuntimeResource() {\r
- DiagramViewer viewer = this.viewer;\r
- return viewer != null ? viewer.getRuntime() : null;\r
- }\r
- \r
-// public Resource getInputResource() {\r
-// DiagramViewer viewer = this.viewer;\r
-// return viewer != null ? viewer.getInputResource() : null;\r
-// }\r
-\r
- @Override\r
- public void initImpl(MPart part) {\r
- super.initImpl(part);\r
- \r
- try {\r
- viewer = createViewer();\r
- } catch (PartInitException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- }\r
- // #TODO Finish this when we are going to use full E4 workbench\r
-// viewer.init(this, getResourceInput());\r
- \r
- EPartService partService = part.getContext().get(EPartService.class);\r
- partService.addPartListener(this);\r
- }\r
-\r
- @Override\r
- public void createPartControl(Composite parent) {\r
- this.parent = parent;\r
- initializeViewer();\r
- }\r
-\r
- private void initializeViewer() {\r
- parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));\r
- viewer.createPartControl(parent);\r
- // It is possible that something goes wrong and the parent gets disposed already\r
- if(parent.isDisposed()) return;\r
- parent.layout(true);\r
- }\r
-\r
- @Override\r
- public void setFocus() {\r
- if (viewer != null)\r
- viewer.setFocus();\r
- }\r
-\r
- @SuppressWarnings("rawtypes")\r
- public Object getAdapter(Class adapter) {\r
- if (adapter == DiagramViewer.class)\r
- return viewer;\r
- if (viewer != null) {\r
- Object result = viewer.getAdapter(adapter);\r
- if (result != null)\r
- return result;\r
- }\r
- return null;\r
- }\r
-\r
-\r
- @Override\r
- public void dispose() {\r
- \r
- EPartService partService = getPart().getContext().get(EPartService.class);\r
- partService.removePartListener(this);\r
-\r
- DISPOSING_POLICY.removeDisposer(disposer);\r
- tryDisposeViewer();\r
-\r
- }\r
-\r
- \r
- public void doSetPartName(String name) {\r
- getPart().setLabel(name);\r
- }\r
-\r
- \r
- public void doSetTitleToolTip(String tooltip) {\r
- getPart().setTooltip(tooltip);\r
- }\r
- \r
- private static final DisposingPolicy DISPOSING_POLICY = \r
- new DisposingPolicy();\r
- \r
- private Runnable disposer = new Runnable() {\r
- @Override\r
- public void run() {\r
- tryDisposeViewer();\r
- }\r
- };\r
-\r
- private void tryDisposeViewer() {\r
- if (viewer != null) {\r
- Composite viewerComposite = viewer.getComposite();\r
- viewer.dispose();\r
- viewer = null;\r
- if (viewerComposite != null) {\r
- viewerComposite.dispose();\r
- }\r
- }\r
- }\r
- \r
- @Override\r
- public void partVisible(MPart part) {\r
- if (getPart().equals(part)) {\r
- DISPOSING_POLICY.removeDisposer(disposer);\r
- if (viewer == null) {\r
- try {\r
- viewer = createViewer();\r
- // #TODO Finish this when we are going to use full E4 workbench\r
-// viewer.init(DiagramEditor.this, getResourceInput());\r
- initializeViewer();\r
- } catch (PartInitException e) {\r
- // This should never happen!\r
- ErrorLogger.defaultLogError(e);\r
- }\r
- }\r
- }\r
- }\r
- \r
- @Override\r
- public void partHidden(MPart part) {\r
- if (getPart().equals(part)) {\r
- DISPOSING_POLICY.addDisposer(disposer);\r
- }\r
- }\r
- \r
- @Override\r
- public void partDeactivated(MPart part) {\r
- }\r
- \r
- @Override\r
- public void partBroughtToTop(MPart part) {\r
- }\r
- \r
- @Override\r
- public void partActivated(MPart part) {\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.diagramEditor.e4;
+
+import java.lang.reflect.Constructor;
+
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExecutableExtension;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+import org.eclipse.e4.ui.workbench.modeling.IPartListener;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.ui.PartInitException;
+import org.osgi.framework.Bundle;
+import org.simantics.db.Resource;
+import org.simantics.modeling.ui.diagramEditor.DisposingPolicy;
+import org.simantics.modeling.ui.diagramEditor.e4.DiagramViewer.DiagramViewerHost;
+import org.simantics.ui.workbench.e4.E4ResourceEditorBase;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * A class for diagram editor parts that contains logic for destruction and
+ * (re)initialization of the actual diagram viewer and its controls during the
+ * life cycle of this editor part.
+ *
+ * <p>
+ * To use this class in an editor part extension, define the following in the
+ * <code>class</code> attribute of the extension:
+ *
+ * <pre>
+ * class="org.simantics.modeling.ui.diagramEditor.DiagramEditor:viewer=%VIEWER%"
+ * </pre>
+ *
+ * where <code>%VIEWER%</code> is the name of the class that either is or
+ * extends {@link org.simantics.modeling.ui.diagramEditor.DiagramViewer}. The
+ * <code>viewer</code> argument tells {@link DiagramEditor} where to find the
+ * initializer for the diagram editor controls. The initializer must have a
+ * default constructor.
+ *
+ * <p>
+ * This class is not intended to be extended by clients. Customizations should
+ * be performed through the viewer class.
+ *
+ * @author Tuukka Lehtonen
+ * @author Antti Villberg
+ */
+public class DiagramEditor extends E4ResourceEditorBase implements DiagramViewerHost, IExecutableExtension, IPartListener {
+
+ /**
+ * The {@value #ARG_VIEWER} argument for this editor part class tells the
+ * name of the class to use for initializing the diagram viewer, i.e.
+ * {@link #viewer}. The class is instantiated through reflection using the
+ * class loader of the bundle named {@link #viewerContributor}.
+ *
+ * @see #setInitializationData(IConfigurationElement, String, Object)
+ */
+ public static final String ARG_VIEWER = "viewer";
+
+ private Composite parent;
+
+ private String viewerContributor;
+ private String viewerClassName;
+
+// private ResourceEditorSupport support;
+ private DiagramViewer viewer;
+
+ /**
+ * Reads the class arguments from the string in the data argument.
+ *
+ * @see org.eclipse.ui.part.EditorPart#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
+ * java.lang.String, java.lang.Object)
+ * @see #createViewer()
+ */
+ @Override
+ public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
+// super.setInitializationData(cfig, propertyName, data);
+
+ if (data instanceof String) {
+ viewerContributor = cfig.getContributor().getName();
+
+ String[] parameters = ((String) data).split(";");
+
+ for (String parameter : parameters) {
+ String[] keyValue = parameter.split("=");
+ if (keyValue.length > 2) {
+ ErrorLogger.defaultLogWarning("Invalid parameter '" + parameter + ". Complete view argument: " + data, null);
+ continue;
+ }
+ String key = keyValue[0];
+ String value = keyValue.length > 1 ? keyValue[1] : "";
+
+ if (ARG_VIEWER.equals(key)) {
+ viewerClassName = value;
+ }
+ }
+ }
+ }
+
+ protected DiagramViewer createViewer() throws PartInitException {
+ if (viewerClassName == null)
+ throw new PartInitException(
+ "DiagramViewer contributor class was not specified in editor extension's class attribute viewer-argument. contributor is '"
+ + viewerContributor + "'");
+
+ try {
+ Bundle b = Platform.getBundle(viewerContributor);
+ if (b == null)
+ throw new PartInitException("DiagramViewer '" + viewerClassName + "' contributor bundle '"
+ + viewerContributor + "' was not found in the platform.");
+
+ Class<?> clazz = b.loadClass(viewerClassName);
+ if (!DiagramViewer.class.isAssignableFrom(clazz))
+ throw new PartInitException("DiagramViewer class '" + viewerClassName + "' is not assignable to "
+ + DiagramViewer.class + ".");
+
+ Constructor<?> ctor = clazz.getConstructor();
+ return (DiagramViewer) ctor.newInstance();
+ } catch (Exception e) {
+ throw new PartInitException("Failed to instantiate DiagramViewer implementation '" + viewerClassName
+ + "' from bundle '" + viewerContributor + "'. See exception for details.", e);
+ }
+ }
+
+ public DiagramViewer getViewer() {
+ return viewer;
+ }
+
+ public Resource getRuntimeResource() {
+ DiagramViewer viewer = this.viewer;
+ return viewer != null ? viewer.getRuntime() : null;
+ }
+
+// public Resource getInputResource() {
+// DiagramViewer viewer = this.viewer;
+// return viewer != null ? viewer.getInputResource() : null;
+// }
+
+ @Override
+ public void initImpl(MPart part) {
+ super.initImpl(part);
+
+ try {
+ viewer = createViewer();
+ } catch (PartInitException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ // #TODO Finish this when we are going to use full E4 workbench
+// viewer.init(this, getResourceInput());
+
+ EPartService partService = part.getContext().get(EPartService.class);
+ partService.addPartListener(this);
+ }
+
+ @Override
+ public void createPartControl(Composite parent) {
+ this.parent = parent;
+ initializeViewer();
+ }
+
+ private void initializeViewer() {
+ parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
+ viewer.createPartControl(parent);
+ // It is possible that something goes wrong and the parent gets disposed already
+ if(parent.isDisposed()) return;
+ parent.layout(true);
+ }
+
+ @Override
+ public void setFocus() {
+ if (viewer != null)
+ viewer.setFocus();
+ }
+
+ @SuppressWarnings("rawtypes")
+ public Object getAdapter(Class adapter) {
+ if (adapter == DiagramViewer.class)
+ return viewer;
+ if (viewer != null) {
+ Object result = viewer.getAdapter(adapter);
+ if (result != null)
+ return result;
+ }
+ return null;
+ }
+
+
+ @Override
+ public void dispose() {
+
+ EPartService partService = getPart().getContext().get(EPartService.class);
+ partService.removePartListener(this);
+
+ DISPOSING_POLICY.removeDisposer(disposer);
+ tryDisposeViewer();
+
+ }
+
+
+ public void doSetPartName(String name) {
+ getPart().setLabel(name);
+ }
+
+
+ public void doSetTitleToolTip(String tooltip) {
+ getPart().setTooltip(tooltip);
+ }
+
+ private static final DisposingPolicy DISPOSING_POLICY =
+ new DisposingPolicy();
+
+ private Runnable disposer = new Runnable() {
+ @Override
+ public void run() {
+ tryDisposeViewer();
+ }
+ };
+
+ private void tryDisposeViewer() {
+ if (viewer != null) {
+ Composite viewerComposite = viewer.getComposite();
+ viewer.dispose();
+ viewer = null;
+ if (viewerComposite != null) {
+ viewerComposite.dispose();
+ }
+ }
+ }
+
+ @Override
+ public void partVisible(MPart part) {
+ if (getPart().equals(part)) {
+ DISPOSING_POLICY.removeDisposer(disposer);
+ if (viewer == null) {
+ try {
+ viewer = createViewer();
+ // #TODO Finish this when we are going to use full E4 workbench
+// viewer.init(DiagramEditor.this, getResourceInput());
+ initializeViewer();
+ } catch (PartInitException e) {
+ // This should never happen!
+ ErrorLogger.defaultLogError(e);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void partHidden(MPart part) {
+ if (getPart().equals(part)) {
+ DISPOSING_POLICY.addDisposer(disposer);
+ }
+ }
+
+ @Override
+ public void partDeactivated(MPart part) {
+ }
+
+ @Override
+ public void partBroughtToTop(MPart part) {
+ }
+
+ @Override
+ public void partActivated(MPart part) {
+ }
+
+}