Read Sean

          Read me, read Sean.
          posts - 508, comments - 655, trackbacks - 9, articles - 4

          [Eclipse筆記]SWT設計思路

          Posted on 2005-03-20 15:06 laogao 閱讀(706) 評論(1)  編輯  收藏 所屬分類: On JavaOn Eclipse

           

          本文部分內容和靈感來自eclipse.org網站,特此聲明。更多內容,請參考:

          http://eclipse.org/articles/Article-SWT-Design-1/SWT-Design-1.html

           

          眾所周知,SWTSwing最大的不同就是它直接使用操作系統提供的現成的本地圖形接口,于是具備本地化的Look & Feel。但是它是怎么做到這一點的呢,當然是通過JNI。我們來看一個例子,假定我們使用Win32API

          我們現在有一個文本框text,通過如下的代碼,我們給它一個字符串,并讓它選擇/highlight3~5[3,5])的字符。

          text.setText(“abcdefgh”);
          text.setSelection(
          36);


          Windows下,這個setSelection方法是怎么實現的呢?我們可以看看源碼:

          public void setSelection (int start, int end) {
                 …
                 OS.SendMessage (handle, OS.EM_SETSEL, start, end);
                 OS.SendMessage (handle, OS.EM_SCROLLCARET, 
          00);
          }

          做過Windows編程的朋友可能一下子就認出了這個SendMessage,這不就是Win32 API中用于向窗體發送消息的函數嗎?呵呵,沒錯,我們再來看一下這個SendMessage方法的原型:

          public static final int SendMessage (int hWnd, int Msg, int wParam, int lParam) {
                 
          if (IsUnicode) return SendMessageW (hWnd, Msg, wParam, lParam);
                 
          return SendMessageA (hWnd, Msg, wParam, lParam);
          }


          public static final native int SendMessageW (int hWnd, int Msg, int wParam, int lParam);

          public static final native int SendMessageA (int hWnd, int Msg, int wParam, int lParam);

          我們看到了兩個版本,一個版本針對Unicode,另一個版本針對ASCII,正好Win32 API也是如此,我們在這里看到的是native的方法,這意味著具體還有一組JNIC代碼來直接與操作系統的函數打交道:

          #ifndef NO_SendMessageW__IIII
          JNIEXPORT jint JNICALL OS_NATIVE(SendMessageW__IIII)
                 (JNIEnv 
          *env, jclass that, jint arg0, jint arg1, jint arg2, jint arg3)
          {
                 jint rc;
                 OS_NATIVE_ENTER(env, that, SendMessageW__IIII_FUNC);
                 rc 
          = (jint)SendMessageW((HWND)arg0, arg1, (WPARAM)arg2, (LPARAM)arg3);
                 OS_NATIVE_EXIT(env, that, SendMessageW__IIII_FUNC);
                 
          return rc;
          }

          #endif

          #ifndef NO_SendMessageA__IIII
          JNIEXPORT jint JNICALL OS_NATIVE(SendMessageA__IIII)
                 (JNIEnv 
          *env, jclass that, jint arg0, jint arg1, jint arg2, jint arg3)
          {
                 jint rc;
                 OS_NATIVE_ENTER(env, that, SendMessageA__IIII_FUNC);
                 rc 
          = (jint)SendMessageA((HWND)arg0, arg1, (WPARAM)arg2, (LPARAM)arg3);
                 OS_NATIVE_EXIT(env, that, SendMessageA__IIII_FUNC);
                 
          return rc;
          }

          #endif

          看到這里,你也許已經恍然大悟:SWT所做的無非就是把Win32API簡單的包裝了一下,我們在SWT這一層調用的方法、傳遞的參數被原封不動的代理到了Win32層。這就是SWT的核心思想。SWT有一個很重要的設計原則,那就是,SWTAPI一對一的封裝OSAPI,完全忠實于操作系統的API實現的行為,如果有bug,那也是OSbug,它不會嘗試去糾正操作系統,因為那樣會潛在的破壞本地化的一些行為。忠實于OS也使得調用者不必但心自己的SWT程序會跟OS的本地GUI有不一致的地方,如有必要直接參考MSDN即可。SWT其實就是這樣一個thin wrapper,我們通過它可以方便的訪問Win32的圖形API,為我們的應用程序提供nativeLook & Feel

          下面給出一個完整的SWT示例:

          package sean.test.swt;

          import org.eclipse.swt.SWT;
          import org.eclipse.swt.layout.FillLayout;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Shell;
          import org.eclipse.swt.widgets.Text;

          public class DummySWT {

                 
          public static void main(String[] args) {
                        final Display display 
          = new Display();
                        final Shell shell 
          = new Shell(display);
                        shell.setLayout(
          new FillLayout());
                        final Text text 
          = new Text(shell, SWT.SINGLE);
                        text.setText(
          "abcdefgh");
                        text.setSelection(
          36);
                        shell.pack();
                        shell.open();
                        
          while (!shell.isDisposed()) {
                               
          if (!display.readAndDispatch()) {
                                      display.sleep();
                               }

                        }

                        display.dispose();
                 }


          }


          Feedback

          # Eclipse筆記[TrackBack]  回復  更多評論   

          2005-03-31 13:25 by hqzc
          Ping Back來自:blog.csdn.net
          [引用提示]hqzc引用了該文章, 地址: http://blog.csdn.net/hqzc/archive/2005/03/31/334601.aspx
          主站蜘蛛池模板: 宜兴市| 禹州市| 浑源县| 毕节市| 西平县| 拜城县| 常州市| 金湖县| 瓮安县| 南郑县| 台湾省| 达拉特旗| 宝山区| 舒兰市| 鄂托克前旗| 连州市| 新乡县| 高州市| 肥城市| 六安市| 辽宁省| 佛学| 陆河县| 盐山县| 普定县| 济阳县| 荃湾区| 安阳市| 临洮县| 广水市| 哈巴河县| 永仁县| 芷江| 化德县| 南漳县| 洛川县| 五家渠市| 安泽县| 沛县| 东丰县| 石楼县|