实现RCP自身的控制台

一、解决基本问题:

在做 RCP 项目的时候经常会遇到一个问题,就是要将一些控制信息输出到 RCP 自身的控制台,那么我们就可以扩展 Eclipse 扩展点 org.eclipse.ui.console.consoleFactories ,来实现我们自己的控制台,解决方法如下:

首先 ,在 plugin.xml 中定义扩展点:

plugin.xml:
<extension
    point="org.eclipse.ui.console.consoleFactories">
    <consoleFactory
        class="com.hnjchina.intro.ConsoleFactory"
        label=" 控制台 "/>
</extension>

其次 ,在 perspective 中加入 console View ,作为控制信息的控制台( console ):

在 Perspective .java 类中的 Public void createInitialLayout(IPageLayout layout) 方法中加入如下:

 ConsoleFactory cf  =   new  ConsoleFactory();

layout.addView(IConsoleConstants.ID_CONSOLE_VIEW, IPageLayout.BOTTOM,0.70f, layout.getEditorArea());

cf.openConsole();

最后 ,自定义 ConsoleFactory 类,主要实现 showConsole() 方法,然后在要输出信息的地方定义 printer 变量如下:

private  MessageConsoleStream  printer =ConsoleFactory.console.newMessageStream();

自定义的 ConsoleFactory 类具体代码如下:

 

Java代码   收藏代码
  1. package  com.essp.eseai.maptool.perspective.views;  
  2.   
  3. import  java.io.IOException;  
  4. import  java.io.PrintStream;  
  5. import  org.eclipse.ui.console.ConsolePlugin;  
  6. import  org.eclipse.ui.console.IConsole;  
  7. import  org.eclipse.ui.console.IConsoleFactory;  
  8. import  org.eclipse.ui.console.IConsoleManager;  
  9. import  org.eclipse.ui.console.MessageConsole;  
  10. import  org.eclipse.ui.console.MessageConsoleStream;  
  11.   
  12. /** */ /**  
  13. * 描述:样式显示控制台视图  
  14. * */   
  15. public   class  ConsoleViewPart  implements  IConsoleFactory ...{  
  16.      
  17.    private   static  MessageConsole console =  new  MessageConsole( "样式显示窗口"null );  
  18.      
  19.    /** */ /**  
  20.     * 描述:打开控制台  
  21.     * */   
  22.    public   void  openConsole() {  
  23.        showConsole();  
  24.    }  
  25.          
  26.    /** */ /**  
  27.     * 描述:显示控制台  
  28.     * */       
  29.    public   static   void  showConsole() {  
  30.        try  ...{  
  31.            if  (console !=  null ) {  
  32.                //得到默认控制台管理器   
  33.                IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();  
  34.                //得到所有的控制台实例   
  35.                IConsole[] existing = manager.getConsoles();  
  36.                boolean  exists =  false ;  
  37.                //新创建的MessageConsole实例不存在就加入到控制台管理器,并显示出来   
  38.                for  ( int  i =  0 ; i < existing.length; i++) {  
  39.                    if  (console == existing[i])  
  40.                        exists = true ;  
  41.                }      
  42.                if (!exists)...{  
  43.                    System.out.println("aaaaaaaa" );  
  44.                    manager.addConsoles(new  IConsole[] { console });  
  45.                }  
  46.                manager.showConsoleView(console);  
  47.                MessageConsoleStream stream = console.newMessageStream();  
  48.                stream.write("测试!" );  
  49.                System.setOut(new  PrintStream(stream));  
  50.            }  
  51.        } catch  (IOException e) {  
  52.            e.printStackTrace();  
  53.        }  
  54.       }  
  55.      
  56.    /** */ /**  
  57.     * 描述:关闭控制台  
  58.     * */     
  59.    public   static   void  closeConsole() {  
  60.        IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();  
  61.        if  (console !=  null ) {  
  62.            manager.removeConsoles(new  IConsole[] { console });  
  63.        }  
  64.    }  
  65.   
  66.    public   static  MessageConsole getConsole() ...{  
  67.        return  console;  
  68.    }  
  69.   
  70. }  

 二、总结:

