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.*;
9
import java.awt.*;
10
import java.awt.event.*;
11
 
12
/**
13
 * AntShieldReleaseFrame
14
 * A dialog box that allows users to select an allowed project
15
 * and release for a specified package.
16
 */
17
public class AntShieldReleaseFrame extends JFrame {
18
 
19
    private String name = null;
20
    private String type = null;
21
    private String project = new String();
22
    private String release = new String();
23
    private String releaseLabelString;
24
    private String titleString;
25
    private ArrayList projects = new ArrayList();
26
    private ArrayList releases = new ArrayList();
27
    private JComboBox projectBox = new JComboBox();
28
    private JComboBox releaseBox = new JComboBox();
29
 
30
    private boolean ok = false;
31
    private boolean undo = false;
32
    private AntShieldLock lock = new AntShieldLock();
33
 
34
    /**
35
     * Constructor.  Populate projects and releases with supplied
36
     * parameters, build the dialog box, and display self.
37
     * @param pkgname
38
     * @param reltype
39
     * @param rellist
40
     */
41
    public AntShieldReleaseFrame(String pkgname, String reltype, ArrayList rellist, boolean undoflag)
42
    {
43
        name = pkgname;
44
        type = reltype;
45
        undo = undoflag;
46
 
47
        if (undo)
48
        {
49
            titleString = new String("Antshield Existing Release - Package \"" + name + "\"");
50
            releaseLabelString = new String("Existing release");
51
        }
52
        else
53
        {
54
            titleString = new String("Antshield New Release - Package \"" + name + "\"");
55
            releaseLabelString = new String("New release");
56
        }
57
        Iterator i = rellist.iterator();
58
        while (i.hasNext())
59
        {
60
            String rel = (String)i.next();
61
            String proj = rel.substring(rel.lastIndexOf(".")+1).trim();
62
            releases.add(rel);
63
            int n = projects.size();
64
            boolean haveIt = false;
65
            for (int j = 0; j < n; j++)
66
            {
67
                String s = (String)projects.get(j);
68
                if (s.equals(proj))
69
                {
70
                    haveIt = true;
71
                    break;
72
                }
73
            }
74
            if (!haveIt)
75
            {
76
                projects.add(proj);
77
            }
78
        }
79
 
80
        init();
81
        setPosition();
82
        updateProjects();
83
        updateReleases();
84
        pack();
85
        setVisible(true);
86
        lock.acquire();
87
    }
88
 
89
    /**
90
     * Build this frame from components.
91
     */
92
    private void init()
93
    {
94
        setTitle(titleString);
95
 
96
        // Proper close if the user closes the window.
97
        addWindowListener(new AntShieldReleaseWindowClose(this));
98
 
99
        // OK button.
100
        JButton okButton = new JButton();
101
        okButton.setActionCommand("OK");
102
        okButton.setText("OK");
103
        okButton.addActionListener(new AntShieldReleaseOKAction(this));
104
 
105
        // Cancel button.
106
        JButton cancelButton = new JButton();
107
        cancelButton.setActionCommand("Cancel");
108
        cancelButton.setText("Cancel");
109
        cancelButton.addActionListener(new AntShieldReleaseCancelAction(this));
110
 
111
        // Labels.
112
        JLabel projectLabel = new JLabel("Project");
113
        projectLabel.setPreferredSize(new Dimension(120, 25));
114
        JLabel typeLabel = new JLabel("Release Type");
115
        typeLabel.setPreferredSize(new Dimension(120, 25));
116
        JLabel releaseLabel = new JLabel(releaseLabelString);
117
        releaseLabel.setPreferredSize(new Dimension(120, 25));
118
 
119
        // Handle changes in project combo box.
120
        projectBox.addActionListener(new AntShieldReleaseProjectAdapter(this));
121
 
122
        // Allow user entry in project combo box.
123
        projectBox.setEditable(true);
124
 
125
        // Allow user entry in release combo box.
126
        releaseBox.setEditable(true);
127
 
128
        // Release-type radio buttons.
129
        JRadioButton majorButton = new JRadioButton("Major");
130
        majorButton.addActionListener(new AntShieldReleaseMajorAdapter(this));
131
 
132
        JRadioButton minorButton = new JRadioButton("Minor");
133
        minorButton.addActionListener(new AntShieldReleaseMinorAdapter(this));
134
 
135
        JRadioButton patchButton = new JRadioButton("Patch");
136
        patchButton.addActionListener(new AntShieldReleasePatchAdapter(this));
137
 
138
        ButtonGroup typeGroup = new ButtonGroup();
139
        typeGroup.add(majorButton);
140
        typeGroup.add(minorButton);
141
        typeGroup.add(patchButton);
142
        if (type.equals("major"))
143
          majorButton.setSelected(true);
144
        else if (type.equals("minor"))
145
          minorButton.setSelected(true);
146
        else
147
          patchButton.setSelected(true);
148
 
149
        // Release-type panel.
150
        JPanel typePanel = new JPanel();
151
        typePanel.setLayout(new GridLayout(3,1));
152
        typePanel.setBorder(new EtchedBorder());
153
        typePanel.add(majorButton);
154
        typePanel.add(minorButton);
155
        typePanel.add(patchButton);
156
 
157
        // South-east panel - holds the OK and Cancel buttons.
158
        JPanel sePanel = new JPanel();
159
        sePanel.add(okButton, null);
160
        sePanel.add(cancelButton, null);
161
 
162
        // South panel - holds south-east panel
163
        JPanel southPanel = new JPanel();
164
        southPanel.setLayout(new BorderLayout());
165
        southPanel.setBorder(new EtchedBorder());
166
        southPanel.add(sePanel, BorderLayout.EAST);
167
 
168
        // Format everything with nice indentation
169
        Box box2 = new Box(BoxLayout.X_AXIS);
170
        box2.add(Box.createHorizontalStrut(50));
171
        box2.add(projectLabel);
172
        box2.add(Box.createHorizontalStrut(10));
173
        box2.add(projectBox);
174
        box2.add(Box.createHorizontalStrut(50));
175
 
176
        Box box3 = new Box(BoxLayout.X_AXIS);
177
        if (!undo)
178
        {
179
            box3.add(Box.createHorizontalStrut(50));
180
            box3.add(typeLabel);
181
            box3.add(Box.createHorizontalStrut(10));
182
            box3.add(typePanel);
183
            box3.add(Box.createHorizontalStrut(50));
184
        }
185
 
186
        Box box4 = new Box(BoxLayout.X_AXIS);
187
        box4.add(Box.createHorizontalStrut(50));
188
        box4.add(releaseLabel);
189
        box4.add(Box.createHorizontalStrut(10));
190
        box4.add(releaseBox);
191
        box4.add(Box.createHorizontalStrut(50));
192
 
193
        Box box5 = new Box(BoxLayout.Y_AXIS);
194
        box5.add(Box.createVerticalStrut(50));
195
        box5.add(box2);
196
        box5.add(Box.createVerticalStrut(10));
197
        box5.add(box3);
198
        box5.add(Box.createVerticalStrut(10));
199
        box5.add(box4);
200
        box5.add(Box.createVerticalStrut(50));
201
 
202
        // Keep our components centred and top-justified
203
        JPanel centrePanel = new JPanel();
204
        centrePanel.setLayout(new BorderLayout());
205
        centrePanel.setBorder(new EtchedBorder());
206
        centrePanel.add(box5, BorderLayout.NORTH);
207
 
208
        getContentPane().setLayout(new BorderLayout());
209
        getContentPane().add(southPanel, BorderLayout.SOUTH);
210
        getContentPane().add(centrePanel, BorderLayout.CENTER);
211
 
212
        // Hitting ENTER activates the OK button
213
        getRootPane().setDefaultButton(okButton);
214
 
215
        pack();
216
    } // init
217
 
218
    /**
219
     * Position this frame in the centre of the screen,
220
     * resizing if necessary.
221
     */
222
    private void setPosition()
223
    {
224
        int scrnWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
225
        int scrnHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
226
        int width = getWidth();
227
        int height = getHeight();
228
        if (width > scrnWidth/2)
229
        {
230
            width = scrnWidth/2;
231
        }
232
        if (width < scrnWidth/8)
233
        {
234
            width = scrnWidth/8;
235
        }
236
        if (height > scrnHeight/2)
237
        {
238
            height = scrnHeight/2;
239
        }
240
        if (height < scrnHeight/8)
241
        {
242
            height = scrnHeight/8;
243
        }
244
        setSize(width, height);
245
        int xpos = scrnWidth/2 - width/2;
246
        int ypos = scrnHeight/2 - height/2;
247
        setLocation(xpos, ypos);
248
    } // setPosition
249
 
250
    /**
251
     * Handle the OK button by verifying everything.  Pop up
252
     * a dialog if all isn't well.
253
     */
254
    public void handleOk()
255
    {
256
        String proj = (String)projectBox.getSelectedItem();
257
        String rel = (String)releaseBox.getSelectedItem();
258
        if ((proj == null) || (proj.trim().length() < 1))
259
        {
260
            new AntShieldRetryDialog("A project must be specified.");
261
        }
262
        else if ((rel == null) || (rel.trim().length() < 1))
263
        {
264
            new AntShieldRetryDialog("A release must be specified.");
265
        }
266
        else if (!verifyReleaseNumber()) //ignore this if in undo mode
267
        {
268
            String msg = new String("Release '");
269
            msg += rel;
270
            msg += "' is invalid for project '";
271
            msg += proj;
272
            msg += "'.";
273
            new AntShieldRetryDialog(msg);
274
        }
275
        else
276
        {
277
            ok = true;
278
            close();
279
        }
280
    } // handleOk
281
 
282
    /**
283
     * Handle the cancel button.
284
     */
285
    public void handleCancel()
286
    {
287
        ok = false;
288
        close();
289
    } // handleCancel
290
 
291
    /**
292
     * Handle changes to the project combo box, especially
293
     * trimming any manually entered project to 3 characters max.
294
     */
295
    public void handleProject() {
296
        project = ((String)projectBox.getSelectedItem()).trim();
297
        if (!projects.contains(project) && (project.length() > 3))
298
        {
299
            project = project.substring(0, 3);
300
            projectBox.setSelectedItem(project);
301
        }
302
        updateReleases();
303
    } // handleProject
304
 
305
    /**
306
     * Handle the major radio button.
307
     * Change the release type to major.
308
     */
309
    public void handleMajor() {
310
        type = "major";
311
        updateReleases();
312
    } // handleMajor
313
 
314
    /**
315
     * Handle the minor radio button.
316
     * Change the release type to minor.
317
     */
318
    public void handleMinor() {
319
        type = "minor";
320
        updateReleases();
321
    } // handleMinor
322
 
323
    /**
324
     * Handle the patch radio button.
325
     * Change the release type to patch.
326
     */
327
    public void handlePatch() {
328
        type = "patch";
329
        updateReleases();
330
    } // handlePatch
331
 
332
    /**
333
     * Return the selected release in the form "x.y.z.proj".
334
     * @return
335
     */
336
    public String getRelease()
337
    {
338
        return release;
339
    } // getRelease
340
 
341
    /**
342
     * Did we exit successfully?
343
     * @return
344
     */
345
    public boolean isOk()
346
    {
347
        return ok;
348
    } // isOk
349
 
350
    /**
351
     * Close this frame.
352
     */
353
    public void close()
354
    {
355
        lock.release();
356
        setVisible(false);
357
    } // close
358
 
359
    /**
360
     * Check that the currently selected release is of the form
361
     * "<int>.<int>.<int>", and that it isn't already in the list
362
     * of existing releases (unless undo is true).
363
     * @return
364
     */
365
    private boolean verifyReleaseNumber()
366
    {
367
        boolean status = false;
368
        String s = (String)releaseBox.getSelectedItem();
369
        int i1 = s.indexOf('.');
370
        int i2 = s.indexOf('.', i1+1);
371
        if ((i1 > 0) && (i2 > i1) && (s.lastIndexOf('.') == i2))
372
        {
373
            try
374
            {
375
                Integer major = new Integer(s.substring(0, i1).trim());
376
                Integer minor = new Integer(s.substring(i1+1, i2).trim());
377
                Integer patch = new Integer(s.substring(i2+1).trim());
378
                release = new String();
379
                release += major.toString();
380
                release += ".";
381
                release += minor.toString();
382
                release += ".";
383
                release += patch.toString();
384
                release += ".";
385
                release += project;
386
 
387
                if (undo)
388
                {
389
 
390
//                     if ((release.indexOf('-') < 0) && releases.contains(release))
391
//                     {
392
                         status = true;
393
//                     }
394
                }
395
                else
396
                {
397
                    if ((release.indexOf('-') < 0) && !releases.contains(release) && !release.substring(0,5).equals("0.0.0"))
398
                    {
399
                        status = true;
400
                    }
401
 
402
                }
403
            }
404
            catch (NumberFormatException e)
405
            {
406
                ;  // do nothing
407
            }
408
        }
409
        return status;
410
    } // verifyReleaseNumber
411
 
412
    /**
413
     * Populate the project combo box.
414
     */
415
    private void updateProjects()
416
    {
417
        projectBox.removeAllItems();
418
        Iterator i = projects.iterator();
419
        while (i.hasNext())
420
        {
421
            projectBox.addItem(i.next());
422
        }
423
        project = (String)projectBox.getSelectedItem();
424
    } // updateProjects
425
 
426
    /**
427
     * Populate the release combo box with the allowed releases
428
     * (if any) for the current project and release type.  Select
429
     * the highest release by default.
430
     */
431
    private void updateReleases()
432
    {
433
        releaseBox.removeAllItems();
434
        if ((project != null) && (project.length() > 0))
435
        {
436
            if (undo)
437
                updateUndoReleases();
438
            else if (type.equals("major"))
439
                updateMajorReleases();
440
            else if (type.equals("minor"))
441
                updateMinorReleases();
442
            else
443
                updatePatchReleases();
444
        }
445
        if (releaseBox.getItemCount() > 0)
446
        {
447
            releaseBox.setSelectedIndex(releaseBox.getItemCount()-1);
448
        }
449
    } // updateReleases
450
 
451
    /**
452
     * Populate the release combo box with the allowed undo releases
453
     * for the current project.
454
     */
455
    private void updateUndoReleases()
456
    {
457
        TreeSet set = new TreeSet();
458
        Iterator i = releases.iterator();
459
        while (i.hasNext())
460
        {
461
            String s = (String)i.next();
462
            int j = s.lastIndexOf('.')+1;
463
            String proj = s.substring(j).trim();
464
            if (proj.equals(project))
465
            {
466
                set.add(s.substring(0, j-1).trim());
467
                //releaseBox.addItem(s.substring(0, j-1).trim());
468
            }
469
        }
470
        i = set.iterator();
471
        while (i.hasNext())
472
        {
473
            releaseBox.addItem(i.next());
474
        }
475
    } // updateUndoReleases
476
 
477
    /**
478
     * Populate the release combo box with the allowed major release
479
     * for the current project.
480
     */
481
    private void updateMajorReleases()
482
    {
483
        int currentMajor = 0;
484
        Iterator i = releases.iterator();
485
        while (i.hasNext())
486
        {
487
            String s = (String)i.next();
488
            String proj = s.substring(s.lastIndexOf('.')+1).trim();
489
            if (proj.equals(project))
490
            {
491
                int m = new Integer(s.substring(0,s.indexOf('.')).trim()).intValue();
492
                if (m > currentMajor)
493
                    currentMajor = m;
494
            }
495
        }
496
        String r = new String(new Integer(currentMajor+1).toString());
497
        r += ".0.0";
498
        releaseBox.addItem(r);
499
    } // updateMajorReleases
500
 
501
    /**
502
     * Populate the release combo box with the allowed minor releases
503
     * for the current project.
504
     */
505
    private void updateMinorReleases()
506
    {
507
        TreeMap majors = new TreeMap();
508
        Iterator i = releases.iterator();
509
        while (i.hasNext())
510
        {
511
            String s = (String)i.next();
512
            String proj = s.substring(s.lastIndexOf('.')+1).trim();
513
            if (proj.equals(project))
514
            {
515
                int i1 = s.indexOf('.');
516
                int i2 = s.indexOf('.', i1+1);
517
                Integer k = new Integer(s.substring(0, i1).trim());
518
                Integer v = new Integer(s.substring(i1+1, i2).trim());
519
                if (majors.containsKey(k))
520
                {
521
                    Integer w = (Integer)majors.get(k);
522
                    if (w.intValue() < v.intValue())
523
                    {
524
                        majors.put(k, v);
525
                    }
526
                }
527
                else
528
                {
529
                    majors.put(k, v);
530
                }
531
            }
532
        }
533
        Set kset = majors.keySet();
534
        Iterator j = kset.iterator();
535
        while (j.hasNext())
536
        {
537
            Integer k = (Integer)j.next();
538
            String r = k.toString();
539
            r += ".";
540
            r += new Integer(((Integer)majors.get(k)).intValue() + 1).toString();
541
            r += ".0";
542
            releaseBox.addItem(r);
543
        }
544
        if (releaseBox.getItemCount() == 0)
545
        {
546
            releaseBox.addItem(new String("0.1.0"));
547
        }
548
    } // updateMinorReleases
549
 
550
    /**
551
     * Populate the release combo box with the allowed patch releases
552
     * (if any) for the current project.
553
     */
554
    private void updatePatchReleases()
555
    {
556
        TreeMap majors = new TreeMap();
557
        Iterator i = releases.iterator();
558
        while (i.hasNext())
559
        {
560
            String s = (String)i.next();
561
            String proj = s.substring(s.lastIndexOf('.')+1).trim();
562
            if (proj.equals(project))
563
            {
564
                int i1 = s.indexOf('.');
565
                int i2 = s.indexOf('.', i1+1);
566
                int i3 = s.indexOf('.', i2+1);
567
                Integer maj = new Integer(s.substring(0, i1).trim());
568
                Integer min = new Integer(s.substring(i1+1, i2).trim());
569
                Integer pat = new Integer(s.substring(i2+1, i3).trim());
570
                if (majors.containsKey(maj))
571
                {
572
                    TreeMap minors = (TreeMap)majors.get(maj);
573
                    if (minors.containsKey(min))
574
                    {
575
                        Integer w = (Integer)minors.get(min);
576
                        if (w.intValue() < pat.intValue())
577
                        {
578
                            minors.put(min, pat);
579
                        }
580
                    }
581
                    else
582
                    {
583
                        minors.put(min, pat);
584
                    }
585
                }
586
                else
587
                {
588
                    TreeMap minors = new TreeMap();
589
                    minors.put(min, pat);
590
                    majors.put(maj, minors);
591
                }
592
            }
593
        }
594
        Set kset1 = majors.keySet();
595
        Iterator j1 = kset1.iterator();
596
        while (j1.hasNext())
597
        {
598
            Integer maj = (Integer)j1.next();
599
            TreeMap minors = (TreeMap)majors.get(maj);
600
            Set kset2 = minors.keySet();
601
            Iterator j2 = kset2.iterator();
602
            while (j2.hasNext())
603
            {
604
                Integer min = (Integer)j2.next();
605
                String r = new String(maj.toString());
606
                r += ".";
607
                r += min.toString();
608
                r += ".";
609
                r += new Integer(((Integer)minors.get(min)).intValue() + 1).toString();
610
                releaseBox.addItem(r);
611
            }
612
        }
613
        if (releaseBox.getItemCount() == 0)
614
        {
615
            releaseBox.addItem(new String("0.0.1"));
616
        }
617
    } // updatePatchReleases
618
}
619
 
