Search in sources :

Example 1 with CancelableFuture

use of org.eclipse.egit.ui.internal.dialogs.CancelableFuture in project egit by eclipse.

the class AsynchronousRefProposalProvider method getCandidates.

@Override
public Collection<? extends Ref> getCandidates() {
    String uri = uriProvider.get();
    if (uri == null) {
        return null;
    }
    CancelableFuture<Collection<Ref>> list = listProvider.apply(uri);
    try {
        if (!list.isFinished()) {
            IRunnableWithProgress operation = monitor -> {
                monitor.beginTask(MessageFormat.format(UIText.AsynchronousRefProposalProvider_FetchingRemoteRefsMessage, uri), IProgressMonitor.UNKNOWN);
                Collection<Ref> result = list.get();
                if (monitor.isCanceled()) {
                    return;
                }
                // If we get here, the ChangeList future is done.
                if (result == null || result.isEmpty()) {
                    // Don't bother if we didn't get any results
                    return;
                }
                // If we do have results now, open the proposals.
                Job showProposals = new WorkbenchJob(UIText.AsynchronousRefProposalProvider_ShowingProposalsJobName) {

                    @Override
                    public boolean shouldRun() {
                        return super.shouldRun() && contentProposer != null;
                    }

                    @Override
                    public IStatus runInUIThread(IProgressMonitor uiMonitor) {
                        // is still the same
                        try {
                            if (container instanceof NonBlockingWizardDialog) {
                                // and focus will be restored
                                if (textField.isDisposed() || !textField.isVisible() || textField != textField.getDisplay().getFocusControl()) {
                                    return Status.CANCEL_STATUS;
                                }
                                String uriNow = uriProvider.get();
                                if (!uriNow.equals(uri)) {
                                    return Status.CANCEL_STATUS;
                                }
                            }
                            contentProposer.openProposalPopup();
                        } catch (SWTException e) {
                            // Disposed already
                            return Status.CANCEL_STATUS;
                        } finally {
                            uiMonitor.done();
                        }
                        return Status.OK_STATUS;
                    }
                };
                showProposals.schedule();
            };
            if (container instanceof NonBlockingWizardDialog) {
                NonBlockingWizardDialog dialog = (NonBlockingWizardDialog) container;
                dialog.run(operation, () -> list.cancel(CancelableFuture.CancelMode.ABANDON));
            } else {
                container.run(true, true, operation);
            }
            return null;
        }
        return list.get();
    } catch (InterruptedException | InvocationTargetException e) {
        return null;
    }
}
Also used : Text(org.eclipse.swt.widgets.Text) Job(org.eclipse.core.runtime.jobs.Job) Collection(java.util.Collection) Status(org.eclipse.core.runtime.Status) ExplicitContentProposalAdapter(org.eclipse.egit.ui.UIUtils.ExplicitContentProposalAdapter) WorkbenchJob(org.eclipse.ui.progress.WorkbenchJob) Function(java.util.function.Function) Supplier(java.util.function.Supplier) NonBlockingWizardDialog(org.eclipse.egit.ui.internal.dialogs.NonBlockingWizardDialog) InvocationTargetException(java.lang.reflect.InvocationTargetException) MessageFormat(java.text.MessageFormat) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) UIText(org.eclipse.egit.ui.internal.UIText) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Ref(org.eclipse.jgit.lib.Ref) SWTException(org.eclipse.swt.SWTException) IWizardContainer(org.eclipse.jface.wizard.IWizardContainer) IContentProposalCandidateProvider(org.eclipse.egit.ui.UIUtils.IContentProposalCandidateProvider) CancelableFuture(org.eclipse.egit.ui.internal.dialogs.CancelableFuture) IStatus(org.eclipse.core.runtime.IStatus) WorkbenchJob(org.eclipse.ui.progress.WorkbenchJob) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) NonBlockingWizardDialog(org.eclipse.egit.ui.internal.dialogs.NonBlockingWizardDialog) SWTException(org.eclipse.swt.SWTException) Collection(java.util.Collection) Job(org.eclipse.core.runtime.jobs.Job) WorkbenchJob(org.eclipse.ui.progress.WorkbenchJob)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MessageFormat (java.text.MessageFormat)1 Collection (java.util.Collection)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 Job (org.eclipse.core.runtime.jobs.Job)1 ExplicitContentProposalAdapter (org.eclipse.egit.ui.UIUtils.ExplicitContentProposalAdapter)1 IContentProposalCandidateProvider (org.eclipse.egit.ui.UIUtils.IContentProposalCandidateProvider)1 UIText (org.eclipse.egit.ui.internal.UIText)1 CancelableFuture (org.eclipse.egit.ui.internal.dialogs.CancelableFuture)1 NonBlockingWizardDialog (org.eclipse.egit.ui.internal.dialogs.NonBlockingWizardDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 IWizardContainer (org.eclipse.jface.wizard.IWizardContainer)1 Ref (org.eclipse.jgit.lib.Ref)1 SWTException (org.eclipse.swt.SWTException)1 Text (org.eclipse.swt.widgets.Text)1 WorkbenchJob (org.eclipse.ui.progress.WorkbenchJob)1