KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > FindElementAction


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 /**
39  * Action which prompts the user to find/navigate to an element in an async tree.
40  */

41 public class FindElementAction extends Action implements IUpdate {
42     
43     private AsynchronousTreeViewer fViewer;
44     private IViewPart fView;
45     private List JavaDoc fLabelResults;
46     private Map JavaDoc fElementToResult = new HashMap JavaDoc();
47     
48     class FindLabelProvider extends LabelProvider {
49         
50         public FindLabelProvider() {
51         }
52
53         public Image getImage(Object JavaDoc 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 JavaDoc getText(Object JavaDoc element) {
65             ILabelResult result = (ILabelResult) fElementToResult.get(element);
66             if (result != null) {
67                 String JavaDoc[] labels = result.getLabels();
68                 if (labels != null && labels.length > 0) {
69                     return labels[0];
70                 }
71             }
72             return ""; //$NON-NLS-1$
73
}
74         
75     }
76
77     public FindElementAction(IViewPart view, AsynchronousTreeViewer viewer) {
78         setText(ActionMessages.FindAction_0);
79         setId(DebugUIPlugin.getUniqueIdentifier() + ".FindElementAction"); //$NON-NLS-1$
80
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 JavaDoc element = fViewer.getControl().getData();
88         IRunnableWithProgress runnable = new IRunnableWithProgress() {
89             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
90                 fLabelResults = fViewer.buildLabels(monitor, element, DebugUIPlugin.removeAccelerators(ActionMessages.FindAction_0));
91                 if (monitor.isCanceled()) {
92                     throw new InterruptedException JavaDoc();
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 JavaDoc e) {
101             DebugUIPlugin.log(e);
102             return;
103         } catch (InterruptedException JavaDoc e) {
104             return;
105         }
106         Iterator JavaDoc 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 JavaDoc[] 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