620
class AntShieldReleaseMajorAdapter implements ActionListener
621
{
622
    private AntShieldReleaseFrame adaptee;
623
 
624
    public AntShieldReleaseMajorAdapter(AntShieldReleaseFrame a)
625
    {
626
        adaptee = a;
627
    }
628
 
629
    public void actionPerformed(ActionEvent e)
630
    {
631
        adaptee.handleMajor();
632
    }
633
}
634
 
635
class AntShieldReleaseMinorAdapter implements ActionListener
636
{
637
    private AntShieldReleaseFrame adaptee;
638
 
639
    public AntShieldReleaseMinorAdapter(AntShieldReleaseFrame a)
640
    {
641
        adaptee = a;
642
    }
643
 
644
    public void actionPerformed(ActionEvent e)
645
    {
646
        adaptee.handleMinor();
647
    }
648
}
649
 
650
class AntShieldReleasePatchAdapter implements ActionListener
651
{
652
    private AntShieldReleaseFrame adaptee;
653
 
654
    public AntShieldReleasePatchAdapter(AntShieldReleaseFrame a)
655
    {
656
        adaptee = a;
657
    }
658
 
659
    public void actionPerformed(ActionEvent e)
660
    {
661
        adaptee.handlePatch();
662
    }
663
}
664
 
