下面列出了怎么用org.eclipse.jface.text.ISynchronizable的API类实例代码及写法,或者点击链接到github查看源代码。
protected void setModel(XtextDocument document, String prefix, String editablePart, String suffix) {
if (this.insertLineBreaks) {
String delimiter = document.getDefaultLineDelimiter();
prefix = prefix + delimiter;
suffix = delimiter + suffix;
}
String model = prefix + editablePart + suffix;
document.set(model);
XtextResource resource = createResource(model);
document.setInput(resource);
AnnotationModel annotationModel = new AnnotationModel();
if (document instanceof ISynchronizable) {
Object lock = ((ISynchronizable) document).getLockObject();
if (lock == null) {
lock = new Object();
((ISynchronizable) document).setLockObject(lock);
}
((ISynchronizable) annotationModel).setLockObject(lock);
}
this.viewer.setDocument(document, annotationModel, prefix.length(), editablePart.length());
}
@Override
public void setContents(String contents) {
synchronized (lock) {
if (fDocument == null) {
if (fTextFileBuffer != null) {
fDocument = fTextFileBuffer.getDocument();
} else {
ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
fDocument = manager.createEmptyDocument(fFile.getFullPath(), LocationKind.IFILE);
}
fDocument.addDocumentListener(this);
((ISynchronizable)fDocument).setLockObject(lock);
}
}
if (!contents.equals(fDocument.get())) {
fDocument.set(contents);
}
}
private void initialize() {
ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
try {
if (fFileStore != null) {
manager.connectFileStore(fFileStore, new NullProgressMonitor());
fTextFileBuffer= manager.getFileStoreTextFileBuffer(fFileStore);
} else {
manager.connect(fPath, fLocationKind, new NullProgressMonitor());
fTextFileBuffer= manager.getTextFileBuffer(fPath, fLocationKind);
}
fDocument= fTextFileBuffer.getDocument();
} catch (CoreException x) {
fDocument= manager.createEmptyDocument(fPath, fLocationKind);
if (fDocument instanceof ISynchronizable)
((ISynchronizable)fDocument).setLockObject(new Object());
}
fDocument.addDocumentListener(this);
fIsClosed= false;
}
/**
* @param document
* the document
* @return the document's lock object
*/
private Object getLockObject(IDocument document) {
if (document instanceof ISynchronizable) {
Object lock = ((ISynchronizable) document).getLockObject();
if (lock != null)
return lock;
}
return document;
}
/**
* Initializes this collector with the given annotation model.
*
* @param annotationModel the annotation model
*/
public TeXSpellingProblemCollector(IAnnotationModel annotationModel) {
Assert.isLegal(annotationModel != null);
fAnnotationModel= annotationModel;
if (fAnnotationModel instanceof ISynchronizable)
fLockObject= ((ISynchronizable)fAnnotationModel).getLockObject();
else
fLockObject= fAnnotationModel;
}
@Override
protected IDocument createEmptyDocument() {
IDocument document= FileBuffers.getTextFileBufferManager().createEmptyDocument(null, LocationKind.IFILE);
if (document instanceof ISynchronizable)
((ISynchronizable)document).setLockObject(new Object());
return document;
}
private Object getLockObject(IAnnotationModel annotationModel) {
if (annotationModel instanceof ISynchronizable) {
Object lock = ((ISynchronizable) annotationModel).getLockObject();
if (lock != null)
return lock;
}
return annotationModel;
}
/**
* Returns the lock object for the given annotation model.
*
* @param annotationModel
* the annotation model
* @return the annotation model's lock object
*
*/
private Object getLockObject(IAnnotationModel annotationModel) {
if (annotationModel instanceof ISynchronizable) {
Object lock = ((ISynchronizable) annotationModel).getLockObject();
if (lock != null)
return lock;
}
return annotationModel;
}
private static Object getLockObject(Object object)
{
if (object instanceof ISynchronizable)
{
Object lock = ((ISynchronizable) object).getLockObject();
if (lock != null)
{
return lock;
}
}
return object;
}
private static Object getLockObject(Object object)
{
if (object instanceof ISynchronizable)
{
Object lock = ((ISynchronizable) object).getLockObject();
if (lock != null)
{
return lock;
}
}
return object;
}
/**
* getAnnotationModelLock
*
* @param annotationModel
* @return
*/
private Object getAnnotationModelLock(IAnnotationModel annotationModel) {
Object result = annotationModel;
if (annotationModel instanceof ISynchronizable) {
Object lock = ((ISynchronizable) annotationModel).getLockObject();
if (lock != null) {
result = lock;
}
}
return result;
}
/**
* Initializes this collector with the given annotation model.
*
* @param annotationModel
* the annotation model
*/
public SpellingProblemCollector(IAnnotationModel annotationModel) {
Assert.isLegal(annotationModel != null);
fAnnotationModel = annotationModel;
if (fAnnotationModel instanceof ISynchronizable) {
fLockObject = ((ISynchronizable) fAnnotationModel).getLockObject();
} else {
fLockObject = fAnnotationModel;
}
}
@Override
protected IDocument createEmptyDocument() {
IDocument document= FileBuffers.getTextFileBufferManager().createEmptyDocument(null, LocationKind.IFILE);
if (document instanceof ISynchronizable)
((ISynchronizable)document).setLockObject(new Object());
return document;
}
/**
* Returns the lock object for the given annotation model.
*
* @param annotationModel the annotation model
* @return the annotation model's lock object
* @since 3.0
*/
private Object getLockObject(IAnnotationModel annotationModel) {
if (annotationModel instanceof ISynchronizable) {
Object lock= ((ISynchronizable)annotationModel).getLockObject();
if (lock != null)
return lock;
}
return annotationModel;
}
/**
* Returns the lock object for the given annotation model.
*
* @param annotationModel the annotation model
* @return the annotation model's lock object
* @since 3.0
*/
private Object getLockObject(IAnnotationModel annotationModel) {
if (annotationModel instanceof ISynchronizable) {
Object lock= ((ISynchronizable)annotationModel).getLockObject();
if (lock != null)
return lock;
}
return annotationModel;
}
/**
* @param document the document
* @return the document's lock object
*/
private Object getLockObject(IDocument document) {
if (document instanceof ISynchronizable) {
Object lock= ((ISynchronizable)document).getLockObject();
if (lock != null)
return lock;
}
return document;
}
/**
* Gotten from JavaEditor#getLockObject
*/
protected Object getLockObject(IAnnotationModel annotationModel) {
if (annotationModel instanceof ISynchronizable) {
return ((ISynchronizable) annotationModel).getLockObject();
} else {
return annotationModel;
}
}
public static Object runWithDocumentSynched(IDocument document, ICallback<Object, IDocument> iCallback) {
Object lockObject = null;
if (document instanceof ISynchronizable) {
ISynchronizable sync = (ISynchronizable) document;
lockObject = sync.getLockObject();
}
if (lockObject != null) {
synchronized (lockObject) {
return iCallback.call(document);
}
} else { //unsynched
return iCallback.call(document);
}
}
public static Object runWithDocumentSynched(IDocument document, ICallback<Object, IDocument> iCallback,
boolean createCopy) {
Object lockObject = null;
if (document instanceof ISynchronizable) {
ISynchronizable sync = (ISynchronizable) document;
lockObject = sync.getLockObject();
}
DocCopy docCopy = null;
try {
if (lockObject != null) {
if (createCopy) {
synchronized (lockObject) {
docCopy = new DocCopy(document);
}
return iCallback.call(docCopy);
} else {
synchronized (lockObject) {
return iCallback.call(document);
}
}
} else { //unsynched
if (createCopy && !(document instanceof DocCopy)) {
docCopy = new DocCopy(document);
return iCallback.call(docCopy);
}
return iCallback.call(document);
}
} finally {
if (docCopy != null) {
docCopy.dispose();
}
}
}
public static <R> R runUnderDocumentLock(IDocument doc, CallableX<R, RuntimeException> runnable) {
if(doc instanceof ISynchronizable) {
ISynchronizable synchronizable = (ISynchronizable) doc;
Object lockObject = synchronizable.getLockObject();
if(lockObject != null) {
synchronized(lockObject) {
return runnable.call();
}
}
}
return runnable.call();
}
public static IDocument createUnsynchedDocIfNeeded(IDocument doc) {
if (doc instanceof ISynchronizable) {
return new DocCopy(doc);
}
return doc;
}
@Override
public void endCollecting() {
List<Object> toRemove = new ArrayList<Object>();
Object fLockObject;
if (fAnnotationModel instanceof ISynchronizable) {
fLockObject = ((ISynchronizable) fAnnotationModel).getLockObject();
} else {
fLockObject = new Object();
}
//let other threads execute before getting the lock on the annotation model
Thread.yield();
Thread thread = Thread.currentThread();
int initiaThreadlPriority = thread.getPriority();
try {
//before getting the lock, let's execute with normal priority, to optimize the time that we'll
//retain that object locked (the annotation model is used on lots of places, so, retaining the lock
//on it on a minimum priority thread is not a good thing.
thread.setPriority(Thread.NORM_PRIORITY);
Iterator<Annotation> iter;
synchronized (fLockObject) {
iter = fAnnotationModel.getAnnotationIterator();
while (iter.hasNext()) {
Object n = iter.next();
if (n instanceof SpellingAnnotation) {
toRemove.add(n);
}
}
iter = null;
}
Annotation[] annotationsToRemove = toRemove.toArray(new Annotation[toRemove.size()]);
//let other threads execute before getting the lock (again) on the annotation model
Thread.yield();
synchronized (fLockObject) {
if (fAnnotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension) fAnnotationModel).replaceAnnotations(annotationsToRemove,
fAddAnnotations);
} else {
for (int i = 0; i < annotationsToRemove.length; i++) {
fAnnotationModel.removeAnnotation(annotationsToRemove[i]);
}
for (iter = fAddAnnotations.keySet().iterator(); iter.hasNext();) {
Annotation annotation = iter.next();
fAnnotationModel.addAnnotation(annotation, fAddAnnotations.get(annotation));
}
}
}
} finally {
thread.setPriority(initiaThreadlPriority);
}
fAddAnnotations = null;
}