Search in sources :

Example 1 with BrowserComponent

use of com.codename1.ui.BrowserComponent in project CodenameOne by codenameone.

the class IOSImplementation method createBrowserComponent.

/*class RunnableCleanup implements Runnable {
        long[] peer;
        Runnable onCompletion;
        public void run() {
            if(onCompletion != null) {
                onCompletion.run();
            }
            if(peer != null && peer[0] != 0) {
                cleanupAudio(peer);
            }
        }
    }
    
    @Override
    public Object createAudio(String uri, Runnable onCompletion) throws IOException {
        RunnableCleanup c = new RunnableCleanup();
        long[] p = new long[] {nativeInstance.createAudio(uri, c)};
        c.peer = p;
        c.onCompletion = onCompletion;
        return p;
    }

    
    @Override
    public Object createAudio(InputStream stream, String mimeType, Runnable onCompletion) throws IOException {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int size = stream.read(buffer);
        while(size > -1) {
            bo.write(buffer, 0, size);
            size = stream.read(buffer);
        }
        bo.close();
        stream.close();
        RunnableCleanup c = new RunnableCleanup();
        long[] p = new long[] {nativeInstance.createAudio(bo.toByteArray(), c)};
        c.peer = p;
        c.onCompletion = onCompletion;
        return p;
    }*/
@Override
public PeerComponent createBrowserComponent(Object browserComponent) {
    long browserPeer = nativeInstance.createBrowserComponent(browserComponent);
    PeerComponent pc = createNativePeer(new long[] { browserPeer });
    nativeInstance.releasePeer(browserPeer);
    return pc;
}
Also used : PeerComponent(com.codename1.ui.PeerComponent)

Example 2 with BrowserComponent

use of com.codename1.ui.BrowserComponent in project CodenameOne by codenameone.

the class JavaSEPort method createBrowserComponent.

public PeerComponent createBrowserComponent(final Object parent) {
    java.awt.Container cnt = canvas.getParent();
    while (!(cnt instanceof JFrame)) {
        cnt = cnt.getParent();
        if (cnt == null) {
            return null;
        }
    }
    final java.awt.Container c = cnt;
    final Exception[] err = new Exception[1];
    final javafx.embed.swing.JFXPanel webContainer = new CN1JFXPanel();
    final SEBrowserComponent[] bc = new SEBrowserComponent[1];
    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            StackPane root = new StackPane();
            final WebView webView = new WebView();
            root.getChildren().add(webView);
            webContainer.setScene(new Scene(root));
            // now wait for the Swing side to finish initializing f'ing JavaFX is so broken its unbeliveable
            final SEBrowserComponent bcc = new SEBrowserComponent(JavaSEPort.this, ((JPanel) canvas.getParent()), webContainer, webView, (BrowserComponent) parent, hSelector, vSelector);
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    bc[0] = bcc;
                    synchronized (bc) {
                        bc.notify();
                    }
                }
            });
        }
    });
    Display.getInstance().invokeAndBlock(new Runnable() {

        @Override
        public void run() {
            synchronized (bc) {
                while (bc[0] == null && err[0] == null) {
                    try {
                        bc.wait(20);
                    } catch (InterruptedException ex) {
                    }
                }
            }
        }
    });
    return bc[0];
}
Also used : Scene(javafx.scene.Scene) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) EOFException(java.io.EOFException) FontFormatException(java.awt.FontFormatException) javax.swing(javax.swing) JFrame(javax.swing.JFrame) BrowserComponent(com.codename1.ui.BrowserComponent) WebView(javafx.scene.web.WebView) StackPane(javafx.scene.layout.StackPane) java.awt(java.awt)

Example 3 with BrowserComponent

use of com.codename1.ui.BrowserComponent in project CodenameOne by codenameone.

the class TestComponent method getComponentAt_int_int_browsercomponent.

