RCP开发中的一些小技艺
RCP开发中的一些小技巧
public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(400, 300)); configurer.setShowCoolBar(false); //设置状态栏 IStatusLineManager statusLine=getWindowConfigurer().getActionBarConfigurer().getStatusLineManager(); statusLine.setMessage("自定义插件的状态栏"); configurer.setShowStatusLine(true); configurer.setTitle("Hello RCP"); }
1. //屏蔽视图上的右键菜单 2. public void postWindowOpen() { 3. PlatformUI.getWorkbench().getDisplay().addFilter(SWT.MouseUp, 4. new Listener() { 5. public void handleEvent(final Event event) { 6. if (event.button == 3&&event.widget == your editor ) { 7. int hwndCursor = OS.GetCapture(); 8. OS.PostMessage(hwndCursor, OS.WM_LBUTTONDOWN, 9. hwndCursor, OS.HTCLIENT 10. | (OS.WM_MOUSEMOVE << 16)); 11. } 12. } 13. }); 14. }
(1)更改TitleAreaDialog窗口的大小
实现TitleAreaDialog中的constrainShellSize方法,在里面写上super.constrainShellSize();
getShell().setSize(350, 200);即可
(2)让swt中的text只能输入数字
text = new Text(group_3, SWT.BORDER); text.addKeyListener(new KeyAdapter() { public void keyPressed(final KeyEvent e) { if (!Converts.checkDate(e.keyCode)) { e.doit = false; } } }); public static boolean checkDate(int n) { if (48 == n || 49 == n || 50 == n || 51 == n || 52 == n || 53 == n || 54 == n || 55 == n || 56 == n || 57 == n) { return true; } return false; }
(3)设置TreeViewer的前景色和背景色
让TreeViewerLabelProvider实现IColorProvider中的getBackground和getForeground方法即可
(4)swt编写界面窗口时让窗口处于屏幕中间
使用SWT本身
public static void centerShell(Display display,Shell shell)...{ Rectangle displayBounds = display.getPrimaryMonitor().getBounds(); Rectangle shellBounds = shell.getBounds(); int x = displayBounds.x + (displayBounds.width - shellBounds.width)>>1; int y = displayBounds.y + (displayBounds.height - shellBounds.height)>>1; shell.setLocation(x, y); }
二、借助AWT包里面获取屏幕大小的方法
import java.awt.Toolkit; /** *//** * 在屏幕中间显示Shell * @param shell 要显示的Shell对象 */ private void centerShell(Shell shell) { //得到屏幕的宽度和高度 int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height; int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width; //得到Shell窗口的宽度和高度 int shellHeight = shell.getBounds().height; int shellWidth = shell.getBounds().width; //如果窗口大小超过屏幕大小,让窗口与屏幕等大 if(shellHeight > screenHeight) shellHeight = screenHeight; if(shellWidth > screenWidth) shellWidth = screenWidth; //让窗口在屏幕中间显示 shell.setLocation(( (screenWidth - shellWidth) / 2),((screenHeight - shellHeight) / 2) ); }
用扩展的方式添加全局快捷键,现在本文用硬编码的形式来给视图添加快捷键,也很简单:
作为示例,这里为“删除”操作增加快捷键支持。为此,需要创建hookKeybordAction()方法,并在createPartControl()调用该方法。
首先监听键盘事件
private void hookKeybordActions(){viewer.getControl().addKeyListener(new KeyAdapter(){public void keyReleased(KeyEvent event){handleKeyReleased(event);}});}
然后绑定键盘操作:
private void makeActions(){ protected void handleKeyReleased(KeyEvent event){ if (SWT.DEL == event.character && 0 == event.stateMask){deleteAction.run();}}
状态栏:
ApplicationWorkbenchWindowAdvisor类中的:
public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(400, 300)); configurer.setShowCoolBar(false); //设置状态栏 IStatusLineManager statusLine=getWindowConfigurer().getActionBarConfigurer().getStatusLineManager(); //statusLine.setMessage("自定义插件的状态栏"); // 定义StatusLineContributionItem对象 final StatusLineContributionItem statusItem = new StatusLineContributionItem(""); // 获取进度监视器,并在状态栏显示 statusLine.getProgressMonitor(); statusItem.setText("自定义插件的状态栏"); statusLine.add(statusItem); configurer.setShowStatusLine(true); configurer.setTitle("Hello RCP"); } eclipsercp开发之屏蔽视图上的右键菜单 :
Eclipse的记录日志:
- Platform.getLog(Platform.getBundle(bundleID)).log(
- new Status(serverity, bundleID, code, message, t));