1 /******************************************************************************* 2 * Copyright 2012 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.backends.gwt.widgets; 18 19 import com.google.gwt.event.dom.client.ClickEvent; 20 import com.google.gwt.event.dom.client.ClickHandler; 21 import com.google.gwt.user.client.ui.Button; 22 import com.google.gwt.user.client.ui.DialogBox; 23 import com.google.gwt.user.client.ui.HasHorizontalAlignment; 24 import com.google.gwt.user.client.ui.HorizontalPanel; 25 import com.google.gwt.user.client.ui.VerticalPanel; 26 27 public class TextInputDialogBox extends DialogBox { 28 private PlaceholderTextBox textBox; 29 TextInputDialogBox(String title, String text, String placeholder)30 public TextInputDialogBox (String title, String text, String placeholder) { 31 // Set the dialog box's caption. 32 setText(title); 33 34 VerticalPanel vPanel = new VerticalPanel(); 35 HorizontalPanel hPanel = new HorizontalPanel(); 36 37 // Enable animation. 38 setAnimationEnabled(true); 39 40 // Enable glass background. 41 setGlassEnabled(true); 42 43 // Center this bad boy. 44 center(); 45 46 vPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT); 47 48 Button ok = new Button("OK"); 49 ok.addClickHandler(new ClickHandler() { 50 public void onClick (ClickEvent event) { 51 TextInputDialogBox.this.onPositive(); 52 } 53 }); 54 55 Button cancel = new Button("Cancel"); 56 cancel.addClickHandler(new ClickHandler() { 57 public void onClick (ClickEvent event) { 58 TextInputDialogBox.this.onNegative(); 59 } 60 }); 61 62 hPanel.add(ok); 63 hPanel.add(cancel); 64 65 textBox = new PlaceholderTextBox(); 66 textBox.setPlaceholder(placeholder); 67 textBox.setWidth("97%"); 68 textBox.setText(text); 69 vPanel.add(textBox); 70 vPanel.add(hPanel); 71 72 setWidget(vPanel); 73 } 74 onPositive()75 protected void onPositive () { 76 if (listener != null) { 77 listener.onPositive(textBox.getText()); 78 } 79 this.hide(); 80 } 81 onNegative()82 protected void onNegative () { 83 if (listener != null) { 84 listener.onNegative(); 85 } 86 this.hide(); 87 } 88 89 TextInputDialogListener listener; 90 setListener(TextInputDialogListener listener)91 public void setListener (TextInputDialogListener listener) { 92 this.listener = listener; 93 } 94 95 public interface TextInputDialogListener { onPositive(String text)96 void onPositive (String text); 97 onNegative()98 void onNegative (); 99 } 100 } 101