Example usage for org.eclipse.swt.ole.win32 OleControlSite OleControlSite

List of usage examples for org.eclipse.swt.ole.win32 OleControlSite OleControlSite

Introduction

In this page you can find the example usage for org.eclipse.swt.ole.win32 OleControlSite OleControlSite.

Prototype

public OleControlSite(Composite parent, int style, String progId) 

Source Link

Document

Create an OleControlSite child widget using style bits to select a particular look or set of properties.

Usage

From source file:org.eclipse.swt.snippets.Snippet123.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 123");
    shell.setLayout(new FillLayout());
    OleControlSite controlSite;//  w  ww .  ja  v  a 2  s . com
    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");
        display.dispose();
        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() {
        @Override
        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.com") };
    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();

}

From source file:OLEActiveXTypeLib.java

public static void main(String[] args) {

    if (args.length == 0) {
        System.out.println("Usage: java Main <program id>");
        return;//from  w w  w. j a  v a 2s  . c  o m
    }

    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();

}

From source file:org.eclipse.swt.snippets.Snippet186.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 186");
    shell.setLayout(new GridLayout(2, false));

    final Text text = new Text(shell, SWT.BORDER);
    text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    Button go = new Button(shell, SWT.PUSH);
    go.setText("Go");
    OleFrame oleFrame = new OleFrame(shell, SWT.NONE);
    oleFrame.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    OleControlSite controlSite;/* w ww . j  a v a 2 s .co  m*/
    OleAutomation automation;
    try {
        controlSite = new OleControlSite(oleFrame, SWT.NONE, "Shell.Explorer");
        automation = new OleAutomation(controlSite);
        controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
    } catch (SWTException ex) {
        System.out.println("Unable to open activeX control");
        display.dispose();
        return;
    }

    final OleAutomation auto = automation;
    go.addListener(SWT.Selection, new Listener() {
        @Override
        public void handleEvent(Event e) {
            String url = text.getText();
            int[] rgdispid = auto.getIDsOfNames(new String[] { "Navigate", "URL" });
            int dispIdMember = rgdispid[0];
            Variant[] rgvarg = new Variant[1];
            rgvarg[0] = new Variant(url);
            int[] rgdispidNamedArgs = new int[1];
            rgdispidNamedArgs[0] = rgdispid[1];
            auto.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);
        }
    });

    // Read PostData whenever we navigate to a site that uses it
    int BeforeNavigate2 = 0xfa;
    controlSite.addEventListener(BeforeNavigate2, new OleListener() {
        @Override
        public void handleEvent(OleEvent event) {
            Variant url = event.arguments[1];
            Variant postData = event.arguments[4];
            if (postData != null) {
                System.out.println("PostData = " + readSafeArray(postData) + ", URL = " + url.getString());
            }
        }
    });

    // Navigate to this web site which uses post data to fill in the text field
    // and put the string "hello world" into the text box
    text.setText("file://" + Snippet186.class.getResource("Snippet186.html").getFile());
    int[] rgdispid = automation.getIDsOfNames(new String[] { "Navigate", "URL", "PostData" });
    int dispIdMember = rgdispid[0];
    Variant[] rgvarg = new Variant[2];
    rgvarg[0] = new Variant(text.getText());
    rgvarg[1] = writeSafeArray("hello world");
    int[] rgdispidNamedArgs = new int[2];
    rgdispidNamedArgs[0] = rgdispid[1];
    rgdispidNamedArgs[1] = rgdispid[2];
    automation.invoke(dispIdMember, rgvarg, rgdispidNamedArgs);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:GetEventsFromIE.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    OleControlSite controlSite;//w  w  w  .j  a  v a 2s. c  o m
    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.com") };
    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();

}

From source file:org.eclipse.swt.snippets.Snippet199.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 199");
    shell.setLayout(new FillLayout());
    OleControlSite controlSite;//from w  w w.  jav a2  s.c  o  m
    try {
        OleFrame frame = new OleFrame(shell, SWT.NONE);
        controlSite = new OleControlSite(frame, SWT.NONE, "Excel.Sheet");
        controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
    } catch (SWTError e) {
        System.out.println("Unable to open activeX control");
        display.dispose();
        return;
    }
    shell.open();

    OleAutomation excelSheet = new OleAutomation(controlSite);
    int[] dispIDs = excelSheet.getIDsOfNames(new String[] { "Application" });
    Variant pVarResult = excelSheet.getProperty(dispIDs[0]);
    OleAutomation application = pVarResult.getAutomation();
    pVarResult.dispose();
    excelSheet.dispose();

    int eventID = SheetSelectionChange;
    OleListener listener = new OleListener() {
        @Override
        public void handleEvent(OleEvent e) {
            System.out.println("selection has changed");
            Variant[] args = e.arguments;
            for (int i = 0; i < args.length; i++) {
                System.out.println(args[i]);
            }
        }
    };
    controlSite.addEventListener(application, IID_AppEvents, eventID, listener);

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    application.dispose();
    display.dispose();
}

