~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to subversion/main/src/org/netbeans/modules/subversion/ui/copy/CreateCopyAction.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.copy;
 
43
 
 
44
import java.io.File;
 
45
import org.netbeans.modules.subversion.FileInformation;
 
46
import org.netbeans.modules.subversion.RepositoryFile;
 
47
import org.netbeans.modules.subversion.Subversion;
 
48
import org.netbeans.modules.subversion.client.SvnClient;
 
49
import org.netbeans.modules.subversion.client.SvnClientExceptionHandler;
 
50
import org.netbeans.modules.subversion.client.SvnProgressSupport;
 
51
import org.netbeans.modules.subversion.ui.actions.ContextAction;
 
52
import org.netbeans.modules.subversion.util.Context;
 
53
import org.netbeans.modules.subversion.util.SvnUtils;
 
54
import org.openide.nodes.Node;
 
55
import org.tigris.subversion.svnclientadapter.ISVNInfo;
 
56
import org.tigris.subversion.svnclientadapter.SVNClientException;
 
57
import org.tigris.subversion.svnclientadapter.SVNRevision;
 
58
import org.tigris.subversion.svnclientadapter.SVNUrl;
 
59
 
 
60
/**
 
61
 *
 
62
 * @author Tomas Stupka
 
63
 */
 
64
public class CreateCopyAction extends ContextAction {
 
65
    
 
66
    /** Creates a new instance of CreateCopyAction */
 
67
    public CreateCopyAction() {
 
68
        
 
69
    }
 
70
 
 
71
    protected String getBaseName(Node[] activatedNodes) {
 
72
        return "CTL_MenuItem_Copy";    // NOI18N
 
73
    }
 
74
 
 
75
    protected int getFileEnabledStatus() {
 
76
        return    FileInformation.STATUS_MANAGED
 
77
               & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
 
78
    }
 
79
 
 
80
    protected int getDirectoryEnabledStatus() {
 
81
        return    FileInformation.STATUS_MANAGED 
 
82
               & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED 
 
83
               & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
 
84
    }
 
85
    
 
86
    protected boolean enable(Node[] nodes) {
 
87
        return nodes != null && nodes.length == 1 &&  getContext(nodes).getRoots().size() > 0;
 
88
    }   
 
89
    
 
90
    protected void performContextAction(final Node[] nodes) {
 
91
        
 
92
        if(!Subversion.getInstance().checkClientAvailable()) {            
 
93
            return;
 
94
        }
 
95
        
 
96
        Context ctx = getContext(nodes);
 
97
 
 
98
        final File root = ctx.getRootFiles()[0];
 
99
        File[] files = Subversion.getInstance().getStatusCache().listFiles(ctx, FileInformation.STATUS_LOCAL_CHANGE);       
 
100
        
 
101
        final boolean hasChanges = files.length > 0; 
 
102
        final SVNUrl repositoryUrl; 
 
103
        final SVNUrl fileUrl;        
 
104
        try {            
 
105
            repositoryUrl = SvnUtils.getRepositoryRootUrl(root); 
 
106
            fileUrl = SvnUtils.getRepositoryUrl(root);        
 
107
        } catch (SVNClientException ex) {
 
108
            SvnClientExceptionHandler.notifyException(ex, true, true);
 
109
            return;
 
110
        }                   
 
111
        final RepositoryFile repositoryFile = new RepositoryFile(repositoryUrl, fileUrl, SVNRevision.HEAD);        
 
112
        
 
113
        final CreateCopy createCopy = new CreateCopy(repositoryFile, root, hasChanges);
 
114
        
 
115
        if(createCopy.showDialog()) {
 
116
            // XXX don't close dialog if error occures!
 
117
            ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this,  nodes) {
 
118
                public void perform() {
 
119
                    performCopy(createCopy, this);
 
120
                }
 
121
            };
 
122
            support.start(createRequestProcessor(nodes));
 
123
        }
 
124
    }
 
125
 
 
126
    private void performCopy(CreateCopy createCopy, SvnProgressSupport support) {
 
127
        RepositoryFile toRepositoryFile = createCopy.getToRepositoryFile();                
 
128
        
 
129
        try {                
 
130
            SvnClient client;
 
131
            try {
 
132
                client = Subversion.getInstance().getClient(toRepositoryFile.getRepositoryUrl());
 
133
            } catch (SVNClientException ex) {
 
134
                SvnClientExceptionHandler.notifyException(ex, true, true);
 
135
                return;
 
136
            }
 
137
 
 
138
            if(!toRepositoryFile.isRepositoryRoot()) {
 
139
                SVNUrl folderToCreate = toRepositoryFile.removeLastSegment().getFileUrl();
 
140
                ISVNInfo info = null;
 
141
                try{
 
142
                    info = client.getInfo(folderToCreate);                                                                
 
143
                } catch (SVNClientException ex) {                                
 
144
                    if(!SvnClientExceptionHandler.isWrongUrl(ex.getMessage())) { 
 
145
                        throw ex;
 
146
                    }
 
147
                }            
 
148
 
 
149
                if(support.isCanceled()) {
 
150
                    return;
 
151
                }
 
152
 
 
153
                if(info == null) {
 
154
                    client.mkdir(folderToCreate,
 
155
                                 true, 
 
156
                                 "[Netbeans SVN client generated message: create a new folder for the copy]: '\n" + createCopy.getMessage() + "\n'"); // NOI18N
 
157
                } else {
 
158
                    if(createCopy.getLocalFile().isFile()) {
 
159
                        // we are copying a file to a destination which already exists. Even if it's a folder - we don't use the exactly same
 
160
                        // as the commandline svn client:
 
161
                        // - the remote file specified in the GUI has to be exactly the file which has to be created at the repository.
 
162
                        // - if the destination is an existent folder the file won't be copied into it, as the svn client would do.
 
163
                        throw new SVNClientException("File allready exists");                                                             
 
164
                    } else {
 
165
                        // XXX warnig: do you realy want to? could be already a project folder!
 
166
                    }
 
167
                }                           
 
168
            }                        
 
169
 
 
170
            if(support.isCanceled()) {
 
171
                return;
 
172
            }
 
173
 
 
174
            if(createCopy.isLocal()) {
 
175
                client.copy(createCopy.getLocalFile(), 
 
176
                            toRepositoryFile.getFileUrl(), 
 
177
                            createCopy.getMessage());            
 
178
            } else {               
 
179
                RepositoryFile fromRepositoryFile = createCopy.getFromRepositoryFile();                
 
180
                client.copy(fromRepositoryFile.getFileUrl(), 
 
181
                            toRepositoryFile.getFileUrl(), 
 
182
                            createCopy.getMessage(), 
 
183
                            fromRepositoryFile.getRevision());
 
184
            }                            
 
185
            
 
186
            if(support.isCanceled()) {
 
187
                return;
 
188
            }
 
189
 
 
190
            if(createCopy.switchTo()) {
 
191
                SwitchToAction.performSwitch(toRepositoryFile, createCopy.getLocalFile(), support);
 
192
            }            
 
193
 
 
194
        } catch (SVNClientException ex) {
 
195
            support.annotate(ex);
 
196
        }
 
197
    }
 
198
}