KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > model > VirtualFindAction


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.viewers.model;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
20 import org.eclipse.debug.internal.ui.actions.ActionMessages;
21 import org.eclipse.debug.internal.ui.viewers.FindElementDialog;
22 import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.jface.viewers.LabelProvider;
27 import org.eclipse.jface.viewers.TreePath;
28 import org.eclipse.jface.viewers.TreeSelection;
29 import org.eclipse.jface.window.Window;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.widgets.Tree;
32 import org.eclipse.swt.widgets.TreeItem;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.texteditor.IUpdate;
35 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
36
37 /**
38  * Action which prompts the user to find/navigate to an element in a virtual tree.
39  *
40  * @since 3.3
41  */

42 public class VirtualFindAction extends Action implements IUpdate {
43     
44     private InternalTreeModelViewer fViewer;
45     private boolean fDone = false;
46     
47     class FindLabelProvider extends LabelProvider {
48         
49         public FindLabelProvider() {
50         }
51
52         public Image getImage(Object JavaDoc element) {
53             return ((TreeItem)element).getImage();
54         }
55
56         public String JavaDoc getText(Object JavaDoc element) {
57             return ((TreeItem)element).getText();
58         }
59         
60     }
61
62     public VirtualFindAction(InternalTreeModelViewer viewer) {
63         setText(ActionMessages.FindAction_0);
64         setId(DebugUIPlugin.getUniqueIdentifier() + ".FindElementAction"); //$NON-NLS-1$
65
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.FIND_ELEMENT_ACTION);
66         setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_REPLACE);
67         fViewer = viewer;
68     }
69
70     public void run() {
71         fDone = false;
72         final Object JavaDoc lock = new Object JavaDoc();
73         ProgressMonitorDialog dialog = new ProgressMonitorDialog(fViewer.getControl().getShell());
74         final IProgressMonitor monitor = dialog.getProgressMonitor();
75         dialog.setCancelable(true);
76         
77         boolean queued = false;
78         ILabelUpdateListener listener = new ILabelUpdateListener() {
79             public void labelUpdatesComplete() {
80                 synchronized (lock) {
81                     fDone = true;
82                     lock.notifyAll();
83                 }
84             }
85             public void labelUpdatesBegin() {
86             }
87             public void labelUpdateStarted(ILabelUpdate update) {
88             }
89             public void labelUpdateComplete(ILabelUpdate update) {
90                 monitor.worked(1);
91             }
92         };
93         fViewer.addLabelUpdateListener(listener);
94         queued = fViewer.populateVitrualItems();
95         
96         if (queued) {
97             IRunnableWithProgress runnable = new IRunnableWithProgress() {
98                 public void run(final IProgressMonitor m) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
99                     m.beginTask(DebugUIPlugin.removeAccelerators(getText()), IProgressMonitor.UNKNOWN);
100                     synchronized (lock) {
101                         if (!fDone) {
102                             lock.wait();
103                         }
104                     }
105                     m.done();
106                 }
107             };
108             try {
109                 dialog.run(true, true, runnable);
110             } catch (InvocationTargetException JavaDoc e) {
111                 DebugUIPlugin.log(e);
112                 return;
113             } catch (InterruptedException JavaDoc e) {
114                 return;
115             }
116         }
117         
118         fViewer.removeLabelUpdateListener(listener);
119         if (!monitor.isCanceled()) {
120             Tree tree = (Tree) fViewer.getControl();
121             List JavaDoc items = new ArrayList JavaDoc();
122             collectItems(items, tree.getItems());
123             performFind(items);
124         }
125     }
126     
127     private void collectItems(List JavaDoc list, TreeItem[] items) {
128         for (int i = 0; i < items.length; i++) {
129             TreeItem treeItem = items[i];
130             list.add(treeItem);
131             if (treeItem.getExpanded()) {
132                 collectItems(list, treeItem.getItems());
133             }
134         }
135     }
136
137     protected void performFind(List JavaDoc items) {
138         FindElementDialog dialog = new FindElementDialog(fViewer.getControl().getShell(), new FindLabelProvider(), items.toArray());
139         dialog.setTitle(ActionMessages.FindDialog_3);
140         dialog.setMessage(ActionMessages.FindDialog_1);
141         if (dialog.open() == Window.OK) {
142             Object JavaDoc[] elements = dialog.getResult();
143             if (elements.length == 1) {
144                 TreeItem item = (TreeItem) elements[0];
145                 List JavaDoc path = new ArrayList JavaDoc();
146                 while (item != null) {
147                     path.add(0, item.getData());
148                     item = item.getParentItem();
149                 }
150                 fViewer.setSelection(new TreeSelection(new TreePath(path.toArray())));
151             }
152         }
153     }
154     
155     public void update() {
156         setEnabled(fViewer.getInput() != null);
157     }
158     
159 }
160
Popular Tags