KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > wizards > RootWizard


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.wizards;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import javax.swing.*;
26 import javax.swing.event.DocumentListener JavaDoc;
27 import org.netbeans.lib.cvsclient.CVSRoot;
28 import org.openide.DialogDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.util.HelpCtx;
31
32 /**
33  * UI that allows to configure selected root
34  * and manage preconfigured roots pool.
35  *
36  * <p>It also allows to edit CVS root field by field.
37  *
38  * @author Petr Kuzel
39  */

40 public final class RootWizard implements ActionListener JavaDoc, DocumentListener JavaDoc {
41
42     private final RepositoryStep repositoryStep;
43     private final CvsRootPanel rootPanel;
44     private DialogDescriptor dd;
45     
46     private RootWizard(RepositoryStep step) {
47         this.repositoryStep = step;
48         rootPanel = null;
49     }
50
51     private RootWizard(CvsRootPanel rootPanel) {
52         repositoryStep = null;
53         this.rootPanel = rootPanel;
54     }
55     
56     /**
57      * Creates root configuration wizard with UI
58      * that allows to set password, proxy, external
59      * command, etc. (depends on root type).
60      *
61      * @return RootWizard
62      */

63     public static RootWizard configureRoot(String JavaDoc root) {
64         RepositoryStep step = new RepositoryStep(root);
65         step.applyStandaloneLayout();
66
67         return new RootWizard(step);
68     }
69
70     /**
71      * Shows field by filed CVS root customizer.
72      *
73      * @return customized value or null on cancel.
74      */

75     public static String JavaDoc editCvsRoot(String JavaDoc root) {
76         CvsRootPanel rootPanel = new CvsRootPanel();
77         RootWizard wizard = new RootWizard(rootPanel);
78         return wizard.customizeRoot(root);
79     }
80
81     private String JavaDoc customizeRoot(String JavaDoc root) {
82         String JavaDoc access = "pserver"; // NOI18N
83
String JavaDoc host = ""; // NOI18N
84
String JavaDoc port = ""; // NOI18N
85
String JavaDoc user = System.getProperty("user.name"); // NOI18N
86
String JavaDoc repository = ""; // NOI18N
87
try {
88             CVSRoot cvsRoot = CVSRoot.parse(root);
89             access = cvsRoot.getMethod();
90             host = cvsRoot.getHostName();
91             int portG = cvsRoot.getPort();
92             if (portG > 0) {
93                 port = "" + portG; // NOI18N
94
}
95             user = cvsRoot.getUserName();
96             repository = cvsRoot.getRepository();
97         } catch (IllegalArgumentException JavaDoc ex) {
98             // use defaults
99
}
100         rootPanel.accessComboBox.setSelectedItem(access);
101         rootPanel.hostTextField.setText(host);
102         rootPanel.portTextField.setText(port);
103         rootPanel.userTextField.setText(user);
104         rootPanel.repositoryTextField.setText(repository);
105         
106         rootPanel.accessComboBox.addActionListener(this);
107         rootPanel.userTextField.getDocument().addDocumentListener(this);
108         rootPanel.hostTextField.getDocument().addDocumentListener(this);
109         rootPanel.userTextField.getDocument().addDocumentListener(this);
110         rootPanel.portTextField.getDocument().addDocumentListener(this);
111         rootPanel.repositoryTextField.getDocument().addDocumentListener(this);
112                 
113         // workaround DD bug,
114
rootPanel.setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
115         dd = new DialogDescriptor(rootPanel, org.openide.util.NbBundle.getMessage(RootWizard.class, "BK2024"));
116         dd.setHelpCtx(new HelpCtx(CvsRootPanel.class));
117         dd.setModal(true);
118         // all components visible
119
rootPanel.setPreferredSize(rootPanel.getPreferredSize());
120         updateVisibility();
121         checkInput();
122
123         Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog(dd);
124         d.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(RootWizard.class, "ACSD_CvsRootPanel"));
125         d.setVisible(true);
126         
127         if (DialogDescriptor.OK_OPTION.equals(dd.getValue())) {
128             try {
129                 String JavaDoc ret = collectRoot();
130                 CVSRoot cvsRoot = CVSRoot.parse(ret);
131                 return ret;
132             } catch (IllegalArgumentException JavaDoc ex) {
133                 // use defaults
134
}
135         }
136
137         return null;
138     }
139     
140     private void updateVisibility() {
141         String JavaDoc access = (String JavaDoc) rootPanel.accessComboBox.getSelectedItem();
142         boolean hostVisible = ("pserver".equals(access) || "ext".equals(access)); // NOI18N
143
rootPanel.userLabel.setVisible(hostVisible);
144         rootPanel.userTextField.setVisible(hostVisible);
145         rootPanel.hostLabel.setVisible(hostVisible);
146         rootPanel.hostTextField.setVisible(hostVisible);
147         rootPanel.portLabel.setVisible(hostVisible);
148         rootPanel.portTextField.setVisible(hostVisible);
149     }
150     
151     private String JavaDoc collectRoot() throws IllegalArgumentException JavaDoc {
152         String JavaDoc method = (String JavaDoc) rootPanel.accessComboBox.getSelectedItem();
153         boolean hasHost = ("pserver".equals(method) || "ext".equals(method)); // NOI18N
154

155         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(":"); // NOI18N
156
sb.append(method);
157         sb.append(":"); // NOI18N
158
if (hasHost) {
159             String JavaDoc s = rootPanel.userTextField.getText();
160             if ("".equals(s.trim())) throw new IllegalArgumentException JavaDoc(); // NOI18N
161
sb.append(s);
162             sb.append("@"); // NOI18N
163
s = rootPanel.hostTextField.getText();
164             if ("".equals(s.trim())) throw new IllegalArgumentException JavaDoc(); // NOI18N
165
sb.append(s);
166             sb.append(":"); // NOI18N
167
String JavaDoc portS = rootPanel.portTextField.getText();
168             if ("".equals(portS.trim()) == false) { // NOI18N
169
int portp = Integer.parseInt(portS); // raise NFE
170
if (portp > 0) {
171                     sb.append(portS);
172                 }
173             }
174         }
175         String JavaDoc s = rootPanel.repositoryTextField.getText();
176         if ("".equals(s.trim())) throw new IllegalArgumentException JavaDoc(); // NOI18N
177
sb.append(s);
178
179         return sb.toString();
180     }
181     
182     private void checkInput() {
183         try {
184             String JavaDoc ret = collectRoot();
185             CVSRoot cvsRoot = CVSRoot.parse(ret);
186             dd.setValid(true);
187         } catch (IllegalArgumentException JavaDoc ex) {
188             dd.setValid(false);
189         }
190         
191     }
192     
193     /**
194      * Gets UI panel representing RootWizard.
195      */

