Example usage for com.google.gwt.gears.client Factory getInstance

List of usage examples for com.google.gwt.gears.client Factory getInstance

Introduction

In this page you can find the example usage for com.google.gwt.gears.client Factory getInstance.

Prototype

public static native Factory getInstance() ;

Source Link

Document

Returns the singleton instance of the Factory class or null if Gears is not installed or accessible.

Usage

From source file:com.google.gwt.gears.sample.database.client.DatabaseDemo.java

License:Apache License

public void onModuleLoad() {
    VerticalPanel outerPanel = new VerticalPanel();
    outerPanel.setSpacing(10);/*from  ww w .  j  av a  2s .c  o  m*/
    outerPanel.getElement().getStyle().setPropertyPx("margin", 15);

    HorizontalPanel textAndButtonsPanel = new HorizontalPanel();
    textAndButtonsPanel.add(new Label("Enter a Phrase: "));
    textAndButtonsPanel.add(input);
    textAndButtonsPanel.add(addButton);
    textAndButtonsPanel.add(clearButton);
    outerPanel.add(textAndButtonsPanel);
    outerPanel.add(new Label("Last 3 Entries:"));
    outerPanel.add(dataTable);

    for (int i = 0; i <= NUM_SAVED_ROWS; ++i) {
        dataTable.insertRow(i);
        for (int j = 0; j < NUM_DATA_TABLE_COLUMNS; j++) {
            dataTable.addCell(i);
        }
    }
    dataTable.setWidget(0, 0, new HTML("<b>Id</b>"));
    dataTable.setWidget(0, 1, new HTML("<b>Phrase</b>"));
    dataTable.setWidget(0, 2, new HTML("<b>Timestamp</b>"));

    // Create the database if it doesn't exist.
    try {
        db = Factory.getInstance().createDatabase();
        db.open("database-demo");
        db.execute(
                "CREATE TABLE IF NOT EXISTS Phrases (Id INTEGER PRIMARY KEY AUTOINCREMENT, Phrase VARCHAR(255), Timestamp INTEGER)");
    } catch (DatabaseException e) {
        RootPanel.get("demo").add(new HTML(
                "Error opening or creating database: <font color=\"red\">" + e.toString() + "</font>"));
        // Fatal error.  Do not build the interface.
        return;
    }

    input.addKeyboardListener(new KeyboardListenerAdapter() {
        @Override
        public void onKeyDown(Widget sender, char keyCode, int modifiers) {
            if (keyCode == KeyboardListener.KEY_ENTER) {
                insertPhrase();
            }
        }
    });

    addButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            insertPhrase();
        }
    });

    clearButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            clearPhrases();
            displayRecentPhrases();
        }
    });

    RootPanel.get("demo").add(outerPanel);
    displayRecentPhrases();
}

From source file:com.google.gwt.gears.sample.databasedemo.client.DatabaseDemo.java

License:Apache License

public void onModuleLoad() {
    VerticalPanel outerPanel = new VerticalPanel();
    outerPanel.setSpacing(10);/* ww w  . j av  a2  s. c om*/
    outerPanel.getElement().getStyle().setPropertyPx("margin", 15);

    HorizontalPanel textAndButtonsPanel = new HorizontalPanel();
    textAndButtonsPanel.add(new Label("Enter a Phrase: "));
    textAndButtonsPanel.add(input);
    textAndButtonsPanel.add(addButton);
    textAndButtonsPanel.add(clearButton);
    outerPanel.add(textAndButtonsPanel);
    outerPanel.add(new Label("Last 3 Entries:"));
    outerPanel.add(dataTable);

    for (int i = 0; i <= NUM_SAVED_ROWS; ++i) {
        dataTable.insertRow(i);
        for (int j = 0; j < NUM_DATA_TABLE_COLUMNS; j++) {
            dataTable.addCell(i);
        }
    }
    dataTable.setWidget(0, 0, new HTML("<b>Id</b>"));
    dataTable.setWidget(0, 1, new HTML("<b>Phrase</b>"));
    dataTable.setWidget(0, 2, new HTML("<b>Timestamp</b>"));

    // Create the database if it doesn't exist.
    try {
        db = Factory.getInstance().createDatabase();
        db.open("database-demo");
        db.execute(
                "CREATE TABLE IF NOT EXISTS Phrases (Id INTEGER PRIMARY KEY AUTOINCREMENT, Phrase VARCHAR(255), Timestamp INTEGER)");
    } catch (DatabaseException e) {
        RootPanel.get("demo").add(new HTML(
                "Error opening or creating database: <font color=\"red\">" + e.toString() + "</font>"));
        // Fatal error. Do not build the interface.
        return;
    }

    input.addKeyDownHandler(new KeyDownHandler() {
        public void onKeyDown(KeyDownEvent event) {
            if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                insertPhrase();
            }
        }
    });

    addButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            insertPhrase();
        }
    });

    clearButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            clearPhrases();
            displayRecentPhrases();
        }
    });

    RootPanel.get("demo").add(outerPanel);
    displayRecentPhrases();
}

