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