View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.gui;
25  
26  import java.util.*;
27  import java.awt.*;
28  import java.awt.event.*;
29  import javax.swing.*;
30  
31  import zeus.util.*;
32  import zeus.gui.fields.*;
33  
34  
35  public class NumberDialog extends JDialog
36                            implements ActionListener {
37     protected JLabel label;
38     protected WholeNumberField textfield;
39  
40     protected JButton okButton;
41     protected JButton cancelButton;
42     protected String OK = "Ok";
43     protected String CANCEL = "Cancel";
44     protected Long answer = null;
45  
46     public NumberDialog(Frame parent, String title, String label_string) {
47        this(parent,title);
48        this.setLabel(label_string);
49     }
50     public NumberDialog(Frame parent, String title, String label_string,
51                         int min, int max, int number ) {
52        this(parent,title,(long)min,(long)max);
53        this.setLabel(label_string);
54        this.setValue(number);
55     }
56     public NumberDialog(Frame parent, String title,
57                         String label_string, int number ) {
58        this(parent,title);
59        this.setLabel(label_string);
60        this.setValue(number);
61     }
62     public NumberDialog(Frame parent, String title, String label_string,
63                         long min, long max, long number ) {
64        this(parent,title,min,max);
65        this.setLabel(label_string);
66        this.setValue(number);
67     }
68     public NumberDialog(Frame parent, String title,
69                         String label_string, long number ) {
70        this(parent,title);
71        this.setLabel(label_string);
72        this.setValue(number);
73     }
74     public NumberDialog(Frame parent, String title) {
75        this(parent,title,Long.MIN_VALUE,Long.MAX_VALUE);
76     }
77     public NumberDialog(Frame parent, String title, long min, long max) {
78        super(parent,title,true);
79        getContentPane().setLayout(new BorderLayout());
80  
81        JPanel p1 = new JPanel();
82        p1.setLayout(new GridLayout(1,2,10,10));
83        okButton = new JButton(OK);
84        cancelButton = new JButton(CANCEL);
85        p1.add(okButton);
86        p1.add(cancelButton);
87  
88        label = new JLabel("--Undefined--");
89        textfield = new WholeNumberField(min,max);
90        textfield.setPreferredSize(new Dimension(100,20));
91        textfield.setMinimumSize(new Dimension(100,20));
92  
93        JPanel p2 = new JPanel();
94        GridBagLayout gb = new GridBagLayout();
95        GridBagConstraints gbc = new GridBagConstraints();
96        p2.setLayout(gb);
97  
98        gbc.fill = GridBagConstraints.NONE;
99        gbc.weightx = gbc.weighty = 0;
100       gbc.insets = new Insets(10,10,0,0);
101       gbc.anchor = GridBagConstraints.WEST;
102       gbc.gridwidth = 1;
103       gb.setConstraints(label,gbc);
104       p2.add(label);
105 
106       gbc.insets = new Insets(10,10,0,10);
107       gbc.fill = GridBagConstraints.HORIZONTAL;
108       gbc.gridwidth = GridBagConstraints.REMAINDER;
109       gbc.weightx = gbc.weighty = 1;
110       gb.setConstraints(textfield,gbc);
111       p2.add(textfield);
112 
113       JSeparator s1 = new JSeparator();
114       gbc.insets = new Insets(10,0,10,0);
115       gbc.fill = GridBagConstraints.HORIZONTAL;
116       gbc.gridwidth = GridBagConstraints.REMAINDER;
117       gbc.weightx = gbc.weighty = 0;
118       gb.setConstraints(s1,gbc);
119       p2.add(s1);
120 
121       getContentPane().add("Center",p2);
122       getContentPane().add("South",p1);
123 
124       // Event handling
125       textfield.addActionListener(this);
126       okButton.addActionListener(this);
127       cancelButton.addActionListener(this);
128 
129       this.addWindowListener(
130           new WindowAdapter() {
131              public void windowClosing(WindowEvent evt) {
132 		setVisible(false);
133 	     }
134           }
135       );
136       this.pack();
137    }
138 
139    public Long getValue() {
140       answer = null;
141       this.setVisible(true);
142       return answer;
143    }
144 
145    public void actionPerformed(ActionEvent evt) {
146       Object source = evt.getSource();
147 
148       if ( source == textfield || source == okButton ) {
149          answer = textfield.getValue();
150          if ( answer == null ) {
151             int result = JOptionPane.showConfirmDialog(this,
152                "Improperly specified integer value\nContinue?",
153 	       "Warning", JOptionPane.YES_NO_OPTION);
154             switch(result) {
155 	       case JOptionPane.YES_OPTION:
156                     break;
157 
158                case JOptionPane.NO_OPTION:
159                     return;
160             }
161          }
162          this.setVisible(false);
163       }
164       else if ( source == cancelButton ) {
165          this.setVisible(false);
166       }
167    }
168 
169    public String getLabel() { return label.getText(); }
170 
171    public void setLabel(String text) {
172       Assert.notNull(text);
173       label.setText(text);
174    }
175 
176    public void setValue(int number) {
177       textfield.setValue(number);
178    }
179    public void setValue(long number) {
180       textfield.setValue(number);
181    }
182 
183    public static void main(String arg[]) {
184       JFrame f = new JFrame("Test");
185       f.setSize(200,200);
186       f.show();
187       NumberDialog m = new NumberDialog(f,"Enter number");
188       m.setValue(123);
189       System.out.println(m.getValue());
190       System.exit(0);
191    }
192 
193 
194 }