Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Open Declarations UI/Responsiveness improvement

Folks,

  This patch addresses two issues with the Open Declarations action 
accessible from the editor right click menu.  

The first issue concerns the lack of user feedback when the action is
kicked off.  It is often possible with large projects that this action
could take a long time during which the user is provided with no feedback.
This patch moves the search into a runnable object which can be displayed
with a progress monitor.  This requires that the BasicSearchResultCollector()
class be patched to have an additional constructor which takes an 
IProgressMonitor object.  The upside of this is that the user can also
now cancel the action if it is taking too long.

The second issue is also that this action should only enable itself when
it is truly able to run.  This is a small enhancement compared to the 
above change.

  This patch looks nasty ... but really it isn't.  There ended up just
being a bunch of code moving around internally in the class =;-)  The
patches were taken against the 1.2 stream, but should be applicable to
both the head and the 1.2 stream.

Thanks,
 Thomas

ChangeLogs
- Allow construction of the BasicSearchResultCollector using an 
  IProgressMonitor object.

- Update the OpenDeclarations action to be enable/disable based on the
  situation and present the user with a dialog when the action is taking
  place.

--- PATCH START - BasicSearchResultCollector ---
Index: search/org/eclipse/cdt/core/search/BasicSearchResultCollector.java
===================================================================
RCS 
file: /home/tools/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/Bas
icSearchResultCollector.java,v
retrieving revision 1.8
diff -u -r1.8 BasicSearchResultCollector.java
--- search/org/eclipse/cdt/core/search/BasicSearchResultCollector.java	23 
Sep 2003 13:54:21 -0000	1.8
+++ search/org/eclipse/cdt/core/search/BasicSearchResultCollector.java	3 
Feb 2004 15:01:00 -0000
@@ -53,6 +53,14 @@
  * Window>Preferences>Java>Code Generation>Code and Comments
  */
 public class BasicSearchResultCollector implements ICSearchResultCollector {
+	IProgressMonitor fProgressMonitor = null;
+
+	public BasicSearchResultCollector() {
+	}
+	
+	public BasicSearchResultCollector(IProgressMonitor monitor) {
+		fProgressMonitor = monitor;
+	}
 
 	public void aboutToStart() {
 		results = new HashSet();
@@ -62,7 +70,7 @@
 	}
 	
 	public IProgressMonitor getProgressMonitor() {
-		return null;
+		return fProgressMonitor;
 	}
 
 	public IMatch createMatch(Object fileResource, int start, int end, 
ISourceElementCallbackDelegate node ) throws CoreException 
--- PATCH START - OpenDeclarations ---
Index: src/org/eclipse/cdt/internal/ui/editor/OpenDeclarationsAction.java
===================================================================
RCS 
file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/O
penDeclarationsAction.java,v
retrieving revision 1.2.2.2
diff -u -r1.2.2.2 OpenDeclarationsAction.java
--- src/org/eclipse/cdt/internal/ui/editor/OpenDeclarationsAction.java	11 
Nov 2003 18:28:20 -0000	1.2.2.2
+++ src/org/eclipse/cdt/internal/ui/editor/OpenDeclarationsAction.java	3 
Feb 2004 14:48:50 -0000
@@ -23,9 +23,10 @@
 import org.eclipse.cdt.ui.CSearchResultLabelProvider;
 import org.eclipse.cdt.ui.CUIPlugin;
 import org.eclipse.cdt.ui.IWorkingCopyManager;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
@@ -35,6 +36,7 @@
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.IUpdate;
 
 /**
  * This action opens a java CEditor on the element represented by text 
selection of
@@ -42,12 +44,11 @@
  * 
  * Use action from package org.eclipse.jdt.ui.actions
  */
-public class OpenDeclarationsAction extends Action {
+public class OpenDeclarationsAction extends Action implements IUpdate {
 		
 	private String fDialogTitle;
 	private String fDialogMessage;
 	protected CEditor fEditor;
-	BasicSearchResultCollector  resultCollector = null;
 	SearchEngine searchEngine = null;
 	
 	/**
@@ -63,7 +64,6 @@
 		setDialogMessage(CEditorMessages.getString
("OpenDeclarations.dialog.message")); //$NON-NLS-1$
 
 		searchEngine = new SearchEngine();
-		resultCollector = new BasicSearchResultCollector();
 	}
 	
 	/**
@@ -93,66 +93,86 @@
 	public void setContentEditor(CEditor editor) {	
 		fEditor= editor;
 	}
+
+	/**
+	 * Return the selected string from the editor
+	 * @return The string currently selected, or null if there is no 
valid selection
+	 */
+	protected String getSelectedStringFromEditor() {
+		if (fEditor.getSelectionProvider() == null) {
+			return null;
+		}
+
+		try {
+			ITextSelection selection= (ITextSelection) 
fEditor.getSelectionProvider().getSelection();
+			String sel = selection.getText();
+			if (sel.equals(""))
+			{
+				int selStart =  selection.getOffset();
+			
+				IDocumentProvider prov = 
fEditor.getDocumentProvider();
+				IDocument doc = prov.getDocument
(fEditor.getEditorInput());
+				sel = getSelection(doc, selStart);
+			}
+			return sel;
+		} catch(Exception x) {
+			return null;
+		}
+	}
 	
 	/**
 	 * @see IAction#actionPerformed
 	 */
 	public void run() {
-		
-		IWorkingCopyManager fManager = CUIPlugin.getDefault
().getWorkingCopyManager();
-		ITranslationUnit unit = fManager.getWorkingCopy
(fEditor.getEditorInput());
-		 
-		if (fEditor.getSelectionProvider() != null) {
-			ITextSelection selection= (ITextSelection) 
fEditor.getSelectionProvider().getSelection();
-			try {
-				ArrayList elementsFound = new ArrayList();
-				String sel = selection.getText();
-				if (sel.equals(""))
-				{
-					int selStart =  selection.getOffset
();
-					
-					IDocumentProvider prov = 
fEditor.getDocumentProvider();
-					IDocument doc = prov.getDocument
(fEditor.getEditorInput());
-					sel = getSelection(doc, selStart);
-				}
-				
-				IFile file = fEditor.getInputFile();
-				if(file == null)
-					return;
-				IProject project = file.getProject();
-				if(project == null)
-					return;
-				
+		final String selectedText = getSelectedStringFromEditor();
+
+		if(selectedText == null) {
+			return;
+		}
+
+		final ArrayList elementsFound = new ArrayList();
+
+		IRunnableWithProgress runnable = new IRunnableWithProgress() 
{
+			public void run(IProgressMonitor monitor) {
+				BasicSearchResultCollector  resultCollector 
=  new BasicSearchResultCollector(monitor);
+				IWorkingCopyManager fManager = 
CUIPlugin.getDefault().getWorkingCopyManager();
+				ITranslationUnit unit = 
fManager.getWorkingCopy(fEditor.getEditorInput());
+
 				ICElement[] projectScopeElement = new 
ICElement[1];
 				projectScopeElement[0] = unit.getCProject
();//(ICElement)currentScope.getCProject();
 				ICSearchScope scope = 
SearchEngine.createCSearchScope(projectScopeElement, true);
 				OrPattern orPattern = new OrPattern();
 				// search for global variables, functions, 
classes, structs, unions, enums and macros
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.VAR, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.FUNCTION, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.METHOD, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.TYPE, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.ENUM, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.FIELD, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.NAMESPACE, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.MACRO, 
ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern
(SearchEngine.createSearchPattern( sel, ICSearchConstants.TYPEDEF, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.VAR, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.FUNCTION, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.METHOD, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.TYPE, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.ENUM, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.FIELD, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, 
ICSearchConstants.NAMESPACE, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.MACRO, 
ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern
(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.TYPEDEF, 
ICSearchConstants.DECLARATIONS, true ));
 				searchEngine.search(CUIPlugin.getWorkspace
(), orPattern, scope, resultCollector, true);
-				elementsFound.addAll
(resultCollector.getSearchResults());
-				
-				if (elementsFound.isEmpty() == false) {
-					IMatch selected= selectCElement
(elementsFound, getShell(), fDialogTitle, fDialogMessage);
-					if (selected != null) {
-						open(selected);
-						return;
-					}
-				}
-			} catch	 (Exception x) {
-				CUIPlugin.getDefault().log(x);
+				elementsFound.addAll
(resultCollector.getSearchResults());	
 			}
-		}
+		};
 
-		getShell().getDisplay().beep();		
+		try {
+			ProgressMonitorDialog progressMonitor = new 
ProgressMonitorDialog(getShell());
+			progressMonitor.run(true, true, runnable);
+
+			if (elementsFound.isEmpty() == true) {
+				return;
+			}
+
+			IMatch selected= selectCElement(elementsFound, 
getShell(), fDialogTitle, fDialogMessage);
+			if (selected != null) {
+				open(selected);
+				return;
+			}
+		} catch(Exception x) {
+			CUIPlugin.getDefault().log(x);
+		}
 	}
 
 	protected Shell getShell() {
@@ -247,5 +267,13 @@
 	
 		return selectedWord;		
 	}
+		
+	/* (non-Javadoc)
+	 * @see org.eclipse.ui.texteditor.IUpdate#update()
+	 */
+	public void update() {
+		setEnabled(getSelectedStringFromEditor() != null);
+	}
+
 }
 

--- PATCH END ---
Index: search/org/eclipse/cdt/core/search/BasicSearchResultCollector.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/BasicSearchResultCollector.java,v
retrieving revision 1.8
diff -u -r1.8 BasicSearchResultCollector.java
--- search/org/eclipse/cdt/core/search/BasicSearchResultCollector.java	23 Sep 2003 13:54:21 -0000	1.8
+++ search/org/eclipse/cdt/core/search/BasicSearchResultCollector.java	3 Feb 2004 15:01:00 -0000
@@ -53,6 +53,14 @@
  * Window>Preferences>Java>Code Generation>Code and Comments
  */
 public class BasicSearchResultCollector implements ICSearchResultCollector {
+	IProgressMonitor fProgressMonitor = null;
+
+	public BasicSearchResultCollector() {
+	}
+	
+	public BasicSearchResultCollector(IProgressMonitor monitor) {
+		fProgressMonitor = monitor;
+	}
 
 	public void aboutToStart() {
 		results = new HashSet();
@@ -62,7 +70,7 @@
 	}
 	
 	public IProgressMonitor getProgressMonitor() {
-		return null;
+		return fProgressMonitor;
 	}
 
 	public IMatch createMatch(Object fileResource, int start, int end, ISourceElementCallbackDelegate node ) throws CoreException 
Index: src/org/eclipse/cdt/internal/ui/editor/OpenDeclarationsAction.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/OpenDeclarationsAction.java,v
retrieving revision 1.2.2.2
diff -u -r1.2.2.2 OpenDeclarationsAction.java
--- src/org/eclipse/cdt/internal/ui/editor/OpenDeclarationsAction.java	11 Nov 2003 18:28:20 -0000	1.2.2.2
+++ src/org/eclipse/cdt/internal/ui/editor/OpenDeclarationsAction.java	3 Feb 2004 14:48:50 -0000
@@ -23,9 +23,10 @@
 import org.eclipse.cdt.ui.CSearchResultLabelProvider;
 import org.eclipse.cdt.ui.CUIPlugin;
 import org.eclipse.cdt.ui.IWorkingCopyManager;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jface.action.Action;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
@@ -35,6 +36,7 @@
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.IUpdate;
 
 /**
  * This action opens a java CEditor on the element represented by text selection of
@@ -42,12 +44,11 @@
  * 
  * Use action from package org.eclipse.jdt.ui.actions
  */
-public class OpenDeclarationsAction extends Action {
+public class OpenDeclarationsAction extends Action implements IUpdate {
 		
 	private String fDialogTitle;
 	private String fDialogMessage;
 	protected CEditor fEditor;
-	BasicSearchResultCollector  resultCollector = null;
 	SearchEngine searchEngine = null;
 	
 	/**
@@ -63,7 +64,6 @@
 		setDialogMessage(CEditorMessages.getString("OpenDeclarations.dialog.message")); //$NON-NLS-1$
 
 		searchEngine = new SearchEngine();
-		resultCollector = new BasicSearchResultCollector();
 	}
 	
 	/**
@@ -93,66 +93,86 @@
 	public void setContentEditor(CEditor editor) {	
 		fEditor= editor;
 	}
+
+	/**
+	 * Return the selected string from the editor
+	 * @return The string currently selected, or null if there is no valid selection
+	 */
+	protected String getSelectedStringFromEditor() {
+		if (fEditor.getSelectionProvider() == null) {
+			return null;
+		}
+
+		try {
+			ITextSelection selection= (ITextSelection) fEditor.getSelectionProvider().getSelection();
+			String sel = selection.getText();
+			if (sel.equals(""))
+			{
+				int selStart =  selection.getOffset();
+			
+				IDocumentProvider prov = fEditor.getDocumentProvider();
+				IDocument doc = prov.getDocument(fEditor.getEditorInput());
+				sel = getSelection(doc, selStart);
+			}
+			return sel;
+		} catch(Exception x) {
+			return null;
+		}
+	}
 	
 	/**
 	 * @see IAction#actionPerformed
 	 */
 	public void run() {
-		
-		IWorkingCopyManager fManager = CUIPlugin.getDefault().getWorkingCopyManager();
-		ITranslationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
-		 
-		if (fEditor.getSelectionProvider() != null) {
-			ITextSelection selection= (ITextSelection) fEditor.getSelectionProvider().getSelection();
-			try {
-				ArrayList elementsFound = new ArrayList();
-				String sel = selection.getText();
-				if (sel.equals(""))
-				{
-					int selStart =  selection.getOffset();
-					
-					IDocumentProvider prov = fEditor.getDocumentProvider();
-					IDocument doc = prov.getDocument(fEditor.getEditorInput());
-					sel = getSelection(doc, selStart);
-				}
-				
-				IFile file = fEditor.getInputFile();
-				if(file == null)
-					return;
-				IProject project = file.getProject();
-				if(project == null)
-					return;
-				
+		final String selectedText = getSelectedStringFromEditor();
+
+		if(selectedText == null) {
+			return;
+		}
+
+		final ArrayList elementsFound = new ArrayList();
+
+		IRunnableWithProgress runnable = new IRunnableWithProgress() {
+			public void run(IProgressMonitor monitor) {
+				BasicSearchResultCollector  resultCollector =  new BasicSearchResultCollector(monitor);
+				IWorkingCopyManager fManager = CUIPlugin.getDefault().getWorkingCopyManager();
+				ITranslationUnit unit = fManager.getWorkingCopy(fEditor.getEditorInput());
+
 				ICElement[] projectScopeElement = new ICElement[1];
 				projectScopeElement[0] = unit.getCProject();//(ICElement)currentScope.getCProject();
 				ICSearchScope scope = SearchEngine.createCSearchScope(projectScopeElement, true);
 				OrPattern orPattern = new OrPattern();
 				// search for global variables, functions, classes, structs, unions, enums and macros
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.VAR, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.FUNCTION, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.METHOD, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.TYPE, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.ENUM, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.FIELD, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.NAMESPACE, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.MACRO, ICSearchConstants.DECLARATIONS, true ));
-				orPattern.addPattern(SearchEngine.createSearchPattern( sel, ICSearchConstants.TYPEDEF, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.VAR, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.FUNCTION, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.METHOD, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.TYPE, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.ENUM, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.FIELD, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.NAMESPACE, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.MACRO, ICSearchConstants.DECLARATIONS, true ));
+				orPattern.addPattern(SearchEngine.createSearchPattern( selectedText, ICSearchConstants.TYPEDEF, ICSearchConstants.DECLARATIONS, true ));
 				searchEngine.search(CUIPlugin.getWorkspace(), orPattern, scope, resultCollector, true);
-				elementsFound.addAll(resultCollector.getSearchResults());
-				
-				if (elementsFound.isEmpty() == false) {
-					IMatch selected= selectCElement(elementsFound, getShell(), fDialogTitle, fDialogMessage);
-					if (selected != null) {
-						open(selected);
-						return;
-					}
-				}
-			} catch	 (Exception x) {
-				CUIPlugin.getDefault().log(x);
+				elementsFound.addAll(resultCollector.getSearchResults());	
 			}
-		}
+		};
 
-		getShell().getDisplay().beep();		
+		try {
+			ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
+			progressMonitor.run(true, true, runnable);
+
+			if (elementsFound.isEmpty() == true) {
+				return;
+			}
+
+			IMatch selected= selectCElement(elementsFound, getShell(), fDialogTitle, fDialogMessage);
+			if (selected != null) {
+				open(selected);
+				return;
+			}
+		} catch(Exception x) {
+			CUIPlugin.getDefault().log(x);
+		}
 	}
 
 	protected Shell getShell() {
@@ -247,5 +267,13 @@
 	
 		return selectedWord;		
 	}
+		
+	/* (non-Javadoc)
+	 * @see org.eclipse.ui.texteditor.IUpdate#update()
+	 */
+	public void update() {
+		setEnabled(getSelectedStringFromEditor() != null);
+	}
+
 }
 

Back to the top