]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / contribution / OperationsMenuContribution.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.ui.contribution;
13
14 import java.lang.ref.WeakReference;
15 import java.util.ArrayList;
16 import java.util.Collection;
17
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.request.Read;
26 import org.simantics.layer0.utils.collections.IContextualList;
27 import org.simantics.layer0.utils.operations.IOperation;
28 import org.simantics.project.IProject;
29 import org.simantics.ui.SimanticsUI;
30 import org.simantics.ui.icons.ImageUtil;
31 import org.simantics.ui.utils.ResourceAdaptionUtils;
32 import org.simantics.utils.datastructures.persistent.ContextMap;
33
34
35 public abstract class OperationsMenuContribution extends DynamicMenuContribution {
36
37     protected abstract Resource getListResource(ReadGraph g) throws Exception;
38
39     protected void assignParameters(ContextMap parameters) {
40     }
41
42     @Override
43     protected boolean preAcceptSelection(Object[] selection) {
44         if(selection == null || selection.length != 1)
45             return false;
46         Resource r = ResourceAdaptionUtils.adaptToResource(selection[0]);
47         return r != null;
48     }
49
50     @Override
51     protected IAction[] getActions(ReadGraph g, Object[] selection) {
52         if(selection.length == 1) {
53             final Resource r = ResourceAdaptionUtils.adaptToResource(selection[0]);
54             if(r == null)
55                 return NO_ACTIONS;
56             try {
57                 return g.syncRequest(new Read<IAction[]>() {
58
59                     @Override
60                     public IAction[] perform(ReadGraph g) throws DatabaseException {
61                         IContextualList list;
62                         try {
63                             list = g.adapt(getListResource(g), IContextualList.class);
64                         } catch (Exception e) {
65                             e.printStackTrace();
66                             return null;
67                         }
68                         final ContextMap parameters = new ContextMap();
69                         parameters.put(IOperation.SUBJECT, r);
70                         IProject project = SimanticsUI.peekProject();
71                         if (project != null)
72                             parameters.put(IOperation.PROJECT, project.get());
73                         assignParameters(parameters);
74                         Collection<Resource> operations = new ArrayList<Resource>();
75                         list.fill(g, parameters, operations);
76
77                         if(!operations.isEmpty()) {
78                             IAction[] actions = new IAction[operations.size()];
79                             int i=0;
80                             for(Resource opRes : operations) {
81                                 final IOperation op = g.adapt(opRes, IOperation.class);
82                                 final ImageDescriptor imgDesc = ImageUtil.adaptImageDescriptor(g, opRes);
83                                 final WeakReference<Session> sessionRef = new WeakReference<Session>(g.getSession());
84                                 actions[i++] = new Action(op.getName(), imgDesc) {
85
86                                     @Override
87                                     public void run() {
88                                         Session session = sessionRef.get();
89                                         if (session != null)
90                                             op.exec(session, parameters);
91                                     }
92
93                                 };
94                             }
95                             return actions;
96                         } else {
97                             return null;
98                         }
99                     }
100
101                 });
102             } catch (DatabaseException e) {
103                 e.printStackTrace();
104             }
105
106         }
107         return NO_ACTIONS;
108     }
109
110 }