/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*/
package org.sintef.thingml.resource.thingml.ui;
/**
* A background parsing strategy that starts parsing after a amount of time after
* the last key stroke. If keys are pressed within the delay interval, the delay
* is reset. If keys are pressed during background parsing the parse thread is
* stopped and a new parse task is scheduled.
*/
public class ThingmlBackgroundParsingStrategy {
private static long DELAY = 500;
/**
* this timer is used to schedule a parsing task and execute it after a given delay
*/
private Object lock = new Object();
/**
* the background parsing task (may be null)
*/
private ParsingJob job = null;
/**
* Schedules a task for background parsing that will be started after a delay.
*/
public void parse(org.eclipse.jface.text.DocumentEvent event, final org.sintef.thingml.resource.thingml.IThingmlTextResource resource, final org.sintef.thingml.resource.thingml.ui.ThingmlEditor editor) {
parse(event.getDocument(), resource, editor, DELAY);
}
/**
* Schedules a task for background parsing that will be started after a delay.
*/
public void parse(org.eclipse.jface.text.IDocument document, final org.sintef.thingml.resource.thingml.IThingmlTextResource resource, final org.sintef.thingml.resource.thingml.ui.ThingmlEditor editor, long delay) {
parse(document.get(), resource, editor, delay);
}
/**
* Schedules a task for background parsing that will be started after a delay.
*/
public void parse(final String contents, final org.sintef.thingml.resource.thingml.IThingmlTextResource resource, final org.sintef.thingml.resource.thingml.ui.ThingmlEditor editor, long delay) {
if (resource == null) {
return;
}
if (contents == null) {
return;
}
// this synchronization is needed to avoid the creation of multiple tasks. without
// the synchronization this could easily happen, when this method is accessed by
// multiple threads. the creation of multiple tasks would imply that multiple
// background parsing threads for one editor are created, which is not desired.
synchronized (lock) {
if (job == null || job.getState() != org.eclipse.core.runtime.jobs.Job.RUNNING) {
// schedule new task
job = new ParsingJob();
job.resource = resource;
job.editor = editor;
job.newContents = contents;
job.schedule();
} else {
job.newContents = contents;
}
}
}
private class ParsingJob extends org.eclipse.core.runtime.jobs.Job {
private org.sintef.thingml.resource.thingml.ui.ThingmlEditor editor;
private org.sintef.thingml.resource.thingml.IThingmlTextResource resource;
public ParsingJob() {
super("parsing document");
}
private String newContents = null;
protected org.eclipse.core.runtime.IStatus run(org.eclipse.core.runtime.IProgressMonitor monitor) {
while (newContents != null ) {
while (newContents != null) {
try {
String currentContent = newContents;
newContents = null;
String encoding = null;
if (resource instanceof org.sintef.thingml.resource.thingml.mopp.ThingmlResource) {
org.sintef.thingml.resource.thingml.mopp.ThingmlResource concreteResource = (org.sintef.thingml.resource.thingml.mopp.ThingmlResource) resource;
encoding = concreteResource.getEncoding(null);
}
byte[] bytes = null;
if (encoding != null) {
bytes = currentContent.getBytes(encoding);
} else {
bytes = currentContent.getBytes();
}
resource.reload(new java.io.ByteArrayInputStream(bytes), null);
if (newContents != null) {
Thread.sleep(DELAY);
}
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
editor.notifyBackgroundParsingFinished();
}
return org.eclipse.core.runtime.Status.OK_STATUS;
}
};
}