1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.contribution;
14 import java.lang.ref.WeakReference;
15 import java.util.ArrayList;
16 import java.util.Collection;
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;
36 public abstract class OperationsMenuContribution extends DynamicMenuContribution {
38 protected abstract Resource getListResource(ReadGraph g) throws Exception;
40 protected void assignParameters(ContextMap parameters) {
44 protected boolean preAcceptSelection(Object[] selection) {
45 if(selection == null || selection.length != 1)
47 Resource r = ResourceAdaptionUtils.adaptToResource(selection[0]);
52 protected IAction[] getActions(ReadGraph g, Object[] selection) throws DatabaseException {
53 if(selection.length == 1) {
54 final Resource r = WorkbenchSelectionUtils.getPossibleResourceFromSelection(g, selection[0]);
58 return g.syncRequest(new Read<IAction[]>() {
61 public IAction[] perform(ReadGraph g) throws DatabaseException {
64 list = g.adapt(getListResource(g), IContextualList.class);
65 } catch (Exception e) {
69 final ContextMap parameters = new ContextMap();
70 parameters.put(IOperation.SUBJECT, r);
71 IProject project = SimanticsUI.peekProject();
73 parameters.put(IOperation.PROJECT, project.get());
74 assignParameters(parameters);
75 Collection<Resource> operations = new ArrayList<Resource>();
76 list.fill(g, parameters, operations);
78 if(!operations.isEmpty()) {
79 IAction[] actions = new IAction[operations.size()];
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) {
89 Session session = sessionRef.get();
91 op.exec(session, parameters);
103 } catch (DatabaseException e) {