]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser2/model/SubscriptionNode.java
161607de6d2d3cc0928e00086de7657de1b14022
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser2 / model / SubscriptionNode.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.modeling.ui.modelBrowser2.model;
13
14 import java.util.List;
15
16 import org.eclipse.jface.viewers.ISelection;
17 import org.simantics.browsing.ui.common.node.IDeletable;
18 import org.simantics.browsing.ui.common.node.IDropTargetNode;
19 import org.simantics.db.Resource;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.variable.VariableReference;
22 import org.simantics.modeling.PropertyVariables;
23 import org.simantics.modeling.subscription.AddSubscriptionItems;
24 import org.simantics.modeling.utils.VariableReferences;
25 import org.simantics.structural.ui.modelBrowser.nodes.AbstractNode;
26 import org.simantics.ui.SimanticsUI;
27 import org.simantics.utils.datastructures.Callback;
28 import org.simantics.utils.ui.ErrorLogger;
29 import org.simantics.utils.ui.ISelectionUtils;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class SubscriptionNode extends AbstractNode implements IDeletable, IDropTargetNode {
35
36     public SubscriptionNode(Resource resource) {
37         super(resource);
38     }
39
40     @Override
41     public void drop(Object data) {
42         if (!(data instanceof ISelection))
43             return;
44         final List<PropertyVariables> vars = ISelectionUtils.filterSelection((ISelection) data, PropertyVariables.class);
45         if (!vars.isEmpty()) {
46             List<VariableReference> references;
47             try {
48                 references = SimanticsUI.getSession().syncRequest(VariableReferences.toReferences(vars));
49                 addSubscriptions(references);
50             } catch (DatabaseException e) {
51                 ErrorLogger.defaultLogError(e);
52             }
53         } else {
54             final List<VariableReference> references = ISelectionUtils.filterSelection((ISelection) data, VariableReference.class);
55             if (!references.isEmpty()) {
56                 addSubscriptions(references);
57             }
58         }
59     }
60
61     private void addSubscriptions(List<VariableReference> references) {
62         SimanticsUI.getSession().asyncRequest(new AddSubscriptionItems(resource, references), new Callback<DatabaseException>() {
63             @Override
64             public void run(DatabaseException e) {
65                 if (e != null)
66                     ErrorLogger.defaultLogError(e);
67             }
68         });
69     }
70
71 }