??xml version="1.0" encoding="utf-8" standalone="yes"?> 原来是设|了q个D?/p> configurer.setSaveAndRestore(true);
解决Ҏ:
代码如下:
一、同时关闭所有打开的view
在view?只有一个close,每次只能关闭当前H口
在eclipse的编辑窗口的标题上点右键,里面?close All
可以使用遍历的方法,遍历所有打开的ViewQ然后将光藏:
AsyncUtil.asyncExec(new Runnable()
{
public void run()
{
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IViewReference[] iViewReferences = page.getViewReferences();
for (IViewReference iViewReference: iViewReferences)
if (!ID.equals(iViewReference.getId()))
page.hideView(iViewReference);
}
});
二、RCP中生目标区的Bar
产生U线区域的Bar,实现Ҏ如下Q?br />
1、这个是perspectiveQ要创徏多个perspectiveQ然后设|某个ؓactive
2、在WorkbenchWindowAdvisor中preWindowOpen内,加下面这D代?br />
public void preWindowOpen() {
IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
apiStore.setValue(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR,
IWorkbenchPreferenceConstants.TOP_RIGHT);
apiStore.setValue(
IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS,
false);
}
3、需要加 config.setShowPespectivebar(true);
三、RCP目Q-H口关闭事g
在ApplicationWorkbenchWindowAdvisor中override
public boolean preWindowShellClose() {
// do nothing, but allow the close() to proceed
MessageBox msgBox = new MessageBox(new Shell(), SWT.YES|SWT.NO|SWT.ICON_QUESTION);
msgBox.setText("退出系l?);
msgBox.setMessage("定退出系l?");
if(msgBox.open()==SWT.YES){
return true;
}
return false;
}
以后使用IWorkbenchWindow的getShell().close();都会调用该处代码.
四、在RCP中设计界面适合桌面大小
final int screenWidth = Display.getCurrent().getBounds().width;
final int screenHeight = Display.getCurrent().getBounds().height;
五、Treeviewer中通过代码Q选中树上的某个node节点
StructuredSelection sel=new StructuredSelection(node);
tree.setSelected(sel);
六、TreeViewer实现双击展开、关?nbsp;
private class DoubleEventPro implements MouseListener {
public void mouseDoubleClick(MouseEvent e) {
TreeItem item = viewer.getTree().getItem(new Point(e.x, e.y));
if (item != null && item.getItem(0).getText().trim().length() == 0) {
viewer.expandToLevel(item.getData(), 1);
return;
}
if (item != null && item.getItemCount() > 0
&& item.getItem(0).getText().trim().length() > 0) {
item.setExpanded(!item.getExpanded());
}
}
public void mouseDown(MouseEvent e) {
}
public void mouseUp(MouseEvent e) {
}
}
七?如何屏蔽掉视囄口上的右键弹单?
Ҏ一Q在 postWindowOpen() 中执行下面语?
PlatformUI.getWorkbench().getDisplay().addFilter(SWT.MouseUp, new Listener() {
public void handleEvent(final Event event) {
if(event.widget == your editor && event.button == 3) {
int hwndCursor = OS.GetCapture ();
OS.PostMessage(hwndCursor, OS.WM_LBUTTONDOWN, hwndCursor, OS.HTCLIENT | (OS.WM_MOUSEMOVE << 16));
}
}
});
其它Qorg.eclipse.ui.internal.presentations.util包中的StandardViewSystemMenucL制着q些菜单Q还没来得急仔l研IӞ研究q后再补?/font>
八、在使用tableviewer的时候导入大量数据的问题
创徏了一个tableviewerQ然后用setInputҎ导入数据Q如果数据量很大的话Ҏ无效率可言了,以下是几U解决的ҎQ?/font>
Ҏ一Q通过Ud滚动条来辑ֈ自动加蝲的目的,在滚动条的事件中加入页的代码,可以参考《Eclipse从入门到_N》第二版的P383
Ҏ二:分页昄Q?/font>http://www.eclipseworld.org/bbs/read-cec-tid-11678-keyword-table.htmlQ?/font>
Ҏ三:使用 Virtual TablesQJFace3.2的Viewer已经支持SWT.VIRTUAL样式
http://www.eclipse.org/articles/Article-SWT-Virtual/Virtual-in-SWT.html
1 int COUNT = 10000;
2 final String [] itemStrings = new String [COUNT];
3 for (int i = 0; i < COUNT; i++) {
4 itemStrings = "item " + i;
5 }
6 final Table table = new Table(parent, SWT.BORDER | SWT.VIRTUAL);
7 table.addListener(SWT.SetData, new Listener() {
8 public void handleEvent(Event event) {
9 TableItem item = (TableItem)event.item;
10 int index = event.index;
11 item.setText(itemStrings [index]);
12 }
13 });
14 table.setItemCount(COUNT);
九、在透视囑ֿh式栏中同时显C多个透视囑ֿh?/font>
如果在程序中做了多个个透视图,默认只显C初始透视囑ֿh式,每次都要 打开透视䏀—otherQ特ȝQ所以,一下提供两U方法:
1、在切换栏中昄
public class PIMWorkbenchAdvisor extends WorkbenchAdvisor {
@Override
public void postStartup() {
super.postStartup();
IWorkbenchWindow activeWorkbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
//2007.01.11 讄同时昄多个透视图标
PerspectiveBarManager barManager=((WorkbenchWindow)activeWorkbenchWindow).getPerspectiveBar();
if(barManager != null){
IPerspectiveDescriptor mailPerspective =
WorkbenchPlugin.getDefault().getPerspectiveRegistry
().findPerspectiveWithId("MyWork_mail.perspective");
PerspectiveBarContributionItem item=new
PerspectiveBarContributionItem(mailPerspective,activeWorkbenchWindow.getActivePage());
barManager.addItem(item);
}
}
2、在下拉框中(shortcut)昄
public class UiPerspective implements IPerspectiveFactory
{
public void createInitialLayout(IPageLayout layout){
....
//增加透视?br />
layout.addPerspectiveShortcut("net.sf.pim.plugin.UiPerspective");
layout.addPerspectiveShortcut("MyWork_mail.perspective");
}
}
十、控?#8220;最q打开文档”的个?br />
RCP中在l承ActionBarAdvisor的类中定?
private IContributionItem reOpenAction = ContributionItemFactory.REOPEN_EDITORS.create(window);
然后在fillMenuBar(IMenuManager menuBar)Ҏ中添加上面的aciton
q行?#8220;最q打开的文?#8221;只有4个,如果惌己控?#8220;最q打开的文?#8221;的数量,则设|一下Workbench中的初始化首选项时RECENT_FILES的默认参数值如Q?br />
WorkbenchPlugin.getDefault().getPreferenceStore().setDefault(IPreferenceConstants.RECENT_FILES,10);
十一、设|Eclipse RCPE序的外观和首选项
RCP应用E序的缺省外观是一个空白窗口,一般我们要通过一个WorkbenchAdvisorcd界面q行定制?
WorkbenchAdvisor有很多回调方法,可以在preWindowOpen()Ҏ里设|菜单、工h、状态栏、进度栏、透视囑ֈ换工h否可
见,在fillActionBars()Ҏ里添加菜单和工具条项Q在getInitialWindowPerspectiveId()Ҏ里指定首选的
透视图?/font>
~省情况下,透视囑ֈ换工具位于窗口左上角Q在Eclipse里可以通过Window->Preferences-> Workbench->Appearance改变它的位置Q那么怎样用程序控制它呢?有两个方法,W一个是使用如下代码讄 IPreferenceStore中的变量Q?/font>
IPreferenceStore apiStore = PrefUtil.getAPIPreferenceStore();
apiStore.setValue(IWorkbenchPreferenceConstants.DOCK_PERSPECTIVE_BAR, IWorkbenchPreferenceConstants.TOP_RIGHT);
另一个方法是在plugin所在目录徏一个名为plugin_customization.ini的文Ӟ里面写如下内容:
your.plugin.id/DOCK_PERSPECTIVE_BAR=topRight
其他与plugin相关的Preference值可以用同样Ҏ讄?/font>
Update:在最新的Eclipse 3.1M5a版本中,对RCP应用E序菜单和工h的定制方法有所改变Q应该用新加入的ActionBarAdvisorcL完成此项工作?/font>
十二、获得自己开发的plugin被安装的目录
/** *//**
* @return 本插件的安装路径
*/
public String getInstallDir() ...{
if (installPath == null) ...{
URL localUrl = null;
try ...{
localUrl = FileLocator.toFileURL(getDefault().getBundle()
.getEntry("/"));
installPath = localUrl.getFile().substring(1);
} catch (Exception e) ...{
log(e);
}
}
return installPath;
}
轉自http://duguanglong002.blog.163.com/blog/static/26955626200955415545/
首先要加入Eclipse的語a包,官方有提?a >www.eclipse.org注意適用的版本?br />
要切換不同的界面語言Q只需的快捷鏈接中加入參數Q?br />
-nl "zh_CN" 體中文
-nl "zh_TW" J體中文
-nl "en_US" 英文
C:\Program Files\eclipse\eclipse.exe -nl "en_US"
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
public class HookSysTray {
private TrayItem trayItem;
public HookSysTray() {
}
public void createSysTray(final IWorkbenchWindow window) {
trayItem = initTrayItem(window);
if(trayItem != null) {
trayPopupMenu(window);
trayMinimize(window);
}
}
//最化到颖P
private void trayMinimize(final IWorkbenchWindow window) {
// TODO Auto-generated method stub
window.getShell().addShellListener(new ShellAdapter(){
@Override
public void shellIconified(ShellEvent e) {
// TODO Auto-generated method stub
window.getShell().setVisible(false);
}
});
trayItem.addListener(SWT.Selection, new Listener(){
public void handleEvent(Event event) {
// TODO Auto-generated method stub
Shell shell = window.getShell();
if(!shell.isVisible()) {
shell.setVisible(true);
window.getShell().setMinimized(false);
}
}
});
}
//颖P?
private void trayPopupMenu(final IWorkbenchWindow window) {
// TODO Auto-generated method stub
trayItem.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
// TODO Auto-generated method stub
MenuManager trayMenu = new MenuManager();
Menu menu = trayMenu.createContextMenu(window.getShell());
fillTrayItem(trayMenu, window);
menu.setVisible(true);
}
});
}
//造颖P菜雾
private void fillTrayItem(IMenuManager trayMenu, final IWorkbenchWindow window) {
Action exitSystem = new Action("退出系i[&E]",
AbstractUIPlugin.imageDescriptorFromPlugin(
IAppConstants.APPLICATION_ID, IImagekey.EXIT_STSTEN)) {
@Override
public void run() {
// TODO Auto-generated method stub
PlatformUI.getWorkbench().close();
}
};
trayMenu.add(exitSystem);
}
//初始?br />
private TrayItem initTrayItem(IWorkbenchWindow window) {
// TODO Auto-generated method stub
final Tray tray = window.getShell().getDisplay().getSystemTray();
if(tray == null)
return null;
trayItem = new TrayItem(tray, SWT.NONE);
trayItem.setImage(CacheImage.getInstance().getImage(
IAppConstants.APPLICATION_ID, IImagekey.WINDOW_IMAGE));
trayItem.setToolTipText(IAppConstants.APPLICATION_TITLE);
return trayItem;
}
//最化E式H口
public void windowMinimized(final Shell shell) {
shell.setMinimized(true);
shell.setVisible(false);
}
//
public void disponse() {
if(trayItem != null)
trayItem.dispose();
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
public class CacheImage {
private final Map<String, Image> imageMap = new HashMap<String, Image>();
private static CacheImage INSTANCE = null;
private CacheImage(){
}
//單例
public static CacheImage getInstance() {
if(INSTANCE == null)
INSTANCE = new CacheImage();
return INSTANCE;
}
//獲得圖片
public Image getImage(String applicationId, String imageName) {
if(imageName == null)
return null;
Image image = imageMap.get(imageName);
if(image == null) {
image = AbstractUIPlugin.imageDescriptorFromPlugin(applicationId,
imageName).createImage();//應用E序的IDQ圖片的路徑
imageMap.put(imageName, image);
}
return image;
}
//dispose資源
public void dispose() {
Iterator<Image> iterator = imageMap.values().iterator();
while(iterator.hasNext())
iterator.next().dispose();
imageMap.clear();
}
}
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.views.IViewDescriptor;
public class ShowViewAction extends Action implements IWorkbenchAction {
private IWorkbenchWindow workbenchWindow;
private IViewDescriptor desc;
public ShowViewAction(IWorkbenchWindow window, IViewDescriptor desc) {
super("");
/** 获得视图的名U?*/
String label = desc.getLabel();
/** 讄操作的名U?*/
setText(label);
/** 讄操作的图标ؓ视图的图?*/
setImageDescriptor(desc.getImageDescriptor());
/** 讄操作的提C文?*/
setToolTipText(label);
setId("ShowView" + desc.getId());
this.workbenchWindow = window;
this.desc = desc;
}
@Override
public void run() {
/** 获得当前工作区及获得工作面 */
IWorkbenchPage page = workbenchWindow.getActivePage();
/** 如果面不ؓnull */
if (page != null) {
try {
/** 昄视图 */
page.showView(desc.getId());
} catch (PartInitException e) {
ErrorDialog.openError(workbenchWindow.getShell(), "打开视图错误
Q?, e.getMessage(), e.getStatus());
}
}
}
@Override
public void dispose() {
workbenchWindow = null;
}
}
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.views.IViewDescriptor;
public class ActionManager {
public static IWorkbenchAction createHelloWorldAction(IWorkbenchWindow window) {
if(window == null)
throw new IllegalArgumentException();
IWorkbenchAction helloWorldAction = new HelloWorldAction(window);
return helloWorldAction;
}
/**获得plugin.xml文g中配|的视图信息*/
public static IWorkbenchAction createShowViewAction(IWorkbenchWindow window, String viewId) {
if(window == null)
throw new IllegalArgumentException();
IViewDescriptor desc = window.getWorkbench().getViewRegistry().find(viewId);
IWorkbenchAction action = new ShowViewAction(window, desc);
return action;
}
}
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
private TrayItem trayItem;//pȝ托盘对象
private Image trayImage;//pȝ托盘图标对象
/** E序的菜单条 */
private ApplicationActionBarAdvisor actionBarAdvisor;
public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
/** 创徏菜单条对?*/
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
actionBarAdvisor = new ApplicationActionBarAdvisor(configurer);
return actionBarAdvisor;
}
/** 打开H口前调用该ҎQ对H口初始化设|?*/
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(600, 400));
configurer.setShowCoolBar(true);//工具?br />
configurer.setShowStatusLine(false);//状态栏
configurer.setShowPerspectiveBar(true);//透视?br />
configurer.setShowProgressIndicator(true);//q度?br />
final IWorkbenchWindow window = super.getWindowConfigurer().getWindow();
/** 创徏pȝ托盘 */
trayItem = initTrayItem(window);
/** 如果支持pȝ托盘Q则创徏托盘的菜?*/
if(trayItem != null) {
createPopupMenu(window);
}
}
/**
* 创徏pȝ托盘菜单
*
* @param window
* 工作台窗口对?br />
*/
private void createPopupMenu(final IWorkbenchWindow window) {
trayItem.addListener(SWT.MenuDetect, new Listener() {
@Override
public void handleEvent(Event event) {
MenuManager trayMenu = new MenuManager();
Menu menu = trayMenu.createContextMenu(window.getShell());
/**
* 调用fillTrayItemҎ创徏pȝ托盘对象Q可以直接利用菜单栏中的操作 而不需要,重新创徏操作
*/
actionBarAdvisor.fillTrayItem(trayMenu);
menu.setVisible(true);
}
});
}
/**
* 初始化系l托盘对?br />
*
* @param window
* 工作台窗口对?br />
* @return 该程序所对应的系l托盘对?br />
*/
private TrayItem initTrayItem(IWorkbenchWindow window) {
final Tray tray = Display.getCurrent().getSystemTray();
if(tray == null)
return null;
TrayItem trayItem = new TrayItem(tray, SWT.NONE);
trayImage = Activator.getImageDescriptor("icons/logo.gif").createImage();
trayItem.setImage(trayImage);
trayItem.setToolTipText("System Tray");
return trayItem;
}
@Override
public void postWindowOpen() {
//H口居中昄
Shell shell = getWindowConfigurer().getWindow().getShell();
Rectangle screenSize = Display.getDefault().getClientArea();
Rectangle frameSize = shell.getBounds();
shell.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
}
/** 释放H口Q释攄l托?*/
public void dispose() {
if (trayImage != null) {
trayImage.dispose();
trayItem.dispose();
}
}
}