From source file:com.google.gwt.gears.sample.gwtnote.client.local.GearsHelper.java

License:Apache License

/**
 * Creates a new GearsHelper.//from  w w w.  j  av a  2  s.  c om
 */
public GearsHelper() {
    try {
        db = Factory.getInstance().createDatabase();
        try {
            db.execute(DB_FETCH_NAMES);
        } catch (GearsException ex) {
            db.execute(DB_CREATE);
        }

        // initialize the localstore and have it update the manifest
        localServer = Factory.getInstance().createLocalServer();
        store = localServer.createManagedStore("GWTGearsNote");
        store.setManifestUrl(GWT.getModuleBaseURL() + "manifest");
        store.checkForUpdate();
        Timer t = new Timer() {
            @Override
            public void run() {
                int status = store.getUpdateStatus();
                if (status == ManagedResourceStore.UPDATE_OK) {
                    this.cancel();
                } else if (status == ManagedResourceStore.UPDATE_FAILED) {
                    this.cancel();
                }
            }
        };
        t.scheduleRepeating(100);
    } catch (Throwable t) { // not just GearsException b/c we can also have NPEs
        localServer = null;
        store = null;
        db = null;
    }
}

From source file:com.google.gwt.gears.sample.managedresourcestore.client.ManagedResourceStoreDemo.java

License:Apache License

public void onModuleLoad() {
    HorizontalPanel hpanel = new HorizontalPanel();
    hpanel.setSpacing(10);/*  ww w  . j  a  v  a 2 s .  c o m*/
    RootPanel.get("demo").add(hpanel);

    hpanel.add(createManagedResourceStoreButton);

    // See if we're already running from a ManagedResourceStore
    try {
        LocalServer server = Factory.getInstance().createLocalServer();

        // This check to see if the host page can be served locally
        if (server.canServeLocally(Window.Location.getPath())) {
            createManagedResourceStoreButton.setText("Refresh Manifest");

            // Give the user an opportunity to delete the MRS
            hpanel.add(removeManagedResourceStoreButton);
        }
    } catch (GearsException e) {
        // Gears probably isn't available (e.g. hosted mode)
    }

    createManagedResourceStoreButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            statusLabel.setText("Starting update");
            createManagedResourceStore();
        }
    });

    removeManagedResourceStoreButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            try {
                LocalServer server = Factory.getInstance().createLocalServer();
                ManagedResourceStore store = Offline.getManagedResourceStore();
                server.removeManagedStore(store.getName());
                statusLabel.setText("Removed ManagedResourceStore.  Press Refresh to see Online version.");
                createManagedResourceStoreButton.setEnabled(false);
                removeManagedResourceStoreButton.setEnabled(false);
            } catch (GearsException e) {
                statusLabel.setText(e.getMessage());
            }
        }
    });

    hpanel.add(statusLabel);
}

From source file:com.google.gwt.gears.sample.managedresourcestoredemo.client.ManagedResourceStoreDemo.java

License:Apache License

public void onModuleLoad() {
    HorizontalPanel hpanel = new HorizontalPanel();
    hpanel.setSpacing(10);/*from  w  w w  .j ava  2s  .  c om*/
    RootPanel.get("demo").add(hpanel);

    hpanel.add(createManagedResourceStoreButton);

    // See if we're already running from a ManagedResourceStore
    try {
        LocalServer server = Factory.getInstance().createLocalServer();

        // This check to see if the host page can be served locally
        if (server.canServeLocally(Window.Location.getPath())) {
            createManagedResourceStoreButton.setText("Refresh Manifest");

            // Give the user an opportunity to delete the MRS
            hpanel.add(removeManagedResourceStoreButton);
        }
    } catch (GearsException e) {
        // Gears probably isn't available (e.g. hosted mode)
    }

    createManagedResourceStoreButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            statusLabel.setText("Starting update");
            createManagedResourceStore();
        }
    });

    removeManagedResourceStoreButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            try {
                LocalServer server = Factory.getInstance().createLocalServer();
                ManagedResourceStore store = Offline.getManagedResourceStore();
                server.removeManagedStore(store.getName());
                statusLabel.setText("Removed ManagedResourceStore.  Press Refresh to see Online version.");
                createManagedResourceStoreButton.setEnabled(false);
                removeManagedResourceStoreButton.setEnabled(false);
            } catch (GearsException e) {
                statusLabel.setText(e.getMessage());
            }
        }
    });

    hpanel.add(statusLabel);
}

