~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to subversion/main/src/org/netbeans/modules/subversion/ui/project/ImportAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.subversion.ui.project;
 
43
 
 
44
import org.netbeans.modules.subversion.Subversion;
 
45
import org.netbeans.modules.subversion.client.SvnProgressSupport;
 
46
import org.netbeans.modules.subversion.ui.commit.CommitAction;
 
47
import org.openide.util.actions.NodeAction;
 
48
import org.openide.util.*;
 
49
import org.openide.nodes.Node;
 
50
import org.openide.filesystems.FileObject;
 
51
import org.openide.filesystems.FileUtil;
 
52
import org.openide.loaders.DataObject;
 
53
import org.openide.loaders.DataShadow;
 
54
import org.netbeans.api.project.*;
 
55
import java.io.*;
 
56
import java.util.*;
 
57
import org.netbeans.modules.subversion.FileInformation;
 
58
import org.netbeans.modules.subversion.FileStatusCache;
 
59
import org.netbeans.modules.subversion.client.SvnClientExceptionHandler;
 
60
import org.netbeans.modules.subversion.ui.wizards.ImportWizard;
 
61
import org.netbeans.modules.subversion.util.Context;
 
62
import org.netbeans.modules.subversion.util.SvnUtils;
 
63
import org.tigris.subversion.svnclientadapter.SVNClientException;
 
64
import org.tigris.subversion.svnclientadapter.SVNUrl;
 
65
 
 
66
/**
 
67
 *
 
68
 * @author Petr Kuzel
 
69
 */
 
70
public final class ImportAction extends NodeAction {
 
71
    
 
72
    public ImportAction() {
 
73
        setIcon(null);
 
74
        putValue("noIconInMenu", Boolean.TRUE); // NOI18N
 
75
    }
 
76
 
 
77
    public String getName() {
 
78
        return NbBundle.getMessage(ImportAction.class, "BK0006"); // NOI18N
 
79
    }
 
80
 
 
81
    public HelpCtx getHelpCtx() {
 
82
        return null;
 
83
    }
 
84
 
 
85
    protected boolean enable(Node[] nodes) {
 
86
        if (nodes.length == 1) {
 
87
            FileStatusCache cache = Subversion.getInstance().getStatusCache();
 
88
            File dir = lookupImportDirectory(nodes[0]);
 
89
            if (dir != null && dir.isDirectory()) {
 
90
                FileInformation status = cache.getStatus(dir);
 
91
                // mutually exclusive enablement logic with commit
 
92
                if ((status.getStatus() & FileInformation.STATUS_MANAGED) == 0) {
 
93
                    // do not allow to import partial/nonatomic project, all must lie under imported common root
 
94
                    FileObject fo = FileUtil.toFileObject(dir);
 
95
                    Project p = FileOwnerQuery.getOwner(fo);
 
96
                    if (p == null) {
 
97
                        return true;
 
98
                    }
 
99
                    FileObject projectDir = p.getProjectDirectory();
 
100
                    return FileUtil.isParentOf(projectDir, fo) == false;
 
101
                }
 
102
            }
 
103
        }
 
104
        return false;
 
105
    }
 
106
 
 
107
    protected boolean asynchronous() {
 
108
        return false;
 
109
    }
 
110
 
 
111
    protected void performAction(Node[] nodes) {
 
112
        
 
113
        if(!Subversion.getInstance().checkClientAvailable()) {            
 
114
            return;
 
115
        }
 
116
    
 
117
        if (nodes.length == 1) {
 
118
            final File importDirectory = lookupImportDirectory(nodes[0]);
 
119
            if (importDirectory != null) {
 
120
 
 
121
                List<File> list = new ArrayList<File>(1);
 
122
                list.add(importDirectory);
 
123
                Context context = new Context(Context.getEmptyList(), list, Context.getEmptyList());
 
124
                ImportWizard wizard = new ImportWizard(context);
 
125
                if (!wizard.show()) return;
 
126
                
 
127
                Map commitFiles = wizard.getCommitFiles();
 
128
                String message = wizard.getMessage();
 
129
                        
 
130
                performAction(context, commitFiles, message);
 
131
            }
 
132
        }
 
133
    }
 
134
 
 
135
    private void performAction(final Context context,
 
136
                               final Map/*<SvnFileNode, CommitOptions>*/ commitFiles,
 
137
                               final String message)
 
138
    {                        
 
139
        SVNUrl repository;
 
140
        try {            
 
141
            repository = SvnUtils.getRepositoryRootUrl(context.getRootFiles()[0]);
 
142
        } catch (SVNClientException ex) {
 
143
            SvnClientExceptionHandler.notifyException(ex, true, true);
 
144
            return;
 
145
        }                 
 
146
        RequestProcessor rp = Subversion.getInstance().getRequestProcessor(repository);
 
147
        SvnProgressSupport support = new SvnProgressSupport() {
 
148
            public void perform() {                    
 
149
                CommitAction.performCommit(message, commitFiles, context, this, true);
 
150
            }
 
151
        };
 
152
        support.start(rp, repository, org.openide.util.NbBundle.getMessage(ImportAction.class, "LBL_Import_Progress"));
 
153
    }
 
154
 
 
155
    public boolean cancel() {
 
156
        return true;
 
157
    }
 
158
    
 
159
    private File lookupImportDirectory(Node node) {
 
160
        File importDirectory = null;
 
161
        Project project = (Project) node.getLookup().lookup(Project.class);
 
162
        if (project != null) {
 
163
            Sources sources = ProjectUtils.getSources(project);
 
164
            SourceGroup[] groups = sources.getSourceGroups(Sources.TYPE_GENERIC);
 
165
            if (groups.length == 1) {
 
166
                FileObject root = groups[0].getRootFolder();
 
167
                importDirectory = FileUtil.toFile(root);
 
168
            } else {
 
169
                importDirectory = FileUtil.toFile(project.getProjectDirectory());
 
170
            }
 
171
        } else {
 
172
            FileObject fo = null;
 
173
            Collection<? extends FileObject> fileObjects = node.getLookup().lookup(new Lookup.Template<FileObject>(FileObject.class)).allInstances();
 
174
            if (fileObjects.size() > 0) {
 
175
                fo = fileObjects.iterator().next();
 
176
            } else {
 
177
                DataObject dataObject = node.getCookie(DataObject.class);
 
178
                if (dataObject instanceof DataShadow) {
 
179
                    dataObject = ((DataShadow) dataObject).getOriginal();
 
180
                }
 
181
                if (dataObject != null) {
 
182
                    fo = dataObject.getPrimaryFile();
 
183
                }
 
184
            }
 
185
 
 
186
            if (fo != null) {
 
187
                File f = FileUtil.toFile(fo);
 
188
                if (f != null && f.isDirectory()) {
 
189
                    importDirectory = f;
 
190
                }
 
191
            }
 
192
        }
 
193
        return importDirectory;
 
194
    }
 
195
    
 
196
}