View Javadoc

1   package de.matthias_burbach.deputy.swing;
2   
3   import java.awt.BorderLayout;
4   import java.awt.FlowLayout;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   
8   import javax.swing.JButton;
9   import javax.swing.JDialog;
10  import javax.swing.JLabel;
11  import javax.swing.JOptionPane;
12  import javax.swing.JPanel;
13  import javax.swing.JScrollPane;
14  import javax.swing.tree.TreeNode;
15  
16  import de.matthias_burbach.deputy.core.project.Project;
17  import de.matthias_burbach.deputy.core.project.ProjectHolderTreeNode;
18  import de.matthias_burbach.deputy.core.rule.ReplacementRule;
19  
20  /***
21   * @author Matthias Burbach
22   */
23  public class ReplacementChooserDialog extends JDialog {
24      /***
25       * The rule to complete.
26       */
27      private ReplacementRule rule;
28  
29      /***
30       * The tree to display within this dialog.
31       */
32      private DeputyTree chooserTree;
33  
34      /***
35       * The label to display on the top of this dialog.
36       */
37      private JLabel label = new JLabel("");
38  
39      /***
40       * @param chooserTree The tree to display within this dialog.
41       */
42      public ReplacementChooserDialog(final DeputyTree chooserTree) {
43          this.chooserTree = chooserTree;
44  
45          setTitle("Choose Replacement for");
46  
47          JScrollPane scrollPane = new JScrollPane(chooserTree);
48  
49          JPanel buttonPanel = new JPanel(new FlowLayout());
50          JButton okButton = new JButton("OK");
51          okButton.addActionListener(new ActionListener() {
52              /*(non-Javadoc)
53               * @see java.awt.event.ActionListener
54               *          #actionPerformed(java.awt.event.ActionEvent)
55               */
56              /***
57               * {@inheritDoc}
58               */
59              public void actionPerformed(final ActionEvent e) {
60                  clickedOK();
61              }
62          });
63          buttonPanel.add(okButton);
64          JButton cancelButton = new JButton("Cancel");
65          cancelButton.addActionListener(new ActionListener() {
66              /*(non-Javadoc)
67               * @see java.awt.event.ActionListener
68               *          #actionPerformed(java.awt.event.ActionEvent)
69               */
70              /***
71               * {@inheritDoc}
72               */
73              public void actionPerformed(final ActionEvent e) {
74                  clickedCancel();
75              }
76          });
77          buttonPanel.add(cancelButton);
78  
79          final float largeFontSize = 12.0f;
80          label.setFont(label.getFont().deriveFont(largeFontSize));
81          JPanel labelPanel = new JPanel(new BorderLayout());
82          labelPanel.add(new JLabel(" "), BorderLayout.NORTH);
83          labelPanel.add(label, BorderLayout.CENTER);
84          JLabel spacerLabel = new JLabel(" ");
85          final float smallFontSize = 6.0f;
86          spacerLabel.setFont(label.getFont().deriveFont(smallFontSize));
87          labelPanel.add(spacerLabel, BorderLayout.SOUTH);
88  
89          JPanel panel = new JPanel(new BorderLayout());
90          panel.add(labelPanel, BorderLayout.NORTH);
91          panel.add(scrollPane, BorderLayout.CENTER);
92          panel.add(buttonPanel, BorderLayout.SOUTH);
93  
94          getContentPane().removeAll();
95          getContentPane().add(panel);
96  
97          setModal(true);
98          final int width = 400;
99          final int height = 500;
100         setSize(width, height);
101 
102         validate();
103     }
104 
105     /***
106      * @param rule The rule to complete through this dialog.
107      */
108     public void setRule(final ReplacementRule rule) {
109         this.rule = rule;
110         String labelText = " " + rule.getDisplacedGroupId() + "/";
111         labelText += rule.getDisplacedArtifactId();
112         if (rule.getDisplacedVersion() != null) {
113             labelText += "-" + rule.getDisplacedVersion();
114         }
115         label.setText(labelText);
116     }
117 
118     /***
119      * Activates and displays this dialog.
120      */
121     public void activate() {
122         chooserTree.setSelectionRow(-1);
123         show();
124     }
125 
126     /***
127      * Deactivates and hides this dialog.
128      */
129     public void deactivate() {
130         hide();
131     }
132 
133     /***
134      * Completes the rule when the user clicked 'OK'.
135      */
136     private void clickedOK() {
137         TreeNode node = chooserTree.getSelectedNode();
138         String groupId = null;
139         String artifactId = null;
140         String version = null;
141         if (node instanceof ProjectHolderTreeNode) {
142             Project project = ((ProjectHolderTreeNode) node).getProject();
143             groupId = project.getGroupId();
144             artifactId = project.getArtifactId();
145             version = project.getVersion();
146         } else {
147             JOptionPane.showMessageDialog(
148                 this,
149                 "You need to choose a version of an artifact"
150                 + " as a replacement for " + label.getText()
151                 + " before you can leave this dialog with OK.",
152                 "Deputy",
153                 JOptionPane.WARNING_MESSAGE);
154         }
155         if (groupId != null) {
156             if ((groupId + "").equals(rule.getDisplacedGroupId() + "")
157                     && (artifactId + "").equals(
158                             rule.getDisplacedArtifactId() + "")
159                     && (version + "").equals(
160                             rule.getDisplacedVersion() + "")) {
161                 JOptionPane.showMessageDialog(
162                     this,
163                     "You cannot replace " + label.getText() + " with itself.",
164                     "Deputy",
165                     JOptionPane.WARNING_MESSAGE);
166             } else {
167                 rule.setGroupId(groupId);
168                 rule.setArtifactId(artifactId);
169                 rule.setVersion(version);
170                 deactivate();
171             }
172         }
173     }
174 
175     /***
176      * Deactivates this dialog when the user clicked 'Cancel'.
177      */
178     private void clickedCancel() {
179         deactivate();
180     }
181 }