KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > copy > CreateCopyAction


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.copy;
21
22 import java.io.File JavaDoc;
23 import org.netbeans.modules.subversion.FileInformation;
24 import org.netbeans.modules.subversion.RepositoryFile;
25 import org.netbeans.modules.subversion.Subversion;
26 import org.netbeans.modules.subversion.client.ExceptionHandler;
27 import org.netbeans.modules.subversion.client.SvnClient;
28 import org.netbeans.modules.subversion.client.SvnProgressSupport;
29 import org.netbeans.modules.subversion.ui.actions.ContextAction;
30 import org.netbeans.modules.subversion.util.Context;
31 import org.netbeans.modules.subversion.util.SvnUtils;
32 import org.openide.ErrorManager;
33 import org.openide.nodes.Node;
34 import org.tigris.subversion.svnclientadapter.ISVNInfo;
35 import org.tigris.subversion.svnclientadapter.SVNClientException;
36 import org.tigris.subversion.svnclientadapter.SVNRevision;
37 import org.tigris.subversion.svnclientadapter.SVNUrl;
38
39 /**
40  *
41  * @author Tomas Stupka
42  */

43 public class CreateCopyAction extends ContextAction {
44     
45     /** Creates a new instance of CreateCopyAction */
46     public CreateCopyAction() {
47         
48     }
49
50     protected String JavaDoc getBaseName(Node[] activatedNodes) {
51         return "CTL_MenuItem_Copy"; // NOI18N
52
}
53
54     protected int getFileEnabledStatus() {
55         return FileInformation.STATUS_MANAGED
56                & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
57     }
58
59     protected int getDirectoryEnabledStatus() {
60         return FileInformation.STATUS_MANAGED
61                & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED
62                & ~FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
63     }
64     
65     protected void performContextAction(final Node[] nodes) {
66         Context ctx = getContext(nodes);
67
68         final File JavaDoc root = ctx.getRootFiles()[0];
69         File JavaDoc[] files = Subversion.getInstance().getStatusCache().listFiles(ctx, FileInformation.STATUS_LOCAL_CHANGE);
70         
71         final boolean hasChanges = files.length > 0;
72         final SVNUrl repositoryUrl = SvnUtils.getRepositoryRootUrl(root);
73         final SVNUrl fileUrl = SvnUtils.getRepositoryUrl(root);
74         final RepositoryFile repositoryFile = new RepositoryFile(repositoryUrl, fileUrl, SVNRevision.HEAD);
75         
76         final CreateCopy createCopy = new CreateCopy(repositoryFile, root, hasChanges);
77         
78         if(createCopy.showDialog()) {
79             // XXX don't close dialog if error occures!
80
ContextAction.ProgressSupport support = new ContextAction.ProgressSupport(this, nodes) {
81                 public void perform() {
82                     performCopy(createCopy, this);
83                 }
84             };
85             support.start(createRequestProcessor(nodes));
86         }
87     }
88
89     private void performCopy(CreateCopy createCopy, SvnProgressSupport support) {
90         RepositoryFile toRepositoryFile = createCopy.getToRepositoryFile();
91         
92         try {
93             SvnClient client;
94             try {
95                 client = Subversion.getInstance().getClient(toRepositoryFile.getRepositoryUrl());
96             } catch (SVNClientException ex) {
97                 ErrorManager.getDefault().notify(ex);
98                 return;
99             }
100
101             if(!toRepositoryFile.isRepositoryRoot()) {
102                 SVNUrl folderToCreate = toRepositoryFile.removeLastSegment().getFileUrl();
103                 ISVNInfo info = null;
104                 try{
105                     info = client.getInfo(folderToCreate);
106                 } catch (SVNClientException ex) {
107                     if(!ExceptionHandler.isWrongUrl(ex.getMessage())) {
108                         throw ex;
109                     }
110                 }
111
112                 if(support.isCanceled()) {
113                     return;
114                 }
115
116                 if(info == null) {
117                     client.mkdir(folderToCreate,
118                                  true,
119                                  "[Netbeans SVN client generated message: create a new folder for the copy]: '\n" + createCopy.getMessage() + "\n'"); // NOI18N
120
} else {
121                     if(createCopy.getLocalFile().isFile()) {
122                         // we are copying a file to a destination which already exists. Even if it's a folder - we don't use the exactly same
123
// as the commandline svn client:
124
// - the remote file specified in the GUI has to be exactly the file which has to be created at the repository.
125
// - if the destination is an existent folder the file won't be copied into it, as the svn client would do.
126
throw new SVNClientException("File allready exists");
127                     } else {
128                         // XXX warnig: do you realy want to? could be already a project folder!
129
}
130                 }
131             }
132
133             if(support.isCanceled()) {
134                 return;
135             }
136
137             if(createCopy.isLocal()) {
138                 client.copy(createCopy.getLocalFile(),
139                             toRepositoryFile.getFileUrl(),
140                             createCopy.getMessage());
141             } else {
142                 RepositoryFile fromRepositoryFile = createCopy.getFromRepositoryFile();
143                 client.copy(fromRepositoryFile.getFileUrl(),
144                             toRepositoryFile.getFileUrl(),
145                             createCopy.getMessage(),
146                             fromRepositoryFile.getRevision());
147             }
148             
149             if(support.isCanceled()) {
150                 return;
151             }
152
153             if(createCopy.switchTo()) {
154                 SwitchToAction.performSwitch(toRepositoryFile, createCopy.getLocalFile(), support);
155             }
156
157         } catch (SVNClientException ex) {
158             support.annotate(ex);
159         }
160     }
161 }
162
Popular Tags