KickJava   Java API By Example, From Geeks To Geeks.

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


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.actions.checkout;
21
22 import org.netbeans.api.project.ProjectUtils;
23 import org.netbeans.modules.versioning.system.cvss.ui.wizards.CheckoutWizard;
24 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
25 import org.netbeans.modules.versioning.system.cvss.ExecutorGroup;
26 import org.netbeans.modules.versioning.system.cvss.FileStatusCache;
27 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
28 import org.netbeans.modules.versioning.system.cvss.executor.CheckoutExecutor;
29 import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
30 import org.netbeans.lib.cvsclient.command.GlobalOptions;
31 import org.netbeans.spi.project.ui.support.ProjectChooser;
32 import org.netbeans.spi.project.ui.support.CommonProjectActions;
33 import org.netbeans.api.project.ProjectManager;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectInformation;
36 import org.netbeans.api.project.ui.OpenProjects;
37 import org.openide.util.*;
38 import org.openide.util.lookup.Lookups;
39 import org.openide.util.actions.SystemAction;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.filesystems.FileObject;
42 import org.openide.ErrorManager;
43 import org.openide.DialogDescriptor;
44 import org.openide.DialogDisplayer;
45
46 import javax.swing.*;
47 import java.awt.event.ActionEvent JavaDoc;
48 import java.awt.event.ActionListener JavaDoc;
49 import java.awt.*;
50 import java.io.File JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.util.*;
53 import java.util.List JavaDoc;
54
55 /**
56  * Shows checkout wizard, performs the checkout,
57  * detects checked out projects and optionally
58  * shows open/create project dialog.
59  *
60  * @author Petr Kuzel
61  */

62 public final class CheckoutAction extends SystemAction {
63
64     private static final String JavaDoc SHOW_CHECKOUT_COMPLETED = "checkoutAction.showCheckoutCompleted";
65     
66     // avoid instance fields, it's singleton
67

68     public CheckoutAction() {
69         setIcon(null);
70         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
71
}
72
73     public String JavaDoc getName() {
74         return NbBundle.getBundle(CheckoutAction.class).getString("CTL_MenuItem_Checkout_Label");
75     }
76
77     public HelpCtx getHelpCtx() {
78         return new HelpCtx(CheckoutAction.class);
79     }
80
81     /**
82      * Shows interactive checkout wizard.
83      */

84     public void actionPerformed(ActionEvent JavaDoc ev) {
85         CheckoutWizard wizard = new CheckoutWizard();
86         if (wizard.show() == false) return;
87
88         final String JavaDoc tag = wizard.getTag();
89         final String JavaDoc modules = wizard.getModules();
90         final String JavaDoc workDir = wizard.getWorkingDir();
91         final String JavaDoc cvsRoot = wizard.getCvsRoot();
92         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
93             public void run() {
94                 checkout(cvsRoot, modules, tag, workDir, true, null);
95             }
96         });
97     }
98
99
100     /**
101      * Perform asynchronous checkout action with preconfigured values.
102      * On succesfull finish shows open project dialog.
103      *
104      * @param modules comma separated list of modules
105      * @param tag branch name of <code>null</code> for trunk
106      * @param workingDir target directory
107      * @param scanProject if true scan folder for projects and show UI (subject of global setting)
108      * @param group if specified checkout is added into tthe group without executing it.
109      *
110      * @return async executor
111      */

