2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
41
package org.netbeans.lib.cvsclient.command.unedit;
45
import org.netbeans.lib.cvsclient.*;
46
import org.netbeans.lib.cvsclient.admin.*;
47
import org.netbeans.lib.cvsclient.command.*;
48
import org.netbeans.lib.cvsclient.command.edit.*;
49
import org.netbeans.lib.cvsclient.connection.*;
50
import org.netbeans.lib.cvsclient.event.*;
51
import org.netbeans.lib.cvsclient.file.*;
52
import org.netbeans.lib.cvsclient.request.*;
55
* @author Thomas Singer
57
public class UneditCommand extends BasicCommand {
59
private Watch temporaryWatch;
62
* Construct a new editors command.
64
public UneditCommand() {
69
* Execute the command.
71
* @param client the client services object that provides any necessary
72
* services to this command, including the ability to actually
73
* process all the requests.
75
public void execute(ClientServices clientServices, EventManager eventManager)
76
throws CommandException, AuthenticationException {
77
clientServices.ensureConnection();
80
super.execute(clientServices, eventManager);
82
addRequestForWorkingDirectory(clientServices);
83
addRequest(CommandRequest.NOOP);
85
clientServices.processRequests(requests);
87
catch (CommandException ex) {
90
catch (EOFException ex) {
91
throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); //NOI18N
93
catch (Exception ex) {
94
throw new CommandException(ex, ex.getLocalizedMessage());
101
protected void addRequestForFile(File file, Entry entry) {
102
String temporaryWatch = Watch.getWatchString(getTemporaryWatch());
103
requests.add(new NotifyRequest(file, "U", temporaryWatch)); // NOI18N
108
catch (IOException ex) {
114
* Called when server responses with "ok" or "error", (when the command
117
public void commandTerminated(TerminationEvent e) {
118
if (builder != null) {
119
builder.outputDone();
124
* This method returns how the tag command would looklike when typed on the
127
public String getCVSCommand() {
128
StringBuffer cvsCommandLine = new StringBuffer("unedit "); //NOI18N
129
cvsCommandLine.append(getCVSArguments());
130
appendFileArguments(cvsCommandLine);
131
return cvsCommandLine.toString();
135
* Takes the arguments and sets the command.
136
* To be mainly used for automatic settings (like parsing the .cvsrc file)
137
* @return true if the option (switch) was recognized and set
139
public boolean setCVSCommand(char opt, String optArg) {
143
else if (opt == 'l') {
153
* String returned by this method defines which options are available for
156
public String getOptString() {
157
return "Rl"; //NOI18N
161
* Resets all switches in the command.
162
* After calling this method, the command should have no switches defined
163
* and should behave defaultly.
165
public void resetCVSCommand() {
170
* Returns the arguments of the command in the command-line style.
171
* Similar to getCVSCommand() however without the files and command's name
173
public String getCVSArguments() {
174
StringBuffer cvsArguments = new StringBuffer();
175
if (!isRecursive()) {
176
cvsArguments.append("-l "); //NOI18N
178
return cvsArguments.toString();
182
* Returns the temporary watch.
184
public Watch getTemporaryWatch() {
185
return temporaryWatch;
189
* Sets the temporary watch.
191
public void setTemporaryWatch(Watch temporaryWatch) {
192
this.temporaryWatch = temporaryWatch;
195
private void uneditFile(File file) throws IOException {
196
removeBaserevEntry(file);
197
EditCommand.getEditBackupFile(file).delete();
198
FileUtils.setFileReadOnly(file, true);
201
private void removeBaserevEntry(File file) throws IOException {
202
File baserevFile = new File(file.getParentFile(), "CVS/Baserev"); // NOI18N
203
File backupFile = new File(baserevFile.getAbsolutePath() + '~');
205
BufferedReader reader = null;
206
BufferedWriter writer = null;
207
final String entryStart = 'B' + file.getName() + '/';
209
writer = new BufferedWriter(new FileWriter(backupFile));
210
reader = new BufferedReader(new FileReader(baserevFile));
212
for (String line = reader.readLine();
214
line = reader.readLine()) {
216
if (line.startsWith(entryStart)) {
224
catch (FileNotFoundException ex) {
228
if (writer != null) {
232
catch (IOException ex) {
236
if (reader != null) {
240
catch (IOException ex) {
245
baserevFile.delete();
246
if (backupFile.length() > 0) {
247
backupFile.renameTo(baserevFile);