Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / ContextMenu.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
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;
23
24 /**
25 * Creates a menu that is opened by right-clicking on a <code>Control</code>.
26 * <p>
27 * Note that menu items are added anew each time the menu is opened.
28 * </p>
29 *
30 * @author p.ciardelli
31 * @created 26.05.2008
32 * @version 1.0
33 */
34 public class ContextMenu {
35 private static final Logger logger = Logger.getLogger(ContextMenu.class);
36
37 private Control control;
38 private Menu menu;
39 private MenuManager menuManager;
40 private List actions;
41 private String label;
42
43 /**
44 * Constructor for top-level menu items.
45 *
46 * @param control
47 */
48 public ContextMenu(Control control) {
49 this.control = control;
50
51 // TODO rewrite class to override Menu
52
53 createContextMenu();
54 }
55
56 public ContextMenu(String label) {
57 this.label = label;
58
59 createContextMenu();
60 }
61
62 public MenuManager getMenuManager() {
63 return menuManager;
64 }
65
66 private void createContextMenu() {
67 if (control != null) {
68 menuManager = new MenuManager();
69 menu = menuManager.createContextMenu(control);
70 control.setMenu(menu);
71 } else {
72 menuManager = new MenuManager(label);
73 }
74
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);
81 }
82 if (action instanceof Separator) {
83 manager.add((Separator) action);
84 }
85 if (action instanceof MenuManager) {
86 manager.add((MenuManager) action);
87 }
88 }
89 }
90 });
91 }
92
93 private List getActions() {
94 if (actions == null) {
95 actions = new ArrayList();
96 }
97 return actions;
98 }
99
100 public void addAction(Action action) {
101 getActions().add(action);
102 }
103
104 public void removeAction(Action action) {
105 getActions().remove(action);
106 }
107
108 public void addSeparator() {
109 getActions().add(new Separator());
110 }
111
112 public void addSubmenu(MenuManager submenu) {
113 getActions().add(submenu);
114 }
115
116 public void setMenuManager(MenuManager menuManager){
117 if(control == null){
118 logger.warn("No control set for context menu");
119 }else{
120 this.menuManager = menuManager;
121 menu = menuManager.createContextMenu(control);
122 control.setMenu(menu);
123
124 menuManager.addMenuListener(new IMenuListener() {
125 public void menuAboutToShow(IMenuManager manager) {
126 for (Object action : getActions()) {
127 if (action instanceof Action) {
128 manager.add((Action) action);
129 }
130 if (action instanceof Separator) {
131 manager.add((Separator) action);
132 }
133 if (action instanceof MenuManager) {
134 manager.add((MenuManager) action);
135 }
136 }
137 }
138 });
139 }
140 }
141
142 public Menu getMenu() {
143 return menu;
144 }
145 }