PageRenderTime 39ms CodeModel.GetById 28ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XInsert/src/InputDialog.java

#
Java | 205 lines | 158 code | 26 blank | 21 comment | 8 complexity | 6c3f723f98fe35ea2b675dd41dea7122 MD5 | raw file
  1/*
  2 *
  3 * InputDialog.java
  4 * Copyright (C) 2001 Dominic Stolerman
  5 * dstolerman@jedit.org
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License
  9 * as published by the Free Software Foundation; either version 2
 10 * of the License, or any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 */
 21 
 22import javax.swing.*;
 23import java.awt.*;
 24import java.awt.event.*;
 25
 26public class InputDialog extends JDialog 
 27{
 28	public static final double MIN_WIDTH  = 290.0;
 29	public static final double MIN_HEIGHT =  110.0;
 30	
 31	public InputDialog(Frame owner, String key, String message, String defValue) {
 32		super(owner, "Please input a value for " + key, true);
 33		parent = owner;
 34		init( key,  message,  defValue);
 35		}
 36	
 37	public InputDialog(Frame owner, String key, String message, String defValue, String[] opts, boolean allowUser) {
 38		super(owner, "Please input a value for " + key, true);
 39		parent = owner;
 40		init( key,  message,  defValue,  opts,  allowUser);
 41		}
 42	
 43	public InputDialog(Dialog owner, String key, String message, String defValue) {
 44		super(owner, "Please input a value for " + key, true);
 45		parent = owner;
 46		init( key,  message,  defValue);
 47		}
 48	
 49	public InputDialog(Dialog owner, String key, String message, String defValue, String[] opts, boolean allowUser) {
 50		super(owner, "Please choose a value for " + key, true);
 51		parent = owner;
 52		init( key, message, defValue, opts, allowUser);
 53		}
 54	
 55	public String getValue() {
 56		return selected;
 57		}
 58	
 59	public String showDialog() {
 60		pack();
 61		setLocationRelativeTo(parent);
 62		show();
 63		return getValue();
 64		}
 65	
 66	public Dimension getMinimumSize() {
 67		Dimension supMin = super.getMinimumSize();
 68		return new Dimension(
 69			(int)Math.max(supMin.getWidth(), MIN_WIDTH),
 70			(int)Math.max(supMin.getHeight(), MIN_HEIGHT)
 71			);
 72		}
 73	
 74	public Dimension getPreferredSize() {
 75		Dimension supMin = super.getPreferredSize();
 76		return new Dimension(
 77			(int)Math.max(supMin.getWidth(), MIN_WIDTH),
 78			(int)Math.max(supMin.getHeight(), MIN_HEIGHT)
 79			);
 80		}
 81	
 82	private void init(String key, String message, String defValue, String[] opts, boolean allowUser) {
 83		centPanel = new CentrePanel();
 84		centPanel.addComboBox(opts, defValue, allowUser);
 85		init(message);
 86		}
 87	
 88	private void init(String key, String message, String defValue) {
 89		centPanel = new CentrePanel();
 90		centPanel.addTextBox(defValue);
 91		init(message);
 92		}
 93	
 94	private void init(String message) {
 95		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
 96		addWindowListener(
 97			new WindowAdapter() {
 98				public void windowClosing(WindowEvent e) {
 99					okJB.doClick();
100					}
101				}
102			);
103		setResizable(false);
104		panel = new JPanel();
105		GridBagLayout gb = new GridBagLayout();
106		GridBagConstraints c = new GridBagConstraints();
107		panel.setLayout(gb);
108		c.gridx = 0;
109		c.fill = GridBagConstraints.HORIZONTAL;
110		c.weightx =  0.5;
111		c.weighty = 0.5;
112
113		c.gridy = 0;
114		label = new JLabel(message);
115		label.setHorizontalAlignment(SwingConstants.LEFT);
116		gb.setConstraints(label, c);
117		panel.add(label);
118
119		c.gridy = 1;
120		gb.setConstraints(centPanel, c);
121		panel.add(centPanel);
122
123		c.gridy = 2;
124		c.fill = GridBagConstraints.NONE;
125		c.anchor = GridBagConstraints.SOUTHEAST;
126		okJB = new JButton("OK");
127		getRootPane().setDefaultButton(okJB);
128		okJB.addActionListener(al);
129		gb.setConstraints(okJB, c);
130		panel.add(okJB);
131
132		setContentPane(panel);
133		//getRootPane().registerKeyboardAction(al, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
134		}
135
136	ActionListener al = new ActionListener() {
137		public void actionPerformed(ActionEvent evt) {
138			selected = centPanel.getValue();
139			setVisible(false);
140			dispose();
141			}
142		};
143
144	private JLabel label;
145	private JButton okJB;
146	private JPanel panel;
147	private CentrePanel centPanel;
148	private Window parent;
149
150	String selected;
151	
152	private class CentrePanel extends JPanel {
153		public CentrePanel() {
154			super();
155			setLayout(new BorderLayout());
156			}
157
158		public void addTextBox(String defValue)	{
159			box = null;
160			add(text = new JTextField(defValue, 20), BorderLayout.CENTER);
161			if(defValue !=null) {
162				text.setSelectionStart(0);
163				text.setSelectionEnd(defValue.length());
164				}
165			text.addActionListener(al);
166			}
167
168		public void addComboBox(String[] opts, String defValue, boolean editable) {
169			text = null;
170			add(box = new JComboBox(opts));
171			if(defValue != null) {
172				boolean defValInc = false;
173				for(int i=0; i < opts.length; i++) {
174					if(opts[i].equals(defValue)) {
175						box.setSelectedIndex(i);
176						defValInc = true;
177						}
178					}
179				if(!defValInc) {
180					box.addItem(defValue);
181					box.setSelectedItem(defValue);
182					}
183				}
184			box.setEditable(editable);
185 			box.addActionListener(
186				new ActionListener() {
187					public void actionPerformed(ActionEvent e) {
188						selected = centPanel.getValue();
189						}
190					}
191				);
192		}
193		
194		public String getValue() {
195			if(box == null)
196				return text.getText();
197			else
198				return (String)box.getSelectedItem();
199			}
200		
201		private JTextField text;
202		private JComboBox box;
203	}
204}
205