001 /* 002 * LAPIS lightweight structured text processing system 003 * 004 * Copyright (C) 1998-2002 Carnegie Mellon University, 005 * Copyright (C) 2003 Massachusetts Institute of Technology. 006 * All rights reserved. 007 * 008 * This library is free software; you can redistribute it 009 * and/or modify it under the terms of the GNU General 010 * Public License as published by the Free Software 011 * Foundation, version 2. 012 * 013 * LAPIS homepage: http://graphics.lcs.mit.edu/lapis/ 014 */ 015 /* A simple Dialog box that contains 3 buttons and an input field. 016 017 */ 018 019 020 021 022 023 024 package lapisx.swing; 025 026 import javax.swing.JOptionPane; 027 import javax.swing.JDialog; 028 import javax.swing.JTextField; 029 import java.beans.*; //Property change stuff 030 import java.awt.*; 031 import java.awt.event.*; 032 033 034 035 public class InputThreeButtonDialog extends JDialog { 036 private String typedText = null; 037 038 039 private JOptionPane optionPane; 040 041 private int selection; 042 043 public String getText() { 044 return typedText; 045 } 046 047 public int getSelection (){ 048 return selection; 049 } 050 public InputThreeButtonDialog(Frame aFrame, String title, String message, String initialText, Object[] option) { 051 super(aFrame, true); 052 setTitle(title); 053 054 final String msgString = message; 055 final Object[] options = option; 056 057 final JTextField textField = new JTextField(initialText); 058 Object[] array = {msgString, textField}; 059 060 optionPane = new JOptionPane(array, 061 JOptionPane.QUESTION_MESSAGE, 062 JOptionPane.YES_NO_CANCEL_OPTION, 063 null, 064 options, 065 null); 066 setContentPane(optionPane); 067 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 068 addWindowListener(new WindowAdapter() { 069 public void windowClosing(WindowEvent we) { 070 /* 071 * Instead of directly closing the window, 072 * we're going to change the JOptionPane's 073 * value property. 074 */ 075 optionPane.setValue(new Integer( 076 JOptionPane.CLOSED_OPTION)); 077 } 078 }); 079 080 textField.addActionListener(new ActionListener() { 081 public void actionPerformed(ActionEvent e) { 082 optionPane.setValue(options[0]); 083 } 084 }); 085 086 optionPane.addPropertyChangeListener(new PropertyChangeListener() { 087 public void propertyChange(PropertyChangeEvent e) { 088 String prop = e.getPropertyName(); 089 090 if (isVisible() 091 && (e.getSource() == optionPane) 092 && (prop.equals(JOptionPane.VALUE_PROPERTY) || 093 prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) { 094 Object value = optionPane.getValue(); 095 if (value == JOptionPane.UNINITIALIZED_VALUE) { 096 //ignore reset 097 return; 098 } 099 100 // Reset the JOptionPane's value. 101 // If you don't do this, then if the user 102 // presses the same button next time, no 103 // property change event will be fired. 104 optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE); 105 //user has chosed typed field 106 if (value.equals(options[0])) { 107 typedText = textField.getText(); 108 if (typedText.trim().equals("")) 109 typedText = null; 110 selection = JOptionPane.YES_OPTION; 111 setVisible(false); 112 113 } 114 //user has chosen pre-set field 115 else if (value.equals(options[1])){ 116 selection = JOptionPane.NO_OPTION; 117 setVisible(false); 118 } 119 else { 120 selection = JOptionPane.CANCEL_OPTION; 121 setVisible(false); 122 } 123 } 124 } 125 }); 126 127 128 129 130 131 } 132 133 protected void setInitialPosition () { 134 Component parent = getParent (); 135 Dimension size = getSize(); 136 Dimension parentSize = (parent != null) ? parent.getSize() : Toolkit.getDefaultToolkit().getScreenSize(); 137 Point origin = (parent != null) ? parent.getLocationOnScreen () : new Point (0, 0); 138 139 if (parentSize != null) { 140 int x = Math.max (0, origin.x + (parentSize.width - size.width) / 2); 141 int y = Math.max (0, origin.y + (parentSize.height - size.height) / 2); 142 setLocation (x, y); 143 } 144 } 145 public void show () { 146 setInitialPosition (); 147 super.show (); 148 } 149 150 151 }