KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > ResourceNode


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;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.compare.internal.Utilities;
17 import org.eclipse.compare.structuremergeviewer.IStructureComparator;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Shell;
22
23 /**
24  * A <code>ResourceNode</code> wraps an <code>IResources</code> so that it can be used
25  * as input for the differencing engine (interfaces <code>IStructureComparator</code> and <code>ITypedElement</code>)
26  * and the <code>ReplaceWithEditionDialog</code> (interfaces <code>ITypedElement</code> and <code>IModificationDate</code>).
27  * <p>
28  * Clients may instantiate this class; it is not intended to be subclassed.
29  * </p>
30  *
31  * @see EditionSelectionDialog
32  */

33 public class ResourceNode extends BufferedContent
34             implements IEncodedStreamContentAccessor, IStructureComparator, ITypedElement,
35                             IEditableContent, IModificationDate, IResourceProvider, IEditableContentExtension {
36             
37     private IResource fResource;
38     private ArrayList JavaDoc fChildren;
39         
40     
41     /**
42      * Creates a <code>ResourceNode</code> for the given resource.
43      *
44      * @param resource the resource
45      */

46     public ResourceNode(IResource resource) {
47         fResource= resource;
48         Assert.isNotNull(resource);
49     }
50         
51     /**
52      * Returns the corresponding resource for this object.
53      *
54      * @return the corresponding resource
55      */

56     public IResource getResource() {
57         return fResource;
58     }
59     
60     /* (non Javadoc)
61      * see IStreamContentAccessor.getContents
62      */

63     public InputStream getContents() throws CoreException {
64         if (fResource instanceof IStorage)
65             return super.getContents();
66         return null;
67     }
68     
69     /* (non Javadoc)
70      * see IModificationDate.getModificationDate
71      */

72     public long getModificationDate() {
73         return fResource.getLocalTimeStamp();
74     }
75     
76     /* (non Javadoc)
77      * see ITypedElement.getName
78      */

79     public String JavaDoc getName() {
80         if (fResource != null)
81             return fResource.getName();
82         return null;
83     }
84         
85     /* (non Javadoc)
86      * see ITypedElement.getType
87      */

88     public String JavaDoc getType() {
89         if (fResource instanceof IContainer)
90             return ITypedElement.FOLDER_TYPE;
91         if (fResource != null) {
92             String JavaDoc s= fResource.getFileExtension();
93             if (s != null)
94                 return s;
95         }
96         return ITypedElement.UNKNOWN_TYPE;
97     }
98     
99     /* (non Javadoc)
100      * see ITypedElement.getImage
101      */

102     public Image getImage() {
103         return CompareUI.getImage(fResource);
104     }
105
106     /*
107      * Returns <code>true</code> if the other object is of type <code>ITypedElement</code>
108      * and their names are identical. The content is not considered.
109      */

110     public boolean equals(Object JavaDoc other) {
111         if (other instanceof ITypedElement) {
112             String JavaDoc otherName= ((ITypedElement)other).getName();
113             return getName().equals(otherName);
114         }
115         return super.equals(other);
116     }
117     
118     /**
119      * Returns the hash code of the name.
120      * @return a hash code value for this object.
121      */

122     public int hashCode() {
123         return getName().hashCode();
124     }
125     
126     /* (non Javadoc)
127      * see IStructureComparator.getChildren
128      */

129     public Object JavaDoc[] getChildren() {
130         if (fChildren == null) {
131             fChildren= new ArrayList JavaDoc();
132             if (fResource instanceof IContainer) {
133                 try {
134                     IResource members[]= ((IContainer)fResource).members();
135                     for (int i= 0; i < members.length; i++) {
136                         IStructureComparator child= createChild(members[i]);
137                         if (child != null)
138                             fChildren.add(child);
139                     }
140                 } catch (CoreException ex) {
141                     // NeedWork
142
}
143             }
144         }
145         return fChildren.toArray();
146     }
147     
148     /**
149      * This hook method is called from <code>getChildren</code> once for every
150      * member of a container resource. This implementation
151      * creates a new <code>ResourceNode</code> for the given child resource.
152      * Clients may override this method to create a different type of
153      * <code>IStructureComparator</code> or to filter children by returning <code>null</code>.
154      *
155      * @param child the child resource for which a <code>IStructureComparator</code> must be returned
156      * @return a <code>ResourceNode</code> for the given child or <code>null</code>
157      */

158     protected IStructureComparator createChild(IResource child) {
159         return new ResourceNode(child);
160     }
161         
162     /**
163      * Returns an open stream if the corresponding resource implements the
164      * <code>IStorage</code> interface. Otherwise the value <code>null</code> is returned.
165      *
166      * @return a buffered input stream containing the contents of this storage
167      * @exception CoreException if the contents of this storage could not be accessed
168      */

169     protected InputStream createStream() throws CoreException {
170         if (fResource instanceof IStorage) {
171             InputStream is= null;
172             IStorage storage= (IStorage) fResource;
173             try {
174                 is= storage.getContents();
175             } catch (CoreException e) {
176                 if (e.getStatus().getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL) {
177                     fResource.refreshLocal(IResource.DEPTH_INFINITE, null);
178                     is= storage.getContents();
179                 } else
180                     throw e;
181             }
182             if (is != null)
183                 return new BufferedInputStream(is);
184         }
185         return null;
186     }
187             
188     /* (non Javadoc)
189      * see IEditableContent.isEditable
190      */

191     public boolean isEditable() {
192         return true;
193     }
194     
195     /* (non Javadoc)
196      * see IEditableContent.replace
197      */

198     public ITypedElement replace(ITypedElement child, ITypedElement other) {
199         return child;
200     }
201
202     /* (non-Javadoc)
203      * @see org.eclipse.compare.IEncodedStreamContentAccessor#getCharset()
204      */

205     public String JavaDoc getCharset() {
206         return Utilities.getCharset(fResource);
207     }
208
209     /* (non-Javadoc)
210      * @see org.eclipse.compare.IEditableContentExtension#isReadOnly()
211      */

212     public boolean isReadOnly() {
213         if (fResource.getType() == IResource.FILE) {
214             ResourceAttributes attrs = fResource.getResourceAttributes();
215             if (attrs != null) {
216                 return attrs.isReadOnly();
217             }
218         }
219         return false;
220     }
221
222     /* (non-Javadoc)
223      * @see org.eclipse.compare.IEditableContentExtension#validateEdit(org.eclipse.swt.widgets.Shell)
224      */

225     public IStatus validateEdit(Shell shell) {
226         if (isReadOnly())
227             return ResourcesPlugin.getWorkspace().validateEdit(new IFile[] { (IFile)fResource}, shell);
228         return Status.OK_STATUS;
229     }
230 }
231
232
Popular Tags