KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > project > ImportAction


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.subversion.ui.project;
21
22 import org.netbeans.modules.subversion.Subversion;
23 import org.netbeans.modules.subversion.SvnFileNode;
24 import org.netbeans.modules.subversion.client.SvnProgressSupport;
25 import org.netbeans.modules.subversion.ui.commit.CommitAction;
26 import org.openide.util.actions.NodeAction;
27 import org.openide.util.*;
28 import org.openide.nodes.Node;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.loaders.DataObject;
32 import org.openide.loaders.DataShadow;
33 import org.netbeans.api.project.*;
34 import java.io.*;
35 import java.util.*;
36 import org.netbeans.modules.subversion.FileInformation;
37 import org.netbeans.modules.subversion.FileStatusCache;
38 import org.netbeans.modules.subversion.ui.wizards.ImportWizard;
39 import org.netbeans.modules.subversion.util.Context;
40 import org.netbeans.modules.subversion.util.SvnUtils;
41 import org.tigris.subversion.svnclientadapter.SVNUrl;
42
43 /**
44  *
45  * @author Petr Kuzel
46  */

47 public final class ImportAction extends NodeAction {
48     
49     public ImportAction() {
50         setIcon(null);
51         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
52
}
53
54     public String JavaDoc getName() {
55         return NbBundle.getMessage(ImportAction.class, "BK0006"); // NOI18N
56
}
57
58     public HelpCtx getHelpCtx() {
59         return null;
60     }
61
62     protected boolean enable(Node[] nodes) {
63         if (nodes.length == 1) {
64             FileStatusCache cache = Subversion.getInstance().getStatusCache();
65             File dir = lookupImportDirectory(nodes[0]);
66             if (dir != null && dir.isDirectory()) {
67                 FileInformation status = cache.getStatus(dir);
68                 // mutually exclusive enablement logic with commit
69
if ((status.getStatus() & FileInformation.STATUS_MANAGED) == 0) {
70                     // do not allow to import partial/nonatomic project, all must lie under imported common root
71
FileObject fo = FileUtil.toFileObject(dir);
72                     Project p = FileOwnerQuery.getOwner(fo);
73                     if (p == null) {
74                         return true;
75                     }
76                     FileObject projectDir = p.getProjectDirectory();
77                     return FileUtil.isParentOf(projectDir, fo) == false;
78                 }
79             }
80         }
81         return false;
82     }
83
84     protected boolean asynchronous() {
85         return false;
86     }
87
88     protected void performAction(Node[] nodes) {
89         if (nodes.length == 1) {
90             final File importDirectory = lookupImportDirectory(nodes[0]);
91             if (importDirectory != null) {
92
93                 List<File> list = new ArrayList<File>(1);
94                 list.add(importDirectory);
95                 Context context = new Context(Context.getEmptyList(), list, Context.getEmptyList());
96                 ImportWizard wizard = new ImportWizard(context);
97                 if (!wizard.show()) return;
98                 
99                 Map commitFiles = wizard.getCommitFiles();
100                 String JavaDoc message = wizard.getMessage();
101                         
102                 performAction(context, commitFiles, message);
103             }
104         }
105     }
106
107     private void performAction(final Context context,
108                                final Map/*<SvnFileNode, CommitOptions>*/ commitFiles,
109                                final String JavaDoc message)
110     {
111         
112         SVNUrl repository = SvnUtils.getRepositoryRootUrl(context.getRootFiles()[0]);
113         RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repository);
114         SvnProgressSupport support = new SvnProgressSupport() {
115             public void perform() {
116                 CommitAction.performCommit(message, commitFiles, context, this, true);
117             }
118         };
119         support.start(rp, repository, org.openide.util.NbBundle.getMessage(ImportAction.class, "LBL_Import_Progress"));
120     }
121
122     public boolean cancel() {
123         return true;
124     }
125     
126     private File lookupImportDirectory(Node node) {
127         File importDirectory = null;
128         Project project = (Project) node.getLookup().lookup(Project.class);
129         if (project != null) {
130             Sources sources = ProjectUtils.getSources(project);
131             SourceGroup[] groups = sources.getSourceGroups(Sources.TYPE_GENERIC);
132             if (groups.length == 1) {
133                 FileObject root = groups[0].getRootFolder();
134                 importDirectory = FileUtil.toFile(root);
135             } else {
136                 importDirectory = FileUtil.toFile(project.getProjectDirectory());
137             }
138         } else {
139             FileObject fo = null;
140             Collection<? extends FileObject> fileObjects = node.getLookup().lookup(new Lookup.Template<FileObject>(FileObject.class)).allInstances();
141             if (fileObjects.size() > 0) {
142                 fo = fileObjects.iterator().next();
143             } else {
144                 DataObject dataObject = node.getCookie(DataObject.class);
145                 if (dataObject instanceof DataShadow) {
146                     dataObject = ((DataShadow) dataObject).getOriginal();
147                 }
148                 if (dataObject != null) {
149                     fo = dataObject.getPrimaryFile();
150                 }
151             }
152
153             if (fo != null) {
154                 File f = FileUtil.toFile(fo);
155                 if (f != null && f.isDirectory()) {
156                     importDirectory = f;
157                 }
158             }
159         }
160         return importDirectory;
161     }
162     
163 }
164
Popular Tags