[Eclipse notes] SWT Design Ideas

Source: Internet
Author: User
Tags final win32
Notes | design


Part of this article and inspiration from the eclipse.org website is hereby declared. For more information, please refer to:

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



As we all know, the biggest difference between SWT and swing is that it directly uses the off-the-shelf local graphics interface provided by the operating system, so it has localized look & Feel. But how does it do that, of course, through JNI. Let's take a look at an example, assuming we use the Win32 API.

We now have a textbox text, through the following code, we give it a string and let it select/highlight from 3~5 ([3,5]) characters.
Text.settext ("abcdefgh"); Text.setselection (3, 6);
How is this setselection method implemented under Windows? We can look at the source code:


public void setselection (int start, int end) {... OS. SendMessage (handle, OS.       Em_setsel, start, end); OS. SendMessage (handle, OS. Em_scrollcaret, 0, 0);
A friend who has done Windows programming may recognize this sendmessage at once, isn't that the function in the Win32 API for sending messages to forms? Oh, yes, let's take a look at the prototype of this SendMessage method:


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);p ublic static final native int Sen Dmessagea (int hWnd, int Msg, int wParam, int lParam);
We've seen two versions, one for Unicode, another for ASCII, and that's just Win32 API, where we see the native approach, which means that there's a set of JNI C code to deal directly with the operating system's functions:


#ifndef  no_sendmessagew__iiiijniexport 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__iiiijniexport 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
See here, you may have realized: what SWT has done is to simply package the API of Win32, the method we call in SWT, the parameters passed are intact agent to the WIN32 layer. This is the core idea of SWT. SWT has a very important design principle, that is, SWT's API, one by one, encapsulates the OS API, is completely faithful to the operating system's API implementations, and if there are bugs, it's OS bugs, and it doesn't try to "correct" the operating system because it potentially destroys some localized behavior. Fidelity to the OS also makes it unnecessary for callers to have their own SWT programs that are inconsistent with the OS's local GUI, and if necessary, refer directly to MSDN. SWT is actually such a thin wrapper that we can easily access the Win32 graphics API, providing native look & Feel for our applications.

A complete SWT example is given below:
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);        &nbSp;      text.settext ("ABCDEFGH");               text.setselection (3, 6);               shell.pack ();               shell.open ();               while  (!shell.isdisposed ())  {                      if  (! Display.readanddispatch ())  {                             display.sleep ( );                      }              }               display.dispose ();        }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.