p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / 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.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;
25
26 /**
27 * Creates a menu that is opened by right-clicking on a <code>Control</code>.
28 * <p>
29 * Note that menu items are added anew each time the menu is opened.
30 * </p>
31 *
32 * @author p.ciardelli
33 * @created 26.05.2008
34 * @version 1.0
35 */
36 public class ContextMenu {
37 private static final Logger logger = Logger.getLogger(ContextMenu.class);
38
39 private Control control;
40 private Menu menu;
41 private MenuManager menuManager;
42 private List actions;
43 private String label;
44
45 /**
46 * Constructor for top-level menu items.
47 *
48 * @param control
49 */
50 public ContextMenu(Control control) {
51 this.control = control;
52
53 // TODO rewrite class to override Menu
54
55 createContextMenu();
56 }
57
58 public ContextMenu(String label) {
59 this.label = label;
60
61 createContextMenu();
62 }
63
64 public MenuManager getMenuManager() {
65 return menuManager;
66 }
67
68 private void createContextMenu() {
69 if (control != null) {
70 menuManager = new MenuManager();
71 menu = menuManager.createContextMenu(control);
72 control.setMenu(menu);
73 } else {
74 menuManager = new MenuManager(label);
75 }
76
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);
83 }
84 if (action instanceof Separator) {
85 manager.add((Separator) action);
86 }
87 if (action instanceof MenuManager) {
88 manager.add((MenuManager) action);
89 }
90 }
91 manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
92 }
93 });
94 }
95
96 private List getActions() {
97 if (actions == null) {
98 actions = new ArrayList();
99 }
100 return actions;
101 }
102
103 public void addAction(Action action) {
104 getActions().add(action);
105 }
106
107 public void removeAction(Action action) {
108 getActions().remove(action);
109 }
110
111 public void addSeparator() {
112 getActions().add(new Separator());
113 }
114
115 public void addSubmenu(MenuManager submenu) {
116 getActions().add(submenu);
117 }
118
119 public void setMenuManager(MenuManager menuManager){
120 if(control == null){
121 logger.warn("No control set for context menu");
122 }else{
123 this.menuManager = menuManager;
124 menu = menuManager.createContextMenu(control);
125 control.setMenu(menu);
126
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);
132 }
133 if (action instanceof Separator) {
134 manager.add((Separator) action);
135 }
136 if (action instanceof MenuManager) {
137 manager.add((MenuManager) action);
138 }
139 }
140 }
141 });
142 }
143 }
144
145 public Menu getMenu() {
146 return menu;
147 }
148 }