-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.annotation.ui.diagram.handlers;\r
-\r
-import java.util.Collection;\r
-\r
-import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.ui.handlers.HandlerUtil;\r
-import org.simantics.Simantics;\r
-import org.simantics.annotation.ui.AnnotationUtils;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.diagram.content.ConnectionUtil;\r
-import org.simantics.diagram.function.PredefinedVariables;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-public class NewAnnotation extends AbstractHandler {\r
- Resource anno = null;\r
- Resource strConnection = null;\r
- Resource model = null;\r
- \r
- @Override\r
- public Object execute(ExecutionEvent event) throws ExecutionException {\r
- \r
- ISelection selection = HandlerUtil.getCurrentSelection(event);\r
- final Resource parent = ResourceAdaptionUtils.toSingleResource(selection);\r
- if(parent == null) return null;\r
- \r
- try {\r
- \r
- Simantics.getSession().syncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- \r
- Variable parentV = Variables.getVariable(graph, parent);\r
- \r
- model = Variables.getModel(graph, parentV);\r
-\r
- ConnectionUtil cu = new ConnectionUtil(graph);\r
- Collection<Resource> connectors = cu.getTerminalConnectors(parent, null);\r
- Resource connection = null;\r
- for (Resource connector : connectors) {\r
- connection = ConnectionUtil.tryGetConnection(graph, connector);\r
- if (connection != null)\r
- break;\r
- }\r
- if (connection == null)\r
- return;\r
- \r
- ModelingResources MOD = ModelingResources.getInstance(graph);\r
- strConnection = graph.getPossibleObject(connection, MOD.DiagramConnectionToConnection);\r
- if (strConnection == null)\r
- return;\r
- \r
- anno = PredefinedVariables.getAnnotation(graph, parent);\r
- }\r
- });\r
-\r
- if (strConnection == null)\r
- return null;\r
- if (anno == null)\r
- AnnotationUtils.newAnnotation(parent, model);\r
-\r
- } catch (DatabaseException e) {\r
- ErrorLogger.defaultLogError(e);\r
- }\r
-\r
- return null;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * 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<Resource> 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;
+ }
+
+}