]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/diagram/handlers/NewAnnotation.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / diagram / handlers / NewAnnotation.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.annotation.ui.diagram.handlers;
13
14 import java.util.Collection;
15
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;
34
35 public class NewAnnotation extends AbstractHandler {
36         Resource anno = null;
37         Resource strConnection = null;
38         Resource model = null;
39         
40         @Override
41     public Object execute(ExecutionEvent event) throws ExecutionException {
42                 
43                 ISelection selection = HandlerUtil.getCurrentSelection(event);
44         final Resource parent = ResourceAdaptionUtils.toSingleResource(selection);
45         if(parent == null) return null;
46         
47         try {
48                 
49                         Simantics.getSession().syncRequest(new ReadRequest() {
50
51                                 @Override
52                                 public void run(ReadGraph graph) throws DatabaseException {
53                                         
54                                         Variable parentV = Variables.getVariable(graph, parent);
55                                         
56                                         model = Variables.getModel(graph, parentV);
57
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)
64                                         break;
65                                 }
66                                 if (connection == null)
67                                         return;
68                                 
69                                 ModelingResources MOD = ModelingResources.getInstance(graph);
70                                 strConnection = graph.getPossibleObject(connection, MOD.DiagramConnectionToConnection);
71                                 if (strConnection == null)
72                                         return;
73                                 
74                                 anno = PredefinedVariables.getAnnotation(graph, parent);
75                                 }
76                         });
77
78                 if (strConnection == null)
79                         return null;
80                 if (anno == null)
81                     AnnotationUtils.newAnnotation(parent, model);
82
83                 } catch (DatabaseException e) {
84                         ErrorLogger.defaultLogError(e);
85                 }
86
87         return null;
88     }
89
90 }