X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.annotation.ui%2Fsrc%2Forg%2Fsimantics%2Fannotation%2Fui%2Fdiagram%2Fhandlers%2FNewAnnotation.java;h=5b5ce96d77b24320a9fa9ae4fee433fb813e4e71;hp=3fbde77ba85d0332aad2f5d5971379ad31ea43b5;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/diagram/handlers/NewAnnotation.java b/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/diagram/handlers/NewAnnotation.java index 3fbde77ba..5b5ce96d7 100644 --- a/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/diagram/handlers/NewAnnotation.java +++ b/bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/diagram/handlers/NewAnnotation.java @@ -1,90 +1,90 @@ -/******************************************************************************* - * Copyright (c) 2012 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.annotation.ui.diagram.handlers; - -import java.util.Collection; - -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.ui.handlers.HandlerUtil; -import org.simantics.Simantics; -import org.simantics.annotation.ui.AnnotationUtils; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.variable.Variable; -import org.simantics.db.layer0.variable.Variables; -import org.simantics.diagram.content.ConnectionUtil; -import org.simantics.diagram.function.PredefinedVariables; -import org.simantics.modeling.ModelingResources; -import org.simantics.ui.utils.ResourceAdaptionUtils; -import org.simantics.utils.ui.ErrorLogger; - -public class NewAnnotation extends AbstractHandler { - Resource anno = null; - Resource strConnection = null; - Resource model = null; - - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - - ISelection selection = HandlerUtil.getCurrentSelection(event); - final Resource parent = ResourceAdaptionUtils.toSingleResource(selection); - if(parent == null) return null; - - try { - - Simantics.getSession().syncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - - Variable parentV = Variables.getVariable(graph, parent); - - model = Variables.getModel(graph, parentV); - - ConnectionUtil cu = new ConnectionUtil(graph); - Collection connectors = cu.getTerminalConnectors(parent, null); - Resource connection = null; - for (Resource connector : connectors) { - connection = ConnectionUtil.tryGetConnection(graph, connector); - if (connection != null) - break; - } - if (connection == null) - return; - - ModelingResources MOD = ModelingResources.getInstance(graph); - strConnection = graph.getPossibleObject(connection, MOD.DiagramConnectionToConnection); - if (strConnection == null) - return; - - anno = PredefinedVariables.getAnnotation(graph, parent); - } - }); - - if (strConnection == null) - return null; - if (anno == null) - AnnotationUtils.newAnnotation(parent, model); - - } catch (DatabaseException e) { - ErrorLogger.defaultLogError(e); - } - - return null; - } - -} +/******************************************************************************* + * Copyright (c) 2012 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.annotation.ui.diagram.handlers; + +import java.util.Collection; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.ui.handlers.HandlerUtil; +import org.simantics.Simantics; +import org.simantics.annotation.ui.AnnotationUtils; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.diagram.content.ConnectionUtil; +import org.simantics.diagram.function.PredefinedVariables; +import org.simantics.modeling.ModelingResources; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.ui.ErrorLogger; + +public class NewAnnotation extends AbstractHandler { + Resource anno = null; + Resource strConnection = null; + Resource model = null; + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + + ISelection selection = HandlerUtil.getCurrentSelection(event); + final Resource parent = ResourceAdaptionUtils.toSingleResource(selection); + if(parent == null) return null; + + try { + + Simantics.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + + Variable parentV = Variables.getVariable(graph, parent); + + model = Variables.getModel(graph, parentV); + + ConnectionUtil cu = new ConnectionUtil(graph); + Collection connectors = cu.getTerminalConnectors(parent, null); + Resource connection = null; + for (Resource connector : connectors) { + connection = ConnectionUtil.tryGetConnection(graph, connector); + if (connection != null) + break; + } + if (connection == null) + return; + + ModelingResources MOD = ModelingResources.getInstance(graph); + strConnection = graph.getPossibleObject(connection, MOD.DiagramConnectionToConnection); + if (strConnection == null) + return; + + anno = PredefinedVariables.getAnnotation(graph, parent); + } + }); + + if (strConnection == null) + return null; + if (anno == null) + AnnotationUtils.newAnnotation(parent, model); + + } catch (DatabaseException e) { + ErrorLogger.defaultLogError(e); + } + + return null; + } + +}