KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > text2 > RetrieverAction


1 /*******************************************************************************
2  * Copyright (c) 2006 Wind River Systems 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  * Markus Schorn - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.search2.internal.ui.text2;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.OperationCanceledException;
17
18 import org.eclipse.swt.custom.CCombo;
19 import org.eclipse.swt.custom.StyledText;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Combo;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.List;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.Table;
26 import org.eclipse.swt.widgets.TableItem;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.swt.widgets.Tree;
29 import org.eclipse.swt.widgets.TreeItem;
30
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.action.IStatusLineManager;
33 import org.eclipse.jface.dialogs.ErrorDialog;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.ISelectionProvider;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37
38 import org.eclipse.jface.text.BadLocationException;
39 import org.eclipse.jface.text.IDocument;
40 import org.eclipse.jface.text.ITextSelection;
41 import org.eclipse.jface.text.TextSelection;
42
43 import org.eclipse.ui.IEditorActionBarContributor;
44 import org.eclipse.ui.IEditorPart;
45 import org.eclipse.ui.IWorkbenchPage;
46 import org.eclipse.ui.IWorkbenchPart;
47 import org.eclipse.ui.forms.editor.FormEditor;
48 import org.eclipse.ui.forms.widgets.FormText;
49 import org.eclipse.ui.model.IWorkbenchAdapter;
50 import org.eclipse.ui.part.EditorActionBarContributor;
51 import org.eclipse.ui.texteditor.ITextEditor;
52
53 import org.eclipse.search.ui.ISearchQuery;
54 import org.eclipse.search.ui.NewSearchUI;
55 import org.eclipse.search.ui.text.TextSearchQueryProvider;
56
57 import org.eclipse.search.internal.ui.SearchPlugin;
58
59 import org.eclipse.search2.internal.ui.SearchMessages;
60
61 abstract public class RetrieverAction extends Action {
62     public RetrieverAction() {
63     }
64
65     public void run() {
66         IWorkbenchPage page= getWorkbenchPage();
67         if (page == null) {
68             return;
69         }
70         TextSearchQueryProvider provider= TextSearchQueryProvider.getPreferred();
71         String JavaDoc searchForString= getSearchForString(page);
72         if (searchForString.length() == 0) {
73             setStatusBarMessage(SearchMessages.RetrieverAction_empty_selection);
74             return;
75         }
76         try {
77             ISearchQuery query= createQuery(provider, searchForString);
78             if (query != null) {
79                 NewSearchUI.runQueryInBackground(query);
80             }
81         } catch (OperationCanceledException ex) {
82             // action cancelled
83
} catch (CoreException e) {
84             ErrorDialog.openError(getShell(), SearchMessages.RetrieverAction_error_title, SearchMessages.RetrieverAction_error_message, e.getStatus());
85         }
86     }
87     
88     private void setStatusBarMessage(String JavaDoc message) {
89         IWorkbenchPart part= getActivePart();
90         if (part instanceof IEditorPart) {
91             IEditorActionBarContributor contributor= ((IEditorPart) part).getEditorSite().getActionBarContributor();
92             if (contributor instanceof EditorActionBarContributor) {
93                 IStatusLineManager manager= ((EditorActionBarContributor) contributor).getActionBars().getStatusLineManager();
94                 manager.setMessage(message);
95             }
96         }
97     }
98
99     private IWorkbenchPart getActivePart() {
100         IWorkbenchPage page= getWorkbenchPage();
101         if (page != null) {
102             return page.getActivePart();
103         }
104         return null;
105     }
106     
107     private Shell getShell() {
108         IWorkbenchPart part= getActivePart();
109         if (part != null) {
110             return part.getSite().getShell();
111         }
112         return SearchPlugin.getActiveWorkbenchShell();
113     }
114
115     abstract protected IWorkbenchPage getWorkbenchPage();
116     abstract protected ISearchQuery createQuery(TextSearchQueryProvider provider, String JavaDoc searchForString) throws CoreException;
117
118     final protected String JavaDoc extractSearchTextFromEditor(IEditorPart editor) {
119         if (editor != null) {
120             ITextSelection selection= null;
121             ISelectionProvider provider= editor.getEditorSite().getSelectionProvider();
122             if (provider != null) {
123                 ISelection s= provider.getSelection();
124                 if (s instanceof ITextSelection) {
125                     selection= (ITextSelection) s;
126                 }
127             }
128
129             if (selection != null) {
130                 if (selection.getLength() == 0) {
131                     ITextEditor txtEditor= getTextEditor(editor);
132                     if (txtEditor != null) {
133                         IDocument document= txtEditor.getDocumentProvider().getDocument(txtEditor.getEditorInput());
134                         selection= expandSelection(selection, document, null);
135                     }
136                 }
137
138                 if (selection.getLength() > 0 && selection.getText() != null) {
139                     return trimSearchString(selection.getText());
140                 }
141             }
142         }
143         return null;
144     }
145
146     final protected String JavaDoc extractSearchTextFromSelection(ISelection sel) {
147         if (sel instanceof ITextSelection) {
148             String JavaDoc text= ((ITextSelection) sel).getText();
149             if (text != null) {
150                 return trimSearchString(text);
151             }
152         } else if (sel instanceof IStructuredSelection) {
153             Object JavaDoc firstElement= ((IStructuredSelection) sel).getFirstElement();
154             if (firstElement instanceof IAdaptable) {
155                 IWorkbenchAdapter wbAdapter= (IWorkbenchAdapter) ((IAdaptable) firstElement).getAdapter(IWorkbenchAdapter.class);
156                 if (wbAdapter != null) {
157                     return wbAdapter.getLabel(firstElement);
158                 }
159             }
160         }
161         return null;
162     }
163
164     final protected String JavaDoc extractSearchTextFromWidget(Control control) {
165         String JavaDoc sel= null;
166         if (control instanceof Combo) {
167             Combo combo= (Combo) control;
168             sel= combo.getText();
169             Point selection= combo.getSelection();
170             sel= sel.substring(selection.x, selection.y);
171         }
172         if (control instanceof CCombo) {
173             CCombo combo= (CCombo) control;
174             sel= combo.getText();
175             Point selection= combo.getSelection();
176             sel= sel.substring(selection.x, selection.y);
177         }
178         else if (control instanceof Text) {
179             Text text= (Text) control;
180             sel= text.getSelectionText();
181         }
182         else if (control instanceof FormText) {
183             FormText text= (FormText) control;
184             sel= text.getSelectionText();
185         }
186         else if (control instanceof StyledText) {
187             StyledText text= (StyledText) control;
188             sel= text.getSelectionText();
189         }
190         else if (control instanceof Tree) {
191             Tree tree= (Tree) control;
192             TreeItem[] s= tree.getSelection();
193             if (s.length > 0) {
194                 sel= s[0].getText();
195             }
196         }
197         else if (control instanceof Table) {
198             Table tree= (Table) control;
199             TableItem[] s= tree.getSelection();
200             if (s.length > 0) {
201                 sel= s[0].getText();
202             }
203         }
204         else if (control instanceof List) {
205             List list= (List) control;
206             String JavaDoc[] s= list.getSelection();
207             if (s.length > 0) {
208                 sel= s[0];
209             }
210         }
211         
212         if (sel != null) {
213             sel= trimSearchString(sel);
214         }
215         return sel;
216     }
217
218     private String JavaDoc trimSearchString(String JavaDoc text) {
219         text= text.trim();
220         int idx= text.indexOf('\n');
221         int idx2= text.indexOf('\r');
222         if (idx2 >= 0 && idx2 < idx) {
223             idx= idx2;
224         }
225         if (idx >= 0) {
226             text= text.substring(0, idx);
227         }
228         return text;
229     }
230
231     private ITextEditor getTextEditor(IEditorPart editor) {
232         if (editor instanceof ITextEditor) {
233             return (ITextEditor) editor;
234         } else
235             if (editor instanceof FormEditor) {
236                 FormEditor me= (FormEditor) editor;
237                 editor= me.getActiveEditor();
238                 if (editor instanceof ITextEditor) {
239                     return (ITextEditor) editor;
240                 }
241             }
242         return null;
243     }
244
245     private ITextSelection expandSelection(ITextSelection sel, IDocument document, String JavaDoc stopChars) {
246         int offset= sel.getOffset();
247         int length= sel.getLength();
248
249         // in case the length is zero we have to decide whether to go
250
// left or right.
251
if (length == 0) {
252             // try right
253
char chr= 0;
254             char chl= 0;
255             try {
256                 chr= document.getChar(offset);
257             } catch (BadLocationException e2) {
258             }
259             try {
260                 chl= document.getChar(offset - 1);
261             } catch (BadLocationException e2) {
262             }
263
264             if (isPartOfIdentifier(chr)) {
265                 length= 1;
266             } else
267                 if (isPartOfIdentifier(chl)) {
268                     offset--;
269                     length= 1;
270                 } else
271                     if (stopChars != null && stopChars.indexOf(chr) == -1) {
272                         length= 1;
273                     } else
274                         if (stopChars != null && stopChars.indexOf(chl) == -1) {
275                             offset--;
276                             length= 1;
277                         } else {
278                             return sel;
279                         }
280         }
281
282         int a= offset + length - 1;
283         int z= a;
284
285         // move z one behind last character.
286
try {
287             char ch= document.getChar(z);
288             while (isValidChar(stopChars, ch)) {
289                 ch= document.getChar(++z);
290             }
291         } catch (BadLocationException e2) {
292         }
293         // move a one before the first character
294
try {
295             char ch= document.getChar(a);
296             while (isValidChar(stopChars, ch)) {
297                 ch= document.getChar(--a);
298             }
299         } catch (BadLocationException e2) {
300         }
301
302         if (a == z) {
303             offset= a;
304             length= 0;
305         } else {
306             offset= a + 1;
307             length= z - a - 1;
308         }
309         return new TextSelection(document, offset, length);
310     }
311
312     private boolean isValidChar(String JavaDoc stopChars, char ch) {
313         return stopChars == null ? isPartOfIdentifier(ch) : stopChars.indexOf(ch) == -1;
314     }
315
316     private boolean isPartOfIdentifier(char ch) {
317         return Character.isLetterOrDigit(ch) || ch == '_';
318     }
319
320     protected String JavaDoc getSearchForString(IWorkbenchPage page) {
321         String JavaDoc searchFor= extractSearchTextFromSelection(page.getSelection());
322         if (searchFor == null || searchFor.length() == 0) {
323             IWorkbenchPart activePart= page.getActivePart();
324             if (activePart instanceof IEditorPart) {
325                 searchFor= extractSearchTextFromEditor((IEditorPart) activePart);
326             }
327             if (searchFor == null) {
328                 Control focus= page.getWorkbenchWindow().getShell().getDisplay().getFocusControl();
329                 if (focus != null)
330                     searchFor= extractSearchTextFromWidget(focus);
331             }
332         }
333         return searchFor == null ? "" : searchFor; //$NON-NLS-1$
334
}
335     
336 }
337
Popular Tags