com.google.gwt.sample.showcase.client.content.other.CwFrame.java Source code

Java tutorial

Introduction

Here is the source code for com.google.gwt.sample.showcase.client.content.other.CwFrame.java

Source

/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.sample.showcase.client.content.other;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.sample.showcase.client.ContentWidget;
import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseData;
import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseSource;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Example file.
 */
public class CwFrame extends ContentWidget {
    /**
     * The constants used in this Content Widget.
     */
    @ShowcaseSource
    public static interface CwConstants extends Constants {
        String cwFrameDescription();

        String cwFrameName();

        String cwFrameSetLocation();
    }

    /**
     * An instance of the constants.
     */
    @ShowcaseData
    private final CwConstants constants;

    /**
     * Constructor.
     *
     * @param constants the constants
     */
    public CwFrame(CwConstants constants) {
        super(constants.cwFrameName(), constants.cwFrameDescription(), false);
        this.constants = constants;
    }

    /**
     * Initialize this example.
     */
    @ShowcaseSource
    @Override
    public Widget onInitialize() {
        // Create a new frame
        String url = GWT.getModuleBaseURL();
        final Frame frame = new Frame(url);
        frame.setSize("700px", "300px");
        frame.ensureDebugId("cwFrame");

        // Create a form to set the location of the frame
        final TextBox locationBox = new TextBox();
        locationBox.setText(url);
        Button setLocationButton = new Button(constants.cwFrameSetLocation());
        HorizontalPanel optionsPanel = new HorizontalPanel();
        optionsPanel.setSpacing(8);
        optionsPanel.add(locationBox);
        optionsPanel.add(setLocationButton);

        // Change the location when the user clicks the button
        setLocationButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                frame.setUrl(locationBox.getText());
            }
        });

        // Change the location when the user presses enter
        locationBox.addKeyDownHandler(new KeyDownHandler() {
            public void onKeyDown(KeyDownEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                    frame.setUrl(locationBox.getText());
                }
            }

        });

        // Add everything to a panel and return it
        VerticalPanel vPanel = new VerticalPanel();
        vPanel.add(optionsPanel);
        vPanel.add(frame);
        return vPanel;
    }

    @Override
    protected void asyncOnInitialize(final AsyncCallback<Widget> callback) {
        GWT.runAsync(CwFrame.class, new RunAsyncCallback() {

            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }

            public void onSuccess() {
                callback.onSuccess(onInitialize());
            }
        });
    }
}