Home | History | Annotate | Download | only in gle2
      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 abstract class SubmenuAction extends Action implements MenuListener, IMenuCreator {
     18     private Menu mMenu;
     19 
     20     SubmenuAction(String title) {
     21         super(title, IAction.AS_DROP_DOWN_MENU);
     22     }
     23 
     24     @Override
     25     public IMenuCreator getMenuCreator() {
     26         return this;
     27     }
     28 
     29     public void dispose() {
     30         if (mMenu != null) {
     31             mMenu.dispose();
     32             mMenu = null;
     33         }
     34     }
     35 
     36     public Menu getMenu(Control parent) {
     37         return null;
     38     }
     39 
     40     public Menu getMenu(Menu parent) {
     41         mMenu = new Menu(parent);
     42         mMenu.addMenuListener(this);
     43         return mMenu;
     44     }
     45 
     46     public void menuHidden(MenuEvent e) {
     47     }
     48 
     49     protected abstract void addMenuItems(Menu menu);
     50 
     51     public void menuShown(MenuEvent e) {
     52         // TODO: Replace this stuff with manager.setRemoveAllWhenShown(true);
     53         MenuItem[] menuItems = mMenu.getItems();
     54         for (int i = 0; i < menuItems.length; i++) {
     55             menuItems[i].dispose();
     56         }
     57         addMenuItems(mMenu);
     58     }
     59 
     60     protected void addDisabledMessageItem(String message) {
     61         IAction action = new Action(message, IAction.AS_PUSH_BUTTON) {
     62             @Override
     63             public void run() {
     64             }
     65         };
     66         action.setEnabled(false);
     67         new ActionContributionItem(action).fill(mMenu, -1);
     68 
     69     }
     70 }
     71