private void getComponentAt_int_int_browsercomponent() {
    int w = Display.getInstance().getDisplayWidth();
    int h = Display.getInstance().getDisplayHeight();
    Form mapDemo = new Form("Maps", new LayeredLayout());
    Toolbar.setOnTopSideMenu(true);
    Toolbar tb = new Toolbar();
    mapDemo.setToolbar(tb);
    mapDemo.setTitle("Maps");
    tb.addCommandToSideMenu(new Command("Test") {

        @Override
        public void actionPerformed(ActionEvent e) {
        // testNestedScrollingLabels();
        }
    });
    BrowserComponent mc = new BrowserComponent();
    mapDemo.add(mc);
    mapDemo.show();
    TestUtils.waitForFormTitle("Maps", 2000);
    Component middleComponent = mapDemo.getComponentAt(w / 2, h / 2);
    assertTrue(mc == middleComponent || mc.contains(middleComponent), "Wrong component found in middle. Expected " + mc + " but found " + middleComponent);
    tb.openSideMenu();
    // wait for side menu to open
    TestUtils.waitFor(500);
    Component res = null;
    res = tb.getComponentAt(10, h / 2);
    // System.out.println("tb size = "+tb.getAbsoluteX()+", "+tb.getAbsoluteY()+", "+tb.getWidth()+", "+tb.getHeight());
    // System.out.println("mb size = "+tb.getMenuBar().getAbsoluteX()+", "+tb.getMenuBar().getAbsoluteY()+", "+tb.getMenuBar().getWidth()+", "+tb.getMenuBar().getHeight());
    // System.out.println("res is "+res);
    res = mapDemo.getComponentAt(10, h / 2);
    // Let's find the interaction dialog on the form
    Component interactionDialog = $("*", mapDemo).filter(c -> {
        return c instanceof InteractionDialog;
    }).asComponent();
    assertTrue(((InteractionDialog) interactionDialog).contains(res), "Toolbar is open so getComponentAt() should return something on the toolbar.  But received " + res + ".  Toolbar is " + tb);
}
Also used : Util(com.codename1.io.Util) BoxLayout(com.codename1.ui.layouts.BoxLayout) ActionEvent(com.codename1.ui.events.ActionEvent) NetworkManager(com.codename1.io.NetworkManager) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) NetworkEvent(com.codename1.io.NetworkEvent) ComponentSelector.$(com.codename1.ui.ComponentSelector.$) ConnectionRequest(com.codename1.io.ConnectionRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) AbstractTest(com.codename1.testing.AbstractTest) TestUtils(com.codename1.testing.TestUtils) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) Cookie(com.codename1.io.Cookie) FileSystemStorage(com.codename1.io.FileSystemStorage) IOException(java.io.IOException) ActionListener(com.codename1.ui.events.ActionListener) JSONParser(com.codename1.io.JSONParser) Log(com.codename1.io.Log) BorderLayout(com.codename1.ui.layouts.BorderLayout) InputStreamReader(java.io.InputStreamReader) Style(com.codename1.ui.plaf.Style) Coord(com.codename1.maps.Coord) ToastBar(com.codename1.components.ToastBar) InteractionDialog(com.codename1.components.InteractionDialog) InteractionDialog(com.codename1.components.InteractionDialog) ActionEvent(com.codename1.ui.events.ActionEvent) LayeredLayout(com.codename1.ui.layouts.LayeredLayout)

Example 4 with BrowserComponent

use of com.codename1.ui.BrowserComponent in project CodenameOne by codenameone.

the class TestComponent method testCookiesInBrowserComponent.

private void testCookiesInBrowserComponent() throws IOException {
    Cookie.clearCookiesFromStorage();
    Form f = new Form("CookiesInBrowser");
    String formName = "CookiesInBrowser";
    f.setName(formName);
    f.setLayout(new BorderLayout());
    BrowserComponent bc = new BrowserComponent();
    f.add(BorderLayout.CENTER, bc);
    f.show();
    TestUtils.waitForFormName(formName, 2000);
    String baseUrl = "http://solutions.weblite.ca/cn1tests/cookie";
    String clearCookiesUrl = baseUrl + "/reset.php";
    String setCookiesUrl = baseUrl + "/set.php";
    String checkCookiesUrl = baseUrl + "/check.php";
    String setCookiesUrlSession = baseUrl + "/set_session.php";
    final BrowserStatus status = new BrowserStatus(bc);
    bc.setURL(clearCookiesUrl);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    Map<String, Object> res = status.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    status.reset();
    bc.setURL(setCookiesUrl);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    res = status.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    status.reset();
    bc.setURL(clearCookiesUrl);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    res = status.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    status.reset();
    bc.setURL(setCookiesUrlSession);
    status.waitReady();
    status.reset();
    bc.setURL(checkCookiesUrl);
    status.waitReady();
    res = status.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    // Now let's try to share cookies between the browser component and
    // a connection request.
    ConnectionRequest.setUseNativeCookieStore(true);
    Cookie.clearCookiesFromStorage();
    BrowserComponent bc2 = new BrowserComponent();
    bc.getParent().replace(bc, bc2, null);
    bc = bc2;
    f.revalidate();
    final BrowserStatus status2 = new BrowserStatus(bc);
    bc.setURL(clearCookiesUrl);
    status2.waitReady();
    status2.reset();
    // First verify that the cookie is not set in either browser or connection request
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    // Next let's set the cookie in the browser, and verify that it is set in both
    // browser and connection request.
    status2.reset();
    bc.setURL(setCookiesUrl);
    status2.waitReady();
    status2.reset();
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    // Now let's delete the cookie in the browser and verify that it is deleted in
    // both the browser and connection request.
    status2.reset();
    bc.setURL(clearCookiesUrl);
    status2.waitReady();
    status2.reset();
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertBool(null == res.get("cookieval"), "Cookie should be null after clearing cookies but was " + res.get("cookieval"));
    // Now let's set the cookie in the ConnectionRequest and verify that it is set in both
    // connection request and browser.
    ConnectionRequest.fetchJSON(setCookiesUrl);
    res = ConnectionRequest.fetchJSON(checkCookiesUrl);
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
    status2.reset();
    bc.setURL(checkCookiesUrl);
    status2.waitReady();
    res = status2.getJSONContent();
    TestUtils.assertEqual("hello", res.get("cookieval"), "Cookie set to incorrect value.");
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout)

