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