RCP软件包括左侧导航视图、右上部编辑器、右下部属性视图;软件启动后,默认打开左侧导航视图和中下部属性视图,左侧导航树只有三级(Family、Parent和Child),其中Family没有 任何属性,Parent和Child只有三个属性(名称、年龄、性别);导航树能提供右键菜单进行(新建、删除、复制、剪切、粘贴、撤销、重做),给出代码和plugin.xml
时间: 2023-07-10 08:10:04 浏览: 113
代码:
```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$
阅读全文