Search in sources :

Example 6 with IJobChangeListener

use of org.eclipse.core.runtime.jobs.IJobChangeListener in project polymap4-core by Polymap4.

the class OperationSupport method execute3.

/**
 *@see #execute(IUndoableOperation, boolean, boolean, IJobChangeListener...)
 */
public void execute3(final IUndoableOperation op, boolean async, boolean progress, Runnable... doneHandlers) {
    IJobChangeListener[] listeners = new IJobChangeListener[doneHandlers.length];
    for (int i = 0; i < doneHandlers.length; i++) {
        Runnable handler = doneHandlers[i];
        listeners[i] = new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent ev) {
                handler.run();
            }
        };
    }
    execute(op, async, progress, listeners);
}
Also used : IJobChangeListener(org.eclipse.core.runtime.jobs.IJobChangeListener) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 7 with IJobChangeListener

use of org.eclipse.core.runtime.jobs.IJobChangeListener in project core by jcryptool.

the class IntegratorWizardPage method makeNewKey.

protected void makeNewKey() {
    Job[] preJobs = Job.getJobManager().find(KeyStoreHelper.KEYSTOREHELPER_FAMILY);
    int preJobCount = preJobs.length;
    KeyStoreHelper.makeSymmetricKeyByWizard(showKeyGroup).addObserver(new Observer() {

        public void update(Observable o, final Object arg) {
            if (arg != null) {
                keyFromKeystoreGroup.getDisplay().syncExec(new Runnable() {

                    public void run() {
                        KeyStoreAlias ref = (KeyStoreAlias) arg;
                        setKeyForShowcase(ref);
                    }
                });
            }
        }
    });
    Job[] jobs = Job.getJobManager().find(KeyStoreHelper.KEYSTOREHELPER_FAMILY);
    if (jobs.length > preJobCount) {
        createNewKeyButton.getDisplay().syncExec(new Runnable() {

            public void run() {
                buttonTextBeforeJobrunningMsg = createNewKeyButton.getText();
                // $NON-NLS-1$
                createNewKeyButton.setText(Messages.getString("IntegratorWizardPage.generatingButtonHint"));
                disableControls();
            }
        });
        Job job = jobs[jobs.length - 1];
        IJobChangeListener listener = new IJobChangeListener() {

            public void sleeping(IJobChangeEvent event) {
            }

            public void done(IJobChangeEvent event) {
                createNewKeyButton.getDisplay().syncExec(new Runnable() {

                    public void run() {
                        createNewKeyButton.setText(buttonTextBeforeJobrunningMsg);
                        enableControls();
                    }
                });
            }

            public void awake(IJobChangeEvent event) {
            }

            public void aboutToRun(IJobChangeEvent event) {
            }

            public void running(IJobChangeEvent event) {
            }

            public void scheduled(IJobChangeEvent event) {
            }
        };
        if (job.getState() != Job.NONE)
            job.addJobChangeListener(listener);
        else {
            listener.done(null);
        }
    }
}
Also used : KeyStoreAlias(org.jcryptool.crypto.keystore.backend.KeyStoreAlias) Observer(java.util.Observer) IJobChangeListener(org.eclipse.core.runtime.jobs.IJobChangeListener) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) Job(org.eclipse.core.runtime.jobs.Job) Observable(java.util.Observable)

Example 8 with IJobChangeListener

use of org.eclipse.core.runtime.jobs.IJobChangeListener in project core by jcryptool.

the class IntegratorWizardPage method makeNewKeypair.

