blob: 852759114432de9e392008a7662978205992a3b3 (
plain) (
blame)
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
|
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
package org.eclipse.compare.internal;
import java.io.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.compare.*;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
/**
* A buffer for a workspace resource.
*/
public class BufferedResourceNode extends ResourceNode {
private boolean fDirty= false;
/**
* Creates a <code>ResourceNode</code> for the given resource.
*
* @param resource the resource
*/
public BufferedResourceNode(IResource resource) {
super(resource);
}
protected IStructureComparator createChild(IResource child) {
return new BufferedResourceNode(child);
}
public void setContent(byte[] contents) {
fDirty= true;
super.setContent(contents);
}
/**
* Commits buffered contents to resource.
*/
public void commit(IProgressMonitor pm) throws CoreException {
if (fDirty) {
IResource resource= getResource();
if (resource instanceof IFile) {
ByteArrayInputStream is= new ByteArrayInputStream(getContent());
try {
IFile file= (IFile) resource;
if (file.exists())
file.setContents(is, false, true, pm);
else
file.create(is, false, pm);
fDirty= false;
} finally {
if (is != null)
try {
is.close();
} catch(IOException ex) {
}
}
}
}
}
public ITypedElement replace(ITypedElement child, ITypedElement other) {
if (child == null) {
// create a node without a resource behind it!
IResource resource= getResource();
if (resource instanceof IFolder) {
IFolder folder= (IFolder) resource;
IFile file= folder.getFile(other.getName());
child= new BufferedResourceNode(file);
}
}
if (other instanceof IStreamContentAccessor && child instanceof IEditableContent) {
IEditableContent dst= (IEditableContent) child;
try {
InputStream is= ((IStreamContentAccessor)other).getContents();
byte[] bytes= Utilities.readBytes(is);
if (bytes != null)
dst.setContent(bytes);
} catch (CoreException ex) {
}
}
return child;
}
}
|