工具栏下拉菜单

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

[img]http://dl.iteye.com/upload/picture/pic/55369/d623c9a3-3792-3ea9-9c2d-f4b53efa16d8.bmp[/img]


<plugin>
<extension
point="org.eclipse.ui.actionSets">
<actionSet
label="Sample Action Set"
visible="true"
id="ym.actionSet">
<menu
label="Sample &Menu"
id="sampleMenu">
<separator
name="sampleGroup">
</separator>
</menu>
<action
class="ym.actions.DruAction"
icon="icons/sample.gif"
id="ym.actions.DruAction"
label="&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来做,也不难

[img]http://dl.iteye.com/upload/picture/pic/55417/f1832970-87d7-37fe-aed4-539d58b0e7fc.bmp[/img]


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);
}
}
});
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值