KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > BufferedResourceNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal;
12
13 import java.io.*;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.compare.*;
18 import org.eclipse.compare.structuremergeviewer.IStructureComparator;
19
20 /**
21  * A buffer for a workspace resource.
22  */

23 public class BufferedResourceNode extends ResourceNode {
24     
25     private boolean fDirty= false;
26     private IFile fDeleteFile;
27         
28     /**
29      * Creates a <code>ResourceNode</code> for the given resource.
30      *
31      * @param resource the resource
32      */

33     public BufferedResourceNode(IResource resource) {
34         super(resource);
35     }
36     
37     /*
38      * Returns <code>true</code> if buffer contains uncommitted changes.
39      */

40     public boolean isDirty() {
41         return fDirty;
42     }
43     
44     protected IStructureComparator createChild(IResource child) {
45         return new BufferedResourceNode(child);
46     }
47         
48     public void setContent(byte[] contents) {
49         fDirty= true;
50         super.setContent(contents);
51     }
52
53     /*
54      * Commits buffered contents to resource.
55      */

56     public void commit(IProgressMonitor pm) throws CoreException {
57         if (fDirty) {
58             
59             if (fDeleteFile != null) {
60                 fDeleteFile.delete(true, true, pm);
61                 return;
62             }
63             
64             IResource resource= getResource();
65             if (resource instanceof IFile) {
66
67                 byte[] bytes= getContent();
68                 ByteArrayInputStream is= new ByteArrayInputStream(bytes);
69                 try {
70                     IFile file= (IFile) resource;
71                     if (file.exists())
72                         file.setContents(is, false, true, pm);
73                     else
74                         file.create(is, false, pm);
75                     fDirty= false;
76                 } finally {
77                     if (is != null)
78                         try {
79                             is.close();
80                         } catch(IOException ex) {
81                             // Silently ignored
82
}
83                 }
84             }
85         }
86     }
87     
88     public ITypedElement replace(ITypedElement child, ITypedElement other) {
89         
90         if (child == null) { // add resource
91
// create a node without a resource behind it!
92
IResource resource= getResource();
93             if (resource instanceof IFolder) {
94                 IFolder folder= (IFolder) resource;
95                 IFile file= folder.getFile(other.getName());
96                 child= new BufferedResourceNode(file);
97             }
98         }
99         
100         if (other == null) { // delete resource
101
IResource resource= getResource();
102             if (resource instanceof IFolder) {
103                 IFolder folder= (IFolder) resource;
104                 IFile file= folder.getFile(child.getName());
105                 if (file != null && file.exists()) {
106                     fDeleteFile= file;
107                     fDirty= true;
108                 }
109             }
110             return null;
111         }
112         
113         if (other instanceof IStreamContentAccessor && child instanceof IEditableContent) {
114             IEditableContent dst= (IEditableContent) child;
115             
116             try {
117                 InputStream is= ((IStreamContentAccessor)other).getContents();
118                 byte[] bytes= Utilities.readBytes(is);
119                 if (bytes != null)
120                     dst.setContent(bytes);
121             } catch (CoreException ex) {
122                 // NeedWork
123
}
124         }
125         return child;
126     }
127 }
128
129
Popular Tags