1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.actions;
\r
14 import java.awt.geom.Point2D;
\r
15 import java.io.File;
\r
16 import java.io.FileNotFoundException;
\r
17 import java.io.IOException;
\r
19 import org.eclipse.core.commands.AbstractHandler;
\r
20 import org.eclipse.core.commands.ExecutionEvent;
\r
21 import org.eclipse.core.commands.ExecutionException;
\r
22 import org.eclipse.swt.widgets.FileDialog;
\r
23 import org.eclipse.swt.widgets.Shell;
\r
24 import org.eclipse.ui.IEditorPart;
\r
25 import org.eclipse.ui.IWorkbenchPart;
\r
26 import org.eclipse.ui.PlatformUI;
\r
27 import org.eclipse.ui.handlers.HandlerUtil;
\r
28 import org.eclipse.ui.part.MultiPageEditorPart;
\r
29 import org.simantics.db.ReadGraph;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.db.WriteGraph;
\r
32 import org.simantics.db.common.ResourceArray;
\r
33 import org.simantics.db.common.request.IndexRoot;
\r
34 import org.simantics.db.common.request.WriteRequest;
\r
35 import org.simantics.db.exception.DatabaseException;
\r
36 import org.simantics.db.request.Read;
\r
37 import org.simantics.g2d.canvas.ICanvasContext;
\r
38 import org.simantics.g2d.participant.MouseUtil;
\r
39 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
\r
40 import org.simantics.modeling.ModelingResources;
\r
41 import org.simantics.scl.commands.Commands;
\r
42 import org.simantics.structural.stubs.StructuralResource2;
\r
43 import org.simantics.ui.SimanticsUI;
\r
44 import org.simantics.ui.workbench.IResourceEditorInput;
\r
45 import org.simantics.ui.workbench.ResourceEditorInput;
\r
46 import org.simantics.utils.FileUtils;
\r
47 import org.simantics.utils.ui.ErrorLogger;
\r
49 public class ImportSVG extends AbstractHandler {
\r
52 public Object execute(ExecutionEvent event) throws ExecutionException {
\r
53 IWorkbenchPart ap = HandlerUtil.getActivePart(event);
\r
54 IEditorPart viewer = null;
\r
55 if (ap instanceof MultiPageEditorPart) {
\r
56 MultiPageEditorPart rfe = (MultiPageEditorPart) ap;
\r
57 IResourceEditorInput in = (ResourceEditorInput) rfe.getEditorInput();
\r
58 final ResourceArray ra = in.getResourceArray();
\r
59 ResourceArray symbolEditorInput;
\r
61 symbolEditorInput = SimanticsUI.getSession().syncRequest(new Read<ResourceArray>() {
\r
63 public ResourceArray perform(ReadGraph graph) throws DatabaseException {
\r
64 StructuralResource2 sr = StructuralResource2.getInstance(graph);
\r
65 ModelingResources mr = ModelingResources.getInstance(graph);
\r
66 Resource symbol = graph.getPossibleObject(ra.resources[0], mr.ComponentTypeToSymbol);
\r
67 Resource definedBy = symbol != null ? graph.getPossibleObject(symbol, sr.IsDefinedBy) : null;
\r
68 ResourceArray result = definedBy != null ? new ResourceArray(definedBy) : ResourceArray.EMPTY;
\r
73 IEditorPart[] eps = rfe.findEditors(new ResourceEditorInput("org.simantics.modeling.ui.symbolEditor", symbolEditorInput));
\r
74 if (eps.length == 0) {
\r
75 System.out.println("symbol editor part not found from multi page editor part: " + ap);
\r
79 } catch (DatabaseException e) {
\r
80 e.printStackTrace();
\r
83 } else if (ap instanceof IEditorPart) {
\r
84 viewer = (IEditorPart) ap;
\r
86 ICanvasContext ctx = (ICanvasContext) viewer.getAdapter(ICanvasContext.class);
\r
88 System.out.println("No canvas context");
\r
91 MouseInfo minfo = ctx.getSingleItem(MouseUtil.class).getMousePressedInfo(0);
\r
93 System.out.println("No mouse info");
\r
96 final Point2D mpos = minfo.canvasPosition;
\r
97 IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();
\r
98 Resource composite = input.getResource();
\r
100 return addSVG(mpos.getX(), mpos.getY(), composite);
\r
103 public static Object addSVG(final double mposX, final double mposY, final Resource composite) {
\r
105 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
\r
107 FileDialog dialog = new FileDialog(shell);
\r
108 dialog.setText("Choose an image to be imported");
\r
109 dialog.setFilterExtensions(new String[] {"*.svg", "*.png"});
\r
111 final String filename = dialog.open();
\r
112 if(filename == null)
\r
115 File file = new File(filename);
\r
117 final byte[] data = FileUtils.readFile(file);
\r
119 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
122 public void perform(WriteGraph g) throws DatabaseException {
\r
123 Commands.get(g, "Simantics/Diagram/createSVGElement")
\r
124 .execute(g, g.syncRequest(new IndexRoot(composite)),
\r
125 composite, suffix(filename), data, mposX, mposY);
\r
129 } catch (FileNotFoundException e) {
\r
130 ErrorLogger.defaultLogError(e);
\r
131 } catch (IOException e) {
\r
132 ErrorLogger.defaultLogError(e);
\r
139 private static String suffix(String fileName) {
\r
140 int len = fileName.length();
\r
141 if(len < 3) return null;
\r
142 else return fileName.substring(len-3,len).toLowerCase();
\r