KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > vcs > ReadOnlyFilesHandlerImpl


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.refactoring.vcs;
21 import java.util.Arrays JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.WeakHashMap JavaDoc;
25 import org.netbeans.api.vcs.VcsManager;
26 import org.netbeans.api.vcs.commands.Command;
27 import org.netbeans.modules.refactoring.api.Problem;
28 import org.netbeans.modules.refactoring.api.RefactoringSession;
29 import org.netbeans.modules.refactoring.spi.*;
30 import org.openide.filesystems.FileObject;
31 import org.openide.util.NbBundle;
32
33 /**
34  *
35  * @author Jan Becicka
36  */

37 public class ReadOnlyFilesHandlerImpl implements ReadOnlyFilesHandler {
38     
39     private WeakHashMap JavaDoc sessions = new WeakHashMap JavaDoc(2);
40     /** Creates a new instance of ReadOnlyFilesHandlerImpl */
41     public ReadOnlyFilesHandlerImpl() {
42     }
43     
44     public Problem createProblem(RefactoringSession session, Collection JavaDoc files) {
45         CheckoutFiles cof = (CheckoutFiles) sessions.get(session);
46         Collection JavaDoc fileSet = null;
47         if (cof != null) {
48             //instance of CheckoutFiles created for this session, try to add files
49
fileSet = new HashSet JavaDoc(cof.getFiles());
50             if (!fileSet.addAll(files)) {
51                 //no files were added
52
return null;
53             }
54         } else {
55             //CheckoutFiles not found - create a new one
56
fileSet = new HashSet JavaDoc(files);
57         }
58         
59         FileObject[] fos = (FileObject[]) fileSet.toArray(new FileObject[0]);
60         Command editCmd;
61         try {
62             editCmd = VcsManager.getDefault().createCommand("EDIT", fos); //NOI18N
63
} catch (IllegalArgumentException JavaDoc iaex) {
64             // The provided files are not under version control
65
editCmd = null;
66         }
67         if (editCmd == null) return null;
68         fos = editCmd.getApplicableFiles(fos);
69         editCmd.setFiles(fos);
70         if (cof == null) {
71             cof = new CheckoutFiles(Arrays.asList(fos), editCmd);
72             sessions.put(session, cof);
73             return new Problem(false, NbBundle.getMessage(ReadOnlyFilesHandlerImpl.class, "MSG_CheckoutWarning"), ProblemDetailsFactory.createProblemDetails(cof));
74         } else {
75             cof.setEditCmd(editCmd);
76             cof.setFiles(Arrays.asList(fos));
77             return null;
78         }
79     }
80 }
81
Popular Tags