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.i18n; 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.ShowcaseRaw; 26 import com.google.gwt.sample.showcase.client.ShowcaseAnnotations.ShowcaseSource; 27 import com.google.gwt.user.client.rpc.AsyncCallback; 28 import com.google.gwt.user.client.ui.Anchor; 29 import com.google.gwt.user.client.ui.FlexTable; 30 import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter; 31 import com.google.gwt.user.client.ui.HTML; 32 import com.google.gwt.user.client.ui.HorizontalPanel; 33 import com.google.gwt.user.client.ui.ListBox; 34 import com.google.gwt.user.client.ui.TextBox; 35 import com.google.gwt.user.client.ui.Widget; 36 37 import java.util.Map; 38 39 /** 40 * Example file. 41 */ 42 @ShowcaseRaw({"ExampleConstants.java", "ExampleConstants.properties"}) 43 public class CwConstantsExample extends ContentWidget { 44 /** 45 * The constants used in this Content Widget. 46 */ 47 @ShowcaseSource 48 public static interface CwConstants extends Constants { cwConstantsExampleDescription()49 String cwConstantsExampleDescription(); 50 cwConstantsExampleLinkText()51 String cwConstantsExampleLinkText(); 52 cwConstantsExampleName()53 String cwConstantsExampleName(); 54 } 55 56 /** 57 * An instance of the constants. 58 */ 59 @ShowcaseData 60 private final CwConstants constants; 61 62 /** 63 * Constructor. 64 * 65 * @param constants the constants 66 */ CwConstantsExample(CwConstants constants)67 public CwConstantsExample(CwConstants constants) { 68 super(constants.cwConstantsExampleName(), 69 constants.cwConstantsExampleDescription(), false, 70 "ExampleConstants.java", "ExampleConstants.properties"); 71 this.constants = constants; 72 } 73 74 /** 75 * Initialize this example. 76 */ 77 @ShowcaseSource 78 @Override onInitialize()79 public Widget onInitialize() { 80 // Create the internationalized constants 81 ExampleConstants exampleConstants = GWT.create(ExampleConstants.class); 82 83 // Use a FlexTable to layout the content 84 FlexTable layout = new FlexTable(); 85 FlexCellFormatter formatter = layout.getFlexCellFormatter(); 86 layout.setCellSpacing(5); 87 88 // Add a link to the source code of the Interface 89 final String rawFile = getSimpleName(ExampleConstants.class); 90 Anchor link = new Anchor(rawFile); 91 link.addClickHandler(new ClickHandler() { 92 public void onClick(ClickEvent event) { 93 fireRawSourceRequest(rawFile + ".java"); 94 } 95 }); 96 HorizontalPanel linkPanel = new HorizontalPanel(); 97 linkPanel.setSpacing(3); 98 linkPanel.add(new HTML(constants.cwConstantsExampleLinkText())); 99 linkPanel.add(link); 100 layout.setWidget(0, 0, linkPanel); 101 formatter.setColSpan(0, 0, 2); 102 103 // Show the first name 104 TextBox firstNameBox = new TextBox(); 105 firstNameBox.setText("Amelie"); 106 firstNameBox.setWidth("17em"); 107 layout.setHTML(1, 0, exampleConstants.firstName()); 108 layout.setWidget(1, 1, firstNameBox); 109 110 // Show the last name 111 TextBox lastNameBox = new TextBox(); 112 lastNameBox.setText("Crutcher"); 113 lastNameBox.setWidth("17em"); 114 layout.setHTML(2, 0, exampleConstants.lastName()); 115 layout.setWidget(2, 1, lastNameBox); 116 117 // Create a list box of favorite colors 118 ListBox colorBox = new ListBox(); 119 Map<String, String> colorMap = exampleConstants.colorMap(); 120 for (Map.Entry<String, String> entry : colorMap.entrySet()) { 121 String key = entry.getKey(); 122 String value = entry.getValue(); 123 colorBox.addItem(value, key); 124 } 125 layout.setHTML(3, 0, exampleConstants.favoriteColor()); 126 layout.setWidget(3, 1, colorBox); 127 128 // Return the layout Widget 129 return layout; 130 } 131 132 @Override asyncOnInitialize(final AsyncCallback<Widget> callback)133 protected void asyncOnInitialize(final AsyncCallback<Widget> callback) { 134 GWT.runAsync(CwConstantsExample.class, new RunAsyncCallback() { 135 136 public void onFailure(Throwable caught) { 137 callback.onFailure(caught); 138 } 139 140 public void onSuccess() { 141 callback.onSuccess(onInitialize()); 142 } 143 }); 144 } 145 } 146