KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > ContentStamps


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ltk.internal.core.refactoring;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IDocumentExtension4;
22
23 import org.eclipse.ltk.core.refactoring.ContentStamp;
24
25 public class ContentStamps {
26     
27     private static class ContentStampImpl extends ContentStamp {
28         private int fKind;
29         private long fValue;
30         private long fFileStamp;
31         
32         public static final int FILE= 1;
33         public static final int DOCUMENT= 2;
34         
35         private static ContentStamp createFileStamp(long value) {
36             return new ContentStampImpl(FILE, value, value);
37         }
38         
39         private static ContentStamp createDocumentStamp(long value, long fileValue) {
40             return new ContentStampImpl(DOCUMENT, value, fileValue);
41         }
42         
43         private ContentStampImpl(int kind, long value, long filestamp) {
44             fKind= kind;
45             fValue= value;
46             fFileStamp= filestamp;
47         }
48         public boolean isFileStamp() {
49             return fKind == FILE;
50         }
51         public boolean isDocumentStamp() {
52             return fKind == DOCUMENT;
53         }
54         public long getValue() {
55             return fValue;
56         }
57         public long getFileValue() {
58             return fFileStamp;
59         }
60         public boolean isNullStamp() {
61             return false;
62         }
63         public boolean equals(Object JavaDoc obj) {
64             if (!(obj instanceof ContentStampImpl))
65                 return false;
66             return ((ContentStampImpl)obj).fValue == fValue;
67         }
68         public int hashCode() {
69             return (int)fValue;
70         }
71         public String JavaDoc toString() {
72             return "Stamp: " + fValue; //$NON-NLS-1$
73
}
74     }
75     
76     private static class NullContentStamp extends ContentStamp {
77         public boolean isNullStamp() {
78             return true;
79         }
80         public String JavaDoc toString() {
81             return "Null Stamp"; //$NON-NLS-1$
82
}
83     }
84     
85     public static final ContentStamp NULL_CONTENT_STAMP= new NullContentStamp();
86     
87     public static ContentStamp get(IFile file, IDocument document) {
88         if (document instanceof IDocumentExtension4) {
89             long stamp= ((IDocumentExtension4)document).getModificationStamp();
90             if (stamp == IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP)
91                 return NULL_CONTENT_STAMP;
92             return ContentStampImpl.createDocumentStamp(stamp, file.getModificationStamp());
93         }
94         long stamp= file.getModificationStamp();
95         if (stamp == IResource.NULL_STAMP)
96             return NULL_CONTENT_STAMP;
97         return ContentStampImpl.createFileStamp(stamp);
98     }
99     
100     public static void set(IFile file, ContentStamp s) throws CoreException {
101         if (!(s instanceof ContentStampImpl))
102             return;
103         ContentStampImpl stamp= (ContentStampImpl)s;
104         long value= stamp.getFileValue();
105         Assert.isTrue(value != IResource.NULL_STAMP);
106         file.revertModificationStamp(value);
107     }
108     
109     public static boolean set(IDocument document, ContentStamp s) throws CoreException {
110         if (!(s instanceof ContentStampImpl))
111             return false;
112         ContentStampImpl stamp= (ContentStampImpl)s;
113         if (document instanceof IDocumentExtension4 && stamp.isDocumentStamp()) {
114             try {
115                 ((IDocumentExtension4)document).replace(0, 0, "", stamp.getValue()); //$NON-NLS-1$
116
return true;
117             } catch (BadLocationException e) {
118                 throw Changes.asCoreException(e);
119             }
120         }
121         return false;
122     }
123 }
124
Popular Tags