KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > ChangeCVSRootAction


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.versioning.system.cvss.ui.actions;
20
21 import org.netbeans.modules.versioning.spi.VCSContext;
22 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
23 import org.netbeans.modules.versioning.util.Utils;
24 import org.netbeans.lib.cvsclient.CVSRoot;
25 import org.openide.util.NbBundle;
26 import org.openide.util.HelpCtx;
27 import org.openide.DialogDescriptor;
28 import org.openide.DialogDisplayer;
29
30 import javax.swing.*;
31 import javax.swing.text.JTextComponent JavaDoc;
32 import javax.swing.event.DocumentListener JavaDoc;
33 import javax.swing.event.DocumentEvent JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.awt.Dialog JavaDoc;
37 import java.awt.Component JavaDoc;
38 import java.io.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.util.*;
41 import java.util.logging.Logger JavaDoc;
42 import java.util.logging.Level JavaDoc;
43
44 /**
45  * @author Maros Sandor
46  */

47 public class ChangeCVSRootAction extends AbstractAction {
48     
49     private final String JavaDoc RECENT_CHANGED_ROOTS = "recentChangedRoots"; // NOI18N
50

51     private final VCSContext ctx;
52
53     public ChangeCVSRootAction(String JavaDoc name, VCSContext ctx) {
54         super(name);
55         this.ctx = ctx;
56     }
57
58     public boolean isEnabled() {
59         return ctx.getRootFiles().size() > 0 && allDirectories();
60     }
61
62     private boolean allDirectories() {
63         for (File JavaDoc file : ctx.getRootFiles()) {
64             if (!file.isDirectory()) return false;
65         }
66         return true;
67     }
68
69     public void actionPerformed(ActionEvent JavaDoc e) {
70         ResourceBundle loc = NbBundle.getBundle(ChangeCVSRootAction.class);
71
72         final ChangeCVSRootPanel panel = new ChangeCVSRootPanel();
73         panel.getWorkingCopy().setText(getWorkingCopy());
74         panel.getCurrentCVSRoot().setText(getCurrentRoot());
75         
76         List<String JavaDoc> roots = Utils.getStringList(CvsModuleConfig.getDefault().getPreferences(), RECENT_CHANGED_ROOTS);
77         panel.getNewRootCombo().setModel(new DefaultComboBoxModel(roots.toArray()));
78         if (roots.size() > 0) {
79             panel.getNewRootCombo().setSelectedItem(roots.get(0));
80         } else {
81             panel.getNewRootCombo().setSelectedItem(getNewRoot());
82         }
83         
84         final JButton changeRoot = new JButton(loc.getString("CTL_CvsRootForm_Action_Change"));
85         changeRoot.setToolTipText(loc.getString("TT_CvsRootForm_Action_Change"));
86         JButton cancel = new JButton(loc.getString("CTL_CvsRootForm_Action_Cancel"));
87         cancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ChangeCVSRootAction.class, "ACSD_CvsRootForm_Action_Cancel")); // NOI18N
88
DialogDescriptor descriptor = new DialogDescriptor(
89                 panel,
90                 loc.getString("CTL_CvsRootForm_Title"), // NOI18N
91
true,
92                 new Object JavaDoc [] { changeRoot, cancel },
93                 changeRoot,
94                 DialogDescriptor.BOTTOM_ALIGN,
95                 null,
96                 null);
97         descriptor.setClosingOptions(null);
98         descriptor.setHelpCtx(new HelpCtx(ChangeCVSRootAction.class));
99         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
100         dialog.getAccessibleContext().setAccessibleDescription(loc.getString("ACSD_CvsRootForm"));
101         
102         Component JavaDoc c = panel.getNewRootCombo().getEditor().getEditorComponent();
103         if (c instanceof JTextComponent JavaDoc) {
104             ((JTextComponent JavaDoc) c).getDocument().addDocumentListener(new DocumentListener JavaDoc() {
105                 public void insertUpdate(DocumentEvent JavaDoc e) {
106                     validate(panel, changeRoot);
107                 }
108
109                 public void removeUpdate(DocumentEvent JavaDoc e) {
110                     validate(panel, changeRoot);
111                 }
112
113                 public void changedUpdate(DocumentEvent JavaDoc e) {
114                     validate(panel, changeRoot);
115                 }
116             });
117         }
118
119         panel.getNewRootCombo().addActionListener(new ActionListener JavaDoc() {
120             public void actionPerformed(ActionEvent JavaDoc e) {
121                 validate(panel, changeRoot);
122             }
123         });
124         
125         dialog.setVisible(true);
126         if (descriptor.getValue() != changeRoot) return;
127         
128         final String JavaDoc newRoot = panel.getNewRootCombo().getEditor().getItem().toString();
129         Utils.insert(CvsModuleConfig.getDefault().getPreferences(), RECENT_CHANGED_ROOTS, newRoot, 20);
130             
131         Utils.createTask(new Runnable JavaDoc() {
132             public void run() {
133                 rewrite(newRoot);
134             }
135         }).schedule(0);
136     }
137     
138     private void rewrite(String JavaDoc newRoot) {
139         for (File JavaDoc root : ctx.getRootFiles()) {
140             CvsRootRewriter rewriter = new CvsRootRewriter(root, newRoot);
141             rewriter.rewrite();
142         }
143     }
144
145     private String JavaDoc getCurrentRoot() {
146         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
147         for (File JavaDoc file : ctx.getRootFiles()) {
148             try {
149                 String JavaDoc root = CvsRootRewriter.getCvsRoot(file);
150                 sb.append(root);
151                 sb.append(", "); // NOI18N
152
} catch (IOException JavaDoc e) {
153                 Logger.getLogger(ChangeCVSRootAction.class.getName()).log(Level.WARNING, "", e); // NOI18N
154
// ignore for now
155
}
156         }
157         sb.delete(sb.length() - 2, sb.length());
158         return sb.toString();
159     }
160
161     private String JavaDoc getNewRoot() {
162         File JavaDoc file = ctx.getRootFiles().iterator().next();
163         try {
164             return CvsRootRewriter.getCvsRoot(file);
165         } catch (IOException JavaDoc e) {
166             Logger.getLogger(ChangeCVSRootAction.class.getName()).log(Level.WARNING, "", e); // NOI18N
167
return ""; // NOI18N
168
}
169     }
170     
171     private String JavaDoc getWorkingCopy() {
172         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
173         for (File JavaDoc file : ctx.getRootFiles()) {
174             sb.append(file.getAbsolutePath());
175             sb.append(", "); // NOI18N
176
}
177         sb.delete(sb.length() - 2, sb.length());
178         return sb.toString();
179     }
180
181     public void validate(ChangeCVSRootPanel panel, JButton ok) {
182         try {
183             CVSRoot.parse(panel.getNewRootCombo().getEditor().getItem().toString());
184             ok.setEnabled(true);
185         } catch (Exception JavaDoc e) {
186             ok.setEnabled(false);
187         }
188     }
189 }
190
Popular Tags