Reuse org.eclipse.ui.viewActions extension point for custom tool and menu bars [message #457318] |
Mon, 30 October 2006 07:32 |
SaschaBur Messages: 5 Registered: July 2009 |
Junior Member |
|
|
This is a multi-part message in MIME format.
--------------070209070007020104040009
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Hello,
Eclipse offers powerful contribution mechanisms which are documented in...
http://www.eclipse.org/articles/Article-action-contribution/ Contributing%20Actions%20to%20the%20Eclipse%20Workbench.html
Eclipse supports well defined contribution points:
- View Tool Bar (org.eclipse.ui.viewActions)
- View Pull Down Menu (org.eclipse.ui.viewActions)
- Context Menu (org.eclipse.ui.popupMenus)
The problem is, that the powerful mechanisms can not be reused for
custom menu and tool bars at other locations. (Functionality is in
internal classes, code must be copied to use it, see attachment)
For example we would like to offer contribution points for business
objects within a view. Example: Here is Person object, anybody which can
do something with that object may add actions here. This is a CreditCard
object, anybody...
Application parts of different business domains must be
separated/decoupled but could have interactions added by integration-
plugins that use the custom contribution points.
The Java Class in the attachments offers what we are looking for but has
many dependencies to Eclipse internal classes.
Hopefully there are others with the same intention.
Kind regards, Sascha Bur
--------------070209070007020104040009
Content-Type: text/plain;
name="ViewActionBuilder.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ViewActionBuilder.java"
/*
* ------------------------------------------------------------ ----------------
* Copyright 2006 by The Swiss Post, PostFinance - all rights reserved
* ------------------------------------------------------------ ----------------
* WHEN WHO DESCRIPTION
*
* ------------------------------------------------------------ ----------------
*/
package ch.post.pf.gui.ocp.contribution.action;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.ActionDescriptor;
import org.eclipse.ui.internal.ActionExpression;
import org.eclipse.ui.internal.IWorkbenchConstants;
import org.eclipse.ui.internal.PluginAction;
import org.eclipse.ui.internal.PluginActionBuilder;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants ;
import org.eclipse.ui.plugin.AbstractUIPlugin;
/**
* This class is a copy of org.eclipse.ui.internal.ViewActionBuilder.
*
* This copy reuses the eclipse contribution functionality for
* filling up custom menus or button bars. Normally eclipse
* just provides View menus on top right of the view.
* This class can be used for menus at any location.
*
* @author burs
* @version 1.0.0
* @since
*/
public class ViewActionBuilder extends PluginActionBuilder
{
public static final String TAG_CONTRIBUTION_TYPE = "viewContribution"; //$NON-NLS-1$
private ISelectionProvider m_provider;
private IViewPart m_part;
private Collection m_actions = new ArrayList();
public ViewActionBuilder(IViewPart part, ISelectionProvider provider)
{
m_provider = provider;
m_part = part;
}
public IAction[] getActions()
{
IAction[] ret = new IAction[m_actions.size()];
Iterator itr = m_actions.iterator();
for (int i = 0; itr.hasNext(); ++i)
{
ActionDescriptor action = (ActionDescriptor) itr.next();
ret[i] = action.getAction();
}
return ret;
}
/**
* (non-Javadoc)
* @see org.eclipse.ui.internal.PluginActionBuilder#createActionDesc riptor(org.eclipse.core.runtime.IConfigurationElement)
*/
protected ActionDescriptor createActionDescriptor(IConfigurationElement element)
{
MyActionDescriptor ad = new MyActionDescriptor(element, m_part);
m_actions.add(ad);
return ad;
}
/* (non-Javadoc)
* Method declared on PluginActionBuilder.
*/
protected BasicContribution createContribution() {
return new ActionContribution(m_provider);
}
/**
* Reads the contributions for a viewer menu.
* This method is typically used in conjunction with <code>contribute</code> to read
* and then insert actions for a particular viewer menu.
*
* @param id the menu id
* @param prov the selection provider for the control containing the menu
* @param part the part containing the menu.
* @return <code>true</code> if 1 or more items were read.
*/
public boolean readViewerContributions(String id, ISelectionProvider prov,
IWorkbenchPart part) {
m_provider = prov;
readContributions(id, IWorkbenchRegistryConstants.TAG_VIEWER_CONTRIBUTION,
IWorkbenchRegistryConstants.PL_POPUP_MENU);
return (cache != null);
}
/**
* Reads and apply all external contributions for this view's ID registered
* in 'viewActions' extension point.
*/
public void readActionExtensions(String id) {
readContributions(id, TAG_CONTRIBUTION_TYPE,
IWorkbenchRegistryConstants.PL_VIEW_ACTIONS);
}
/**
* Dispose of the action builder
*/
public void dispose() {
if (cache != null) {
for (int i = 0; i < cache.size(); i++) {
((BasicContribution) cache.get(i)).dispose();
}
cache = null;
}
}
/* (non-Javadoc)
* Method declared on PluginActionBuilder.
*/
protected boolean readElement(IConfigurationElement element) {
String tag = element.getName();
// Found visibility sub-element
if (currentContribution != null && tag.equals(IWorkbenchRegistryConstants.TAG_VISIBILITY)) {
((ActionContribution) currentContribution)
.setVisibilityTest(element);
return true;
}
return super.readElement(element);
}
private class MyActionDescriptor extends ActionDescriptor
{
private String m_id;
private PluginAction m_action;
public MyActionDescriptor(IConfigurationElement actionElement, IViewPart viewPart)
{
super(actionElement, ActionDescriptor.T_VIEW, m_part);
m_action = new ContributionAction(actionElement, getId(), SWT.NONE, viewPart);
// unfortunatly the method ActionDescriptor.createAction() is private
// if this method were protected here we need just one line of code
// this code is copied from org.eclipse.ui.internal.ActionDescriptor
Action action = m_action;
m_id = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_I D);
String label = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_L ABEL);
String tooltip = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_T OOLTIP);
String helpContextId = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_H ELP_CONTEXT_ID);
String mpath = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_M ENUBAR_PATH);
String tpath = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_T OOLBAR_PATH);
String style = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_S TYLE);
String icon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_I CON);
String hoverIcon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_H OVERICON);
String disabledIcon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_D ISABLEDICON);
String description = actionElement.getAttribute(IWorkbenchRegistryConstants.TAG_D ESCRIPTION);
String accelerator = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_A CCELERATOR);
if (label == null) {
WorkbenchPlugin
.log("Invalid action declaration (label == null): " + m_id); //$NON-NLS-1$
label = WorkbenchMessages.ActionDescriptor_invalidLabel;
}
if (action.getText() == null) // may have been set by delegate
action.setText(label);
if (action.getToolTipText() == null && tooltip != null) // may have been set by delegate
action.setToolTipText(tooltip);
if (helpContextId != null) {
String fullID = helpContextId;
if (helpContextId.indexOf(".") == -1) //$NON-NLS-1$
// For backward compatibility we auto qualify the id if it is not
// qualified)
fullID = actionElement.getNamespace()
+ "." + helpContextId;//$NON-NLS-1$
PlatformUI.getWorkbench().getHelpSystem().setHelp(action, fullID);
}
if (description != null)
action.setDescription(description);
if (style != null) {
// Since 2.1, the "state" and "pulldown" attributes means something different
// when the new "style" attribute has been set. See doc for more info.
String state = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_S TATE);
if (state != null) {
if (style.equals(STYLE_RADIO) || style.equals(STYLE_TOGGLE))
action.setChecked(state.equals("true"));//$NON-NLS-1$
}
} else {
// Keep for backward compatibility for actions not using the
// new style attribute.
String state = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_S TATE);
if (state != null) {
action.setChecked(state.equals("true"));//$NON-NLS-1$
}
}
String extendingPluginId = actionElement.getDeclaringExtension()
.getNamespace();
if (icon != null) {
action.setImageDescriptor(AbstractUIPlugin
.imageDescriptorFromPlugin(extendingPluginId, icon));
}
if (hoverIcon != null) {
action.setHoverImageDescriptor(AbstractUIPlugin
.imageDescriptorFromPlugin(extendingPluginId, hoverIcon));
}
if (disabledIcon != null) {
action
.setDisabledImageDescriptor(AbstractUIPlugin
.imageDescriptorFromPlugin(extendingPluginId,
disabledIcon));
}
if (accelerator != null)
processAccelerator(action, accelerator);
}
/**
* copied from org.eclipse.ui.internal.ActionDescriptor
*/
private void processAccelerator(IAction action, String acceleratorText) {
if (acceleratorText.length() == 0)
return;
//Is it a numeric definition?
if (Character.isDigit(acceleratorText.charAt(0))) {
try {
action.setAccelerator(Integer.valueOf(acceleratorText)
.intValue());
} catch (NumberFormatException e) {
WorkbenchPlugin.log("Invalid accelerator declaration for action: " + m_id, e); //$NON-NLS-1$
}
} else
action.setAccelerator(Action.convertAccelerator(acceleratorT ext));
}
public PluginAction getAction()
{
return m_action;
}
}
private static class ContributionAction extends PluginAction
{
private IViewPart m_part;
public ContributionAction(IConfigurationElement actionElement, String id, int style, IViewPart viewPart)
{
super(actionElement, id, style);
m_part = viewPart;
initDelegate();
}
/* (non-Javadoc)
* Method declared on PluginAction.
*/
protected void initDelegate() {
super.initDelegate();
((IViewActionDelegate) getDelegate()).init(m_part);
}
}
private static class ActionContribution extends BasicContribution implements ISelectionChangedListener
{
private ActionExpression m_visibilityTest;
private final ISelectionProvider m_selProvider;
public ActionContribution(ISelectionProvider selProvider)
{
m_selProvider = selProvider;
if (selProvider != null) {
selProvider.addSelectionChangedListener(this);
updateSelection(selProvider.getSelection());
}
}
public void selectionChanged(SelectionChangedEvent event)
{
updateSelection(event.getSelection());
}
private void updateSelection(ISelection selection)
{
if (actions != null)
{
Iterator itr = actions.iterator();
while (itr.hasNext())
{
ActionDescriptor action = (ActionDescriptor) itr.next();
action.getAction().selectionChanged(selection);
}
}
}
/**
* Set the visibility test.
*
* @param element the element
*/
public void setVisibilityTest(IConfigurationElement element) {
m_visibilityTest = new ActionExpression(element);
}
/* (non-Javadoc)
* @see org.eclipse.ui.internal.PluginActionBuilder.BasicContributio n#dispose()
*/
public void dispose() {
if (m_selProvider != null) {
m_selProvider.removeSelectionChangedListener(this);
}
disposeActions();
super.dispose();
}
}
}
--------------070209070007020104040009--
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03049 seconds