025811d3e6098f7c5b4e1535b6175e58625571de
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
.IMenuListener
;
18 import org
.eclipse
.jface
.action
.IMenuManager
;
19 import org
.eclipse
.jface
.action
.MenuManager
;
20 import org
.eclipse
.jface
.action
.Separator
;
21 import org
.eclipse
.swt
.widgets
.Control
;
22 import org
.eclipse
.swt
.widgets
.Menu
;
25 * Creates a menu that is opened by right-clicking on a <code>Control</code>.
27 * Note that menu items are added anew each time the menu is opened.
34 public class ContextMenu
{
35 private static final Logger logger
= Logger
.getLogger(ContextMenu
.class);
37 private Control control
;
39 private MenuManager menuManager
;
44 * Constructor for top-level menu items.
48 public ContextMenu(Control control
) {
49 this.control
= control
;
51 // TODO rewrite class to override Menu
56 public ContextMenu(String label
) {
62 public MenuManager
getMenuManager() {
66 private void createContextMenu() {
67 if (control
!= null) {
68 menuManager
= new MenuManager();
69 menu
= menuManager
.createContextMenu(control
);
70 control
.setMenu(menu
);
72 menuManager
= new MenuManager(label
);
75 menuManager
.setRemoveAllWhenShown(true);
76 menuManager
.addMenuListener(new IMenuListener() {
77 public void menuAboutToShow(IMenuManager manager
) {
78 for (Object action
: getActions()) {
79 if (action
instanceof Action
) {
80 manager
.add((Action
) action
);
82 if (action
instanceof Separator
) {
83 manager
.add((Separator
) action
);
85 if (action
instanceof MenuManager
) {
86 manager
.add((MenuManager
) action
);
93 private List
getActions() {
94 if (actions
== null) {
95 actions
= new ArrayList();
100 public void addAction(Action action
) {
101 getActions().add(action
);
104 public void removeAction(Action action
) {
105 getActions().remove(action
);
108 public void addSeparator() {
109 getActions().add(new Separator());
112 public void addSubmenu(MenuManager submenu
) {
113 getActions().add(submenu
);
116 public Menu
getMenu() {