1 /* 2 * $Id$ 3 * Copyright (c) 2005-2007 Bruno Lowagie, Carsten Hammer 4 * 5 * Permission is hereby granted, free of charge, to any person 6 * obtaining a copy of this software and associated documentation 7 * files (the "Software"), to deal in the Software without 8 * restriction, including without limitation the rights to use, 9 * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following 12 * conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 /* 28 * This class was originally published under the MPL by Bruno Lowagie 29 * and Carsten Hammer. 30 * It was a part of iText, a Java-PDF library. You can now use it under 31 * the MIT License; for backward compatibility you can also use it under 32 * the MPL version 1.1: http://www.mozilla.org/MPL/ 33 * A copy of the MPL license is bundled with the source code FYI. 34 */ 35 package com.lowagie.toolbox.swing; 36 37 import java.awt.Toolkit; 38 39 import javax.swing.*; 40 import javax.swing.text.*; 41 42 /** 43 * @since 2.1.1 (imported from itexttoolbox project) 44 */ 45 public class CustomDialog { 46 JDialog dialog = null; 47 private JTextField textField = new JTextField(10); 48 49 JPanel jPanel1 = new JPanel(); 50 PlainDocument plainDocument; 51 52 String msgString1; 53 54 Object[] array; 55 56 private JOptionPane optionPane; 57 CustomDialog(String msgstring, PlainDocument plainDocument)58 public CustomDialog(String msgstring, PlainDocument plainDocument) { 59 super(); 60 this.setMsgString1(msgstring); 61 this.plainDocument = plainDocument; 62 try { 63 jbInit(); 64 } catch (Exception ex) { 65 ex.printStackTrace(); 66 } 67 } 68 CustomDialog()69 public CustomDialog() { 70 this("Enter a value:", new PlainDocument()); 71 } 72 jbInit()73 private void jbInit() throws Exception { 74 textField.setDocument(plainDocument); 75 } 76 instantiateFloatDocument()77 public static PlainDocument instantiateFloatDocument() { 78 PlainDocument floatDocument = new PlainDocument() { 79 private static final long serialVersionUID = 1874451914306029381L; 80 81 public void insertString(int offset, String str, AttributeSet a) throws 82 BadLocationException { 83 super.insertString(offset, str, a); 84 try { 85 Float.parseFloat(super.getText(0, this.getLength())); 86 } catch (Exception ex) { 87 super.remove(offset, 1); 88 Toolkit.getDefaultToolkit().beep(); 89 return; 90 } 91 } 92 }; 93 return floatDocument; 94 } 95 instantiateIntegerDocument()96 public static PlainDocument instantiateIntegerDocument() { 97 PlainDocument intDocument = new PlainDocument() { 98 private static final long serialVersionUID = -8735280090112457273L; 99 100 public void insertString(int offset, String str, AttributeSet a) throws 101 BadLocationException { 102 super.insertString(offset, str, a); 103 try { 104 Integer.parseInt(super.getText(0, this.getLength())); 105 } catch (Exception ex) { 106 super.remove(offset, 1); 107 Toolkit.getDefaultToolkit().beep(); 108 return; 109 } 110 } 111 }; 112 return intDocument; 113 } 114 instantiateStringDocument()115 public static PlainDocument instantiateStringDocument() { 116 PlainDocument stringDocument = new PlainDocument() { 117 private static final long serialVersionUID = -1244429733606195330L; 118 119 public void insertString(int offset, String str, AttributeSet a) throws 120 BadLocationException { 121 super.insertString(offset, str, a); 122 } 123 }; 124 return stringDocument; 125 } 126 127 setMsgString1(String msgString1)128 public void setMsgString1(String msgString1) { 129 this.msgString1 = msgString1; 130 array = new Object[] {msgString1, textField}; 131 optionPane = new JOptionPane(array, JOptionPane.QUESTION_MESSAGE, 132 JOptionPane.OK_CANCEL_OPTION); 133 dialog = optionPane.createDialog(UIManager.getString( 134 "OptionPane.inputDialogTitle", null)); 135 } 136 showInputDialog(String startvalue)137 public String showInputDialog(String startvalue) { 138 textField.setText(startvalue); 139 dialog.setVisible(true); 140 dialog.dispose(); 141 return textField.getText(); 142 } 143 } 144