protected void makeNewKeypair() {
    Job[] preJobs = Job.getJobManager().find(KeyStoreHelper.KEYSTOREHELPER_FAMILY);
    int preJobCount = preJobs.length;
    KeyStoreHelper.makeKeyPairByWizard(showKeyGroup).addObserver(new Observer() {

        public void update(Observable o, final Object arg) {
            if (arg != null) {
                keyFromKeystoreGroup.getDisplay().syncExec(new Runnable() {

                    public void run() {
                        KeyStoreAlias ref = (KeyStoreAlias) arg;
                        setKeyForShowcase(ref);
                    }
                });
            }
        }
    });
    Job[] jobs = Job.getJobManager().find(KeyStoreHelper.KEYSTOREHELPER_FAMILY);
    if (jobs.length > preJobCount) {
        createNewKeyButton.getDisplay().syncExec(new Runnable() {

            public void run() {
                buttonTextBeforeJobrunningMsg = createNewKeyButton.getText();
                // $NON-NLS-1$
                createNewKeyButton.setText(Messages.getString("IntegratorWizardPage.generatingButtonHint"));
                disableControls();
            }
        });
        Job job = jobs[jobs.length - 1];
        IJobChangeListener listener = new IJobChangeListener() {

            public void sleeping(IJobChangeEvent event) {
            }

            public void done(IJobChangeEvent event) {
                createNewKeyButton.getDisplay().syncExec(new Runnable() {

                    public void run() {
                        createNewKeyButton.setText(buttonTextBeforeJobrunningMsg);
                        enableControls();
                    }
                });
            }

            public void awake(IJobChangeEvent event) {
            }

            public void aboutToRun(IJobChangeEvent event) {
            }

            public void running(IJobChangeEvent event) {
            }

            public void scheduled(IJobChangeEvent event) {
            }
        };
        if (job.getState() != Job.NONE)
            job.addJobChangeListener(listener);
        else {
            listener.done(null);
        }
    }
}
Also used : KeyStoreAlias(org.jcryptool.crypto.keystore.backend.KeyStoreAlias) Observer(java.util.Observer) IJobChangeListener(org.eclipse.core.runtime.jobs.IJobChangeListener) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) Job(org.eclipse.core.runtime.jobs.Job) Observable(java.util.Observable)

Example 9 with IJobChangeListener

use of org.eclipse.core.runtime.jobs.IJobChangeListener in project polymap4-core by Polymap4.

the class UIJob method joinAndDispatch.

/**
 * Waits until this job is finished. This method will block the calling thread
 * until the job has finished executing, or until this thread has been
 * interrupted, or the timeout exceeds. A job must not be joined from within the
 * scope of its run method.
 * <p/>
 * This method must be called from the UIThread. It calls readAndDispatch() of
 * the current display, so that the UI is responsive during the call is blocked.
 *
 * @param timeoutMillis
 * @return False if the job did not finish within the given timeout or the
 *         calling thread was interrupted.
 */
public boolean joinAndDispatch(long timeoutMillis) {
    final AtomicBoolean done = new AtomicBoolean(getState() == Job.NONE);
    IJobChangeListener jobListener = new JobChangeAdapter() {

        public void done(IJobChangeEvent event) {
            synchronized (done) {
                done.set(true);
                done.notify();
            }
        }
    };
    addJobChangeListener(jobListener);
    // registered; this avoids race cond. between the two
    if (getState() == Job.NONE) {
        removeJobChangeListener(jobListener);
        return true;
    }
    final Display threadDisplay = Display.getCurrent();
    final Timer timer = new Timer();
    while (!done.get() && timer.elapsedTime() < timeoutMillis && (threadDisplay == null || !threadDisplay.isDisposed())) {
        Thread.yield();
        if (threadDisplay != null) {
            if (!threadDisplay.readAndDispatch()) {
                synchronized (done) {
                    try {
                        done.wait(300);
                        log.debug("wake after: " + timer.elapsedTime() + "ms");
                    } catch (InterruptedException e) {
                    }
                }
            // // just wait on #done blocks hangs;
            // // display.sleep() might wait forever, so we need this watchdog
            // Polymap.executorService().execute( new Runnable() {
            // public void run() {
            // synchronized (done) {
            // try { done.wait( 300 ); } catch (InterruptedException e) {}
            // }
            // log.info( "wake ..." );
            // threadDisplay.wake();
            // }
            // });
            // threadDisplay.sleep();
            }
        } else {
            synchronized (done) {
                try {
                    done.wait(250);
                } catch (InterruptedException e) {
                }
            }
        }
    }
    removeJobChangeListener(jobListener);
    return done.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IJobChangeListener(org.eclipse.core.runtime.jobs.IJobChangeListener) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) Display(org.eclipse.swt.widgets.Display)

Aggregations

IJobChangeListener (org.eclipse.core.runtime.jobs.IJobChangeListener)9 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)8 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)6 Observable (java.util.Observable)2 Observer (java.util.Observer)2 Job (org.eclipse.core.runtime.jobs.Job)2 KeyStoreAlias (org.jcryptool.crypto.keystore.backend.KeyStoreAlias)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 IUndoableOperation (org.eclipse.core.commands.operations.IUndoableOperation)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 Display (org.eclipse.swt.widgets.Display)1 Test (org.junit.Test)1 UIJob (org.polymap.core.runtime.UIJob)1