如何在ViewPart上添加ViewToolBar

ViewToolBar其实就是Actions。在ViewPart上添加Action其实是非常简单的(如果用SWT Design的话)。其实就是几行代码的事情:

1 ExpandedBlockStart.gif ContractedBlock.gif public   void  createPartControl(Composite parent)  dot.gif {
               //SWT Design在生成代码的时下面两句可能会颠倒,我也不知道为什么,至少我的是位置颠倒了。运行起来总报错!
2InBlock.gif        createActions();
3InBlock.gif        initializeToolBar();
4ExpandedBlockEnd.gif}
先产生Action,在把Action加入到toolBarManager上:
 1 ExpandedBlockStart.gif ContractedBlock.gif      private   void  initializeToolBar()  dot.gif {
 2InBlock.gif        IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();//取得此View的toolBarManager
 3InBlock.gif
 4InBlock.gif        toolBarManager.add(partManageAction);
 5InBlock.gif
 6InBlock.gif        toolBarManager.add(setupCraftworkManage);
 7InBlock.gif
 8InBlock.gif        toolBarManager.add(baseBOMManage);
 9InBlock.gif
10InBlock.gif        toolBarManager.add(standardProductManage);
11ExpandedBlockEnd.gif    }

下面是来自鬼子站的资料,用的都是SWT design做的~有钱啊!
Wizards - RCP - ViewPart

wizard_eclipse_viewpart3.gifSubclasses of the Eclipse RCP ViewPart class can be created using the RCP ViewPart wizard. The wizard can be selected from the drop down Designer wizard menu or from the Eclipse New wizard.

To use the wizard, select the project source folder and package to contain the class. Then enter the class name and view name and hit the Finish button.

wizard_eclipse_viewpart1.gif  wizard_eclipse_viewpart2.gif

The wizard generates the following code.

import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.part.ViewPart;

public static final String ID = "sample.rcp.EclipseViewPartTest";

public class EclipseViewPartTest extends ViewPart {
    public void createPartControl(Composite parent) {         Composite container = new Composite(parent, SWT.NONE);         createActions();         initializeToolBar();         initializeMenu();     }     private void createActions() {     }     private void initializeToolBar() {         IToolBarManager tbm =             getViewSite().getActionBars().getToolBarManager();     }     private void initializeMenu() {         IMenuManager manager =            getViewSite().getActionBars().getMenuManager();     }     public void setFocus() {     } }

If the ViewPart is created in an existing plugin project, the plugin.xml file is also updated with the appropriate view declaration.

wizard_eclipse_viewpart10.gif

When editing ViewParts, Designer presents two tabs in the design area, one for laying out the contents of the view (using any widgets or layout managersr) and a second for defining actions and assigning them to the view's toolbar and menu.

wizard_eclipse_viewpart4.gif

On the Actions tab, the first column, Actions, is used to define new actions (which are created as inner classes of the current class). New actions may be created using the Add button (plus icon) above the column, existing actions may be added using the Open button (folder icon),  and existing actions may be deleted using the Delete button (minus) icon. Selecting an action allows you to edit its icons, label and tool tip text within the property pane. 

The second column, Toolbar, is used to define the contents of the view's toolbar. Actions and separators may be dragged from the Actions column and dropped in the Toolbar column. Toolbar actions may be rearranged using the Move Up and Move Down buttons and deleted using the Delete button above the column. The order of individual items may also be rearranged via drag and drop.

The third column, Menu, is used to define the contents of the view's menu. Actions, separators and menu managers may be dragged from the Actions column and dropped in the Menu column. Menus may be expanded and collapse using the Expand All and Collapse All buttons and deleted using the Delete button above the column. The order of individual items may also be rearranged via drag and drop.

wizard_eclipse_viewpart5.gif

When the ViewPart itself is selected in the property pane, its icon, category and title may be edited. New categories may also be created.

wizard_eclipse_viewpart6.gif   wizard_eclipse_viewpart7.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现ScrollingGraphicalViewer的缩放,你需要使用ZoomManager。以下是一个简单的示例代码: ```java ScrollingGraphicalViewer viewer = new ScrollingGraphicalViewer(); ZoomManager zoomManager = new ZoomManager(viewer.getRootEditPart().getZoomManager(), viewer.getViewport()); zoomManager.setZoomLevels(new double[] { 0.1, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 3.0, 4.0, 5.0 }); zoomManager.setZoomLevelContributions(Arrays.asList(ZoomManager.FIT_ALL, ZoomManager.FIT_HEIGHT, ZoomManager.FIT_WIDTH)); viewer.setProperty(MouseWheelHandler.KeyGenerator.getKey(SWT.MOD1), MouseWheelZoomHandler.SINGLETON); viewer.setEditDomain(new DefaultEditDomain(null)); viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer)); viewer.setViewport(new Viewport(true)); viewer.setZoomManager(zoomManager); ``` 上述示例中,我们首先创建了一个ScrollingGraphicalViewer实例,并获取其ZoomManager。然后我们设置了可用的缩放级别以及缩放模式(FIT_ALL,FIT_HEIGHT,FIT_WIDTH)。接着通过MouseWheelZoomHandler实现鼠标滚轮缩放功能,并将其添加viewer中。最后设置viewer的EditDomain、KeyHandler、Viewport和ZoomManager。 要在拖动Viewpart时通知ScrollingGraphicalViewer的内容重新布局使其一直居中,你需要实现ViewPart中的dragOver方法。以下是一个简单的示例代码: ```java @Override public boolean dragOver(DropTargetEvent event) { if (viewer != null) { Point point = Display.getDefault().map(null, viewer.getControl(), event.x, event.y); viewer.scrollTo(point.x, point.y); return true; } return false; } ``` 上述示例中,我们首先将屏幕坐标转换为viewer坐标。然后调用viewer的scrollTo方法来保持viewer内容在屏幕中心。最后返回true以指示拖放操作已成功处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值