2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.jface
.action
.Action
;
17 import org
.eclipse
.jface
.action
.GroupMarker
;
18 import org
.eclipse
.jface
.action
.IMenuListener
;
19 import org
.eclipse
.jface
.action
.IMenuManager
;
20 import org
.eclipse
.jface
.action
.MenuManager
;
21 import org
.eclipse
.jface
.action
.Separator
;
22 import org
.eclipse
.swt
.widgets
.Control
;
23 import org
.eclipse
.swt
.widgets
.Menu
;
24 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
27 * Creates a menu that is opened by right-clicking on a <code>Control</code>.
29 * Note that menu items are added anew each time the menu is opened.
36 public class ContextMenu
{
37 private static final Logger logger
= Logger
.getLogger(ContextMenu
.class);
39 private Control control
;
41 private MenuManager menuManager
;
46 * Constructor for top-level menu items.
50 public ContextMenu(Control control
) {
51 this.control
= control
;
53 // TODO rewrite class to override Menu
58 public ContextMenu(String label
) {
64 public MenuManager
getMenuManager() {
68 private void createContextMenu() {
69 if (control
!= null) {
70 menuManager
= new MenuManager();
71 menu
= menuManager
.createContextMenu(control
);
72 control
.setMenu(menu
);
74 menuManager
= new MenuManager(label
);
77 menuManager
.setRemoveAllWhenShown(true);
78 menuManager
.addMenuListener(new IMenuListener() {
79 public void menuAboutToShow(IMenuManager manager
) {
80 for (Object action
: getActions()) {
81 if (action
instanceof Action
) {
82 manager
.add((Action
) action
);
84 if (action
instanceof Separator
) {
85 manager
.add((Separator
) action
);
87 if (action
instanceof MenuManager
) {
88 manager
.add((MenuManager
) action
);
91 manager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
96 private List
getActions() {
97 if (actions
== null) {
98 actions
= new ArrayList();
103 public void addAction(Action action
) {
104 getActions().add(action
);
107 public void removeAction(Action action
) {
108 getActions().remove(action
);
111 public void addSeparator() {
112 getActions().add(new Separator());
115 public void addSubmenu(MenuManager submenu
) {
116 getActions().add(submenu
);
119 public void setMenuManager(MenuManager menuManager
){
121 logger
.warn("No control set for context menu");
123 this.menuManager
= menuManager
;
124 menu
= menuManager
.createContextMenu(control
);
125 control
.setMenu(menu
);
127 menuManager
.addMenuListener(new IMenuListener() {
128 public void menuAboutToShow(IMenuManager manager
) {
129 for (Object action
: getActions()) {
130 if (action
instanceof Action
) {
131 manager
.add((Action
) action
);
133 if (action
instanceof Separator
) {
134 manager
.add((Separator
) action
);
136 if (action
instanceof MenuManager
) {
137 manager
.add((MenuManager
) action
);
145 public Menu
getMenu() {