Java Tutorial/SWT/WIN32

Материал из Java эксперт
Перейти к: навигация, поиск

17. Add System Setting Change Listener

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class SystemSettingChangeListener {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    display.addListener(SWT.Settings, new Listener() {
      public void handleEvent(Event event) {
        System.out.println("Setting changed");
      }
    });
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





17. Detect a system settings change

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/* 
 * example snippet: detect a system settings change
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class DetectSystemSettingChange {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("The SWT.Settings Event");
    shell.setLayout(new GridLayout());
    Label label = new Label(shell, SWT.WRAP);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    label.setText("Change a system setting and the table below will be updated.");
    final Table table = new Table(shell, SWT.BORDER);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    TableColumn column = new TableColumn(table, SWT.NONE);
    column = new TableColumn(table, SWT.NONE);
    column.setWidth(150);
    column = new TableColumn(table, SWT.NONE);
    for (int i = 0; i < colorIds.length; i++) {
      TableItem item = new TableItem(table, SWT.NONE);
      Color color = display.getSystemColor(colorIds[i]);
      item.setText(0, colorNames[i]);
      item.setBackground(1, color);
      item.setText(2, color.toString());
    }
    TableColumn[] columns = table.getColumns();
    columns[0].pack();
    columns[2].pack();
    display.addListener(SWT.Settings, new Listener() {
      public void handleEvent(Event event) {
        for (int i = 0; i < colorIds.length; i++) {
          Color color = display.getSystemColor(colorIds[i]);
          TableItem item = table.getItem(i);
          item.setBackground(1, color);
        }
        TableColumn[] columns = table.getColumns();
        columns[0].pack();
        columns[2].pack();
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  static int[] colorIds = new int[] { SWT.COLOR_INFO_BACKGROUND, SWT.COLOR_INFO_FOREGROUND,
      SWT.COLOR_LIST_BACKGROUND, SWT.COLOR_LIST_FOREGROUND, SWT.COLOR_LIST_SELECTION,
      SWT.COLOR_LIST_SELECTION_TEXT, SWT.COLOR_TITLE_BACKGROUND,
      SWT.COLOR_TITLE_BACKGROUND_GRADIENT, SWT.COLOR_TITLE_FOREGROUND,
      SWT.COLOR_TITLE_INACTIVE_BACKGROUND, SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT,
      SWT.COLOR_TITLE_INACTIVE_FOREGROUND, SWT.COLOR_WIDGET_BACKGROUND, SWT.COLOR_WIDGET_BORDER,
      SWT.COLOR_WIDGET_DARK_SHADOW, SWT.COLOR_WIDGET_FOREGROUND, SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW,
      SWT.COLOR_WIDGET_LIGHT_SHADOW, SWT.COLOR_WIDGET_NORMAL_SHADOW, };
  static String[] colorNames = new String[] { "SWT.COLOR_INFO_BACKGROUND",
      "SWT.COLOR_INFO_FOREGROUND", "SWT.COLOR_LIST_BACKGROUND", "SWT.COLOR_LIST_FOREGROUND",
      "SWT.COLOR_LIST_SELECTION", "SWT.COLOR_LIST_SELECTION_TEXT", "SWT.COLOR_TITLE_BACKGROUND",
      "SWT.COLOR_TITLE_BACKGROUND_GRADIENT", "SWT.COLOR_TITLE_FOREGROUND",
      "SWT.COLOR_TITLE_INACTIVE_BACKGROUND", "SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT",
      "SWT.COLOR_TITLE_INACTIVE_FOREGROUND", "SWT.COLOR_WIDGET_BACKGROUND",
      "SWT.COLOR_WIDGET_BORDER", "SWT.COLOR_WIDGET_DARK_SHADOW", "SWT.COLOR_WIDGET_FOREGROUND",
      "SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW", "SWT.COLOR_WIDGET_LIGHT_SHADOW",
      "SWT.COLOR_WIDGET_NORMAL_SHADOW", };
}





17. Embed Word in an applet (win32 only)

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * example snippet: Embed Word in an applet (win32 only)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.0
 */
import java.applet.Applet;
public class MainClass extends Applet {
  org.eclipse.swt.widgets.Display display;
  org.eclipse.swt.widgets.Shell swtParent;
  java.awt.Canvas awtParent;
  public void init() {
    Thread thread = new Thread(new Runnable() {
      public void run() {
        setLayout(new java.awt.GridLayout(1, 1));
        awtParent = new java.awt.Canvas();
        add(awtParent);
        display = new org.eclipse.swt.widgets.Display();
        swtParent = org.eclipse.swt.awt.SWT_AWT.new_Shell(display, awtParent);
        swtParent.setLayout(new org.eclipse.swt.layout.FillLayout());
        org.eclipse.swt.ole.win32.OleFrame frame = new org.eclipse.swt.ole.win32.OleFrame(
            swtParent, org.eclipse.swt.SWT.NONE);
        org.eclipse.swt.ole.win32.OleClientSite site;
        try {
          site = new org.eclipse.swt.ole.win32.OleClientSite(frame, org.eclipse.swt.SWT.NONE,
              "Word.Document");
        } catch (org.eclipse.swt.SWTException e) {
          String str = "Create OleClientSite Error" + e.toString();
          System.out.println(str);
          return;
        }
        setSize(500, 500);
        validate();
        site.doVerb(org.eclipse.swt.ole.win32.OLE.OLEIVERB_SHOW);
        while (swtParent != null && !swtParent.isDisposed()) {
          if (!display.readAndDispatch())
            display.sleep();
        }
      }
    });
    thread.start();
  }
  public void stop() {
    if (display != null && !display.isDisposed()) {
      display.syncExec(new Runnable() {
        public void run() {
          if (swtParent != null && !swtParent.isDisposed())
            swtParent.dispose();
          swtParent = null;
          display.dispose();
          display = null;
        }
      });
      remove(awtParent);
      awtParent = null;
    }
  }
}





17. How to access About, Preferences and Quit menus on carbon.

/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/*
 * How to access About, Preferences and Quit menus on carbon.
 * NOTE: This snippet uses internal SWT packages that are
 * subject to change without notice.
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.carbon.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class Snippet178 {
  private static final int kHICommandPreferences = ("p" << 24) + ("r" << 16) + ("e" << 8) + "f";
  private static final int kHICommandAbout = ("a" << 24) + ("b" << 16) + ("o" << 8) + "u";
  private static final int kHICommandServices = ("s" << 24) + ("e" << 16) + ("r" << 8) + "v";
  public static void main(String[] arg) {
    Display.setAppName("AppMenu"); // sets name in Dock
    Display display = new Display();
    hookApplicationMenu(display, "About AppMenu");
    Shell shell = new Shell(display);
    shell.setText("Main Window");
    shell.open();
    while (!shell.isDisposed())
      if (!display.readAndDispatch())
        display.sleep();
    display.dispose();
  }
  static void hookApplicationMenu(Display display, final String aboutName) {
    // Callback target
    Object target = new Object() {
      int commandProc(int nextHandler, int theEvent, int userData) {
        if (OS.GetEventKind(theEvent) == OS.kEventProcessCommand) {
          HICommand command = new HICommand();
          OS.GetEventParameter(theEvent, OS.kEventParamDirectObject, OS.typeHICommand, null,
              HICommand.sizeof, null, command);
          switch (command.rumandID) {
          case kHICommandPreferences:
            return handleCommand("Preferences"); //$NON-NLS-1$
          case kHICommandAbout:
            return handleCommand(aboutName);
          default:
            break;
          }
        }
        return OS.eventNotHandledErr;
      }
      int handleCommand(String command) {
        Shell shell = new Shell();
        MessageBox preferences = new MessageBox(shell, SWT.ICON_WARNING);
        preferences.setText(command);
        preferences.open();
        shell.dispose();
        return OS.noErr;
      }
    };
    final Callback commandCallback = new Callback(target, "commandProc", 3); //$NON-NLS-1$
    int commandProc = commandCallback.getAddress();
    if (commandProc == 0) {
      commandCallback.dispose();
      return; // give up
    }
    // Install event handler for commands
    int[] mask = new int[] { OS.kEventClassCommand, OS.kEventProcessCommand };
    OS.InstallEventHandler(OS.GetApplicationEventTarget(), commandProc, mask.length / 2, mask, 0,
        null);
    // create About ... menu command
    int[] outMenu = new int[1];
    short[] outIndex = new short[1];
    if (OS.GetIndMenuItemWithCommandID(0, kHICommandPreferences, 1, outMenu, outIndex) == OS.noErr
        && outMenu[0] != 0) {
      int menu = outMenu[0];
      int l = aboutName.length();
      char buffer[] = new char[l];
      aboutName.getChars(0, l, buffer, 0);
      int str = OS.CFStringCreateWithCharacters(OS.kCFAllocatorDefault, buffer, l);
      OS.InsertMenuItemTextWithCFString(menu, str, (short) 0, 0, kHICommandAbout);
      OS.CFRelease(str);
      // add separator between About & Preferences
      OS.InsertMenuItemTextWithCFString(menu, 0, (short) 1, OS.kMenuItemAttrSeparator, 0);
      // enable pref menu
      OS.EnableMenuCommand(menu, kHICommandPreferences);
      // disable services menu
      OS.DisableMenuCommand(menu, kHICommandServices);
    }
    // schedule disposal of callback object
    display.disposeExec(new Runnable() {
      public void run() {
        commandCallback.dispose();
      }
    });
  }
}





17. Invoke an external batch file

import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
public class ExternalBatchFileInvoke {
  public static void main(String[] args) {
    Display display = new Display();
    Program.launch("c:\\cygwin\\cygwin.bat");
    display.dispose();
  }
}





17. Invoke the system text editor on autoexec.bat

import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
public class InvokeSystemTextEditor {
  public static void main(String[] args) {
    Display display = new Display();
    Program p = Program.findProgram(".txt");
    if (p != null)
      p.execute("c:\\autoexec.bat"); // Windows-specific filename
    display.dispose();
  }
}





17. Load System File Icon

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SystemFileIconLoad {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());
    Label label = new Label(shell, SWT.NONE);
    Image image = null;
    
    Program p = Program.findProgram(".txt");
    ImageData data = p.getImageData();
    image = new Image(display, data);
    label.setImage(image);
    p = Program.findProgram(".bmp");
    data = p.getImageData();
    image = new Image(display, data);
    label = new Label(shell, SWT.NONE);
    label.setImage(image);
    p = Program.findProgram(".doc");
    data = p.getImageData();
    image = new Image(display, data);
    label = new Label(shell, SWT.NONE);
    label.setImage(image);
    
    
    label.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    if (image != null)
      image.dispose();
    display.dispose();
  }
}





17. OLE and ActiveX: browse the typelibinfo for a program id (win32 only)

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * OLE and ActiveX example snippet: browse the typelibinfo for a program id (win32 only)
 * NOTE: This snippet uses internal SWT packages that are
 * subject to change without notice.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.internal.ole.win32.TYPEATTR;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleFunctionDescription;
import org.eclipse.swt.ole.win32.OlePropertyDescription;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class OLEActiveXTypeLib {
  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("Usage: java Main <program id>");
      return;
    }
    String progID = args[0];
    Display display = new Display();
    Shell shell = new Shell(display);
    OleFrame frame = new OleFrame(shell, SWT.NONE);
    OleControlSite site = null;
    OleAutomation auto = null;
    try {
      site = new OleControlSite(frame, SWT.NONE, progID);
      auto = new OleAutomation(site);
    } catch (SWTException ex) {
      System.out.println("Unable to open type library for " + progID);
      return;
    }
    printTypeInfo(auto);
    auto.dispose();
    shell.dispose();
    display.dispose();
  }
  private static void printTypeInfo(OleAutomation auto) {
    TYPEATTR typeattr = auto.getTypeInfoAttributes();
    if (typeattr != null) {
      if (typeattr.cFuncs > 0)
        System.out.println("Functions :\n");
      for (int i = 0; i < typeattr.cFuncs; i++) {
        OleFunctionDescription data = auto.getFunctionDescription(i);
        String argList = "";
        int firstOptionalArgIndex = data.args.length - data.optionalArgCount;
        for (int j = 0; j < data.args.length; j++) {
          argList += "[";
          if (j >= firstOptionalArgIndex)
            argList += "optional, ";
          argList += getDirection(data.args[j].flags) + "] " + getTypeName(data.args[j].type) + " "
              + data.args[j].name;
          if (j < data.args.length - 1)
            argList += ", ";
        }
        System.out.println(getInvokeKind(data.invokeKind) + " (id = " + data.id + ") : "
            + "\n\tSignature   : " + getTypeName(data.returnType) + " " + data.name + "(" + argList
            + ")" + "\n\tDescription : " + data.documentation + "\n\tHelp File   : "
            + data.helpFile + "\n");
      }
      if (typeattr.cVars > 0)
        System.out.println("\n\nVariables  :\n");
      for (int i = 0; i < typeattr.cVars; i++) {
        OlePropertyDescription data = auto.getPropertyDescription(i);
        System.out.println("PROPERTY (id = " + data.id + ") :" + "\n\tName : " + data.name
            + "\n\tType : " + getTypeName(data.type) + "\n");
      }
    }
  }
  private static String getTypeName(int type) {
    switch (type) {
    case OLE.VT_BOOL:
      return "boolean";
    case OLE.VT_R4:
      return "float";
    case OLE.VT_R8:
      return "double";
    case OLE.VT_I4:
      return "int";
    case OLE.VT_DISPATCH:
      return "IDispatch";
    case OLE.VT_UNKNOWN:
      return "IUnknown";
    case OLE.VT_I2:
      return "short";
    case OLE.VT_BSTR:
      return "String";
    case OLE.VT_VARIANT:
      return "Variant";
    case OLE.VT_CY:
      return "Currency";
    case OLE.VT_DATE:
      return "Date";
    case OLE.VT_UI1:
      return "unsigned char";
    case OLE.VT_UI4:
      return "unsigned int";
    case OLE.VT_USERDEFINED:
      return "UserDefined";
    case OLE.VT_HRESULT:
      return "int";
    case OLE.VT_VOID:
      return "void";
    case OLE.VT_BYREF | OLE.VT_BOOL:
      return "boolean *";
    case OLE.VT_BYREF | OLE.VT_R4:
      return "float *";
    case OLE.VT_BYREF | OLE.VT_R8:
      return "double *";
    case OLE.VT_BYREF | OLE.VT_I4:
      return "int *";
    case OLE.VT_BYREF | OLE.VT_DISPATCH:
      return "IDispatch *";
    case OLE.VT_BYREF | OLE.VT_UNKNOWN:
      return "IUnknown *";
    case OLE.VT_BYREF | OLE.VT_I2:
      return "short *";
    case OLE.VT_BYREF | OLE.VT_BSTR:
      return "String *";
    case OLE.VT_BYREF | OLE.VT_VARIANT:
      return "Variant *";
    case OLE.VT_BYREF | OLE.VT_CY:
      return "Currency *";
    case OLE.VT_BYREF | OLE.VT_DATE:
      return "Date *";
    case OLE.VT_BYREF | OLE.VT_UI1:
      return "unsigned char *";
    case OLE.VT_BYREF | OLE.VT_UI4:
      return "unsigned int *";
    case OLE.VT_BYREF | OLE.VT_USERDEFINED:
      return "UserDefined *";
    }
    return "unknown " + type;
  }
  private static String getDirection(int direction) {
    String dirString = "";
    boolean comma = false;
    if ((direction & OLE.IDLFLAG_FIN) != 0) {
      dirString += "in";
      comma = true;
    }
    if ((direction & OLE.IDLFLAG_FOUT) != 0) {
      if (comma)
        dirString += ", ";
      dirString += "out";
      comma = true;
    }
    if ((direction & OLE.IDLFLAG_FLCID) != 0) {
      if (comma)
        dirString += ", ";
      dirString += "lcid";
      comma = true;
    }
    if ((direction & OLE.IDLFLAG_FRETVAL) != 0) {
      if (comma)
        dirString += ", ";
      dirString += "retval";
    }
    return dirString;
  }
  private static String getInvokeKind(int invKind) {
    switch (invKind) {
    case OLE.INVOKE_FUNC:
      return "METHOD";
    case OLE.INVOKE_PROPERTYGET:
      return "PROPERTY GET";
    case OLE.INVOKE_PROPERTYPUT:
      return "PROPERTY PUT";
    case OLE.INVOKE_PROPERTYPUTREF:
      return "PROPERTY PUT BY REF";
    }
    return "unknown " + invKind;
  }
}





17. OLE and ActiveX: get events from IE control (win32 only)

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * OLE and ActiveX example snippet: get events from IE control (win32 only)
 * NOTE: This snippet uses internal SWT packages that are
 * subject to change without notice.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.internal.ole.win32.ru;
import org.eclipse.swt.internal.ole.win32.ruObject;
import org.eclipse.swt.internal.ole.win32.GUID;
import org.eclipse.swt.internal.ole.win32.IDispatch;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleEvent;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GetEventsFromIE {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    OleControlSite controlSite;
    try {
      OleFrame frame = new OleFrame(shell, SWT.NONE);
      controlSite = new OleControlSite(frame, SWT.NONE, "Shell.Explorer");
      controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
    } catch (SWTError e) {
      System.out.println("Unable to open activeX control");
      return;
    }
    shell.open();
    // IWebBrowser
    final OleAutomation webBrowser = new OleAutomation(controlSite);
    // When a new document is loaded, access the document object for the new
    // page.
    int DownloadComplete = 104;
    controlSite.addEventListener(DownloadComplete, new OleListener() {
      public void handleEvent(OleEvent event) {
        int[] htmlDocumentID = webBrowser.getIDsOfNames(new String[] { "Document" });
        if (htmlDocumentID == null)
          return;
        Variant pVarResult = webBrowser.getProperty(htmlDocumentID[0]);
        if (pVarResult == null || pVarResult.getType() == 0)
          return;
        // IHTMLDocument2
        OleAutomation htmlDocument = pVarResult.getAutomation();
        // Request to be notified of click, double click and key down events
        EventDispatch myDispatch = new EventDispatch(EventDispatch.onclick);
        IDispatch idispatch = new IDispatch(myDispatch.getAddress());
        Variant dispatch = new Variant(idispatch);
        htmlDocument.setProperty(EventDispatch.onclick, dispatch);
        myDispatch = new EventDispatch(EventDispatch.ondblclick);
        idispatch = new IDispatch(myDispatch.getAddress());
        dispatch = new Variant(idispatch);
        htmlDocument.setProperty(EventDispatch.ondblclick, dispatch);
        myDispatch = new EventDispatch(EventDispatch.onkeydown);
        idispatch = new IDispatch(myDispatch.getAddress());
        dispatch = new Variant(idispatch);
        htmlDocument.setProperty(EventDispatch.onkeydown, dispatch);
        // Remember to release OleAutomation Object
        htmlDocument.dispose();
      }
    });
    // Navigate to a web site
    int[] ids = webBrowser.getIDsOfNames(new String[] { "Navigate", "URL" });
    Variant[] rgvarg = new Variant[] { new Variant("http://www.google.ru") };
    int[] rgdispidNamedArgs = new int[] { ids[1] };
    webBrowser.invoke(ids[0], rgvarg, rgdispidNamedArgs);
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    // Remember to release OleAutomation Object
    webBrowser.dispose();
    display.dispose();
  }
}
// EventDispatch implements a simple IDispatch interface which will be called
// when the event is fired.
class EventDispatch {
  private COMObject iDispatch;
  private int refCount = 0;
  private int eventID;
  final static int onhelp = 0x8001177d;
  final static int onclick = 0x80011778;
  final static int ondblclick = 0x80011779;
  final static int onkeyup = 0x80011776;
  final static int onkeydown = 0x80011775;
  final static int onkeypress = 0x80011777;
  final static int onmouseup = 0x80011773;
  final static int onmousedown = 0x80011772;
  final static int onmousemove = 0x80011774;
  final static int onmouseout = 0x80011771;
  final static int onmouseover = 0x80011770;
  final static int onreadystatechange = 0x80011789;
  final static int onafterupdate = 0x80011786;
  final static int onrowexit = 0x80011782;
  final static int onrowenter = 0x80011783;
  final static int ondragstart = 0x80011793;
  final static int onselectstart = 0x80011795;
  EventDispatch(int eventID) {
    this.eventID = eventID;
    createCOMInterfaces();
  }
  int getAddress() {
    return iDispatch.getAddress();
  }
  private void createCOMInterfaces() {
    iDispatch = new COMObject(new int[] { 2, 0, 0, 1, 3, 4, 8 }) {
      public int method0(int[] args) {
        return QueryInterface(args[0], args[1]);
      }
      public int method1(int[] args) {
        return AddRef();
      }
      public int method2(int[] args) {
        return Release();
      }
      // method3 GetTypeInfoCount - not implemented
      // method4 GetTypeInfo - not implemented
      // method5 GetIDsOfNames - not implemented
      public int method6(int[] args) {
        return Invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
      }
    };
  }
  private void disposeCOMInterfaces() {
    if (iDispatch != null)
      iDispatch.dispose();
    iDispatch = null;
  }
  private int AddRef() {
    refCount++;
    return refCount;
  }
  private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags, int pDispParams,
      int pVarResult, int pExcepInfo, int pArgErr) {
    switch (eventID) {
    case onhelp:
      System.out.println("onhelp");
      break;
    case onclick:
      System.out.println("onclick");
      break;
    case ondblclick:
      System.out.println("ondblclick");
      break;
    case onkeyup:
      System.out.println("onkeyup");
      break;
    case onkeydown:
      System.out.println("onkeydown");
      break;
    case onkeypress:
      System.out.println("onkeypress");
      break;
    case onmouseup:
      System.out.println("onmouseup");
      break;
    case onmousedown:
      System.out.println("onmousedown");
      break;
    case onmousemove:
      System.out.println("onmousemove");
      break;
    case onmouseout:
      System.out.println("onmouseout");
      break;
    case onmouseover:
      System.out.println("onmouseover");
      break;
    case onreadystatechange:
      System.out.println("onreadystatechange");
      break;
    case onafterupdate:
      System.out.println("onafterupdate");
      break;
    case onrowexit:
      System.out.println("onrowexit");
      break;
    case onrowenter:
      System.out.println("onrowenter");
      break;
    case ondragstart:
      System.out.println("ondragstart");
      break;
    case onselectstart:
      System.out.println("onselectstart");
      break;
    }
    return COM.S_OK;
  }
  private int QueryInterface(int riid, int ppvObject) {
    if (riid == 0 || ppvObject == 0)
      return COM.E_INVALIDARG;
    GUID guid = new GUID();
    COM.MoveMemory(guid, riid, GUID.sizeof);
    if (COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDispatch)) {
      COM.MoveMemory(ppvObject, new int[] { iDispatch.getAddress() }, 4);
      AddRef();
      return COM.S_OK;
    }
    COM.MoveMemory(ppvObject, new int[] { 0 }, 4);
    return COM.E_NOINTERFACE;
  }
  int Release() {
    refCount--;
    if (refCount == 0) {
      disposeCOMInterfaces();
    }
    return refCount;
  }
}





17. Using System Icon Image

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SystemIconImage {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());

    Label label = new Label (shell, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_ERROR));
    label = new Label (shell, SWT.NONE);
    label.setText("SWT.ICON_ERROR");
    label = new Label (shell, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
    label = new Label (shell, SWT.NONE);
    label.setText("SWT.ICON_INFORMATION");
    label = new Label (shell, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_WARNING));
    label = new Label (shell, SWT.NONE);
    label.setText("SWT.ICON_WARNING");
    label = new Label (shell, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_QUESTION));
    label = new Label (shell, SWT.NONE);
    label.setText("SWT.ICON_QUESTION");
    shell.setSize(400, 350);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}