X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Factions%2FImportSVG.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Factions%2FImportSVG.java;h=8e5a01e4cba5686a2dfcc21882ac34e446a4ab89;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ImportSVG.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ImportSVG.java new file mode 100644 index 000000000..8e5a01e4c --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ImportSVG.java @@ -0,0 +1,145 @@ +/******************************************************************************* + * 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() { + @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(); + } + +}