|
15 | 15 | import org.eclipse.jface.action.Separator; |
16 | 16 | import org.eclipse.swt.widgets.Tree; |
17 | 17 | import org.eclipse.ui.IWorkbenchActionConstants; |
| 18 | +import org.eclipse.ui.services.IDisposable; |
18 | 19 |
|
19 | 20 | import net.sourceforge.pmd.RulePriority; |
20 | 21 | import net.sourceforge.pmd.eclipse.plugin.PMDPlugin; |
|
35 | 36 | * |
36 | 37 | */ |
37 | 38 |
|
38 | | -public class ViolationOverviewMenuManager { |
| 39 | +public class ViolationOverviewMenuManager implements IDisposable { |
39 | 40 | private final ViolationOverview overview; |
40 | 41 | private PriorityFilterAction[] priorityActions; |
| 42 | + private MenuManager contextMenuManager; |
41 | 43 |
|
42 | 44 | public ViolationOverviewMenuManager(ViolationOverview overview) { |
43 | 45 | this.overview = overview; |
@@ -92,40 +94,42 @@ public void createDropDownMenu(IMenuManager manager) { |
92 | 94 | * Creates the Context Menu |
93 | 95 | */ |
94 | 96 | public void createContextMenu() { |
95 | | - MenuManager manager = new MenuManager(); |
96 | | - manager.setRemoveAllWhenShown(true); |
97 | | - manager.addMenuListener(new IMenuListener() { |
98 | | - public void menuAboutToShow(IMenuManager manager) { |
99 | | - MenuManager submenuManager; |
100 | | - |
101 | | - // one SubMenu for filtering Projects |
102 | | - submenuManager = new MenuManager(getString(StringKeys.VIEW_MENU_RESOURCE_FILTER)); |
103 | | - createProjectFilterMenu(submenuManager); |
104 | | - manager.add(submenuManager); |
105 | | - |
106 | | - // ... another one for filtering Priorities |
107 | | - submenuManager = new MenuManager(getString(StringKeys.VIEW_MENU_PRIORITY_FILTER)); |
108 | | - for (PriorityFilterAction priorityAction : priorityActions) { |
109 | | - submenuManager.add(priorityAction); |
110 | | - } |
111 | | - manager.add(submenuManager); |
112 | | - |
113 | | - // ... another one for showing the presentation types |
114 | | - submenuManager = new MenuManager(getString(StringKeys.VIEW_MENU_PRESENTATION_TYPE)); |
115 | | - createShowTypeSubmenu(submenuManager); |
116 | | - manager.add(submenuManager); |
117 | | - |
118 | | - // additions Action: Clear PMD Violations |
119 | | - manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
120 | | - manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); |
| 97 | + if (contextMenuManager == null) { |
| 98 | + contextMenuManager = new MenuManager(); |
| 99 | + contextMenuManager.setRemoveAllWhenShown(true); |
| 100 | + contextMenuManager.addMenuListener(new IMenuListener() { |
| 101 | + public void menuAboutToShow(IMenuManager manager) { |
| 102 | + MenuManager submenuManager; |
| 103 | + |
| 104 | + // one SubMenu for filtering Projects |
| 105 | + submenuManager = new MenuManager(getString(StringKeys.VIEW_MENU_RESOURCE_FILTER)); |
| 106 | + createProjectFilterMenu(submenuManager); |
| 107 | + manager.add(submenuManager); |
| 108 | + |
| 109 | + // ... another one for filtering Priorities |
| 110 | + submenuManager = new MenuManager(getString(StringKeys.VIEW_MENU_PRIORITY_FILTER)); |
| 111 | + for (PriorityFilterAction priorityAction : priorityActions) { |
| 112 | + submenuManager.add(priorityAction); |
| 113 | + } |
| 114 | + manager.add(submenuManager); |
| 115 | + |
| 116 | + // ... another one for showing the presentation types |
| 117 | + submenuManager = new MenuManager(getString(StringKeys.VIEW_MENU_PRESENTATION_TYPE)); |
| 118 | + createShowTypeSubmenu(submenuManager); |
| 119 | + manager.add(submenuManager); |
| 120 | + |
| 121 | + // additions Action: Clear PMD Violations |
| 122 | + manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); |
| 123 | + manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); |
121 | 124 |
|
122 | | - } |
123 | | - }); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
124 | 128 |
|
125 | 129 | Tree tree = overview.getViewer().getTree(); |
126 | | - tree.setMenu(manager.createContextMenu(tree)); |
| 130 | + tree.setMenu(contextMenuManager.createContextMenu(tree)); |
127 | 131 |
|
128 | | - overview.getSite().registerContextMenu(manager, overview.getViewer()); |
| 132 | + overview.getSite().registerContextMenu(contextMenuManager, overview.getViewer()); |
129 | 133 | } |
130 | 134 |
|
131 | 135 | /** |
@@ -178,4 +182,12 @@ private void createShowTypeSubmenu(IMenuManager manager) { |
178 | 182 | private String getString(String key) { |
179 | 183 | return PMDPlugin.getDefault().getStringTable().getString(key); |
180 | 184 | } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void dispose() { |
| 188 | + if (contextMenuManager != null) { |
| 189 | + contextMenuManager.dispose(); |
| 190 | + contextMenuManager = null; |
| 191 | + } |
| 192 | + } |
181 | 193 | } |
0 commit comments