196     public JPanel getPanel() {
197         RepositoryPanel repositoryPanel = (RepositoryPanel) repositoryStep.getComponent();
198         return repositoryPanel;
199     }
200
201     /**
202      * Propagates configuration changes
203      * from UI into {@link org.netbeans.modules.versioning.system.cvss.settings.CvsRootSettings}.
204      *
205      * @param validate on true only valid values are commited
206      * @return <code>null</codE> on successfull commit otherwise error message.
207      */

208     public String JavaDoc commit(boolean validate) {
209         if (validate) {
210             repositoryStep.prepareValidation();
211             repositoryStep.validateBeforeNext();
212             if (repositoryStep.isValid() == false) {
213                 return repositoryStep.getErrorMessage();
214             }
215         }
216         repositoryStep.storeValidValues();
217         return null;
218
219     }
220
221     /** Return result of light-weight validation.*/
222     public boolean isValid() {
223         return repositoryStep.isValid();
224     }
225
226     /** Allows to listen on valid. */
227     public void addChangeListener(ChangeListener JavaDoc l) {
228         repositoryStep.addChangeListener(l);
229     }
230
231     public void removeChangeListener(ChangeListener JavaDoc l) {
232         repositoryStep.removeChangeListener(l);
233     }
234
235     public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent) {
236         updateVisibility();
237         checkInput();
238     }
239
240     public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc documentEvent) {
241     }
242
243     public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc documentEvent) {
244         checkInput();
245     }
246
247     public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc documentEvent) {
248         checkInput();
249     }
250 }
251
Popular Tags