-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in 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.actions;\r
-\r
-import java.awt.geom.Point2D;\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-\r
-import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.swt.widgets.FileDialog;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.ui.IEditorPart;\r
-import org.eclipse.ui.IWorkbenchPart;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.eclipse.ui.part.MultiPageEditorPart;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.ResourceArray;\r
-import org.simantics.db.common.request.IndexRoot;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.g2d.canvas.ICanvasContext;\r
-import org.simantics.g2d.participant.MouseUtil;\r
-import org.simantics.g2d.participant.MouseUtil.MouseInfo;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.scl.commands.Commands;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.ui.workbench.IResourceEditorInput;\r
-import org.simantics.ui.workbench.ResourceEditorInput;\r
-import org.simantics.utils.FileUtils;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-public class ImportSVG extends AbstractHandler {\r
-\r
- @Override\r
- public Object execute(ExecutionEvent event) throws ExecutionException {\r
- IWorkbenchPart ap = HandlerUtil.getActivePart(event);\r
- IEditorPart viewer = null;\r
- if (ap instanceof MultiPageEditorPart) {\r
- MultiPageEditorPart rfe = (MultiPageEditorPart) ap;\r
- IResourceEditorInput in = (ResourceEditorInput) rfe.getEditorInput();\r
- final ResourceArray ra = in.getResourceArray();\r
- ResourceArray symbolEditorInput;\r
- try {\r
- symbolEditorInput = SimanticsUI.getSession().syncRequest(new Read<ResourceArray>() {\r
- @Override\r
- public ResourceArray perform(ReadGraph graph) throws DatabaseException {\r
- StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
- ModelingResources mr = ModelingResources.getInstance(graph);\r
- Resource symbol = graph.getPossibleObject(ra.resources[0], mr.ComponentTypeToSymbol);\r
- Resource definedBy = symbol != null ? graph.getPossibleObject(symbol, sr.IsDefinedBy) : null;\r
- ResourceArray result = definedBy != null ? new ResourceArray(definedBy) : ResourceArray.EMPTY;\r
- return result;\r
- }\r
- });\r
-\r
- IEditorPart[] eps = rfe.findEditors(new ResourceEditorInput("org.simantics.modeling.ui.symbolEditor", symbolEditorInput));\r
- if (eps.length == 0) {\r
- System.out.println("symbol editor part not found from multi page editor part: " + ap);\r
- return null;\r
- }\r
- viewer = eps[0];\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- return null;\r
- }\r
- } else if (ap instanceof IEditorPart) {\r
- viewer = (IEditorPart) ap;\r
- }\r
- ICanvasContext ctx = (ICanvasContext) viewer.getAdapter(ICanvasContext.class);\r
- if (ctx == null) {\r
- System.out.println("No canvas context");\r
- return null;\r
- }\r
- MouseInfo minfo = ctx.getSingleItem(MouseUtil.class).getMousePressedInfo(0);\r
- if(minfo == null) {\r
- System.out.println("No mouse info");\r
- return null;\r
- }\r
- final Point2D mpos = minfo.canvasPosition;\r
- IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();\r
- Resource composite = input.getResource();\r
-\r
- return addSVG(mpos.getX(), mpos.getY(), composite);\r
- }\r
-\r
- public static Object addSVG(final double mposX, final double mposY, final Resource composite) {\r
-\r
- Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
-\r
- FileDialog dialog = new FileDialog(shell);\r
- dialog.setText("Choose an image to be imported");\r
- dialog.setFilterExtensions(new String[] {"*.svg", "*.png"});\r
-\r
- final String filename = dialog.open();\r
- if(filename == null)\r
- return null;\r
-\r
- File file = new File(filename);\r
- try {\r
- final byte[] data = FileUtils.readFile(file);\r
-\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph g) throws DatabaseException {\r
- Commands.get(g, "Simantics/Diagram/createSVGElement")\r
- .execute(g, g.syncRequest(new IndexRoot(composite)),\r
- composite, suffix(filename), data, mposX, mposY);\r
- }\r
-\r
- });\r
- } catch (FileNotFoundException e) {\r
- ErrorLogger.defaultLogError(e);\r
- } catch (IOException e) {\r
- ErrorLogger.defaultLogError(e);\r
- }\r
-\r
- return null;\r
-\r
- }\r
-\r
- private static String suffix(String fileName) {\r
- int len = fileName.length();\r
- if(len < 3) return null;\r
- else return fileName.substring(len-3,len).toLowerCase();\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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.actions;
+
+import java.awt.geom.Point2D;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.ui.part.MultiPageEditorPart;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.ResourceArray;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Read;
+import org.simantics.g2d.canvas.ICanvasContext;
+import org.simantics.g2d.participant.MouseUtil;
+import org.simantics.g2d.participant.MouseUtil.MouseInfo;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.scl.commands.Commands;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.ui.workbench.IResourceEditorInput;
+import org.simantics.ui.workbench.ResourceEditorInput;
+import org.simantics.utils.FileUtils;
+import org.simantics.utils.ui.ErrorLogger;
+
+public class ImportSVG extends AbstractHandler {
+
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ IWorkbenchPart ap = HandlerUtil.getActivePart(event);
+ IEditorPart viewer = null;
+ if (ap instanceof MultiPageEditorPart) {
+ MultiPageEditorPart rfe = (MultiPageEditorPart) ap;
+ IResourceEditorInput in = (ResourceEditorInput) rfe.getEditorInput();
+ final ResourceArray ra = in.getResourceArray();
+ ResourceArray symbolEditorInput;
+ try {
+ symbolEditorInput = SimanticsUI.getSession().syncRequest(new Read<ResourceArray>() {
+ @Override
+ public ResourceArray perform(ReadGraph graph) throws DatabaseException {
+ StructuralResource2 sr = StructuralResource2.getInstance(graph);
+ ModelingResources mr = ModelingResources.getInstance(graph);
+ Resource symbol = graph.getPossibleObject(ra.resources[0], mr.ComponentTypeToSymbol);
+ Resource definedBy = symbol != null ? graph.getPossibleObject(symbol, sr.IsDefinedBy) : null;
+ ResourceArray result = definedBy != null ? new ResourceArray(definedBy) : ResourceArray.EMPTY;
+ return result;
+ }
+ });
+
+ IEditorPart[] eps = rfe.findEditors(new ResourceEditorInput("org.simantics.modeling.ui.symbolEditor", symbolEditorInput));
+ if (eps.length == 0) {
+ System.out.println("symbol editor part not found from multi page editor part: " + ap);
+ return null;
+ }
+ viewer = eps[0];
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ return null;
+ }
+ } else if (ap instanceof IEditorPart) {
+ viewer = (IEditorPart) ap;
+ }
+ ICanvasContext ctx = (ICanvasContext) viewer.getAdapter(ICanvasContext.class);
+ if (ctx == null) {
+ System.out.println("No canvas context");
+ return null;
+ }
+ MouseInfo minfo = ctx.getSingleItem(MouseUtil.class).getMousePressedInfo(0);
+ if(minfo == null) {
+ System.out.println("No mouse info");
+ return null;
+ }
+ final Point2D mpos = minfo.canvasPosition;
+ IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();
+ Resource composite = input.getResource();
+
+ return addSVG(mpos.getX(), mpos.getY(), composite);
+ }
+
+ public static Object addSVG(final double mposX, final double mposY, final Resource composite) {
+
+ Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+
+ FileDialog dialog = new FileDialog(shell);
+ dialog.setText("Choose an image to be imported");
+ dialog.setFilterExtensions(new String[] {"*.svg", "*.png"});
+
+ final String filename = dialog.open();
+ if(filename == null)
+ return null;
+
+ File file = new File(filename);
+ try {
+ final byte[] data = FileUtils.readFile(file);
+
+ SimanticsUI.getSession().asyncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ Commands.get(g, "Simantics/Diagram/createSVGElement")
+ .execute(g, g.syncRequest(new IndexRoot(composite)),
+ composite, suffix(filename), data, mposX, mposY);
+ }
+
+ });
+ } catch (FileNotFoundException e) {
+ ErrorLogger.defaultLogError(e);
+ } catch (IOException e) {
+ ErrorLogger.defaultLogError(e);
+ }
+
+ return null;
+
+ }
+
+ private static String suffix(String fileName) {
+ int len = fileName.length();
+ if(len < 3) return null;
+ else return fileName.substring(len-3,len).toLowerCase();
+ }
+
+}