112     public CheckoutExecutor checkout(String JavaDoc cvsRoot, String JavaDoc modules, String JavaDoc tag, String JavaDoc workingDir, boolean scanProject, ExecutorGroup group) {
113         CheckoutCommand cmd = new CheckoutCommand();
114
115         String JavaDoc moduleString = modules;
116         if (moduleString == null || moduleString.length() == 0) {
117             moduleString = "."; // NOI18N
118
}
119         StringTokenizer tokenizer = new StringTokenizer(moduleString, ",;"); // NOI18N
120
if (tokenizer.countTokens() == 1) {
121             cmd.setModule(moduleString);
122         } else {
123             List JavaDoc moduleList = new ArrayList();
124             while( tokenizer.hasMoreTokens()) {
125                 String JavaDoc s = tokenizer.nextToken().trim();
126                 moduleList.add(s);
127             }
128             String JavaDoc[] modules2 = (String JavaDoc[]) moduleList.toArray(new String JavaDoc[moduleList.size()]);
129             cmd.setModules(modules2);
130         }
131
132         cmd.setDisplayName(NbBundle.getMessage(CheckoutAction.class, "BK1006"));
133
134         if (tag != null) {
135             cmd.setCheckoutByRevision(tag);
136         } else {
137             cmd.setResetStickyOnes(true);
138         }
139         cmd.setPruneDirectories(true);
140         cmd.setRecursive(true);
141
142         File JavaDoc workingFolder = new File JavaDoc(workingDir);
143         File JavaDoc[] files = new File JavaDoc[] {workingFolder};
144         cmd.setFiles(files);
145
146         CvsVersioningSystem cvs = CvsVersioningSystem.getInstance();
147         GlobalOptions gtx = CvsVersioningSystem.createGlobalOptions();
148         gtx.setCVSRoot(cvsRoot);
149
150         boolean execute = false;
151         if (group == null) {
152             execute = true;
153             group = new ExecutorGroup(NbBundle.getMessage(CheckoutAction.class, "BK1013"));
154         }
155         CheckoutExecutor executor = new CheckoutExecutor(cvs, cmd, gtx);
156         group.addExecutor(executor);
157         if (CvsModuleConfig.getDefault().getPreferences().getBoolean(SHOW_CHECKOUT_COMPLETED, true) && scanProject) {
158             group.addBarrier(new CheckoutCompletedController(executor, workingFolder, scanProject));
159         }
160
161         if (execute) {
162             group.execute();
163         }
164         return executor;
165     }
166
167     /** On task finish shows next steps UI.*/
168     private class CheckoutCompletedController implements Runnable JavaDoc, ActionListener JavaDoc {
169
170         private final CheckoutExecutor executor;
171         private final File JavaDoc workingFolder;
172         private final boolean openProject;
173
174         private CheckoutCompletedPanel panel;
175         private Dialog dialog;
176         private Project projectToBeOpened;
177
178         public CheckoutCompletedController(CheckoutExecutor executor, File JavaDoc workingFolder, boolean openProject) {
179             this.executor = executor;
180             this.workingFolder = workingFolder;
181             this.openProject = openProject;
182         }
183
184         public void run() {
185
186             if (executor.isSuccessful() == false) {
187                 return;
188             }
189
190             List JavaDoc checkedOutProjects = new LinkedList();
191             File JavaDoc normalizedWorkingFolder = FileUtil.normalizeFile(workingFolder);
192             // checkout creates new folders and cache must be aware of them
193
refreshRecursively(normalizedWorkingFolder);
194             FileObject fo = FileUtil.toFileObject(normalizedWorkingFolder);
195             if (fo != null) {
196                 String JavaDoc name = NbBundle.getMessage(CheckoutAction.class, "BK1007");
197                 executor.getGroup().progress(name);
198                 Iterator it = executor.getExpandedModules().iterator();
199                 while (it.hasNext()) {
200                     String JavaDoc module = (String JavaDoc) it.next();
201                     if (".".equals(module)) { // NOI18N
202
checkedOutProjects = ProjectUtilities.scanForProjects(fo);
203                         break;
204                     } else {
205                         FileObject subfolder = fo.getFileObject(module);
206                         if (subfolder != null) {
207                             executor.getGroup().progress(name);
208                             checkedOutProjects.addAll(ProjectUtilities.scanForProjects(subfolder));
209                         }
210                     }
211                 }
212             }
213
214             panel = new CheckoutCompletedPanel();
215             panel.openButton.addActionListener(this);
216             panel.createButton.addActionListener(this);
217             panel.closeButton.addActionListener(this);
218             panel.setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
219             panel.againCheckBox.setVisible(openProject == false);
220             String JavaDoc title = NbBundle.getMessage(CheckoutAction.class, "BK1008");
221             DialogDescriptor descriptor = new DialogDescriptor(panel, title);
222             descriptor.setModal(true);
223
224             // move buttons from dialog to descriptor
225
panel.remove(panel.openButton);
226             panel.remove(panel.createButton);
227             panel.remove(panel.closeButton);
228
229             Object JavaDoc[] options = null;
230             if (checkedOutProjects.size() > 1) {
231                 String JavaDoc msg = NbBundle.getMessage(CheckoutAction.class, "BK1009", new Integer JavaDoc(checkedOutProjects.size()));
232                 panel.jLabel1.setText(msg);
233                 options = new Object JavaDoc[] {
234                     panel.openButton,
235                     panel.closeButton
236                 };
237             } else if (checkedOutProjects.size() == 1) {
238                 Project project = (Project) checkedOutProjects.iterator().next();
239                 projectToBeOpened = project;
240                 ProjectInformation projectInformation = ProjectUtils.getInformation(project);
241                 String JavaDoc projectName = projectInformation.getDisplayName();
242                 String JavaDoc msg = NbBundle.getMessage(CheckoutAction.class, "BK1011", projectName);
243                 panel.jLabel1.setText(msg);
244                 panel.openButton.setText(NbBundle.getMessage(CheckoutAction.class, "BK1012"));
245                 options = new Object JavaDoc[] {
246                     panel.openButton,
247                     panel.closeButton
248                 };
249             } else {
250                 String JavaDoc msg = NbBundle.getMessage(CheckoutAction.class, "BK1010");
251                 panel.jLabel1.setText(msg);
252                 options = new Object JavaDoc[] {
253                     panel.createButton,
254                     panel.closeButton
255                 };
256
257             }
258
259             descriptor.setMessageType(DialogDescriptor.INFORMATION_MESSAGE);
260             descriptor.setOptions(options);
261             descriptor.setClosingOptions(options);
262             descriptor.setHelpCtx(new HelpCtx(CheckoutCompletedPanel.class));
263             dialog = DialogDisplayer.getDefault().createDialog(descriptor);
264             dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CheckoutAction.class, "ACSD_CheckoutCompleted_Dialog"));
265
266             SwingUtilities.invokeLater(new Runnable JavaDoc() {
267                 public void run() {
268                     dialog.setVisible(true);
269                 }
270             });
271         }
272
273         /**
274          * Refreshes statuses of this folder and all its parent folders up to filesystem root.
275          *
276          * @param folder folder to refresh
277          */