From source file:org.eclipse.swt.examples.ole.win32.OLEExample.java

OleClientSite createSite(OleFrame frame, String progID) {
    if (createClientSite) {
        return new OleClientSite(frame, SWT.NONE, progID);
    } else {/*from www. j a va 2  s .co m*/
        return new OleControlSite(frame, SWT.NONE, progID);
    }
}

From source file:org.eclipse.swt.examples.ole.win32.OleBrowserView.java

/**
 * Creates Web browser control.//from w w w .  ja  va  2 s .c  o m
 */
private void createBrowserControl() {
    try {
        // Create an Automation object for access to extended capabilities
        webControlSite = new OleControlSite(webFrame, SWT.NONE, "Shell.Explorer");
        Variant download = new Variant(DLCTL_NO_SCRIPTS);
        webControlSite.setSiteProperty(DISPID_AMBIENT_DLCONTROL, download);
        OleAutomation oleAutomation = new OleAutomation(webControlSite);
        webBrowser = new OleWebBrowser(oleAutomation);
    } catch (SWTException ex) {
        // Creation may have failed because control is not installed on machine
        Label label = new Label(webFrame, SWT.BORDER);
        OlePlugin.logError(OlePlugin.getResourceString("error.CouldNotCreateBrowserControl"), ex);
        label.setText(OlePlugin.getResourceString("error.CouldNotCreateBrowserControl"));
        return;
    }

    // Respond to ProgressChange events by updating the Progress bar
    webControlSite.addEventListener(OleWebBrowser.ProgressChange, event -> {
        Variant progress = event.arguments[0];
        Variant maxProgress = event.arguments[1];
        if (progress == null || maxProgress == null)
            return;
        webProgress.setMaximum(maxProgress.getInt());
        webProgress.setSelection(progress.getInt());
    });

    // Respond to StatusTextChange events by updating the Status Text label
    webControlSite.addEventListener(OleWebBrowser.StatusTextChange, event -> {
        Variant statusText = event.arguments[0];
        if (statusText == null)
            return;
        String text = statusText.getString();
        if (text != null)
            webStatus.setText(text);
    });

    // Listen for changes to the ready state and print out the current state 
    webControlSite.addPropertyListener(OleWebBrowser.DISPID_READYSTATE, event -> {
        if (event.detail == OLE.PROPERTY_CHANGING)
            return;
        int state = webBrowser.getReadyState();
        switch (state) {
        case OleWebBrowser.READYSTATE_UNINITIALIZED:
            webStatus.setText(OlePlugin.getResourceString("browser.State.Uninitialized.text"));
            webCommandBackward.setEnabled(false);
            webCommandForward.setEnabled(false);
            webCommandHome.setEnabled(false);
            webCommandRefresh.setEnabled(false);
            webCommandStop.setEnabled(false);
            webCommandSearch.setEnabled(false);
            break;
        case OleWebBrowser.READYSTATE_LOADING:
            webStatus.setText(OlePlugin.getResourceString("browser.State.Loading.text"));
            webCommandHome.setEnabled(true);
            webCommandRefresh.setEnabled(true);
            webCommandStop.setEnabled(true);
            webCommandSearch.setEnabled(true);
            break;
        case OleWebBrowser.READYSTATE_LOADED:
            webStatus.setText(OlePlugin.getResourceString("browser.State.Loaded.text"));
            webCommandStop.setEnabled(true);
            break;
        case OleWebBrowser.READYSTATE_INTERACTIVE:
            webStatus.setText(OlePlugin.getResourceString("browser.State.Interactive.text"));
            webCommandStop.setEnabled(true);
            break;
        case OleWebBrowser.READYSTATE_COMPLETE:
            webStatus.setText(OlePlugin.getResourceString("browser.State.Complete.text"));
            webCommandStop.setEnabled(false);
            break;
        }
    });

    // Listen for changes to the active command states
    webControlSite.addEventListener(OleWebBrowser.CommandStateChange, event -> {
        if (event.type != OleWebBrowser.CommandStateChange)
            return;
        final int commandID = (event.arguments[0] != null) ? event.arguments[0].getInt() : 0;
        final boolean commandEnabled = (event.arguments[1] != null) ? event.arguments[1].getBoolean() : false;

        switch (commandID) {
        case OleWebBrowser.CSC_NAVIGATEBACK:
            webCommandBackward.setEnabled(commandEnabled);
            break;
        case OleWebBrowser.CSC_NAVIGATEFORWARD:
            webCommandForward.setEnabled(commandEnabled);
            break;
        }
    });

    // in place activate the ActiveX control      
    activated = (webControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE) == OLE.S_OK);
    if (activated)
        webBrowser.GoHome();
}