RCP CommonNavigator导航视图问题

         最近在做一个RCP项目时,发现左边导航视图用CommonNavigator时,新建项目后关闭程序,再次打开发现导航视图中新建的项目没有出现,要再次点击右键才能出现,经过翻阅资料发现需要在

ApplicationWorkbenchAdvisor类增加以下2个方法就可以解决

   

@Override
public  IAdaptable getDefaultPageInput() {
return  ResourcesPlugin.getWorkspace().getRoot();
}
 
@Override
public  void  initialize(IWorkbenchConfigurer configurer) {
IDE.registerAdapters();
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码: ```java package com.example.rcp.application; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.Separator; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.ISelectionService; import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.ViewPart; public class RCPApplication extends ApplicationWindow { private TreeViewer treeViewer; public RCPApplication() { super(null); } protected Control createContents(Composite parent) { Composite container = new Composite(parent, SWT.NONE); container.setLayout(new FillLayout()); treeViewer = new TreeViewer(container, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); Tree tree = treeViewer.getTree(); tree.setHeaderVisible(false); tree.setLinesVisible(false); TreeItem familyItem = new TreeItem(tree, SWT.NONE); familyItem.setText("Family"); TreeItem parentItem = new TreeItem(familyItem, SWT.NONE); parentItem.setText("Parent"); TreeItem childItem = new TreeItem(parentItem, SWT.NONE); childItem.setText("Child"); treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection)event.getSelection(); if (selection.isEmpty()) { return; } Object element = selection.getFirstElement(); if (element instanceof TreeItem) { TreeItem item = (TreeItem)element; String text = item.getText(); int index = item.getParentItem() == null ? tree.indexOf(item) : item.getParentItem().indexOf(item); showMessageBox("Selected", "Text: " + text + ", Index: " + index); } } }); MenuManager menuManager = new MenuManager(); menuManager.add(new NewAction()); menuManager.add(new DeleteAction()); menuManager.add(new CopyAction()); menuManager.add(new CutAction()); menuManager.add(new PasteAction()); menuManager.add(new Separator()); menuManager.add(new UndoAction()); menuManager.add(new RedoAction()); Menu menu = menuManager.createContextMenu(tree); tree.setMenu(menu); return container; } protected MenuManager createMenuManager() { MenuManager menuManager = new MenuManager(); MenuManager fileMenu = new MenuManager("&File"); MenuManager editMenu = new MenuManager("&Edit"); MenuManager helpMenu = new MenuManager("&Help"); menuManager.add(fileMenu); menuManager.add(editMenu); menuManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); menuManager.add(helpMenu); fileMenu.add(new ExitAction()); editMenu.add(new CopyAction()); editMenu.add(new PasteAction()); helpMenu.add(new AboutAction()); return menuManager; } protected void configureShell(Shell shell) { super.configureShell(shell); shell.setSize(500, 500); shell.setText("RCP Application"); PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, "com.example.rcp.application"); } public static void main(String[] args) { RCPApplication application = new RCPApplication(); application.setBlockOnOpen(true); application.open(); Display.getCurrent().dispose(); } private class ExitAction extends Action { public ExitAction() { super("&Exit@Ctrl+Q", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_QUIT)); } public void run() { close(); } } private class CopyAction extends Action { public CopyAction() { super("&Copy@Ctrl+C", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); } public void run() { // copy logic } } private class CutAction extends Action { public CutAction() { super("Cu&t@Ctrl+X", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_CUT)); } public void run() { // cut logic } } private class PasteAction extends Action { public PasteAction() { super("&Paste@Ctrl+V", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_PASTE)); } public void run() { // paste logic } } private class DeleteAction extends Action { public DeleteAction() { super("&Delete@Del", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); } public void run() { // delete logic } } private class NewAction extends Action { public NewAction() { super("&New@Alt+Shift+N", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJ_ADD)); } public void run() { // new logic } } private class UndoAction extends Action { public UndoAction() { super("&Undo@Ctrl+Z", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_UNDO)); } public void run() { // undo logic } } private class RedoAction extends Action { public RedoAction() { super("&Redo@Ctrl+Y", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_REDO)); } public void run() { // redo logic } } private class AboutAction extends Action { public AboutAction() { super("&About", PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK)); } public void run() { // about dialog } } private void showMessageBox(String title, String message) { MessageBox messageBox = new MessageBox(getShell(), SWT.ICON_INFORMATION | SWT.OK); messageBox.setText(title); messageBox.setMessage(message); messageBox.open(); } public static class NavigationView extends ViewPart { public void createPartControl(Composite parent) { // navigation view logic } public void setFocus() { // focus logic } } public static class PropertyView extends ViewPart { public void createPartControl(Composite parent) { // property view logic } public void setFocus() { // focus logic } } } ``` plugin.xml: ```xml <?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.4"?> <plugin> <extension point="org.eclipse.ui.views"> <view name="Navigation View" class="com.example.rcp.application.RCPApplication$NavigationView" id="com.example.rcp.application.navigationView"> </view> <view name="Property View" class="com.example.rcp.application.RCPApplication$PropertyView" id="com.example.rcp.application.propertyView"> </view> </extension> <extension point="org.eclipse.ui.perspectiveExtensions"> <perspectiveExtension targetID="org.eclipse.ui.resourcePerspective"> <view id="com.example.rcp.application.navigationView" relationship="left" relative="org.eclipse.ui.views.ResourceNavigator"> </view> <view id="com.example.rcp.application.propertyView" relationship="bottom" relative="org.eclipse.ui.views.PropertySheet"> </view> </perspectiveExtension> </extension> <extension point="org.eclipse.ui.popupMenus"> <objectContribution adaptable="false" id="com.example.rcp.application.newObjectContribution" name="New"> <action class="com.example.rcp.application.RCPApplication$NewAction" enablesFor="1" id="com.example.rcp.application.newAction" label="New"> </action> <objectClass name="*"> </objectClass> </objectContribution> <objectContribution adaptable="false" id="com.example.rcp.application.deleteObjectContribution" name="Delete"> <action class="com.example.rcp.application.RCPApplication$DeleteAction" enablesFor="1" id="com.example.rcp.application.deleteAction" label="Delete"> </action> <objectClass name="*"> </objectClass> </objectContribution> <objectContribution adaptable="false" id="com.example.rcp.application.copyObjectContribution" name="Copy"> <action class="com.example.rcp.application.RCPApplication$CopyAction" enablesFor="1" id="com.example.rcp.application.copyAction" label="Copy"> </action> <objectClass name="*"> </objectClass> </objectContribution> <objectContribution adaptable="false" id="com.example.rcp.application.cutObjectContribution" name="Cut"> <action class="com.example.rcp.application.RCPApplication$CutAction" enablesFor="1" id="com.example.rcp.application.cutAction" label="Cut"> </action> <objectClass name="*"> </objectClass> </objectContribution> <objectContribution adaptable="false" id="com.example.rcp.application.pasteObjectContribution" name="Paste"> <action class="com.example.rcp.application.RCPApplication$PasteAction" enablesFor="1" id="com.example.rcp.application.pasteAction" label="Paste"> </action> <objectClass name="*"> </objectClass> </objectContribution> <objectContribution adaptable="false" id="com.example.rcp.application.undoObjectContribution" name="Undo"> <action class="com.example.rcp.application.RCPApplication$

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值