1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*****************************************************************************
* Copyright (c) 2011 Atos Origin.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Mathieu Velten (Atos Origin) mathieu.velten@atosorigin.com - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.readonly;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.eclipse.core.commands.operations.IOperationApprover2;
import org.eclipse.core.commands.operations.IOperationHistory;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.internal.command.ICommandWithSettableResult;
import org.eclipse.papyrus.onefile.model.IPapyrusFile;
import org.eclipse.papyrus.onefile.model.PapyrusModelHelper;
import org.eclipse.papyrus.onefile.utils.OneFileUtils;
public class FSReadOnlyOneFileApprover implements IOperationApprover2 {
public IStatus proceedRedoing(IUndoableOperation operation, IOperationHistory history, IAdaptable info) {
return proceedExecuting(operation, history, info);
}
public IStatus proceedUndoing(IUndoableOperation operation, IOperationHistory history, IAdaptable info) {
return proceedExecuting(operation, history, info);
}
public IStatus proceedExecuting(IUndoableOperation operation, IOperationHistory history, IAdaptable info) {
if(operation instanceof ICommand) {
ICommand command = (ICommand)operation;
List<IFile> affectedFiles = command.getAffectedFiles();
HashSet<IFile> filesToCheckForLock = new HashSet<IFile>();
for(IFile affectedFile : affectedFiles) {
if(affectedFile == null)
continue;
if(affectedFile.exists()) {
// the file is in the workspace
IPapyrusFile papFile = PapyrusModelHelper.getPapyrusModelFactory().createIPapyrusFile(affectedFile);
filesToCheckForLock.addAll(Arrays.asList(OneFileUtils.getAssociatedFiles(papFile)));
} else {
// the file is not in the workspace
IPath path = affectedFile.getRawLocation();
if(path == null) {
// cancel if we can't find the file
setCommandResult(command, Status.CANCEL_STATUS);
return Status.CANCEL_STATUS;
}
File file = path.toFile();
if(file != null && file.exists() && !file.canWrite()) {
// cancel if we find a read-only file outside the
// workspace
setCommandResult(command, Status.CANCEL_STATUS);
return Status.CANCEL_STATUS;
}
}
}
for(IFile file : affectedFiles) {
URI.createPlatformResourceURI(file.getFullPath().toString(), true);
}
if(filesToCheckForLock.size() > 0) {
IFile[] filesToCheckForLockArray = filesToCheckForLock.toArray(new IFile[filesToCheckForLock.size()]);
if(ReadOnlyManager.isReadOnly(filesToCheckForLock.toArray(filesToCheckForLockArray))) {
boolean ok = ReadOnlyManager.enableWrite(filesToCheckForLockArray);
if(!ok) {
return Status.CANCEL_STATUS;
}
}
}
}
return Status.OK_STATUS;
}
/**
* Sets the command result of the specified command to a CommandResult
* having the specified status.
*
* @param command
* ICommand to set the CommandResult for
* @param status
* IStatus of the CommandResult that will be set on the
* command
*/
public static void setCommandResult(ICommand command, IStatus status) {
if(command instanceof ICommandWithSettableResult) {
((ICommandWithSettableResult)command).internalSetResult(new CommandResult(status));
}
}
}
|