工具栏下拉菜单

eclipse插件开发实现如下效果的工具栏下拉菜单

工具栏下拉菜单

<plugin>
   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="ym.actionSet">
         <menu
               label="Sample &amp;Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               class="ym.actions.DruAction"
               icon="icons/sample.gif"
               id="ym.actions.DruAction"
               label="&amp;DuxAction"
               menubarPath="sampleMenu/sampleGroup"
               style="pulldown"
               toolbarPath="sampleGroup"
               tooltip="Good Luck">
         </action>
      </actionSet>
   </extension>
</plugin>



重点是设置  style="pulldown"

public class DruAction  extends Action implements
IMenuCreator, IWorkbenchWindowPulldownDelegate2{

	private Menu fMenu;
	private Menu dropDownMenu;
 	 
	public void dispose() {
		
	}

	public Menu getMenu(Control parent) {
		if (dropDownMenu != null){
			return dropDownMenu;
		}
		dropDownMenu = new Menu(parent);
		MenuItem newMCITrxItem = new MenuItem(dropDownMenu, 0);
		newMCITrxItem.setText("下拉菜单示例");
		
		return dropDownMenu;
	}

	public Menu getMenu(Menu parent) {
		if (fMenu != null){
			return fMenu;
		}
		fMenu = new Menu(parent);
		MenuItem newMCITrxItem = new MenuItem(fMenu, 0);
		newMCITrxItem.setText("菜单栏菜单示例");
		
		return fMenu;
	}

	public void init(IWorkbenchWindow window) {
		
	}

 
	public void run(IAction action) {
	 
	}
	
	
	public void selectionChanged(IAction action, ISelection selection) {
		
	}

}


重点是 IMenuCreator 接口,用于生成菜单
-----------------------------------------------------------------

如果要直接用SWT来做,也不难

工具栏下拉菜单

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import com.swtdesigner.SWTResourceManager;

import swing2swt.layout.BorderLayout;

public class DruActionMenu {

	protected Shell shell;

	public static void main(String[] args) {
		try {
			DruActionMenu window = new DruActionMenu();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window
	 */
	public void open() {
		final Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	/**
	 * Create contents of the window
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setLayout(new BorderLayout(0, 0));
		shell.setSize(500, 375);
		shell.setText("SWT Application");

		final CoolBar coolBar = new CoolBar(shell, SWT.NONE);
		coolBar.setLayoutData(BorderLayout.NORTH);

		final CoolItem newItemCoolItem = new CoolItem(coolBar, SWT.PUSH);
		newItemCoolItem.setMinimumSize(new Point(30, 26));
		newItemCoolItem.setText("New item");
		 
		ToolBar winToolBar = new ToolBar(coolBar, SWT.FLAT | SWT.WRAP);
		newItemCoolItem.setControl(winToolBar);

		final ToolItem newItemToolItem = new ToolItem(winToolBar, SWT.PUSH);
		newItemToolItem.setImage(SWTResourceManager.getImage(DruActionMenu.class, "/com/sun/java/swing/plaf/windows/icons/Computer.gif"));

		final ToolItem newItemToolItem_1 = new ToolItem(winToolBar, SWT.DROP_DOWN);
		newItemToolItem_1.setImage(SWTResourceManager.getImage(DruActionMenu.class, "/com/sun/java/swing/plaf/windows/icons/TreeClosed.gif"));

		final Menu menu= new Menu(winToolBar);
		addDropDown(newItemToolItem_1, menu);

		final MenuItem menuItem = new MenuItem(menu, SWT.NONE);
		menuItem.setText("Menu Item");

		final MenuItem menuItem_1 = new MenuItem(menu, SWT.CASCADE);
		menuItem_1.setText("Menu Item");

		final Menu menu_1 = new Menu(menuItem_1);
		menuItem_1.setMenu(menu_1);

		final MenuItem menuItem_2 = new MenuItem(menu_1, SWT.NONE);
		menuItem_2.setText("Menu Item");

		new MenuItem(menu, SWT.SEPARATOR);

		final MenuItem menuItem_3 = new MenuItem(menu, SWT.NONE);
		menuItem_3.setText("Menu Item");

		final ToolItem newItemToolItem_2 = new ToolItem(winToolBar, SWT.PUSH);
		newItemToolItem_2.setImage(SWTResourceManager.getImage(DruActionMenu.class, "/com/sun/java/swing/plaf/windows/icons/HardDrive.gif"));
	}

	/**
	 *  加载下拉菜单
	 *  
	 * @param item
	 * @param menu
	 */
	private static void addDropDown(final ToolItem item, final Menu menu) {
		item.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.ARROW) {
					Rectangle rect = item.getBounds();
					Point pt = new Point(rect.x, rect.y + rect.height);
					pt = item.getParent().toDisplay(pt);
					menu.setLocation(pt.x, pt.y);
					menu.setVisible(true);
				}
			}
		});
	}
}



你可能感兴趣的:(eclipse,windows,swing,sun,idea)