1 
2 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
3 
4 import org.eclipse.jface.action.Action;
5 import org.eclipse.jface.action.ActionContributionItem;
6 import org.eclipse.jface.action.IAction;
7 import org.eclipse.jface.action.IMenuCreator;
8 import org.eclipse.swt.events.MenuEvent;
9 import org.eclipse.swt.events.MenuListener;
10 import org.eclipse.swt.widgets.Control;
11 import org.eclipse.swt.widgets.Menu;
12 import org.eclipse.swt.widgets.MenuItem;
13 
14 /**
15  * Action which creates a submenu that is dynamically populated by subclasses
16  */
17 public abstract class SubmenuAction extends Action implements MenuListener, IMenuCreator {
18     private Menu mMenu;
19 
SubmenuAction(String title)20     public SubmenuAction(String title) {
21         super(title, IAction.AS_DROP_DOWN_MENU);
22     }
23 
24     @Override
getMenuCreator()25     public IMenuCreator getMenuCreator() {
26         return this;
27     }
28 
29     @Override
dispose()30     public void dispose() {
31         if (mMenu != null) {
32             mMenu.dispose();
33             mMenu = null;
34         }
35     }
36 
37     @Override
getMenu(Control parent)38     public Menu getMenu(Control parent) {
39         return null;
40     }
41 
42     @Override
getMenu(Menu parent)43     public Menu getMenu(Menu parent) {
44         mMenu = new Menu(parent);
45         mMenu.addMenuListener(this);
46         return mMenu;
47     }
48 
49     @Override
menuHidden(MenuEvent e)50     public void menuHidden(MenuEvent e) {
51     }
52 
addMenuItems(Menu menu)53     protected abstract void addMenuItems(Menu menu);
54 
55     @Override
menuShown(MenuEvent e)56     public void menuShown(MenuEvent e) {
57         // TODO: Replace this stuff with manager.setRemoveAllWhenShown(true);
58         MenuItem[] menuItems = mMenu.getItems();
59         for (int i = 0; i < menuItems.length; i++) {
60             menuItems[i].dispose();
61         }
62         addMenuItems(mMenu);
63     }
64 
addDisabledMessageItem(String message)65     protected void addDisabledMessageItem(String message) {
66         IAction action = new Action(message, IAction.AS_PUSH_BUTTON) {
67             @Override
68             public void run() {
69             }
70         };
71         action.setEnabled(false);
72         new ActionContributionItem(action).fill(mMenu, -1);
73 
74     }
75 }
76