PageRenderTime 44ms CodeModel.GetById 13ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. import javax.swing.*;
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. public class InputDialog extends JDialog
  25. {
  26. public static final double MIN_WIDTH = 290.0;
  27. public static final double MIN_HEIGHT = 110.0;
  28. public InputDialog(Frame owner, String key, String message, String defValue) {
  29. super(owner, "Please input a value for " + key, true);
  30. parent = owner;
  31. init( key, message, defValue);
  32. }
  33. public InputDialog(Frame owner, String key, String message, String defValue, String[] opts, boolean allowUser) {
  34. super(owner, "Please input a value for " + key, true);
  35. parent = owner;
  36. init( key, message, defValue, opts, allowUser);
  37. }
  38. public InputDialog(Dialog owner, String key, String message, String defValue) {
  39. super(owner, "Please input a value for " + key, true);
  40. parent = owner;
  41. init( key, message, defValue);
  42. }
  43. public InputDialog(Dialog owner, String key, String message, String defValue, String[] opts, boolean allowUser) {
  44. super(owner, "Please choose a value for " + key, true);
  45. parent = owner;
  46. init( key, message, defValue, opts, allowUser);
  47. }
  48. public String getValue() {
  49. return selected;
  50. }
  51. public String showDialog() {
  52. pack();
  53. setLocationRelativeTo(parent);
  54. show();
  55. return getValue();
  56. }
  57. public Dimension getMinimumSize() {
  58. Dimension supMin = super.getMinimumSize();
  59. return new Dimension(
  60. (int)Math.max(supMin.getWidth(), MIN_WIDTH),
  61. (int)Math.max(supMin.getHeight(), MIN_HEIGHT)
  62. );
  63. }
  64. public Dimension getPreferredSize() {
  65. Dimension supMin = super.getPreferredSize();
  66. return new Dimension(
  67. (int)Math.max(supMin.getWidth(), MIN_WIDTH),
  68. (int)Math.max(supMin.getHeight(), MIN_HEIGHT)
  69. );
  70. }
  71. private void init(String key, String message, String defValue, String[] opts, boolean allowUser) {
  72. centPanel = new CentrePanel();
  73. centPanel.addComboBox(opts, defValue, allowUser);
  74. init(message);
  75. }
  76. private void init(String key, String message, String defValue) {
  77. centPanel = new CentrePanel();
  78. centPanel.addTextBox(defValue);
  79. init(message);
  80. }
  81. private void init(String message) {
  82. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  83. addWindowListener(
  84. new WindowAdapter() {
  85. public void windowClosing(WindowEvent e) {
  86. okJB.doClick();
  87. }
  88. }
  89. );
  90. setResizable(false);
  91. panel = new JPanel();
  92. GridBagLayout gb = new GridBagLayout();
  93. GridBagConstraints c = new GridBagConstraints();
  94. panel.setLayout(gb);
  95. c.gridx = 0;
  96. c.fill = GridBagConstraints.HORIZONTAL;
  97. c.weightx = 0.5;
  98. c.weighty = 0.5;
  99. c.gridy = 0;
  100. label = new JLabel(message);
  101. label.setHorizontalAlignment(SwingConstants.LEFT);
  102. gb.setConstraints(label, c);
  103. panel.add(label);
  104. c.gridy = 1;
  105. gb.setConstraints(centPanel, c);
  106. panel.add(centPanel);
  107. c.gridy = 2;
  108. c.fill = GridBagConstraints.NONE;
  109. c.anchor = GridBagConstraints.SOUTHEAST;
  110. okJB = new JButton("OK");
  111. getRootPane().setDefaultButton(okJB);
  112. okJB.addActionListener(al);
  113. gb.setConstraints(okJB, c);
  114. panel.add(okJB);
  115. setContentPane(panel);
  116. //getRootPane().registerKeyboardAction(al, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
  117. }
  118. ActionListener al = new ActionListener() {
  119. public void actionPerformed(ActionEvent evt) {
  120. selected = centPanel.getValue();
  121. setVisible(false);
  122. dispose();
  123. }
  124. };
  125. private JLabel label;
  126. private JButton okJB;
  127. private JPanel panel;
  128. private CentrePanel centPanel;
  129. private Window parent;
  130. String selected;
  131. private class CentrePanel extends JPanel {
  132. public CentrePanel() {
  133. super();
  134. setLayout(new BorderLayout());
  135. }
  136. public void addTextBox(String defValue) {
  137. box = null;
  138. add(text = new JTextField(defValue, 20), BorderLayout.CENTER);
  139. if(defValue !=null) {
  140. text.setSelectionStart(0);
  141. text.setSelectionEnd(defValue.length());
  142. }
  143. text.addActionListener(al);
  144. }
  145. public void addComboBox(String[] opts, String defValue, boolean editable) {
  146. text = null;
  147. add(box = new JComboBox(opts));
  148. if(defValue != null) {
  149. boolean defValInc = false;
  150. for(int i=0; i < opts.length; i++) {
  151. if(opts[i].equals(defValue)) {
  152. box.setSelectedIndex(i);
  153. defValInc = true;
  154. }
  155. }
  156. if(!defValInc) {
  157. box.addItem(defValue);
  158. box.setSelectedItem(defValue);
  159. }
  160. }
  161. box.setEditable(editable);
  162. box.addActionListener(
  163. new ActionListener() {
  164. public void actionPerformed(ActionEvent e) {
  165. selected = centPanel.getValue();
  166. }
  167. }
  168. );
  169. }
  170. public String getValue() {
  171. if(box == null)
  172. return text.getText();
  173. else
  174. return (String)box.getSelectedItem();
  175. }
  176. private JTextField text;
  177. private JComboBox box;
  178. }
  179. }