KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > update > RevertModifications


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 package org.netbeans.modules.subversion.ui.update;
20
21 import java.awt.Dialog JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JRadioButton JavaDoc;
29 import javax.swing.event.DocumentEvent JavaDoc;
30 import javax.swing.event.DocumentListener JavaDoc;
31 import org.netbeans.modules.subversion.RepositoryFile;
32 import org.netbeans.modules.subversion.ui.browser.RepositoryPaths;
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.ErrorManager;
36 import org.openide.util.HelpCtx;
37 import org.tigris.subversion.svnclientadapter.SVNRevision;
38
39 /**
40  *
41  * @author Tomas Stupka
42  */

43 public class RevertModifications implements PropertyChangeListener JavaDoc {
44
45     private RevertModificationsPanel panel;
46     private JButton JavaDoc okButton;
47     private JButton JavaDoc cancelButton;
48     private RevertType[] types;
49     
50     /** Creates a new instance of RevertModifications */
51     public RevertModifications(RepositoryFile repositoryFile) {
52         this (repositoryFile, null);
53     }
54
55     /** Creates a new instance of RevertModifications */
56     public RevertModifications(RepositoryFile repositoryFile, String JavaDoc defaultRevision) {
57         OneCommitRevertType ocrt = new OneCommitRevertType(repositoryFile, getPanel().oneCommitRadioButton);
58         types = new RevertType[] {
59             new LocalRevertType(getPanel().localChangesRadioButton),
60             ocrt,
61             new MoreCommitsRevertType(repositoryFile, getPanel().moreCommitsRadioButton)
62         };
63         okButton = new JButton JavaDoc(org.openide.util.NbBundle.getMessage(RevertModifications.class, "CTL_RevertForm_Action_Revert")); // NOI18N
64
okButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(RevertModifications.class, "ACSD_RevertForm_Action_Revert")); // NOI18N
65
cancelButton = new JButton JavaDoc(org.openide.util.NbBundle.getMessage(RevertModifications.class, "CTL_RevertForm_Action_Cancel")); // NOI18N
66
cancelButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(RevertModifications.class, "ACSD_RevertForm_Action_Cancel")); // NOI18N
67
if (defaultRevision != null) {
68             panel.oneCommitRadioButton.setSelected(true);
69             panel.oneRevisionTextField.setText(defaultRevision);
70             ocrt.actionPerformed(null);
71         }
72     }
73     
74     private RevertModificationsPanel getPanel() {
75         if(panel == null) {
76             panel = new RevertModificationsPanel();
77         }
78         return panel;
79     }
80
81     RevisionInterval getRevisionInterval() {
82         for (int i = 0; i < types.length; i++) {
83             if(types[i].isSelected()) {
84                 return types[i].getRevisionInterval();
85             }
86         }
87         return null;
88     }
89
90     boolean revertNewFiles() {
91         for (int i = 0; i < types.length; i++) {
92             if(types[i].isSelected()) {
93                 return types[i].revertNewFiles();
94             }
95         }
96         return false;
97     }
98     
99     public boolean showDialog() {
100         DialogDescriptor dialogDescriptor = new DialogDescriptor(panel, org.openide.util.NbBundle.getMessage(RevertModifications.class, "CTL_RevertDialog")); // NOI18N
101
dialogDescriptor.setOptions(new Object JavaDoc[] {okButton, cancelButton});
102         
103         dialogDescriptor.setModal(true);
104         dialogDescriptor.setHelpCtx(new HelpCtx(this.getClass()));
105         dialogDescriptor.setValid(false);
106         
107         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
108         dialog.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(RevertModifications.class, "ACSD_RevertDialog")); // NOI18N
109
dialog.setVisible(true);
110         dialog.setResizable(false);
111         boolean ret = dialogDescriptor.getValue() == okButton;
112         return ret;
113     }
114
115     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
116         if( evt.getPropertyName().equals(RepositoryPaths.PROP_VALID) ) {
117             if(okButton != null) {
118                 boolean valid = ((Boolean JavaDoc)evt.getNewValue()).booleanValue();
119                 okButton.setEnabled(valid);
120             }
121         }
122     }
123
124     protected void setMoreCommitsFieldsEnabled(boolean b) {
125         getPanel().startRevisionTextField.setEnabled(b);
126         getPanel().endRevisionTextField.setEnabled(b);
127         getPanel().startSearchButton.setEnabled(b);
128         getPanel().endSearchButton.setEnabled(b);
129     }
130
131     protected void setOneCommitFieldsEnabled(boolean b) {
132         getPanel().oneRevisionSearchButton.setEnabled(b);
133         getPanel().oneRevisionTextField.setEnabled(b);
134     }
135         
136     static class RevisionInterval {
137         SVNRevision startRevision;
138         SVNRevision endRevision;
139     }
140
141     private abstract class RevertType implements ActionListener JavaDoc, DocumentListener JavaDoc {
142         private JRadioButton JavaDoc button;
143
144         RevertType(JRadioButton JavaDoc button) {
145             this.button = button;
146             button.addActionListener(this);
147         }
148
149         boolean isSelected() {
150             return button.isSelected();
151         }
152
153         boolean revertNewFiles() {
154             return panel.revertNewFilesCheckBox.isSelected();
155         }
156         
157         public void insertUpdate(DocumentEvent JavaDoc e) {
158             validateUserInput();
159         }
160
161         public void removeUpdate(DocumentEvent JavaDoc e) {
162             validateUserInput();
163         }
164
165         public void changedUpdate(DocumentEvent JavaDoc e) {
166             validateUserInput();
167         }
168
169         void validateUserInput() {
170             // default means nothing to do
171
}
172
173         RevisionInterval getRevisionInterval() {
174             return null; // default means null
175
}
176         
177         protected SVNRevision getRevision(RepositoryPaths path) {
178             try {
179                 return path.getRepositoryFiles()[0].getRevision();
180             } catch (NumberFormatException JavaDoc ex) {
181                 // should be already checked and
182
// not happen at this place anymore
183
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
184             } catch (MalformedURLException JavaDoc ex) {
185                 // should be already checked and
186
// not happen at this place anymore
187
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
188             }
189             return null;
190         }
191
192         protected boolean validateRevision(SVNRevision revision) {
193             boolean valid = revision == null || revision.equals(SVNRevision.HEAD) || revision.getKind() == SVNRevision.Kind.number;
194             RevertModifications.this.okButton.setEnabled(valid);
195             return valid;
196         }
197     }
198
199     private class LocalRevertType extends RevertType {
200
201         LocalRevertType (JRadioButton JavaDoc button) {
202             super(button);
203         }
204
205         RevertModifications.RevisionInterval getRevisionInterval() {
206             return null;
207         }
208
209         public void actionPerformed(ActionEvent JavaDoc e) {
210             setOneCommitFieldsEnabled(false);
211             setMoreCommitsFieldsEnabled(false);
212         }
213     }
214
215     private class OneCommitRevertType extends RevertType {
216
217         private RepositoryPaths oneRevisionPath;
218
219         OneCommitRevertType (RepositoryFile repositoryFile, JRadioButton JavaDoc button) {
220             super(button);
221             oneRevisionPath =
222                 new RepositoryPaths(
223                     repositoryFile,
224                     null,
225                     null,
226                     getPanel().oneRevisionTextField,
227                     getPanel().oneRevisionSearchButton
228                 );
229             oneRevisionPath.addPropertyChangeListener(RevertModifications.this);
230         }
231
232         RevertModifications.RevisionInterval getRevisionInterval() {
233             SVNRevision revision = getRevision(oneRevisionPath);
234             RevisionInterval ret = new RevisionInterval();
235             ret.startRevision = revision;
236             ret.endRevision = revision;
237             return ret;
238         }
239
240         void validateUserInput() {
241             validateRevision(getRevision(oneRevisionPath));
242         }
243
244         public void actionPerformed(ActionEvent JavaDoc e) {
245             setOneCommitFieldsEnabled(true);
246             setMoreCommitsFieldsEnabled(false);
247             validateUserInput();
248         }
249
250     }
251
252     private class MoreCommitsRevertType extends RevertType {
253
254         private RepositoryPaths endPath;
255         private RepositoryPaths startPath;
256
257         MoreCommitsRevertType (RepositoryFile repositoryFile, JRadioButton JavaDoc button) {
258             super(button);
259             startPath =
260                 new RepositoryPaths(
261                     repositoryFile,
262                     null,
263                     null,
264                     getPanel().startRevisionTextField,
265                     getPanel().startSearchButton
266                 );
267             startPath.addPropertyChangeListener(RevertModifications.this);
268
269             endPath =
270                 new RepositoryPaths(
271                     repositoryFile,
272                     null,
273                     null,
274                     getPanel().endRevisionTextField,
275                     getPanel().endSearchButton
276                 );
277             endPath.addPropertyChangeListener(RevertModifications.this);
278         }
279
280         RevertModifications.RevisionInterval getRevisionInterval() {
281             SVNRevision revision1 = getRevision(startPath);
282             SVNRevision revision2 = getRevision(endPath);
283             if(revision1 == null || revision2 == null) {
284                 return null;
285             }
286
287             return getResortedRevisionInterval(revision1, revision2);
288         }
289
290         void validateUserInput() {
291             if(!validateRevision(getRevision(startPath))) {
292                 return;
293             }
294             if(!validateRevision(getRevision(endPath))) {
295                 return;
296             }
297         }
298
299         public void actionPerformed(ActionEvent JavaDoc e) {
300             setMoreCommitsFieldsEnabled(true);
301             setOneCommitFieldsEnabled(false);
302             validateUserInput();
303         }
304
305         private RevisionInterval getResortedRevisionInterval(SVNRevision revision1, SVNRevision revision2) {
306             RevisionInterval ret = new RevisionInterval ();
307             if(revision1.equals(SVNRevision.HEAD) &&
308                revision1.equals(SVNRevision.HEAD))
309             {
310                 ret.startRevision = revision1;
311                 ret.endRevision = revision2;
312             } else if (revision1.equals(SVNRevision.HEAD)) {
313                 ret.startRevision = revision2;
314                 ret.endRevision = revision1;
315             } else if (revision2.equals(SVNRevision.HEAD)) {
316                 ret.startRevision = revision1;
317                 ret.endRevision = revision2;
318             } else {
319                 Long JavaDoc r1 = Long.parseLong(revision1.toString());
320                 Long JavaDoc r2 = Long.parseLong(revision2.toString());
321                 if(r1.compareTo(r2) < 0) {
322                     ret.startRevision = revision1;
323                     ret.endRevision = revision2;
324                 } else {
325                     ret.startRevision = revision2;
326                     ret.endRevision = revision1;
327                 }
328             }
329             return ret;
330         }
331         
332     }
333
334 }
335
Popular Tags