1    package com.instantbank.common.uiutils;
2    
3    import java.awt.Frame;
4    import java.awt.BorderLayout;
5    import java.awt.GridLayout;
6    import java.awt.event.ActionListener;
7    import java.awt.event.ActionEvent;
8    import javax.swing.JDialog;
9    import javax.swing.JPanel;
10   import javax.swing.JButton;
11   import javax.swing.JLabel;
12   import javax.swing.ButtonGroup;
13   import javax.swing.JRadioButton;
14   import javax.swing.JTextField;
15   import javax.swing.BorderFactory;
16   import javax.swing.SwingConstants;
17   import javax.swing.Box;
18   import javax.swing.AbstractAction;
19   
20   /**
21    * Modal dialog that allows to capture an integer value. The user interface
22    * restricts the input to numeric strings.
23    *
24    * @author InstantBank (Rodrigo Lopez)
25    * @created nov-2002
26    */
27   public class DeltaDialog extends JDialog {
28     /**
29      * Direction of the spinning button
30      */
31     public static final int UP = 1;
32   
33     /**
34      * Direction of the spinning button.
35      */
36     public static final int DOWN = -1;
37   
38     JPanel pnlRadio = new JPanel();
39     JPanel pnlCtrl = new JPanel();
40     JPanel pnlInf = new JPanel();
41     JButton btnOK = new JButton("OK");
42     JButton btnCancel = new JButton("Cancel");
43     JPanel pnlTitle = new JPanel();
44     JLabel lblTitle = new JLabel();
45     JLabel lblSubTitle = new JLabel();
46     JLabel lblUnits = new JLabel();
47     final ButtonGroup group = new ButtonGroup();
48     JRadioButton rbtPlus = new JRadioButton("Plus", true);
49     JRadioButton rbtMinus = new JRadioButton("Minus", false);
50     Spinner spin = new Spinner();
51     JTextField txtSpin = new JTextField(4);
52     JPanel pnlSpin = new JPanel();
53     JPanel pnlSpin1 = new JPanel();
54   
55     /**
56      * Place holder for the answer after the user interaction. Must be
57      * provided by the caller.
58      */
59     private Object[] answer;
60   
61   
62     /**
63      * DeltaDialog constructor.
64      *
65      * @param answer Place holder for the answer.
66      */
67     public DeltaDialog(Object[] answer) {
68       super((Frame)null, "", true);
69       //Modal.
70       this.answer = answer;
71   
72       guiInit();
73     }
74   
75   
76     /**
77      * Sets a title inside the dialog window.
78      *
79      * @param s The new header value
80      */
81     public void setHeader(String s) {
82       lblTitle.setText(s);
83     }
84   
85   
86     public void setSubHeader(String s) {
87       lblSubTitle.setText(s);
88     }
89   
90   
91     /**
92      * Sets a text identifying the captured units.
93      *
94      * @param s The new units value
95      */
96     public void setUnits(String s) {
97       lblUnits.setText(s);
98     }
99   
100  
101    /**
102     * Inits all the swing controls in this dialog.
103     */
104    private void guiInit() {
105      this.setSize(295, 210);
106  
107      getContentPane().add(pnlCtrl, BorderLayout.CENTER);
108  
109      lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
110      lblSubTitle.setHorizontalAlignment(SwingConstants.CENTER);
111      pnlTitle.setLayout(new GridLayout(2, 1));
112      pnlTitle.add(lblTitle);
113      pnlTitle.add(lblSubTitle);
114      pnlTitle.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
115      getContentPane().add(pnlTitle, BorderLayout.NORTH);
116  
117      group.add(rbtPlus);
118      group.add(rbtMinus);
119      pnlRadio.setLayout(new GridLayout(2, 1));
120      pnlRadio.add(rbtPlus);
121      pnlRadio.add(rbtMinus);
122  
123      pnlCtrl.add(pnlRadio);
124  
125      pnlSpin1.setLayout(new GridLayout(2, 1));
126      txtSpin.setDocument(new IntegerDocument(IntegerDocument.POSITIVE));
127      pnlSpin1.add(txtSpin);
128      lblUnits.setHorizontalAlignment(SwingConstants.CENTER);
129      pnlSpin1.add(lblUnits);
130      pnlSpin.add(pnlSpin1);
131      pnlSpin.add(spin);
132      pnlSpin.setBorder(BorderFactory.createEtchedBorder());
133      pnlCtrl.add(Box.createHorizontalStrut(10));
134      pnlCtrl.add(pnlSpin);
135  
136      pnlInf.add(btnOK);
137      pnlInf.add(btnCancel);
138      getContentPane().add(pnlInf, BorderLayout.SOUTH);
139  
140      spin.getIncrementButton().addActionListener(new UpDownAction(UP));
141      spin.getDecrementButton().addActionListener(new UpDownAction(DOWN));
142  
143      btnOK.addActionListener(
144        new ActionListener() {
145          int val = 0;
146  
147  
148          public void actionPerformed(ActionEvent e) {
149            try {
150              String txt = txtSpin.getText().trim();
151              if(!txt.equals("")) {
152                val = Integer.parseInt(txt) * (rbtPlus.isSelected() ? 1 : -1);
153              }
154  
155            }
156            catch(Exception ex) {
157              //Cannot enter here
158            }
159            answer[1] = new Long(val);
160            answer[0] = new Object();
161            DeltaDialog.this.dispose();
162          }
163        }
164        );
165  
166      btnCancel.addActionListener(
167        new ActionListener() {
168          public void actionPerformed(ActionEvent e) {
169            answer[0] = null;
170            DeltaDialog.this.dispose();
171          }
172        }
173        );
174    }
175  
176  
177    /**
178     * Auxiliary class. Allows to define simetric behavior for the Up and Down
179     * spinning buttons.
180     */
181    class UpDownAction extends AbstractAction {
182      private int direction;  // UP or DOWN
183  
184  
185      /**
186       * UpDownAction constructor.
187       *
188       * @param dir Direction for the action (UP, DOWN).
189       */
190      public UpDownAction(int dir) {
191        direction = dir;
192      }
193  
194  
195      /**
196       * Common (simetric) action bound to the Up and Down spinning buttons.
197       *
198       * @param e Description of the Parameter
199       */
200      public void actionPerformed(ActionEvent e) {
201        int val = 0;
202        try {
203          val = Integer.parseInt(txtSpin.getText());
204        }
205        catch(Exception ex) {  //If text is null.
206          val = 0;
207        }
208        if(direction == UP) {
209          txtSpin.setText(Integer.toString(++val));
210        }
211        else {
212          txtSpin.setText(Integer.toString(val == 0 ? 0 : --val));
213        }
214      }
215    }
216  }
217