Search in sources :

Example 1 with ExclusiveSchedulingRule

use of eu.esdihumboldt.hale.ui.util.jobs.ExclusiveSchedulingRule in project hale by halestudio.

the class CompilingSourceViewer method init.

@Override
protected void init() {
    compileJob = new Job("Compile") {

        @Override
        public boolean shouldRun() {
            return compilationEnabled;
        }

        @Override
        public boolean shouldSchedule() {
            return compilationEnabled;
        }

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            String content;
            changeLock.lock();
            try {
                if (!changed) {
                    return Status.OK_STATUS;
                }
                IDocument doc = getDocument();
                if (doc != null) {
                    content = doc.get();
                } else {
                    content = null;
                }
                changed = false;
            } finally {
                changeLock.unlock();
            }
            C result = null;
            if (content != null) {
                try {
                    // this is the potentially long running stuff
                    result = compile(content);
                } catch (Exception e) {
                    // ignore, but log
                    log.warn("Error compiling document content", e);
                }
            }
            boolean notify = false;
            C previous = null;
            changeLock.lock();
            try {
                /*
					 * Only notify listeners if the document was not changed in
					 * the meantime.
					 */
                notify = !changed;
                if (notify) {
                    // set result
                    previous = compiled;
                    compiled = result;
                    // set result for futures
                    for (SettableFuture<C> future : toUpdate) {
                        future.set(result);
                    }
                    toUpdate.clear();
                }
            } finally {
                changeLock.unlock();
            }
            if (notify) {
                // notify listeners
                PropertyChangeEvent event = new PropertyChangeEvent(CompilingSourceViewer.this, PROPERTY_COMPILED, previous, result);
                notifyOnPropertyChange(event);
            }
            return Status.OK_STATUS;
        }
    };
    compileJob.setSystem(true);
    compileJob.setRule(new ExclusiveSchedulingRule(compileJob));
    super.init();
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) Job(org.eclipse.core.runtime.jobs.Job) IDocument(org.eclipse.jface.text.IDocument) ExclusiveSchedulingRule(eu.esdihumboldt.hale.ui.util.jobs.ExclusiveSchedulingRule)

Example 2 with ExclusiveSchedulingRule

use of eu.esdihumboldt.hale.ui.util.jobs.ExclusiveSchedulingRule in project hale by halestudio.

the class ValidatingSourceViewer method init.

/**
 * Initialize the Job and listener.
 */
protected void init() {
    validateJob = new Job("Source viewer validation") {

        @Override
        public boolean shouldRun() {
            return validationEnabled.get();
        }

        @Override
        public boolean shouldSchedule() {
            return validationEnabled.get();
        }

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            String content;
            changeLock.lock();
            try {
                if (!changed) {
                    return Status.OK_STATUS;
                }
                IDocument doc = getDocument();
                if (doc != null) {
                    content = doc.get();
                } else {
                    content = "";
                }
                changed = false;
            } finally {
                changeLock.unlock();
            }
            boolean success = false;
            try {
                // this is the potentially long running stuff
                success = validate(content);
            } catch (Exception e) {
                // ignore, but log
                log.warn("Error validating document content", e);
                success = false;
            }
            boolean notify = false;
            changeLock.lock();
            try {
                /*
					 * Only notify listeners if the document was not changed in
					 * the meantime and the valid state is different than
					 * before.
					 */
                notify = !changed && valid != success;
                if (notify) {
                    // set result
                    valid = success;
                }
            } finally {
                changeLock.unlock();
            }
            if (notify) {
                PropertyChangeEvent event = new PropertyChangeEvent(ValidatingSourceViewer.this, PROPERTY_VALID, !success, success);
                notifyOnPropertyChange(event);
            }
            return Status.OK_STATUS;
        }
    };
    validateJob.setUser(false);
    validateJob.setRule(new ExclusiveSchedulingRule(this));
    documentListener = new IDocumentListener() {

        @Override
        public void documentChanged(DocumentEvent event) {
            scheduleValidation();
        }

        @Override
        public void documentAboutToBeChanged(DocumentEvent event) {
        // do nothing
        }
    };
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) IDocumentListener(org.eclipse.jface.text.IDocumentListener) Job(org.eclipse.core.runtime.jobs.Job) DocumentEvent(org.eclipse.jface.text.DocumentEvent) IDocument(org.eclipse.jface.text.IDocument) ExclusiveSchedulingRule(eu.esdihumboldt.hale.ui.util.jobs.ExclusiveSchedulingRule)

Aggregations

ExclusiveSchedulingRule (eu.esdihumboldt.hale.ui.util.jobs.ExclusiveSchedulingRule)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IStatus (org.eclipse.core.runtime.IStatus)2 Job (org.eclipse.core.runtime.jobs.Job)2 IDocument (org.eclipse.jface.text.IDocument)2 PropertyChangeEvent (org.eclipse.jface.util.PropertyChangeEvent)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 DocumentEvent (org.eclipse.jface.text.DocumentEvent)1 IDocumentListener (org.eclipse.jface.text.IDocumentListener)1