重用 Console 有两种办法:
1 、作为组件来重用:
 //getConsole 就是 new MessageConsole("", null);

 mainConsole = ConsoleFactory.getConsole();

 mainTab = new TabItem(tabFolder, SWT.NONE);

TextConsoleViewer tcv = new TextConsoleViewer(tabFolder, mainConsole);

 mainTab.setText(" 主线程 ");

 mainTab.setControl(tcv.getControl());

 MessageConsoleStream printer = mainConsole.newMessageStream();

 printer.setColor(Display.getCurrent() .getSystemColor(SWT.COLOR_BLACK));
 

 ConsoleFactory.java :

 public static MessageConsole getConsole() {
       return new MessageConsole("", null);
 }

2 、作为 view 重用:
<extension
      id="Hapcent.ConsoleFactory"
      name="Console Factory"
      point="org.eclipse.ui.console.consoleFactories">
    <consoleFactory
        class="edu.fudan.hapcent.UI.ConsoleFactory"
        icon="icons/sample2.gif"
        label="Hapcent.consoleFactory"/>

</extension>
 
ConsoleFactory.java :
关键是一个方法:
public void openConsole() {
       IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();
       IConsole[] existing = manager.getConsoles();
       boolean exists = false;
       for (int i = 0; i < existing.length; i++) {

               if (console == existing)
                   exists = true;
       }
       if (!exists) {

               manager.addConsoles(new IConsole[] { console });
       }
       manager.showConsoleView(console );

 }

三、经常遇到的错误:

在显示视图时遇到如下错误:

java.lang.NoClassDefFoundError: org/eclipse/ui/console/IConsoleFactory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
……

<!-- [endif]-->

首先,应该检查在上图位置的地方是否加入需要的插件,即: org.eclipse.ui.console

2 .在 Dependences 里面已经加入了运行需要的包,但是将 RCP 作为一个 eclipse 项目来运行时还是报错,而在项目的 .product 文件里先配置好运行需要用到的包,然后用 launch the product 选项 test 项目,可以运行:

这是运行配置的问题,作为项目运行的时候是使用的以前的配置,而那个配置里面没有添加这个包。同样道理,如果以后你再添加了其他的包而没有在你现在 .product 文件中添加的话,它同样不会自动添加,所以也会出问题。所以这是应该看看你运行时的这个地方有没有勾上 org.eclipse.ui.console ,如下图:

另外,在项目的 .product 文件里有一个 Synchronize, 点击这里可以同步你的运行配置

3 .当显示 Eclipse 自身的控制台后,状态栏的内容没有了:

分析:调用 IStatusLineManager.setMessage(String message) 会将信息写入 Eclipse 状态栏的共享区域,其他的插件例如 Console 也可以对这个区域进行修改,这样当第一次选中 Console 时,原有状态栏中的信息就会被清除。
        解决:解决的方法是自己实现一个 IContributionItem ,并在 fill(Composite parent) 方法中定义其布局,然后在 ActionBarAdvisor.fillStatusLine(IStatusLineManager statusLine) 中调用 statusLine.add(IContributionItem item) 将其加入 Eclipse 的状态栏即可。

4如何默认就是自己实现的控制台,通常情况下默认的控制台不是自己扩展的那个,而非得在视图里切换一下才行,还有就是能否把控制台视图里的那些ACTION不显示出来?

         解决:
调用openConsole() 就可以默认显示你自己扩展的控制台了

四、自定义控制台,向其输出 RCP 中的一些实时信息:

 

 

