Skip to content
Snippets Groups Projects
Commit 76e718f9 authored by David Trachtenherz's avatar David Trachtenherz
Browse files

Base classes for ContextMenuContributor for sub-menus

refs 451
parent de065f88
No related branches found
No related tags found
No related merge requests found
/*--------------------------------------------------------------------------+
$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.ui.extension.base;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.fortiss.tooling.kernel.model.INamedElement;
import org.fortiss.tooling.kernel.ui.extension.IContextMenuContributor;
import org.fortiss.tooling.kernel.ui.extension.base.MenuActionBase.ActionFactory;
import org.fortiss.tooling.kernel.ui.extension.data.ContextMenuContextProvider;
import org.fortiss.tooling.kernel.ui.service.IContextMenuService;
/**
* Base class for context menu contributors creating a sub-menu
* {@link IMenuManager} with actions provided by actions factories
* {@link ActionFactory} returned by {@link #getActionFactories()}.
*
* <p>
*
* The generic type parameter {@code T} indicated the type of selection
* elements.
*
* @author trachtenherz
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating RED Hash:
*/
public abstract class ContextMenuSubMenuContributorBase implements
IContextMenuContributor {
/** Constructor. */
public ContextMenuSubMenuContributorBase() {
super();
}
/** The name of the sub-menu */
protected abstract String getMenuName();
/** Returns the action factories for the actions to be shown in the menu */
protected abstract Collection<ActionFactory<INamedElement>> getActionFactories();
/** Creates action instances for the given selection. */
protected Collection<Action> createActionsForElement(INamedElement selection) {
ArrayList<Action> list = new ArrayList<Action>();
for (ActionFactory<INamedElement> factory : getActionFactories()) {
list.add(factory.createAction(selection));
}
return list;
}
/** The image for the sub-menu */
protected ImageDescriptor getMenuIcon() {
return null;
}
/** The Id for the sub-menu */
protected String getMenuId() {
return null;
}
/**
* Creates a sub-menu {@link IMenuManager} containing specified actions.
*/
protected IMenuManager createMenu(Collection<Action> actions) {
IMenuManager menu = new MenuManager(getMenuName(), getMenuIcon(),
getMenuId());
for (Action a : actions) {
menu.add(a);
}
return menu;
}
/** {@inheritDoc} */
@Override
public final List<IContributionItem> getContributedItems(EObject selection,
ContextMenuContextProvider contextProvider) {
if (selection instanceof INamedElement
&& acceptSelection((INamedElement) selection, contextProvider)) {
List<IContributionItem> contributionItems = new ArrayList<IContributionItem>();
contributionItems
.add(createMenu(createActionsForElement((INamedElement) selection)));
return contributionItems;
}
return Collections.emptyList();
}
/** {@inheritDoc} */
@Override
public String getMenuSectionID() {
return IContextMenuService.BOTTOM_MOST_MENU_SECTION_ID;
}
/** Returns {@code true} if the selected element is acceptable. */
protected abstract boolean acceptSelection(INamedElement selection,
ContextMenuContextProvider contextProvider);
}
\ No newline at end of file
/*--------------------------------------------------------------------------+
$Id: EasyStartMenuBase.java 1907 2011-11-11 14:04:26Z hoelzl $
| |
| Copyright 2011 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.ui.extension.base;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
/**
* Base class for menu actions.
*
* @author trachtenherz
* @author $Author: hoelzl $
* @version $Rev: 1907 $
* @ConQAT.Rating YELLOW Hash: A55559E6A76127EF46123531794FB17B
*/
public abstract class MenuActionBase<T> extends Action {
/** Factory for the action instances used for this menu */
public static interface ActionFactory<T> {
/** Creates and returns an action instance for the given target. */
MenuActionBase<T> createAction(T target);
}
/** The target to be processed. */
protected final T target;
/** Name of the action. */
protected abstract String getActionName();
/** Icon for the action. */
protected ImageDescriptor getActionIcon() {
return null;
}
/** Constructor. */
public MenuActionBase(T target) {
setText(getActionName());
setImageDescriptor(getActionIcon());
this.target = target;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment