/******************************************************************************* * 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.Simantics; 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.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 = Simantics.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)); //$NON-NLS-1$ if (eps.length == 0) { System.out.println("symbol editor part not found from multi page editor part: " + ap); //$NON-NLS-1$ 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"); //$NON-NLS-1$ return null; } MouseInfo minfo = ctx.getSingleItem(MouseUtil.class).getMousePressedInfo(0); if(minfo == null) { System.out.println("No mouse info"); //$NON-NLS-1$ 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(Messages.ImportSVG_ChooseImportImage); dialog.setFilterExtensions(new String[] {"*.svg", "*.png"}); //$NON-NLS-1$ //$NON-NLS-2$ final String filename = dialog.open(); if(filename == null) return null; File file = new File(filename); try { final byte[] data = FileUtils.readFile(file); Simantics.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) throws DatabaseException { Commands.get(g, "Simantics/Diagram/createSVGElement") //$NON-NLS-1$ .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(); } }