像WEB一样的树形导航RCP,废话不说,先看效果图。
点击不同的树节点可以动态切换右边的view,界面切换是一瞬间的,不闪。
上代码:
Perspective
package org.hermit.pojotools.tt;
import org.eclipse.ui.IFolderLayout;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;
public class pp implements IPerspectiveFactory {
/**
* Creates the initial layout for a page.
*/
public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);
addFastViews(layout);
addViewShortcuts(layout);
addPerspectiveShortcuts(layout);
layout.addView(t.ID, IPageLayout.LEFT, 0.5f, editorArea);
IFolderLayout folderLayout = layout.createFolder("folder", IPageLayout.LEFT, 0.5f,editorArea);
folderLayout.addView(t1.ID);
}
/**
* Add fast views to the perspective.
*/
private void addFastViews(IPageLayout layout) {
}
/**
* Add view shortcuts to the perspective.
*/
private void addViewShortcuts(IPageLayout layout) {
}
/**
* Add perspective shortcuts to the perspective.
*/
private void addPerspectiveShortcuts(IPageLayout layout) {
}
}
树形导航view
package org.hermit.pojotools.tt;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
public class t extends ViewPart {
private Tree tree;
public static final String ID = "org.hermit.pojotools.tt.t"; //$NON-NLS-1$
private String curID = t1.ID;
/**
* Create contents of the view part
*
* @param parent
*/
@Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new FillLayout());
final TreeViewer treeViewer = new TreeViewer(container, SWT.BORDER);
tree = treeViewer.getTree();
tree.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
TreeItem item = (TreeItem) tree.getSelection()[0];
String id = (String) item.getData();
if (id != null && !id.equals(curID)) {
IWorkbenchPage wbp = getViewSite().getPage();
try {
IViewPart view = wbp.findView(curID);
if (view != null) {
//下面两行不能颠倒,一定要先打开新的VIEW才能关闭老的,否则布局就会乱掉
wbp.showView(id);
wbp.hideView(view);
curID = id;
}
} catch (PartInitException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
});
builderTree();
createActions();
initializeToolBar();
initializeMenu();
}
public void builderTree() {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText("导航");
item.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER));
TreeItem treeitem1 = new TreeItem(item, SWT.NONE);
treeitem1.setText("模块一");
treeitem1.setData(t1.ID);
treeitem1.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER));
TreeItem treeitem2 = new TreeItem(item, SWT.NONE);
treeitem2.setText("模块二");
treeitem2.setData(t2.ID);
treeitem2.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER));
TreeItem treeitem3 = new TreeItem(item, SWT.NONE);
treeitem3.setText("模块三");
treeitem3.setData(t3.ID);
treeitem3.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER));
item.setExpanded(true);
}
/**
* Create the actions
*/
private void createActions() {
// Create the actions
}
/**
* Initialize the toolbar
*/
private void initializeToolBar() {
IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
}
/**
* Initialize the menu
*/
private void initializeMenu() {
IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
}
@Override
public void setFocus() {
// Set the focus
}
}
下面是另外三个VIEW
package org.hermit.pojotools.tt;
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.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;
public class t1 extends ViewPart {
public static final String ID = "org.hermit.pojotools.tt.t1"; //$NON-NLS-1$
/**
* Create contents of the view part
* @param parent
*/
@Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
final Label t1Label = new Label(container, SWT.NONE);
t1Label.setText("这是模块一");
t1Label.setBounds(29, 27, 74, 12);
//
createActions();
initializeToolBar();
initializeMenu();
}
/**
* Create the actions
*/
private void createActions() {
// Create the actions
}
/**
* Initialize the toolbar
*/
private void initializeToolBar() {
IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
}
/**
* Initialize the menu
*/
private void initializeMenu() {
IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
}
@Override
public void setFocus() {
// Set the focus
}
}
package org.hermit.pojotools.tt;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
public class t2 extends ViewPart ...{
public static final String ID = "org.hermit.pojotools.tt.t2"; //$NON-NLS-1$
/** *//**
* Create contents of the view part
* @param parent
*/
@Override
public void createPartControl(Composite parent) ...{
Composite container = new Composite(parent, SWT.NONE);
final Button button = new Button(container, SWT.NONE);
button.setText("这是模块二");
button.setBounds(10, 10, 72, 22);
//
createActions();
initializeToolBar();
initializeMenu();
}
/** *//**
* Create the actions
*/
private void createActions() ...{
// Create the actions
}
/** *//**
* Initialize the toolbar
*/
private void initializeToolBar() ...{
IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
}
/** *//**
* Initialize the menu
*/
private void initializeMenu() ...{
IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
}
@Override
public void setFocus() ...{
// Set the focus
}
}
package org.hermit.pojotools.tt;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
public class t3 extends ViewPart {
private CCombo combo;
public static final String ID = "org.hermit.pojotools.tt.t3"; //$NON-NLS-1$
/**
* Create contents of the view part
* @param parent
*/
@Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
combo = new CCombo(container, SWT.NONE);
combo.setText("这是模块三");
combo.setBounds(42, 25, 84, 17);
//
createActions();
initializeToolBar();
initializeMenu();
}
/**
* Create the actions
*/
private void createActions() {
// Create the actions
}
/**
* Initialize the toolbar
*/
private void initializeToolBar() {
IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
}
/**
* Initialize the menu
*/
private void initializeMenu() {
IMenuManager menuManager = getViewSite().getActionBars().getMenuManager();
}
@Override
public void setFocus() {
// Set the focus
}
}
大功告成!
posted on 2006-12-22 11:10
交口称赞 阅读(4610)
评论(7) 编辑 收藏 所属分类:
Eclipse RCP SWT