Example 5 with BrowserComponent

use of com.codename1.ui.BrowserComponent in project CodenameOne by codenameone.

the class VServAds method getPendingAd.

/**
 * {@inheritDoc}
 */
protected Component getPendingAd() {
    if (imageURL == null) {
        return null;
    }
    if (renderNotify != null && renderNotify.length() > 0) {
        ConnectionRequest c = new ConnectionRequest();
        c.setFailSilently(true);
        c.setUrl(renderNotify);
        c.setPost(false);
        NetworkManager.getInstance().addToQueue(c);
    }
    if ("image".equalsIgnoreCase(contentType)) {
        Button adComponent = new Button() {

            public void setIcon(Image icon) {
                if (icon != null && isScaleMode()) {
                    icon = icon.scaledWidth(Display.getInstance().getDisplayWidth());
                }
                super.setIcon(icon);
            }
        };
        adComponent.setUIID("Container");
        adComponent.getStyle().setBgColor(backgroundColor);
        adComponent.getStyle().setOpacity(0xff);
        ImageDownloadService imd = new ImageDownloadService(imageURL, adComponent);
        NetworkManager.getInstance().addToQueueAndWait(imd);
        /*adComponent.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    Display.getInstance().execute(getAdDestination());
                }
            });*/
        return adComponent;
    } else {
        WebBrowser wb = new WebBrowser();
        if (wb.getInternal() instanceof BrowserComponent) {
            BrowserComponent bc = (BrowserComponent) wb.getInternal();
            bc.setBrowserNavigationCallback(new BrowserNavigationCallback() {

                public boolean shouldNavigate(final String url) {
                    unlock(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            Display.getInstance().execute(url);
                        }
                    });
                    return false;
                }
            });
        }
        wb.setURL(imageURL);
        return wb;
    }
}
Also used : ImageDownloadService(com.codename1.io.services.ImageDownloadService) ConnectionRequest(com.codename1.io.ConnectionRequest) WebBrowser(com.codename1.components.WebBrowser) ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) BrowserNavigationCallback(com.codename1.ui.events.BrowserNavigationCallback)

Aggregations

ActionEvent (com.codename1.ui.events.ActionEvent)5 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 ActionListener (com.codename1.ui.events.ActionListener)3 ConnectionRequest (com.codename1.io.ConnectionRequest)2 BrowserComponent (com.codename1.ui.BrowserComponent)2 Style (com.codename1.ui.plaf.Style)2 IOException (java.io.IOException)2 InteractionDialog (com.codename1.components.InteractionDialog)1 ToastBar (com.codename1.components.ToastBar)1 WebBrowser (com.codename1.components.WebBrowser)1 Cookie (com.codename1.io.Cookie)1 FileSystemStorage (com.codename1.io.FileSystemStorage)1 JSONParser (com.codename1.io.JSONParser)1 Log (com.codename1.io.Log)1 NetworkEvent (com.codename1.io.NetworkEvent)1 NetworkManager (com.codename1.io.NetworkManager)1 Util (com.codename1.io.Util)1 ImageDownloadService (com.codename1.io.services.ImageDownloadService)1 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)1 Coord (com.codename1.maps.Coord)1