1 /* 2 * Copyright 2008 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.google.gwt.sample.showcase.client.content.widgets; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.core.client.RunAsyncCallback; 20 import com.google.gwt.event.dom.client.ClickEvent; 21 import com.google.gwt.event.dom.client.ClickHandler; 22 import com.google.gwt.i18n.client.Constants; 23 import com.google.gwt.sample.showcase.client.ContentWidget; 24 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseData; 25 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseSource; 26 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseStyle; 27 import com.google.gwt.user.client.Window; 28 import com.google.gwt.user.client.rpc.AsyncCallback; 29 import com.google.gwt.user.client.ui.Button; 30 import com.google.gwt.user.client.ui.HorizontalPanel; 31 import com.google.gwt.user.client.ui.Widget; 32 33 /** 34 * Example file. 35 */ 36 @ShowcaseStyle(".gwt-Button") 37 public class CwBasicButton extends ContentWidget { 38 /** 39 * The constants used in this Content Widget. 40 */ 41 @ShowcaseSource 42 public static interface CwConstants extends Constants { cwBasicButtonClickMessage()43 String cwBasicButtonClickMessage(); 44 cwBasicButtonDescription()45 String cwBasicButtonDescription(); 46 cwBasicButtonDisabled()47 String cwBasicButtonDisabled(); 48 cwBasicButtonName()49 String cwBasicButtonName(); 50 cwBasicButtonNormal()51 String cwBasicButtonNormal(); 52 } 53 54 /** 55 * An instance of the constants. 56 */ 57 @ShowcaseData 58 private final CwConstants constants; 59 60 /** 61 * Constructor. 62 * 63 * @param constants the constants 64 */ CwBasicButton(CwConstants constants)65 public CwBasicButton(CwConstants constants) { 66 super(constants.cwBasicButtonName(), constants.cwBasicButtonDescription(), 67 true); 68 this.constants = constants; 69 } 70 71 /** 72 * Initialize this example. 73 */ 74 @ShowcaseSource 75 @Override onInitialize()76 public Widget onInitialize() { 77 // Create a panel to align the widgets 78 HorizontalPanel hPanel = new HorizontalPanel(); 79 hPanel.setSpacing(10); 80 81 // Add a normal button 82 Button normalButton = new Button( 83 constants.cwBasicButtonNormal(), new ClickHandler() { 84 public void onClick(ClickEvent event) { 85 Window.alert(constants.cwBasicButtonClickMessage()); 86 } 87 }); 88 normalButton.ensureDebugId("cwBasicButton-normal"); 89 hPanel.add(normalButton); 90 91 // Add a disabled button 92 Button disabledButton = new Button(constants.cwBasicButtonDisabled()); 93 disabledButton.ensureDebugId("cwBasicButton-disabled"); 94 disabledButton.setEnabled(false); 95 hPanel.add(disabledButton); 96 97 // Return the panel 98 return hPanel; 99 } 100 101 @Override asyncOnInitialize(final AsyncCallback<Widget> callback)102 protected void asyncOnInitialize(final AsyncCallback<Widget> callback) { 103 GWT.runAsync(CwBasicButton.class, new RunAsyncCallback() { 104 105 public void onFailure(Throwable caught) { 106 callback.onFailure(caught); 107 } 108 109 public void onSuccess() { 110 callback.onSuccess(onInitialize()); 111 } 112 }); 113 } 114 } 115