X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fcontribution%2FOperationsMenuContribution.java;h=b194694b58e01a3868861dd0e82bd6847a6e533b;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=1b5dcfecea30b7d9bf11cc5d7edb7bb24289ccdb;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java index 1b5dcfece..b194694b5 100644 --- a/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java +++ b/bundles/org.simantics.ui/src/org/simantics/ui/contribution/OperationsMenuContribution.java @@ -1,110 +1,110 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.ui.contribution; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.Collection; - -import org.eclipse.jface.action.Action; -import org.eclipse.jface.action.IAction; -import org.eclipse.jface.resource.ImageDescriptor; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.Session; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.request.Read; -import org.simantics.layer0.utils.collections.IContextualList; -import org.simantics.layer0.utils.operations.IOperation; -import org.simantics.project.IProject; -import org.simantics.ui.SimanticsUI; -import org.simantics.ui.icons.ImageUtil; -import org.simantics.ui.utils.ResourceAdaptionUtils; -import org.simantics.utils.datastructures.persistent.ContextMap; - - -public abstract class OperationsMenuContribution extends DynamicMenuContribution { - - protected abstract Resource getListResource(ReadGraph g) throws Exception; - - protected void assignParameters(ContextMap parameters) { - } - - @Override - protected boolean preAcceptSelection(Object[] selection) { - if(selection == null || selection.length != 1) - return false; - Resource r = ResourceAdaptionUtils.adaptToResource(selection[0]); - return r != null; - } - - @Override - protected IAction[] getActions(ReadGraph g, Object[] selection) { - if(selection.length == 1) { - final Resource r = ResourceAdaptionUtils.adaptToResource(selection[0]); - if(r == null) - return NO_ACTIONS; - try { - return g.syncRequest(new Read() { - - @Override - public IAction[] perform(ReadGraph g) throws DatabaseException { - IContextualList list; - try { - list = g.adapt(getListResource(g), IContextualList.class); - } catch (Exception e) { - e.printStackTrace(); - return null; - } - final ContextMap parameters = new ContextMap(); - parameters.put(IOperation.SUBJECT, r); - IProject project = SimanticsUI.peekProject(); - if (project != null) - parameters.put(IOperation.PROJECT, project.get()); - assignParameters(parameters); - Collection operations = new ArrayList(); - list.fill(g, parameters, operations); - - if(!operations.isEmpty()) { - IAction[] actions = new IAction[operations.size()]; - int i=0; - for(Resource opRes : operations) { - final IOperation op = g.adapt(opRes, IOperation.class); - final ImageDescriptor imgDesc = ImageUtil.adaptImageDescriptor(g, opRes); - final WeakReference sessionRef = new WeakReference(g.getSession()); - actions[i++] = new Action(op.getName(), imgDesc) { - - @Override - public void run() { - Session session = sessionRef.get(); - if (session != null) - op.exec(session, parameters); - } - - }; - } - return actions; - } else { - return null; - } - } - - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } - - } - return NO_ACTIONS; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.ui.contribution; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.Collection; + +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.resource.ImageDescriptor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.layer0.utils.collections.IContextualList; +import org.simantics.layer0.utils.operations.IOperation; +import org.simantics.project.IProject; +import org.simantics.ui.SimanticsUI; +import org.simantics.ui.icons.ImageUtil; +import org.simantics.ui.utils.ResourceAdaptionUtils; +import org.simantics.utils.datastructures.persistent.ContextMap; + + +public abstract class OperationsMenuContribution extends DynamicMenuContribution { + + protected abstract Resource getListResource(ReadGraph g) throws Exception; + + protected void assignParameters(ContextMap parameters) { + } + + @Override + protected boolean preAcceptSelection(Object[] selection) { + if(selection == null || selection.length != 1) + return false; + Resource r = ResourceAdaptionUtils.adaptToResource(selection[0]); + return r != null; + } + + @Override + protected IAction[] getActions(ReadGraph g, Object[] selection) { + if(selection.length == 1) { + final Resource r = ResourceAdaptionUtils.adaptToResource(selection[0]); + if(r == null) + return NO_ACTIONS; + try { + return g.syncRequest(new Read() { + + @Override + public IAction[] perform(ReadGraph g) throws DatabaseException { + IContextualList list; + try { + list = g.adapt(getListResource(g), IContextualList.class); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + final ContextMap parameters = new ContextMap(); + parameters.put(IOperation.SUBJECT, r); + IProject project = SimanticsUI.peekProject(); + if (project != null) + parameters.put(IOperation.PROJECT, project.get()); + assignParameters(parameters); + Collection operations = new ArrayList(); + list.fill(g, parameters, operations); + + if(!operations.isEmpty()) { + IAction[] actions = new IAction[operations.size()]; + int i=0; + for(Resource opRes : operations) { + final IOperation op = g.adapt(opRes, IOperation.class); + final ImageDescriptor imgDesc = ImageUtil.adaptImageDescriptor(g, opRes); + final WeakReference sessionRef = new WeakReference(g.getSession()); + actions[i++] = new Action(op.getName(), imgDesc) { + + @Override + public void run() { + Session session = sessionRef.get(); + if (session != null) + op.exec(session, parameters); + } + + }; + } + return actions; + } else { + return null; + } + } + + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + } + return NO_ACTIONS; + } + +}