278         private void refreshRecursively(File JavaDoc folder) {
279             if (folder == null) return;
280             refreshRecursively(folder.getParentFile());
281             CvsVersioningSystem.getInstance().getStatusCache().refresh(folder, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
282         }
283
284         public void actionPerformed(ActionEvent JavaDoc e) {
285             Object JavaDoc src = e.getSource();
286             dialog.setVisible(false);
287             if (panel.openButton.equals(src)) {
288                 // show project chooser
289
if (projectToBeOpened == null) {
290                     JFileChooser chooser = ProjectChooser.projectChooser();
291                     chooser.setCurrentDirectory(workingFolder);
292                     chooser.setMultiSelectionEnabled(true);
293                     chooser.showOpenDialog(null);
294                     File JavaDoc [] projectDirs = chooser.getSelectedFiles();
295                     for (int i = 0; i < projectDirs.length; i++) {
296                         File JavaDoc projectDir = projectDirs[i];
297                         FileObject projectFolder = FileUtil.toFileObject(projectDir);
298                         if (projectFolder != null) {
299                             try {
300                                 Project p = ProjectManager.getDefault().findProject(projectFolder);
301                                 if (p != null) {
302                                     openProject(p);
303                                 }
304                             } catch (IOException JavaDoc e1) {
305                                 ErrorManager err = ErrorManager.getDefault();
306                                 err.annotate(e1, NbBundle.getMessage(CheckoutAction.class, "BK1014", projectFolder));
307                                 err.notify(e1);
308                             }
309                         }
310                     }
311                 } else {
312                     if (projectToBeOpened == null) return;
313                     openProject(projectToBeOpened);
314                 }
315
316             } else if (panel.createButton.equals(src)) {
317                 ProjectUtilities.newProjectWizard(workingFolder);
318             }
319             if (panel.againCheckBox.isSelected()) {
320                 CvsModuleConfig.getDefault().getPreferences().putBoolean(SHOW_CHECKOUT_COMPLETED, false);
321             }
322         }
323
324         private void openProject(Project p) {
325             Project[] projects = new Project[] {p};
326             OpenProjects.getDefault().open(projects, false);
327
328             // set as main project and expand
329
ContextAwareAction action = (ContextAwareAction) CommonProjectActions.setAsMainProjectAction();
330             Lookup ctx = Lookups.singleton(p);
331             Action ctxAction = action.createContextAwareInstance(ctx);
332             ctxAction.actionPerformed(new ActionEvent JavaDoc(this, 0, "")); // NOI18N
333
ProjectUtilities.selectAndExpandProject(p);
334         }
335     }
336 }
337
Popular Tags