665
class AntShieldReleaseProjectAdapter implements ActionListener
666
{
667
    private AntShieldReleaseFrame adaptee;
668
 
669
    public AntShieldReleaseProjectAdapter(AntShieldReleaseFrame a)
670
    {
671
        adaptee = a;
672
    }
673
 
674
    public void actionPerformed(ActionEvent e)
675
    {
676
        adaptee.handleProject();
677
    }
678
}
679
 
680
class AntShieldReleaseOKAction implements ActionListener
681
{
682
    AntShieldReleaseFrame adaptee;
683
 
684
    public AntShieldReleaseOKAction(AntShieldReleaseFrame a)
685
    {
686
        adaptee = a;
687
    }
688
 
689
    public void actionPerformed(ActionEvent e)
690
    {
691
        adaptee.handleOk();
692
    }
693
}
694
 
695
class AntShieldReleaseCancelAction implements ActionListener
696
{
697
    AntShieldReleaseFrame adaptee;
698
 
699
    public AntShieldReleaseCancelAction(AntShieldReleaseFrame a)
700
    {
701
        adaptee = a;
702
    }
703
 
704
    public void actionPerformed(ActionEvent e)
705
    {
706
        adaptee.handleCancel();
707
    }
708
}
709
 
710
class AntShieldReleaseWindowClose extends WindowAdapter
711
{
712
    private AntShieldReleaseFrame parent;
713
 
714
    public AntShieldReleaseWindowClose(AntShieldReleaseFrame a)
715
    {
716
        parent = a;
717
    }
718
 
719
    public void windowClosing(WindowEvent e)
720
    {
721
        parent.close();
722
    }
723
}