隨筆 - 1  文章 - 37  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          留言簿(16)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          test

          搜索

          •  

          最新評論

          在開發一個基于Eclipse的產品應用中,類似于Eclipse控制臺的小部件是必不可少的,例如:
          1. 開發過程中需要一個控制臺來專門輸出調試信息,而在產品發布后卻不需要
          2. 需要一個控制臺用于輸出系統信息
          3. 需要一個控制臺用于輸出普通消息

          先定義個簡單的接口MConsole
          public interface MConsole {
              
              
          public MessageConsole getMessageConsole();
              
              
          public void println(String msg);

          }


          MConsoleFactory 還是需要的,就像Log4j的Logger一樣,要控制臺的時候,只需要來一條如下語句就可以了
          private static final MConsole mConsole = MConsoleFactory.CONSOLE_SYSTEM;

          public class MConsoleFactory {

              
          public static final MConsole CONSOLE_MESSAGE = new AbstractMConsole(
                      
          new MessageConsole("控制臺"null));
              
          public static final MConsole CONSOLE_SYSTEM = new AbstractMConsole(
                      
          new MessageConsole("控制臺"null));
              
          public static final MConsole DEBUG_SYSTEM = new AbstractMConsole(
                      
          new MessageConsole("控制臺"null));

              
          static{
                  
          {

                      IConsoleManager manager 
          = ConsolePlugin.getDefault()
                              .getConsoleManager();
                      
          // IConsole[] existing = manager.getConsoles();
                      manager.addConsoles(new IConsole[] {
                              CONSOLE_SYSTEM.getMessageConsole(),
                              CONSOLE_MESSAGE.getMessageConsole(),
                              DEBUG_SYSTEM.getMessageConsole() }
          );

                  }

              }


              
          private MConsoleFactory() {
              }

          }

          接下來就是AbstractMConsole了
          class AbstractMConsole implements MConsole {
              
              
          private MessageConsole console;
              
              
          private MessageConsoleStream stream= null;
              
              
          public AbstractMConsole(MessageConsole console){
                  
          this.console = console;
                  
          this.stream = console.newMessageStream();
              }

              
              
          /**
               * 這個MessageConsole應該避免暴露
               
          */

              
          public MessageConsole getMessageConsole(){
                  
          return this.console;
              }

              
          /**
               * 這里的println有很大的發揮空間
               
          */

              
          public void println(String msg){
                  StringBuffer sb 
          = new StringBuffer();
                  sb.append(
          new SimpleDateFormat("[HH:mm:ss]").format(new Date()));
                  sb.append(msg);
                  
          this.stream.println(sb.toString());
              }


          }

          上面3個部分只是在MConsoleFactory中加入了3個MessageConsole,下面的代碼就是具體的部分了
          先看看[系統消息控制臺]

          public class ConsoleView extends ViewPart {

              
          public static final String ID = "donf.yang.mir3g.ui.view.consoleView";

              
          public ConsoleView() {
              }


              @Override
              
          public void createPartControl(Composite parent) {
                  MessageConsole mainConsole 
          = MConsoleFactory.CONSOLE_SYSTEM
                          .getMessageConsole();
                  
          final TextConsoleViewer tcv = new TextConsoleViewer(parent, mainConsole);
                  
          /* 如果這個console不是在第一個lab,當切換到該lab時,如果要自動滾屏到最后一條輸出,需要加下面這行 */
                  toTopIndex(tcv);
                  tcv.addTextListener(
          new ITextListener() {
                      
          public void textChanged(TextEvent event) {
                          toTopIndex(tcv);
                      }

                  }
          );

                  
          /**
                   * 下面2行注釋的代碼也是與上面的toTopIndex具有相同的功效, 只是看起來有點別扭,因為會延遲50毫秒,具體請參照Eclipse的代碼
                   
          */

                  
          // IOConsoleViewer iov = new IOConsoleViewer(parent,mainConsole);
                  
          // iov.setAutoScroll(true);
              }


              
          /**
               * 自動滾屏
               * 
               * 
          @param tcv
               
          */

              
          private void toTopIndex(final TextConsoleViewer tcv) {
                  StyledText textWidget 
          = tcv.getTextWidget();
                  
          if (textWidget != null && !textWidget.isDisposed()) {
                      
          int lineCount = textWidget.getLineCount();
                      tcv.setTopIndex(lineCount 
          - 1);
                  }

              }


              @Override
              
          public void setFocus() {
              }


              
          /**
               * 這個才是控制臺的名字
               
          */

              
          public String getPartName() {
                  
          return "系統消息";
              }


          }

          其他2個View也是這樣的代碼了,只是名字不同

          這樣設計出來的控制臺有較好的擴展性,耦合度也較低,至于Log4的應用需要在createPartControl中加入如下代碼

          MessageConsoleStream stream = mainConsole.newMessageStream();
                  System.setOut(
          new PrintStream(stream));
                  
          try {
                      Properties props 
          = new Properties();
                      props.load(
          this.getClass().getResourceAsStream("/log4j.properties"));
                      PropertyConfigurator.configure(props);
                  }
           catch (Exception e) {

                  }

          posted on 2008-06-10 11:20 Phrancol Yang 閱讀(1264) 評論(2)  編輯  收藏 所屬分類: Eclipse

          FeedBack:
          # re: [原]Eclipse產品應用——控制臺的使用 2008-07-03 10:21 冰河の泥魚
          如果代碼全一些就會更好了.發現MessageConsole這個類沒有.  回復  更多評論
            
          # re: [原]Eclipse產品應用——控制臺的使用 2008-07-03 10:23 冰河の泥魚
          哈,找到了.  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 清流县| 南宫市| 福鼎市| 封丘县| 忻州市| 蓝山县| 井陉县| 富平县| 轮台县| 白河县| 临潭县| 海丰县| 远安县| 准格尔旗| 应用必备| 西充县| 宁海县| 宜兰县| 武城县| 穆棱市| 崇信县| 台前县| 子长县| 新河县| 兖州市| 青龙| 明水县| 黔西县| 出国| 襄城县| 潮安县| 湘阴县| 商洛市| 庆城县| 嘉义县| 遵义县| 驻马店市| 克东县| 盖州市| 塘沽区| 惠水县|