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
27 public class DeltaDialog extends JDialog {
28
31 public static final int UP = 1;
32
33
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
59 private Object[] answer;
60
61
62
67 public DeltaDialog(Object[] answer) {
68 super((Frame)null, "", true);
69
70 this.answer = answer;
71
72 guiInit();
73 }
74
75
76
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
96 public void setUnits(String s) {
97 lblUnits.setText(s);
98 }
99
100
101
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
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
181 class UpDownAction extends AbstractAction {
182 private int direction;
183
184
185
190 public UpDownAction(int dir) {
191 direction = dir;
192 }
193
194
195
200 public void actionPerformed(ActionEvent e) {
201 int val = 0;
202 try {
203 val = Integer.parseInt(txtSpin.getText());
204 }
205 catch(Exception ex) {
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