Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1974 jgill 1
/**
2
 * AntShield - A project release tool for ant.
3
 */
4
package com.erggroup.mass.ant;
5
 
6
import javax.swing.*;
7
import javax.swing.border.EtchedBorder;
8
import java.util.ArrayList;
9
import java.util.Iterator;
10
import java.util.AbstractSet;
11
import java.awt.*;
12
import java.awt.event.*;
13
 
14
/**
15
 * AntShieldFieldFrame
16
 * A dialog box that allows users to enter values for
17
 * specified fields, displaying either text boxes or combo boxes.
18
 */
19
public class AntShieldFieldFrame extends JFrame {
20
 
21
    private int numfields = 0;
22
    private ArrayList fields = null;
23
    private ArrayList labelArray = new ArrayList();
24
    private ArrayList fieldArray = new ArrayList();
25
    private ArrayList comboArray = new ArrayList();
26
    private boolean ok = false;
27
    private AntShieldLock lock = new AntShieldLock();
28
 
29
    /**
30
     * Constructor.  Builds and displays dialog.
31
     * @param fieldlist
32
     */
33
    public AntShieldFieldFrame(ArrayList fieldlist)
34
    {
35
        fields = fieldlist;
36
        init();
37
        setPosition();
38
        updateFields();
39
        pack();
40
        setVisible(true);
41
        lock.acquire();
42
    }
43
 
44
    /**
45
     * Build this frame from components.
46
     */
47
    private void init()
48
    {
49
        setTitle("Antshield Option Fields");
50
 
51
        // Proper close if the user closes the window.
52
        addWindowListener(new AntShieldFieldWindowClose(this));
53
 
54
        // OK button.
55
        JButton okButton = new JButton();
56
        okButton.setActionCommand("OK");
57
        okButton.setText("OK");
58
        okButton.addActionListener(new AntShieldFieldOKAction(this));
59
 
60
        // Cancel button.
61
        JButton cancelButton = new JButton();
62
        cancelButton.setActionCommand("Cancel");
63
        cancelButton.setText("Cancel");
64
        cancelButton.addActionListener(new AntShieldFieldCancelAction(this));
65
 
66
        // South-east panel - holds the OK and Cancel buttons.
67
        JPanel sePanel = new JPanel();
68
        sePanel.add(okButton, null);
69
        sePanel.add(cancelButton, null);
70
 
71
        // South panel - holds south-east panel
72
        JPanel southPanel = new JPanel();
73
        southPanel.setLayout(new BorderLayout());
74
        southPanel.setBorder(new EtchedBorder());
75
        southPanel.add(sePanel, BorderLayout.EAST);
76
 
77
        // Fill up arrays of JLabels, JTextFields and JComboBoxes
78
        numfields = 0;
79
        Iterator i = fields.iterator();
80
        while (i.hasNext())
81
        {
82
            FieldElement e = (FieldElement)i.next();
83
 
84
            JLabel l = new JLabel();
85
            l.setText(" " + e.getPrompt() + " ");
86
            labelArray.add(l);
87
 
88
            String s = e.getDefaultValue();
89
            if (s == null)
90
            {
91
                s = new String("");
92
            }
93
 
94
            // No options specified - us a JTextField
95
            if (e.getOptions().size() == 0)
96
            {
97
                JTextField f = new JTextField();
98
                f.setText(s);
99
                fieldArray.add(f);
100
                comboArray.add(null);
101
            }
102
 
103
            // Options specified - use a JComboBox
104
            else
105
            {
106
                JComboBox c = new JComboBox();
107
                c.setEditable(true);
108
                c.setSelectedItem(s);
109
                // Populate combo box with specified options
110
                AbstractSet options = e.getOptions();
111
                Iterator j = options.iterator();
112
                while (j.hasNext())
113
                {
114
                    c.addItem(j.next());
115
                }
116
                fieldArray.add(null);
117
                comboArray.add(c);
118
            }
119
 
120
            numfields++;
121
        }
122
 
123
        // Panel with n x 2 grid
124
        JPanel gridPanel = new JPanel();
125
        gridPanel.setLayout(new GridLayout(numfields, 2));
126
        for (int n = 0; n < numfields; n++)
127
        {
128
            // Left-hand side gets the JLabel
129
            gridPanel.add((JLabel)labelArray.get(n));
130
 
131
            // Right-hand side gets the JTextField or JComboBox
132
            if (fieldArray.get(n) != null)
133
            {
134
                gridPanel.add((JTextField)fieldArray.get(n));
135
            }
136
            else
137
            {
138
                gridPanel.add((JComboBox)comboArray.get(n));
139
            }
140
        }
141
 
142
        // Keep our grid panel centred and top-justified
143
        JPanel centrePanel = new JPanel();
144
        centrePanel.setLayout(new BorderLayout());
145
        centrePanel.add(gridPanel, BorderLayout.NORTH);
146
 
147
        // A scroll pane for large numbers of fields
148
        JScrollPane scrollPanel = new JScrollPane();
149
        scrollPanel.getViewport().add(centrePanel, null);
150
 
151
        // Format everything with nice indentation
152
        Box box1 = new Box(BoxLayout.X_AXIS);
153
        box1.add(Box.createHorizontalStrut(20));
154
        box1.add(scrollPanel);
155
        box1.add(Box.createHorizontalStrut(20));
156
 
157
        Box box2 = new Box(BoxLayout.Y_AXIS);
158
        box2.add(Box.createVerticalStrut(20));
159
        box2.add(box1);
160
        box2.add(Box.createVerticalStrut(20));
161
 
162
        getContentPane().setLayout(new BorderLayout());
163
        getContentPane().add(southPanel,  BorderLayout.SOUTH);
164
        getContentPane().add(box2, BorderLayout.CENTER);
165
 
166
        // Hitting ENTER activates the OK button
167
        getRootPane().setDefaultButton(okButton);
168
 
169
        pack();
170
    } // init
171
 
172
    /**
173
     * Position this frame in the centre of the screen,
174
     * resizing if necessary.
175
     */
176
    private void setPosition()
177
    {
178
        int scrnWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
179
        int scrnHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
180
        int width = getWidth();
181
        int height = getHeight();
182
        if (width > scrnWidth/2)
183
        {
184
            width = scrnWidth/2;
185
        }
186
        if (width < scrnWidth/8)
187
        {
188
            width = scrnWidth/8;
189
        }
190
        if (height > scrnHeight/2)
191
        {
192
            height = scrnHeight/2;
193
        }
194
        if (height < scrnHeight/8)
195
        {
196
            height = scrnHeight/8;
197
        }
198
        setSize(width, height);
199
        int xpos = scrnWidth/2 - width/2;
200
        int ypos = scrnHeight/2 - height/2;
201
        setLocation(xpos, ypos);
202
    } // setPosition
203
 
204
    /**
205
     * Calls setValue() on each FieldElement with values
206
     * supplied in the text and combo boxes.
207
     */
208
    private void updateFields()
209
    {
210
        for (int i = 0; i < numfields; i++)
211
        {
212
            FieldElement e = (FieldElement)fields.get(i);
213
            String v;
214
            if (fieldArray.get(i) != null)
215
            {
216
                v = ((JTextField)fieldArray.get(i)).getText();
217
            }
218
            else
219
            {
220
                v = (String)((JComboBox)comboArray.get(i)).getSelectedItem();
221
            }
222
            if (v == null)
223
            {
224
                v = new String("");
225
            }
226
            e.setValue(v.trim());
227
        }
228
    } // updateFields
229
 
230
    /**
231
     * Did we exit successfully?
232
     * @return
233
     */
234
    public boolean isOk()
235
    {
236
        return ok;
237
    } // isOk
238
 
239
    /**
240
     * Handle the OK button by checking that all fields have values.
241
     * If not, pop up a dialog.
242
     */
243
    public void handleOk()
244
    {
245
        ok = true;
246
        updateFields();
247
        Iterator i = fields.iterator();
248
        while (i.hasNext())
249
        {
250
            FieldElement element = (FieldElement)i.next();
251
            String s = element.getValue();
252
            if ((s == null) || (s.length() < 1))
253
            {
254
                ok = false;
255
                break;
256
            }
257
        }
258
        if (ok)
259
        {
260
            close();
261
        }
262
        else
263
        {
264
            new AntShieldRetryDialog("Values must be provided for all fields.");
265
        }
266
    } // handleOk
267
 
268
    /**
269
     * Handle the cancel button.
270
     */
271
    public void handleCancel()
272
    {
273
        ok = false;
274
        close();
275
    } // handleCancel
276
 
277
    /**
278
     * Close this frame.
279
     */
280
    public void close()
281
    {
282
        lock.release();
283
        setVisible(false);
284
    } // close
285
}
286
 
287
class AntShieldFieldOKAction implements ActionListener
288
{
289
    private AntShieldFieldFrame adaptee;
290
 
291
    public AntShieldFieldOKAction(AntShieldFieldFrame a)
292
    {
293
        adaptee = a;
294
    }
295
 
296
    public void actionPerformed(ActionEvent e)
297
    {
298
        adaptee.handleOk();
299
    }
300
}
301
 
302
class AntShieldFieldCancelAction implements ActionListener
303
{
304
    private AntShieldFieldFrame adaptee;
305
 
306
    public AntShieldFieldCancelAction(AntShieldFieldFrame a)
307
    {
308
        adaptee = a;
309
    }
310
 
311
    public void actionPerformed(ActionEvent e)
312
    {
313
        adaptee.handleCancel();
314
    }
315
}
316
 
317
class AntShieldFieldWindowClose extends WindowAdapter
318
{
319
    private AntShieldFieldFrame parent;
320
 
321
    public AntShieldFieldWindowClose(AntShieldFieldFrame a)
322
    {
323
        parent = a;
324
    }
325
 
326
    public void windowClosing(WindowEvent e)
327
    {
328
        parent.close();
329
    }
330
}