1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.actions;
14 import java.awt.geom.Point2D;
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
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;
49 public class ImportSVG extends AbstractHandler {
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;
61 symbolEditorInput = Simantics.getSession().syncRequest(new Read<ResourceArray>() {
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;
73 IEditorPart[] eps = rfe.findEditors(new ResourceEditorInput("org.simantics.modeling.ui.symbolEditor", symbolEditorInput)); //$NON-NLS-1$
74 if (eps.length == 0) {
75 System.out.println("symbol editor part not found from multi page editor part: " + ap); //$NON-NLS-1$
79 } catch (DatabaseException e) {
83 } else if (ap instanceof IEditorPart) {
84 viewer = (IEditorPart) ap;
86 ICanvasContext ctx = (ICanvasContext) viewer.getAdapter(ICanvasContext.class);
88 System.out.println("No canvas context"); //$NON-NLS-1$
91 MouseInfo minfo = ctx.getSingleItem(MouseUtil.class).getMousePressedInfo(0);
93 System.out.println("No mouse info"); //$NON-NLS-1$
96 final Point2D mpos = minfo.canvasPosition;
97 IResourceEditorInput input = (IResourceEditorInput)viewer.getEditorInput();
98 Resource composite = input.getResource();
100 return addSVG(mpos.getX(), mpos.getY(), composite);
103 public static Object addSVG(final double mposX, final double mposY, final Resource composite) {
105 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
107 FileDialog dialog = new FileDialog(shell);
108 dialog.setText(Messages.ImportSVG_ChooseImportImage);
109 dialog.setFilterExtensions(new String[] {"*.svg", "*.png"}); //$NON-NLS-1$ //$NON-NLS-2$
111 final String filename = dialog.open();
115 File file = new File(filename);
117 final byte[] data = FileUtils.readFile(file);
119 Simantics.getSession().asyncRequest(new WriteRequest() {
122 public void perform(WriteGraph g) throws DatabaseException {
123 Commands.get(g, "Simantics/Diagram/createSVGElement") //$NON-NLS-1$
124 .execute(g, g.syncRequest(new IndexRoot(composite)),
125 composite, suffix(filename), data, mposX, mposY);
129 } catch (FileNotFoundException e) {
130 ErrorLogger.defaultLogError(e);
131 } catch (IOException e) {
132 ErrorLogger.defaultLogError(e);
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();