1 11 package org.eclipse.debug.internal.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.debug.internal.ui.DebugUIPlugin; 21 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 22 import org.eclipse.debug.internal.ui.viewers.AsynchronousTreeNavigationDialog; 23 import org.eclipse.debug.internal.ui.viewers.AsynchronousTreeViewer; 24 import org.eclipse.debug.internal.ui.viewers.ILabelResult; 25 import org.eclipse.jface.action.Action; 26 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 27 import org.eclipse.jface.operation.IRunnableWithProgress; 28 import org.eclipse.jface.viewers.LabelProvider; 29 import org.eclipse.jface.viewers.TreePath; 30 import org.eclipse.jface.viewers.TreeSelection; 31 import org.eclipse.jface.window.Window; 32 import org.eclipse.swt.graphics.Image; 33 import org.eclipse.ui.IViewPart; 34 import org.eclipse.ui.PlatformUI; 35 import org.eclipse.ui.texteditor.IUpdate; 36 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 37 38 41 public class FindElementAction extends Action implements IUpdate { 42 43 private AsynchronousTreeViewer fViewer; 44 private IViewPart fView; 45 private List fLabelResults; 46 private Map fElementToResult = new HashMap (); 47 48 class FindLabelProvider extends LabelProvider { 49 50 public FindLabelProvider() { 51 } 52 53 public Image getImage(Object element) { 54 ILabelResult result = (ILabelResult) fElementToResult.get(element); 55 if (result != null) { 56 Image[] images = result.getImages(); 57 if (images != null && images.length > 0) { 58 return images[0]; 59 } 60 } 61 return null; 62 } 63 64 public String getText(Object element) { 65 ILabelResult result = (ILabelResult) fElementToResult.get(element); 66 if (result != null) { 67 String [] labels = result.getLabels(); 68 if (labels != null && labels.length > 0) { 69 return labels[0]; 70 } 71 } 72 return ""; } 74 75 } 76 77 public FindElementAction(IViewPart view, AsynchronousTreeViewer viewer) { 78 setText(ActionMessages.FindAction_0); 79 setId(DebugUIPlugin.getUniqueIdentifier() + ".FindElementAction"); PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.FIND_ELEMENT_ACTION); 81 setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_REPLACE); 82 fViewer = viewer; 83 fView = view; 84 } 85 86 public void run() { 87 final Object element = fViewer.getControl().getData(); 88 IRunnableWithProgress runnable = new IRunnableWithProgress() { 89 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 90 fLabelResults = fViewer.buildLabels(monitor, element, DebugUIPlugin.removeAccelerators(ActionMessages.FindAction_0)); 91 if (monitor.isCanceled()) { 92 throw new InterruptedException (); 93 } 94 } 95 }; 96 ProgressMonitorDialog dialog = new ProgressMonitorDialog(fView.getSite().getShell()); 97 dialog.setCancelable(true); 98 try { 99 dialog.run(true, true, runnable); 100 } catch (InvocationTargetException e) { 101 DebugUIPlugin.log(e); 102 return; 103 } catch (InterruptedException e) { 104 return; 105 } 106 Iterator iter = fLabelResults.iterator(); 107 while (iter.hasNext()) { 108 ILabelResult result = (ILabelResult) iter.next(); 109 fElementToResult.put(result.getElement(), result); 110 } 111 performFind(); 112 fElementToResult.clear(); 113 fLabelResults.clear(); 114 } 115 116 protected void performFind() { 117 AsynchronousTreeNavigationDialog dialog = new AsynchronousTreeNavigationDialog(fView.getSite().getShell(), new FindLabelProvider(), fElementToResult.keySet().toArray()); 118 dialog.setTitle(ActionMessages.FindDialog_3); 119 dialog.setMessage(ActionMessages.FindDialog_1); 120 if (dialog.open() == Window.OK) { 121 Object [] elements = dialog.getResult(); 122 if (elements.length == 1) { 123 ILabelResult result = (ILabelResult) fElementToResult.get(elements[0]); 124 TreePath treePath = result.getTreePath(); 125 if (treePath != null) { 126 fViewer.setSelection(new TreeSelection(treePath), true, true); 127 } 128 } 129 } 130 } 131 132 public void update() { 133 setEnabled(fViewer.getInput() != null); 134 } 135 136 } 137 | Popular Tags |