實現RCP自身的控制檯

轉自 http://cai555.iteye.com/blog/469777

一、解決基本問題:

在做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. }  
package com.essp.eseai.maptool.perspective.views;import java.io.IOException;import java.io.PrintStream;import org.eclipse.ui.console.ConsolePlugin;import org.eclipse.ui.console.IConsole;import org.eclipse.ui.console.IConsoleFactory;import org.eclipse.ui.console.IConsoleManager;import org.eclipse.ui.console.MessageConsole;import org.eclipse.ui.console.MessageConsoleStream;/** *//*** 描述:樣式顯示控制檯視圖* */public class ConsoleViewPart implements IConsoleFactory ...{      private static MessageConsole console = new MessageConsole("樣式顯示窗口", null);      /** *//**    * 描述:打開控制檯    * */   public void openConsole() {       showConsole();   }          /** *//**    * 描述:顯示控制檯    * */       public static void showConsole() {       try ...{           if (console != null) {               //得到默認控制檯管理器               IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();               //得到所有的控制檯實例               IConsole[] existing = manager.getConsoles();               boolean exists = false;               //新創建的MessageConsole實例不存在就加入到控制檯管理器,並顯示出來               for (int i = 0; i < existing.length; i++) {                   if (console == existing[i])                       exists = true;               }                   if(!exists)...{                   System.out.println("aaaaaaaa");                   manager.addConsoles(new IConsole[] { console });               }               manager.showConsoleView(console);               MessageConsoleStream stream = console.newMessageStream();               stream.write("測試!");               System.setOut(new PrintStream(stream));           }       } catch (IOException e) {           e.printStackTrace();       }      }      /** *//**    * 描述:關閉控制檯    * */     public static void closeConsole() {       IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager();       if (console != null) {           manager.removeConsoles(new IConsole[] { console });       }   }   public static MessageConsole getConsole() ...{       return console;   }}

 二、總結:

重用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*1000, 1*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(0, 2);  
  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(135, 2);  
  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(170, 2);  
  236.   
  237.                    downFindbutton.setSize(30,20);  
  238.   
  239.                    downFindbutton.setText("下查");  
  240.   
  241.                    //全部查詢  
  242.   
  243.                    allFindbutton = new Button(composite, SWT.NONE);  
  244.   
  245.                    allFindbutton.setLocation(205, 2);  
  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.   



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章