1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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.annotation.ui.diagram.handlers;
14 import java.util.Collection;
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.ui.handlers.HandlerUtil;
21 import org.simantics.Simantics;
22 import org.simantics.annotation.ui.AnnotationUtils;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.db.layer0.variable.Variables;
29 import org.simantics.diagram.content.ConnectionUtil;
30 import org.simantics.diagram.function.PredefinedVariables;
31 import org.simantics.modeling.ModelingResources;
32 import org.simantics.ui.utils.ResourceAdaptionUtils;
33 import org.simantics.utils.ui.ErrorLogger;
35 public class NewAnnotation extends AbstractHandler {
37 Resource strConnection = null;
38 Resource model = null;
41 public Object execute(ExecutionEvent event) throws ExecutionException {
43 ISelection selection = HandlerUtil.getCurrentSelection(event);
44 final Resource parent = ResourceAdaptionUtils.toSingleResource(selection);
45 if(parent == null) return null;
49 Simantics.getSession().syncRequest(new ReadRequest() {
52 public void run(ReadGraph graph) throws DatabaseException {
54 Variable parentV = Variables.getVariable(graph, parent);
56 model = Variables.getModel(graph, parentV);
58 ConnectionUtil cu = new ConnectionUtil(graph);
59 Collection<Resource> connectors = cu.getTerminalConnectors(parent, null);
60 Resource connection = null;
61 for (Resource connector : connectors) {
62 connection = ConnectionUtil.tryGetConnection(graph, connector);
63 if (connection != null)
66 if (connection == null)
69 ModelingResources MOD = ModelingResources.getInstance(graph);
70 strConnection = graph.getPossibleObject(connection, MOD.DiagramConnectionToConnection);
71 if (strConnection == null)
74 anno = PredefinedVariables.getAnnotation(graph, parent);
78 if (strConnection == null)
81 AnnotationUtils.newAnnotation(parent, model);
83 } catch (DatabaseException e) {
84 ErrorLogger.defaultLogError(e);