From source file:com.google.gwt.gears.sample.resourcestore.client.ResourceStoreDemo.java

License:Apache License

/**
 * This is the entry point method.//from  www  .j  a  v  a 2 s.  c om
 */
public void onModuleLoad() {
    captureButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            captureUrls();
        }
    });

    createStoreButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            createResourceStore();
        }
    });

    removeStoreButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            removeStore();
        }
    });

    uncaptureButton.addClickListener(new ClickListener() {
        public void onClick(Widget sender) {
            removeURLs();
        }
    });

    buttonPanel.add(createStoreButton);
    buttonPanel.add(captureButton);
    buttonPanel.add(uncaptureButton);
    buttonPanel.add(removeStoreButton);

    setButtons(true, false, false, false);

    VerticalPanel outerPanel = new VerticalPanel();
    outerPanel.add(buttonPanel);
    outerPanel.add(statusLabel);
    RootPanel.get("demo").add(outerPanel);

    localServer = Factory.getInstance().createLocalServer();
}

From source file:com.google.gwt.gears.sample.resourcestoredemo.client.ResourceStoreDemo.java

License:Apache License

/**
 * This is the entry point method.//from w  w w. j  a v  a2  s  .  co  m
 */
public void onModuleLoad() {
    captureButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            captureUrls();
        }
    });

    createStoreButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            createResourceStore();
        }
    });

    removeStoreButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            removeStore();
        }
    });

    uncaptureButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
            removeURLs();
        }
    });

    buttonPanel.add(createStoreButton);
    buttonPanel.add(captureButton);
    buttonPanel.add(uncaptureButton);
    buttonPanel.add(removeStoreButton);

    setButtons(true, false, false, false);

    VerticalPanel outerPanel = new VerticalPanel();
    outerPanel.add(buttonPanel);
    outerPanel.add(statusLabel);
    RootPanel.get("demo").add(outerPanel);

    localServer = Factory.getInstance().createLocalServer();
}

From source file:com.google.gwt.gears.sample.workerpool.client.WorkerPoolDemo.java

License:Apache License

/**
 * Creates a worker if necessary, and sends it a message to start computing Pi
 * to the number of digits requested. The method returns immediately after
 * sending the message. Meanwhile, the computation starts to run in the worker
 * when it receives the message./*from   w  ww.j  a  v a 2  s  .  co m*/
 * 
 * @param numDigits the number of digits of Pi to compute.
 */
private void doAsyncCalculation(int numDigits) {

    if (workerPool == null) {
        workerPool = Factory.getInstance().createWorkerPool();
        primeWorkerId = workerPool.createWorkerFromUrl("pi_spigot_worker.js");
        workerPool.setMessageHandler(this);
    }
    workerPool.sendMessage("START " + numDigits, primeWorkerId);
}

From source file:de.lilawelt.zmachine.client.offline.Downloader.java

License:Open Source License

private void doLoad(String url) {
    LocalServer server = Factory.getInstance().createLocalServer();

    addStatusLine("Starting download...");
    ResourceStoreUrlCaptureHandler callback = new ResourceStoreUrlCaptureHandler() {

        public void onCapture(ResourceStoreUrlCaptureEvent event) {
            if (event.isSuccess()) {
                addStatusLine("Download finished.");
            } else {
                addStatusLine("Download failed.");
            }//from ww  w  .  java2 s.co  m

        }
    };

    final ResourceStore resource = server.createStore("games");

    resource.capture(callback, url);

}

From source file:de.lilawelt.zmachine.client.offline.OfflineMenuImplReal.java

License:Open Source License

@Override
public void initialize() {
    p.add(statusLabel, DockPanel.CENTER);

    f = Factory.getInstance();
    if (f == null) {
        statusLabel.setText("May not access gears");
        return;//  w w  w  . j a v a2s . c  o m
    }

    if ((!f.hasPermission()) && (!f.getPermission())) {
        statusLabel.setText("May not access gears");
        return;
    }

    // Draw a different interface if the application can be served offline.
    try {
        LocalServer server = Factory.getInstance().createLocalServer();
        // This check to see if the host page can be served locally
        if (server.canServeLocally("/zmachine/hosted.html")) {
            createManagedResourceStoreOffline();
            createManagedResourceStore();
            showGames();
        } else {
            createManagedResourceStoreOffline();
            createManagedResourceStore();
        }
    } catch (GearsException ex) {
        statusLabel.setText("Fatal error: " + ex.getMessage());
        return;
    }

}