/*******************************************************************************
* Copyright (c) 2000, 2010 IBM Corporation and others.
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.aptana.ide.search.epl.filesystem.ui.text;
import java.io.File;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension4;
import org.eclipse.ltk.core.refactoring.ContentStamp;
import org.eclipse.ltk.internal.core.refactoring.Changes;
/**
* Modified to take java.io.File as the content control.
*/
public class FileSystemContentStamps {
private static class ContentStampImpl extends ContentStamp {
private int fKind;
private long fValue;
private long fFileStamp;
public static final int FILE = 1;
public static final int DOCUMENT = 2;
private static ContentStamp createFileStamp(long value) {
return new ContentStampImpl(FILE, value, value);
}
private static ContentStamp createDocumentStamp(long value,
long fileValue) {
return new ContentStampImpl(DOCUMENT, value, fileValue);
}
private ContentStampImpl(int kind, long value, long filestamp) {
fKind = kind;
fValue = value;
fFileStamp = filestamp;
}
public boolean isDocumentStamp() {
return fKind == DOCUMENT;
}
public long getValue() {
return fValue;
}
public long getFileValue() {
return fFileStamp;
}
public boolean isNullStamp() {
return false;
}
public boolean equals(Object obj) {
if (!(obj instanceof ContentStampImpl))
return false;
return ((ContentStampImpl) obj).fValue == fValue;
}
public int hashCode() {
return (int) fValue;
}
public String toString() {
return "Stamp: " + fValue; //$NON-NLS-1$
}
}
private static class NullContentStamp extends ContentStamp {
public boolean isNullStamp() {
return true;
}
public String toString() {
return "Null Stamp"; //$NON-NLS-1$
}
}
public static final ContentStamp NULL_CONTENT_STAMP = new NullContentStamp();
public static ContentStamp get(File file, IDocument document) {
if (document instanceof IDocumentExtension4) {
long stamp = ((IDocumentExtension4) document)
.getModificationStamp();
if (stamp == IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP)
return NULL_CONTENT_STAMP;
return ContentStampImpl.createDocumentStamp(stamp, file
.lastModified());
}
long stamp = file.lastModified();
if (stamp == 0)
return NULL_CONTENT_STAMP;
return ContentStampImpl.createFileStamp(stamp);
}
public static void set(File file, ContentStamp s) throws CoreException {
if (!(s instanceof ContentStampImpl))
return;
ContentStampImpl stamp = (ContentStampImpl) s;
long value = stamp.getFileValue();
Assert.isTrue(value != 0);
file.setLastModified(0);
}
public static boolean set(IDocument document, ContentStamp s)
throws CoreException {
if (!(s instanceof ContentStampImpl))
return false;
ContentStampImpl stamp = (ContentStampImpl) s;
if (document instanceof IDocumentExtension4 && stamp.isDocumentStamp()) {
try {
((IDocumentExtension4) document).replace(0, 0,
"", stamp.getValue()); //$NON-NLS-1$
return true;
} catch (BadLocationException e) {
throw Changes.asCoreException(e);
}
}
return false;
}
}