diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/Messages.java index 89c1ed33840..7230e5dfd6c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/Messages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/Messages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Wind River Systems, Inc. and others. + * Copyright (c) 2007 2008 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -17,6 +17,7 @@ public class Messages extends NLS { public static String IndexUI_infoNotInIndex; public static String IndexUI_infoNotInSource; public static String IndexUI_infoSelectIndexAllFiles; + public static String SelectionListenerWithASTManager_jobName; static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/SelectionListenerWithASTManager.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/SelectionListenerWithASTManager.java index abbc9414241..cbf577e6ba3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/SelectionListenerWithASTManager.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/SelectionListenerWithASTManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 IBM Corporation and others. + * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,14 +7,17 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Anton Leherbauer (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.ui.viewsupport; import java.util.HashMap; import java.util.Map; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.ListenerList; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.text.ITextSelection; @@ -58,6 +61,11 @@ public class SelectionListenerWithASTManager { private ISelectionChangedListener fSelectionListener; private Job fCurrentJob; private ListenerList fAstListeners; + /** + * Lock to avoid having more than one calculateAndInform job in parallel. + * Only jobs may synchronize on this as otherwise deadlocks are possible. + */ + private final Object fJobLock= new Object(); public PartListenerGroup(ITextEditor editorPart) { fPart= editorPart; @@ -116,13 +124,29 @@ public class SelectionListenerWithASTManager { fCurrentJob.cancel(); } - IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(fPart.getEditorInput()); + final IWorkingCopy workingCopy = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(fPart.getEditorInput()); if (workingCopy == null) return; - ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() { + fCurrentJob= new Job(Messages.SelectionListenerWithASTManager_jobName) { + public IStatus run(IProgressMonitor monitor) { + if (monitor == null) { + monitor= new NullProgressMonitor(); + } + synchronized (fJobLock) { + return calculateASTandInform(workingCopy, selection, monitor); + } + } + }; + fCurrentJob.setPriority(Job.DECORATE); + fCurrentJob.setSystem(true); + fCurrentJob.schedule(); + } + + protected IStatus calculateASTandInform(final IWorkingCopy workingCopy, final ITextSelection selection, final IProgressMonitor monitor) { + return ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, monitor, new ASTRunnable() { public IStatus runOnAST(ILanguage lang, IASTTranslationUnit astRoot) { - if (astRoot != null) { + if (astRoot != null && !monitor.isCanceled()) { Object[] listeners; synchronized (PartListenerGroup.this) { listeners= fAstListeners.getListeners(); @@ -130,18 +154,19 @@ public class SelectionListenerWithASTManager { for (int i= 0; i < listeners.length; i++) { ((ISelectionListenerWithAST) listeners[i]).selectionChanged(fPart, selection, astRoot); } + return Status.OK_STATUS; } - return Status.OK_STATUS; + return Status.CANCEL_STATUS; } }); } } - private Map fListenerGroups; + private Map fListenerGroups; private SelectionListenerWithASTManager() { - fListenerGroups= new HashMap(); + fListenerGroups= new HashMap(); } /** @@ -151,7 +176,7 @@ public class SelectionListenerWithASTManager { */ public void addListener(ITextEditor part, ISelectionListenerWithAST listener) { synchronized (this) { - PartListenerGroup partListener= (PartListenerGroup) fListenerGroups.get(part); + PartListenerGroup partListener= fListenerGroups.get(part); if (partListener == null) { partListener= new PartListenerGroup(part); fListenerGroups.put(part, partListener); @@ -167,7 +192,7 @@ public class SelectionListenerWithASTManager { */ public void removeListener(ITextEditor part, ISelectionListenerWithAST listener) { synchronized (this) { - PartListenerGroup partListener= (PartListenerGroup) fListenerGroups.get(part); + PartListenerGroup partListener= fListenerGroups.get(part); if (partListener != null) { partListener.uninstall(listener); if (partListener.isEmpty()) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/messages.properties index 93c8c2c0d19..f672ee384a2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/messages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/messages.properties @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (c) 2007 Wind River Systems, Inc. and others. +# Copyright (c) 2007, 2008 Wind River Systems, Inc. and others. # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Public License v1.0 # which accompanies this distribution, and is available at @@ -11,3 +11,4 @@ IndexUI_infoNotInSource=The element ''{0}'' does not belong to a source file. IndexUI_infoNotInIndex=The file ''{0}'' is currently not part of the index. IndexUI_infoSelectIndexAllFiles=For headers that are never included, or sources that are not part of the build, consider selecting the preference 'Index all files'. +SelectionListenerWithASTManager_jobName=Notifying selection listeners