Java代码   收藏代码
  1. public   class  SystemInfoView  extends  ViewPart ...{  
  2.   
  3.        public   static   final  String ID =  "com.winscad.view.SystemInfoView" ;  
  4.   
  5.        String strGetRespone = "" ;  
  6.   
  7.        Text textSysInfo;      
  8.   
  9.        /** */ /**  
  10.  
  11.         * Create contents of the view part  
  12.  
  13.         * @param parent  
  14.  
  15.         */   
  16.   
  17.        @Override   
  18.   
  19.        public   void  createPartControl(Composite parent) ...{  
  20.   
  21.               final  Composite container =  new  Composite(parent, SWT.NONE);  
  22.   
  23.               //设置面板布局   
  24.   
  25.               container.setLayout(new  FillLayout());  
  26.   
  27.               //创建带有水平和垂直滚动条的文本框   
  28.   
  29.               textSysInfo = new  Text(container,SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);  
  30.   
  31.               //设置文本框的滚动条一直处于最下端   
  32.   
  33.               textSysInfo.setTopIndex(0 );            
  34.   
  35.               final  Timer timer =  new  Timer( true );  
  36.   
  37.               //设置每隔1秒去读一次业务返回的响应数据,并循环显示(刷新)   
  38.   
  39.              timer.scheduleAtFixedRate(new  TimerTask()...{  
  40.   
  41.                      public   void  run() ...{  
  42.   
  43.                             Display.getDefault().asyncExec(new  Runnable()...{  
  44.   
  45.                                    public   void  run() ...{  
  46.   
  47.    
  48.   
  49.                                    }  
  50.   
  51.                             });  
  52.   
  53.                      }}, 6 * 10001 * 1000 );  
  54.             
  55.   
  56.               createActions();  
  57.   
  58.               initializeToolBar();  
  59.   
  60.               initializeMenu();  
  61.   
  62.        }      
  63.   
  64.        /** */ /**  
  65.  
  66.         * Create the actions  
  67.  
  68.         */   
  69.   
  70.        private   void  createActions() ...{  
  71.   
  72.               // Create the actions   
  73.   
  74.        }  
  75.   
  76.    
  77.   
  78.        /** */ /**  
  79.  
  80.         * Initialize the toolbar  
  81.  
  82.         */   
  83.   
  84.        private   void  initializeToolBar() ...{  
  85.   
  86.               IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();  
  87.   
  88.               Action deleteAction = new  Action()...{  
  89.   
  90.                      public   void  run()...{  
  91.   
  92.                             textSysInfo.setText("" );  
  93.   
  94.                      }  
  95.   
  96.               };  
  97.   
  98.               deleteAction.setText(Message.getString("ParameterView.Clear" )); //清空   
  99.   
  100.               deleteAction.setToolTipText(Message.getString("ParameterView.ClearSystem" )); //清空系统信息   
  101.   
  102.               deleteAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().  
  103.   
  104.                                      getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));  
  105.   
  106.               toolbarManager.add(deleteAction);  
  107.   
  108.               //为ToolBarManager添加自定义控件   
  109.   
  110.               ComboContribution combo = new  ComboContribution( "Combo.contribution" );  
  111.   
  112.               toolbarManager.add(combo);  
  113.   
  114.               toolbarManager.add(new  ComboContribution2());  
  115.   
  116.        }  
  117.   
  118.         
  119.   
  120.        //自定义控件   
  121.   
  122.        class  ComboContribution  extends  ControlContribution...{  
  123.   
  124.            public  ComboContribution(String id)...{  
  125.   
  126.                super (id);  
  127.   
  128.            }  
  129.   
  130.            @Override   
  131.   
  132.            protected  Control createControl(Composite parent)...{  
  133.   
  134.                Combo combo = new  Combo(parent, SWT.READ_ONLY);  
  135.   
  136.                combo.setItems(new  String[]...{  "First""Secend""Third"  });  
  137.   
  138.                combo.addSelectionListener(new  SelectionListener()...{  
  139.   
  140.                             public   void  widgetDefaultSelected(SelectionEvent e) ...{  
  141.   
  142.                                    // TODO Auto-generated method stub                                 
  143.   
  144.                             }  
  145.   
  146.                             public   void  widgetSelected(SelectionEvent e) ...{  
  147.   
  148.                                    // TODO Auto-generated method stub   
  149.   
  150.                                    textSysInfo.append("View工具栏测试!" );  
  151.   
  152.                             }                     
  153.   
  154.                });  
  155.   
  156.                return  combo;  
  157.   
  158.            }  
  159.   
  160.        }  
  161.   
  162.        //自定义控件   
  163.   
  164.         class  ComboContribution2  extends  ContributionItem...{       
  165.   
  166.                private  ToolItem toolitem;  
  167.   
  168.                private  Combo fFindCombo;  
  169.   
  170.                private  Button upFindbutton;  
  171.   
  172.                private  Button downFindbutton;  
  173.   
  174.                private  Button allFindbutton;  
  175.   
  176.    
  177.   
  178.                public  ComboContribution2() ...{  
  179.   
  180.                    super ();  
  181.   
  182.                }                    
  183.   
  184.                protected  Control createControl(Composite parent) ...{  
  185.   
  186.                      
  187.   
  188.                    Composite composite = new  Composite(parent, SWT.NONE);                  
  189.   
  190.                    //查询框   
  191.   
  192.                    fFindCombo = new  Combo(composite,SWT.NONE);  
  193.   
  194.                    fFindCombo.setLocation(02 );  
  195.   
  196.                    fFindCombo.setSize(130 , 20 );  
  197.   
  198.                    System.out.println(" fFindCombo == "  + fFindCombo.getBounds());  
  199.   
  200.                    //向上查   
  201.   
  202.                    upFindbutton = new  Button(composite, SWT.NONE);  
  203.   
  204.                    upFindbutton.setLocation(1352 );  
  205.   
  206.                    upFindbutton.setSize(30 , 20 );  
  207.   
  208.                    upFindbutton.setText("上查" );     
  209.   
  210.                    upFindbutton.addSelectionListener(new  SelectionListener()...{  
  211.   
  212.                        public   void  widgetDefaultSelected(SelectionEvent e) ...{  
  213.   
  214.                            // TODO 自动生成方法存根   
  215.   
  216.                             
  217.   
  218.                        }  
  219.   
  220.                        public   void  widgetSelected(SelectionEvent e) ...{  
  221.   
  222.                            fFindCombo.add(fFindCombo.getText());  
  223.                            
  224.   
  225.                        }                      
  226.   
  227.                    });  
  228.   
  229.                    System.out.println(" upFindbutton == "  + upFindbutton.getBounds());  
  230.   
  231.                    //向下查   
  232.   
  233.                    downFindbutton = new  Button(composite, SWT.NONE);  
  234.   
  235.                    downFindbutton.setLocation(1702 );  
  236.   
  237.                    downFindbutton.setSize(30 , 20 );  
  238.   
  239.                    downFindbutton.setText("下查" );  
  240.   
  241.                    //全部查询   
  242.   
  243.                    allFindbutton = new  Button(composite, SWT.NONE);  
  244.   
  245.                    allFindbutton.setLocation(2052 );  
  246.   
  247.                    allFindbutton.setSize(30 , 20 );  
  248.   
  249.                    allFindbutton.setText("全部" );   
  250.   
  251.                    toolitem.setWidth(240 );  
  252.   
  253.                    return  composite;  
  254.   
  255.                }  
  256.   
  257.                public   void  fill(ToolBar parent,  int  index) ...{  
  258.   
  259.                    toolitem = new  ToolItem(parent, SWT.SEPARATOR, index);  
  260.   
  261.                    Control control = createControl(parent);  
  262.   
  263.                    toolitem.setControl(control);  
  264.   
  265.                }  
  266.   
  267.            }  
  268.   
  269.        /** */ /**  
  270.  
  271.         * Initialize the menu  
  272.  
  273.         */   
  274.   
  275.        private   void  initializeMenu() ...{  
  276.   
  277.               IMenuManager menuManager = getViewSite().getActionBars()  
  278.   
  279.                             .getMenuManager();  
  280.   
  281.        }  
  282.   
  283.        @Override   
  284.   
  285.        public   void  setFocus() ...{  
  286.   
  287.               // Set the focus   
  288.   
  289.        }  
  290.   
  291.        public  String getStrGetRespone() ...{  
  292.   
  293.               return  strGetRespone;  
  294.   
  295.        }  
  296.   
  297.        public   void  setStrGetRespone(String strGetRespone) ...{  
  298.   
  299.               this .strGetRespone = strGetRespone;  
  300.   
  301.        }   
  302.   
  303. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值