KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > JavaSearchPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IAdaptable;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.ModifyEvent;
22 import org.eclipse.swt.events.ModifyListener;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Group;
32 import org.eclipse.swt.widgets.Label;
33
34 import org.eclipse.jface.dialogs.Dialog;
35 import org.eclipse.jface.dialogs.DialogPage;
36 import org.eclipse.jface.dialogs.IDialogSettings;
37 import org.eclipse.jface.viewers.ISelection;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39
40 import org.eclipse.jface.text.ITextSelection;
41
42 import org.eclipse.ui.IEditorPart;
43 import org.eclipse.ui.IWorkbenchPage;
44 import org.eclipse.ui.IWorkingSet;
45 import org.eclipse.ui.IWorkingSetManager;
46 import org.eclipse.ui.PlatformUI;
47 import org.eclipse.ui.model.IWorkbenchAdapter;
48
49 import org.eclipse.search.ui.ISearchPage;
50 import org.eclipse.search.ui.ISearchPageContainer;
51 import org.eclipse.search.ui.NewSearchUI;
52
53 import org.eclipse.jdt.core.IClassFile;
54 import org.eclipse.jdt.core.ICompilationUnit;
55 import org.eclipse.jdt.core.IField;
56 import org.eclipse.jdt.core.IImportDeclaration;
57 import org.eclipse.jdt.core.IJavaElement;
58 import org.eclipse.jdt.core.IMethod;
59 import org.eclipse.jdt.core.IType;
60 import org.eclipse.jdt.core.JavaCore;
61 import org.eclipse.jdt.core.JavaModelException;
62 import org.eclipse.jdt.core.Signature;
63 import org.eclipse.jdt.core.formatter.IndentManipulation;
64 import org.eclipse.jdt.core.search.IJavaSearchConstants;
65 import org.eclipse.jdt.core.search.IJavaSearchScope;
66 import org.eclipse.jdt.core.search.SearchPattern;
67
68 import org.eclipse.jdt.ui.search.ElementQuerySpecification;
69 import org.eclipse.jdt.ui.search.PatternQuerySpecification;
70 import org.eclipse.jdt.ui.search.QuerySpecification;
71
72 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
73 import org.eclipse.jdt.internal.ui.JavaPlugin;
74 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
75 import org.eclipse.jdt.internal.ui.browsing.LogicalPackage;
76 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
77 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
78 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
79
80 public class JavaSearchPage extends DialogPage implements ISearchPage {
81     
82     private static class SearchPatternData {
83         private int searchFor;
84         private int limitTo;
85         private String JavaDoc pattern;
86         private boolean isCaseSensitive;
87         private IJavaElement javaElement;
88         private int includeMask;
89         private int scope;
90         private IWorkingSet[] workingSets;
91         
92         public SearchPatternData(int searchFor, int limitTo, boolean isCaseSensitive, String JavaDoc pattern, IJavaElement element, int includeMask) {
93             this(searchFor, limitTo, pattern, isCaseSensitive, element, ISearchPageContainer.WORKSPACE_SCOPE, null, includeMask);
94         }
95         
96         public SearchPatternData(int searchFor, int limitTo, String JavaDoc pattern, boolean isCaseSensitive, IJavaElement element, int scope, IWorkingSet[] workingSets, int includeMask) {
97             this.searchFor= searchFor;
98             this.limitTo= limitTo;
99             this.pattern= pattern;
100             this.isCaseSensitive= isCaseSensitive;
101             this.scope= scope;
102             this.workingSets= workingSets;
103             this.includeMask= includeMask;
104             
105             setJavaElement(element);
106         }
107         
108         public void setJavaElement(IJavaElement javaElement) {
109             this.javaElement= javaElement;
110         }
111
112         public boolean isCaseSensitive() {
113             return isCaseSensitive;
114         }
115
116         public IJavaElement getJavaElement() {
117             return javaElement;
118         }
119
120         public int getLimitTo() {
121             return limitTo;
122         }
123
124         public String JavaDoc getPattern() {
125             return pattern;
126         }
127
128         public int getScope() {
129             return scope;
130         }
131
132         public int getSearchFor() {
133             return searchFor;
134         }
135
136         public IWorkingSet[] getWorkingSets() {
137             return workingSets;
138         }
139         
140         public int getIncludeMask() {
141             return includeMask;
142         }
143         
144         public void store(IDialogSettings settings) {
145             settings.put("searchFor", searchFor); //$NON-NLS-1$
146
settings.put("scope", scope); //$NON-NLS-1$
147
settings.put("pattern", pattern); //$NON-NLS-1$
148
settings.put("limitTo", limitTo); //$NON-NLS-1$
149
settings.put("javaElement", javaElement != null ? javaElement.getHandleIdentifier() : ""); //$NON-NLS-1$ //$NON-NLS-2$
150
settings.put("isCaseSensitive", isCaseSensitive); //$NON-NLS-1$
151
if (workingSets != null) {
152                 String JavaDoc[] wsIds= new String JavaDoc[workingSets.length];
153                 for (int i= 0; i < workingSets.length; i++) {
154                     wsIds[i]= workingSets[i].getName();
155                 }
156                 settings.put("workingSets", wsIds); //$NON-NLS-1$
157
} else {
158                 settings.put("workingSets", new String JavaDoc[0]); //$NON-NLS-1$
159
}
160             settings.put("includeMask", includeMask); //$NON-NLS-1$
161
}
162         
163         public static SearchPatternData create(IDialogSettings settings) {
164             String JavaDoc pattern= settings.get("pattern"); //$NON-NLS-1$
165
if (pattern.length() == 0) {
166                 return null;
167             }
168             IJavaElement elem= null;
169             String JavaDoc handleId= settings.get("javaElement"); //$NON-NLS-1$
170
if (handleId != null && handleId.length() > 0) {
171                 IJavaElement restored= JavaCore.create(handleId);
172                 if (restored != null && isSearchableType(restored) && restored.exists()) {
173                     elem= restored;
174                 }
175             }
176             String JavaDoc[] wsIds= settings.getArray("workingSets"); //$NON-NLS-1$
177
IWorkingSet[] workingSets= null;
178             if (wsIds != null && wsIds.length > 0) {
179                 IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager();
180                 workingSets= new IWorkingSet[wsIds.length];
181                 for (int i= 0; workingSets != null && i < wsIds.length; i++) {
182                     workingSets[i]= workingSetManager.getWorkingSet(wsIds[i]);
183                     if (workingSets[i] == null) {
184                         workingSets= null;
185                     }
186                 }
187             }
188
189             try {
190                 int searchFor= settings.getInt("searchFor"); //$NON-NLS-1$
191
int scope= settings.getInt("scope"); //$NON-NLS-1$
192
int limitTo= settings.getInt("limitTo"); //$NON-NLS-1$
193
boolean isCaseSensitive= settings.getBoolean("isCaseSensitive"); //$NON-NLS-1$
194

195                 int includeMask;
196                 if (settings.get("includeMask") != null) { //$NON-NLS-1$
197
includeMask= settings.getInt("includeMask"); //$NON-NLS-1$
198
} else {
199                     includeMask= JavaSearchScopeFactory.NO_JRE;
200                     if (settings.get("includeJRE") == null ? forceIncludeAll(limitTo, elem) : settings.getBoolean("includeJRE")) { //$NON-NLS-1$ //$NON-NLS-2$
201
includeMask= JavaSearchScopeFactory.ALL;
202                     }
203                 }
204                 return new SearchPatternData(searchFor, limitTo, pattern, isCaseSensitive, elem, scope, workingSets, includeMask);
205             } catch (NumberFormatException JavaDoc e) {
206                 return null;
207             }
208         }
209         
210     }
211     
212     // search for
213
private final static int TYPE= IJavaSearchConstants.TYPE;
214     private final static int METHOD= IJavaSearchConstants.METHOD;
215     private final static int PACKAGE= IJavaSearchConstants.PACKAGE;
216     private final static int CONSTRUCTOR= IJavaSearchConstants.CONSTRUCTOR;
217     private final static int FIELD= IJavaSearchConstants.FIELD;
218     
219     // limit to
220
private final static int DECLARATIONS= IJavaSearchConstants.DECLARATIONS;
221     private final static int IMPLEMENTORS= IJavaSearchConstants.IMPLEMENTORS;
222     private final static int REFERENCES= IJavaSearchConstants.REFERENCES;
223     private final static int ALL_OCCURRENCES= IJavaSearchConstants.ALL_OCCURRENCES;
224     private final static int READ_ACCESSES= IJavaSearchConstants.READ_ACCESSES;
225     private final static int WRITE_ACCESSES= IJavaSearchConstants.WRITE_ACCESSES;
226     
227     public static final String JavaDoc PARTICIPANT_EXTENSION_POINT= "org.eclipse.jdt.ui.queryParticipants"; //$NON-NLS-1$
228

229     public static final String JavaDoc EXTENSION_POINT_ID= "org.eclipse.jdt.ui.JavaSearchPage"; //$NON-NLS-1$
230

231     private static final int HISTORY_SIZE= 12;
232     
233     // Dialog store id constants
234
private final static String JavaDoc PAGE_NAME= "JavaSearchPage"; //$NON-NLS-1$
235
private final static String JavaDoc STORE_CASE_SENSITIVE= "CASE_SENSITIVE"; //$NON-NLS-1$
236
private final static String JavaDoc STORE_INCLUDE_MASK= "INCLUDE_MASK"; //$NON-NLS-1$
237
private final static String JavaDoc STORE_HISTORY= "HISTORY"; //$NON-NLS-1$
238
private final static String JavaDoc STORE_HISTORY_SIZE= "HISTORY_SIZE"; //$NON-NLS-1$
239

240     private final List JavaDoc fPreviousSearchPatterns;
241     
242     private SearchPatternData fInitialData;
243     private IJavaElement fJavaElement;
244     private boolean fFirstTime= true;
245     private IDialogSettings fDialogSettings;
246     private boolean fIsCaseSensitive;
247     
248     private Combo fPattern;
249     private ISearchPageContainer fContainer;
250     private Button fCaseSensitive;
251     
252     private Button[] fSearchFor;
253     private Button[] fLimitTo;
254     private Button[] fIncludeMasks;
255
256     
257     /**
258      *
259      */

260     public JavaSearchPage() {
261         fPreviousSearchPatterns= new ArrayList JavaDoc();
262     }
263     
264     
265     //---- Action Handling ------------------------------------------------
266

267     public boolean performAction() {
268         return performNewSearch();
269     }
270     
271     private boolean performNewSearch() {
272         SearchPatternData data= getPatternData();
273
274         // Setup search scope
275
IJavaSearchScope scope= null;
276         String JavaDoc scopeDescription= ""; //$NON-NLS-1$
277

278         int searchFor= data.getSearchFor();
279         int limitTo= data.getLimitTo();
280         
281         int includeMask= data.getIncludeMask();
282         JavaSearchScopeFactory factory= JavaSearchScopeFactory.getInstance();
283         
284         switch (getContainer().getSelectedScope()) {
285             case ISearchPageContainer.WORKSPACE_SCOPE:
286                 scopeDescription= factory.getWorkspaceScopeDescription(includeMask);
287                 scope= factory.createWorkspaceScope(includeMask);
288                 break;
289             case ISearchPageContainer.SELECTION_SCOPE:
290                 IJavaElement[] javaElements= factory.getJavaElements(getContainer().getSelection());
291                 scope= factory.createJavaSearchScope(javaElements, includeMask);
292                 scopeDescription= factory.getSelectionScopeDescription(javaElements, includeMask);
293                 break;
294             case ISearchPageContainer.SELECTED_PROJECTS_SCOPE: {
295                 String JavaDoc[] projectNames= getContainer().getSelectedProjectNames();
296                 scope= factory.createJavaProjectSearchScope(projectNames, includeMask);
297                 scopeDescription= factory.getProjectScopeDescription(projectNames, includeMask);
298                 break;
299             }
300             case ISearchPageContainer.WORKING_SET_SCOPE: {
301                 IWorkingSet[] workingSets= getContainer().getSelectedWorkingSets();
302                 // should not happen - just to be sure
303
if (workingSets == null || workingSets.length < 1)
304                     return false;
305                 scopeDescription= factory.getWorkingSetScopeDescription(workingSets, includeMask);
306                 scope= factory.createJavaSearchScope(workingSets, includeMask);
307                 SearchUtil.updateLRUWorkingSets(workingSets);
308             }
309         }
310         
311         QuerySpecification querySpec= null;
312         if (data.getJavaElement() != null && getPattern().equals(fInitialData.getPattern())) {
313             if (limitTo == REFERENCES)
314                 SearchUtil.warnIfBinaryConstant(data.getJavaElement(), getShell());
315             querySpec= new ElementQuerySpecification(data.getJavaElement(), limitTo, scope, scopeDescription);
316         } else {
317             querySpec= new PatternQuerySpecification(data.getPattern(), searchFor, data.isCaseSensitive(), data.getLimitTo(), scope, scopeDescription);
318             data.setJavaElement(null);
319         }
320         
321         JavaSearchQuery textSearchJob= new JavaSearchQuery(querySpec);
322         NewSearchUI.runQueryInBackground(textSearchJob);
323         return true;
324     }
325     
326     private int getLimitTo() {
327         for (int i= 0; i < fLimitTo.length; i++) {
328             Button button= fLimitTo[i];
329             if (button.getSelection()) {
330                 return getIntData(button);
331             }
332         }
333         return -1;
334     }
335
336     private int setLimitTo(int searchFor, int limitTo) {
337         if (searchFor != TYPE && limitTo == IMPLEMENTORS) {
338             limitTo= REFERENCES;
339         }
340
341         if (searchFor != FIELD && (limitTo == READ_ACCESSES || limitTo == WRITE_ACCESSES)) {
342             limitTo= REFERENCES;
343         }
344         
345         for (int i= 0; i < fLimitTo.length; i++) {
346             Button button= fLimitTo[i];
347             int val= getIntData(button);
348             button.setSelection(limitTo == val);
349             
350             switch (val) {
351                 case DECLARATIONS:
352                 case REFERENCES:
353                 case ALL_OCCURRENCES:
354                     button.setEnabled(true);
355                     break;
356                 case IMPLEMENTORS:
357                     button.setEnabled(searchFor == TYPE);
358                     break;
359                 case READ_ACCESSES:
360                 case WRITE_ACCESSES:
361                     button.setEnabled(searchFor == FIELD);
362                     break;
363             }
364         }
365         return limitTo;
366     }
367     
368     private int getIncludeMask() {
369         int mask= 0;
370         for (int i= 0; i < fIncludeMasks.length; i++) {
371             Button button= fIncludeMasks[i];
372             if (button.getSelection()) {
373                 mask |= getIntData(button);
374             }
375         }
376         return mask;
377     }
378     
379     private void setIncludeMask(int includeMask, int limitTo) {
380         for (int i= 0; i < fIncludeMasks.length; i++) {
381             Button button= fIncludeMasks[i];
382             button.setSelection((includeMask & getIntData(button)) != 0);
383         }
384     }
385     
386
387     private String JavaDoc[] getPreviousSearchPatterns() {
388         // Search results are not persistent
389
int patternCount= fPreviousSearchPatterns.size();
390         String JavaDoc [] patterns= new String JavaDoc[patternCount];
391         for (int i= 0; i < patternCount; i++)
392             patterns[i]= ((SearchPatternData) fPreviousSearchPatterns.get(i)).getPattern();
393         return patterns;
394     }
395     
396     private int getSearchFor() {
397         for (int i= 0; i < fSearchFor.length; i++) {
398             Button button= fSearchFor[i];
399             if (button.getSelection()) {
400                 return getIntData(button);
401             }
402         }
403         Assert.isTrue(false, "shouldNeverHappen"); //$NON-NLS-1$
404
return -1;
405     }
406     
407     private void setSearchFor(int searchFor) {
408         for (int i= 0; i < fSearchFor.length; i++) {
409             Button button= fSearchFor[i];
410             button.setSelection(searchFor == getIntData(button));
411         }
412     }
413     
414     private int getIntData(Button button) {
415         return ((Integer JavaDoc) button.getData()).intValue();
416     }
417     
418     private String JavaDoc getPattern() {
419         return fPattern.getText();
420     }
421
422     
423     private SearchPatternData findInPrevious(String JavaDoc pattern) {
424         for (Iterator JavaDoc iter= fPreviousSearchPatterns.iterator(); iter.hasNext();) {
425             SearchPatternData element= (SearchPatternData) iter.next();
426             if (pattern.equals(element.getPattern())) {
427                 return element;
428             }
429         }
430         return null;
431     }
432     
433     /**
434      * Return search pattern data and update previous searches.
435      * An existing entry will be updated.
436      * @return the pattern data
437      */

438     private SearchPatternData getPatternData() {
439         String JavaDoc pattern= getPattern();
440         SearchPatternData match= findInPrevious(pattern);
441         if (match != null) {
442             fPreviousSearchPatterns.remove(match);
443         }
444         match= new SearchPatternData(
445                 getSearchFor(),
446                 getLimitTo(),
447                 pattern,
448                 fCaseSensitive.getSelection(),
449                 fJavaElement,
450                 getContainer().getSelectedScope(),
451                 getContainer().getSelectedWorkingSets(),
452                 getIncludeMask()
453         );
454             
455         fPreviousSearchPatterns.add(0, match); // insert on top
456
return match;
457     }
458
459     /*
460      * Implements method from IDialogPage
461      */

462     public void setVisible(boolean visible) {
463         if (visible && fPattern != null) {
464             if (fFirstTime) {
465                 fFirstTime= false;
466                 // Set item and text here to prevent page from resizing
467
fPattern.setItems(getPreviousSearchPatterns());
468                 initSelections();
469             }
470             fPattern.setFocus();
471         }
472         updateOKStatus();
473         super.setVisible(visible);
474     }
475     
476     public boolean isValid() {
477         return true;
478     }
479
480     //---- Widget creation ------------------------------------------------
481

482     
483     /* (non-Javadoc)
484      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
485      */

486     public void createControl(Composite parent) {
487         initializeDialogUnits(parent);
488         readConfiguration();
489         
490         Composite result= new Composite(parent, SWT.NONE);
491         
492         GridLayout layout= new GridLayout(2, false);
493         layout.horizontalSpacing= 10;
494         result.setLayout(layout);
495         
496         Control expressionComposite= createExpression(result);
497         expressionComposite.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 1));
498         
499         Label separator= new Label(result, SWT.NONE);
500         separator.setVisible(false);
501         GridData data= new GridData(GridData.FILL, GridData.FILL, false, false, 2, 1);
502         data.heightHint= convertHeightInCharsToPixels(1) / 3;
503         separator.setLayoutData(data);
504         
505         Control searchFor= createSearchFor(result);
506         searchFor.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1));
507
508         Control limitTo= createLimitTo(result);
509         limitTo.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1));
510
511         Control includeMask= createIncludeMask(result);
512         includeMask.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
513                 
514         //createParticipants(result);
515

516         SelectionAdapter javaElementInitializer= new SelectionAdapter() {
517             public void widgetSelected(SelectionEvent event) {
518                 if (getSearchFor() == fInitialData.getSearchFor())
519                     fJavaElement= fInitialData.getJavaElement();
520                 else
521                     fJavaElement= null;
522                 int limitToVal= setLimitTo(getSearchFor(), getLimitTo());
523                 setIncludeMask(getIncludeMask(), limitToVal);
524                 doPatternModified();
525             }
526         };
527
528         for (int i= 0; i < fSearchFor.length; i++) {
529             fSearchFor[i].addSelectionListener(javaElementInitializer);
530         }
531
532         setControl(result);
533
534         Dialog.applyDialogFont(result);
535         PlatformUI.getWorkbench().getHelpSystem().setHelp(result, IJavaHelpContextIds.JAVA_SEARCH_PAGE);
536     }
537     
538     
539     /*private Control createParticipants(Composite result) {
540         if (!SearchParticipantsExtensionPoint.hasAnyParticipants())
541             return new Composite(result, SWT.NULL);
542         Button selectParticipants= new Button(result, SWT.PUSH);
543         selectParticipants.setText(SearchMessages.getString("SearchPage.select_participants.label")); //$NON-NLS-1$
544         GridData gd= new GridData();
545         gd.verticalAlignment= GridData.VERTICAL_ALIGN_BEGINNING;
546         gd.horizontalAlignment= GridData.HORIZONTAL_ALIGN_END;
547         gd.grabExcessHorizontalSpace= false;
548         gd.horizontalAlignment= GridData.END;
549         gd.horizontalSpan= 2;
550         selectParticipants.setLayoutData(gd);
551         selectParticipants.addSelectionListener(new SelectionAdapter() {
552             public void widgetSelected(SelectionEvent e) {
553                 PreferencePageSupport.showPreferencePage(getShell(), "org.eclipse.jdt.ui.preferences.SearchParticipantsExtensionPoint", new SearchParticipantsExtensionPoint()); //$NON-NLS-1$
554             }
555
556         });
557         return selectParticipants;
558     }*/

559
560
561     private Control createExpression(Composite parent) {
562         Composite result= new Composite(parent, SWT.NONE);
563         GridLayout layout= new GridLayout(2, false);
564         layout.marginWidth= 0;
565         layout.marginHeight= 0;
566         result.setLayout(layout);
567
568         // Pattern text + info
569
Label label= new Label(result, SWT.LEFT);
570         label.setText(SearchMessages.SearchPage_expression_label);
571         label.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false, 2, 1));
572
573         // Pattern combo
574
fPattern= new Combo(result, SWT.SINGLE | SWT.BORDER);
575         fPattern.addSelectionListener(new SelectionAdapter() {
576             public void widgetSelected(SelectionEvent e) {
577                 handlePatternSelected();
578                 updateOKStatus();
579             }
580         });
581         fPattern.addModifyListener(new ModifyListener() {
582             public void modifyText(ModifyEvent e) {
583                 doPatternModified();
584                 updateOKStatus();
585
586             }
587         });
588         TextFieldNavigationHandler.install(fPattern);
589         GridData data= new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
590         data.widthHint= convertWidthInCharsToPixels(50);
591         fPattern.setLayoutData(data);
592
593         // Ignore case checkbox
594
fCaseSensitive= new Button(result, SWT.CHECK);
595         fCaseSensitive.setText(SearchMessages.SearchPage_expression_caseSensitive);
596         fCaseSensitive.addSelectionListener(new SelectionAdapter() {
597             public void widgetSelected(SelectionEvent e) {
598                 fIsCaseSensitive= fCaseSensitive.getSelection();
599             }
600         });
601         fCaseSensitive.setLayoutData(new GridData(GridData.FILL, GridData.FILL, false, false, 1, 1));
602         
603         return result;
604     }
605     
606     final void updateOKStatus() {
607         boolean isValid= isValidSearchPattern();
608         getContainer().setPerformActionEnabled(isValid);
609     }
610     
611     private boolean isValidSearchPattern() {
612         if (getPattern().length() == 0) {
613             return false;
614         }
615         if (fJavaElement != null) {
616             return true;
617         }
618         return SearchPattern.createPattern(getPattern(), getSearchFor(), getLimitTo(), SearchPattern.R_EXACT_MATCH) != null;
619     }
620     
621     
622     /* (non-Javadoc)
623      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
624      */

625     public void dispose() {
626         writeConfiguration();
627         super.dispose();
628     }
629
630     private void doPatternModified() {
631         if (fInitialData != null && getPattern().equals(fInitialData.getPattern()) && fInitialData.getJavaElement() != null && fInitialData.getSearchFor() == getSearchFor()) {
632             fCaseSensitive.setEnabled(false);
633             fCaseSensitive.setSelection(true);
634             fJavaElement= fInitialData.getJavaElement();
635         } else {
636             fCaseSensitive.setEnabled(true);
637             fCaseSensitive.setSelection(fIsCaseSensitive);
638             fJavaElement= null;
639         }
640     }
641
642     private void handlePatternSelected() {
643         int selectionIndex= fPattern.getSelectionIndex();
644         if (selectionIndex < 0 || selectionIndex >= fPreviousSearchPatterns.size())
645             return;
646         
647         SearchPatternData initialData= (SearchPatternData) fPreviousSearchPatterns.get(selectionIndex);
648
649         setSearchFor(initialData.getSearchFor());
650         int limitToVal= setLimitTo(initialData.getSearchFor(), initialData.getLimitTo());
651         setIncludeMask(initialData.getIncludeMask(), limitToVal);
652
653         fPattern.setText(initialData.getPattern());
654         fIsCaseSensitive= initialData.isCaseSensitive();
655         fJavaElement= initialData.getJavaElement();
656         fCaseSensitive.setEnabled(fJavaElement == null);
657         fCaseSensitive.setSelection(initialData.isCaseSensitive());
658
659         
660         if (initialData.getWorkingSets() != null)
661             getContainer().setSelectedWorkingSets(initialData.getWorkingSets());
662         else
663             getContainer().setSelectedScope(initialData.getScope());
664         
665         fInitialData= initialData;
666     }
667     
668
669     private Control createSearchFor(Composite parent) {
670         Group result= new Group(parent, SWT.NONE);
671         result.setText(SearchMessages.SearchPage_searchFor_label);
672         result.setLayout(new GridLayout(2, true));
673
674         fSearchFor= new Button[] {
675             createButton(result, SWT.RADIO, SearchMessages.SearchPage_searchFor_type, TYPE, true),
676             createButton(result, SWT.RADIO, SearchMessages.SearchPage_searchFor_method, METHOD, false),
677             createButton(result, SWT.RADIO, SearchMessages.SearchPage_searchFor_package, PACKAGE, false),
678             createButton(result, SWT.RADIO, SearchMessages.SearchPage_searchFor_constructor, CONSTRUCTOR, false),
679             createButton(result, SWT.RADIO, SearchMessages.SearchPage_searchFor_field, FIELD, false)
680         };
681             
682         // Fill with dummy radio buttons
683
Label filler= new Label(result, SWT.NONE);
684         filler.setVisible(false);
685         filler.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
686
687         return result;
688     }
689     
690     private Control createLimitTo(Composite parent) {
691         Group result= new Group(parent, SWT.NONE);
692         result.setText(SearchMessages.SearchPage_limitTo_label);
693         result.setLayout(new GridLayout(2, true));
694
695         fLimitTo= new Button[] {
696             createButton(result, SWT.RADIO, SearchMessages.SearchPage_limitTo_declarations, DECLARATIONS, false),
697             createButton(result, SWT.RADIO, SearchMessages.SearchPage_limitTo_implementors, IMPLEMENTORS, false),
698             createButton(result, SWT.RADIO, SearchMessages.SearchPage_limitTo_references, REFERENCES, true),
699             createButton(result, SWT.RADIO, SearchMessages.SearchPage_limitTo_allOccurrences, ALL_OCCURRENCES, false),
700             createButton(result, SWT.RADIO, SearchMessages.SearchPage_limitTo_readReferences, READ_ACCESSES, false),
701             createButton(result, SWT.RADIO, SearchMessages.SearchPage_limitTo_writeReferences, WRITE_ACCESSES, false)
702         };
703         
704         SelectionAdapter listener= new SelectionAdapter() {
705             public void widgetSelected(SelectionEvent e) {
706                 updateUseJRE();
707             }
708         };
709         for (int i= 0; i < fLimitTo.length; i++) {
710             fLimitTo[i].addSelectionListener(listener);
711         }
712         return result;
713     }
714     
715     private Control createIncludeMask(Composite parent) {
716         Group result= new Group(parent, SWT.NONE);
717         result.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
718         result.setText(SearchMessages.SearchPage_searchIn_label);
719         result.setLayout(new GridLayout(4, false));
720         fIncludeMasks= new Button[] {
721             createButton(result, SWT.CHECK, SearchMessages.SearchPage_searchIn_sources, JavaSearchScopeFactory.SOURCES, true),
722             createButton(result, SWT.CHECK, SearchMessages.SearchPage_searchIn_projects, JavaSearchScopeFactory.PROJECTS, true),
723             createButton(result, SWT.CHECK, SearchMessages.SearchPage_searchIn_jre, JavaSearchScopeFactory.JRE, false),
724             createButton(result, SWT.CHECK, SearchMessages.SearchPage_searchIn_libraries, JavaSearchScopeFactory.LIBS, true),
725         };
726         return result;
727     }
728     
729     private Button createButton(Composite parent, int style, String JavaDoc text, int data, boolean isSelected) {
730         Button button= new Button(parent, style);
731         button.setText(text);
732         button.setData(new Integer JavaDoc(data));
733         button.setLayoutData(new GridData());
734         button.setSelection(isSelected);
735         return button;
736     }
737     
738     private void initSelections() {
739         ISelection sel= getContainer().getSelection();
740         SearchPatternData initData= null;
741
742         if (sel instanceof IStructuredSelection) {
743             initData= tryStructuredSelection((IStructuredSelection) sel);
744         } else if (sel instanceof ITextSelection) {
745             IEditorPart activePart= getActiveEditor();
746             if (activePart instanceof JavaEditor) {
747                 try {
748                     IJavaElement[] elements= SelectionConverter.codeResolve((JavaEditor) activePart);
749                     if (elements != null && elements.length > 0) {
750                         initData= determineInitValuesFrom(elements[0]);
751                     }
752                 } catch (JavaModelException e) {
753                     // ignore
754
}
755             }
756             if (initData == null) {
757                 initData= trySimpleTextSelection((ITextSelection) sel);
758             }
759         }
760         if (initData == null) {
761             initData= getDefaultInitValues();
762         }
763         
764         fInitialData= initData;
765         fJavaElement= initData.getJavaElement();
766         fCaseSensitive.setSelection(initData.isCaseSensitive());
767         fCaseSensitive.setEnabled(fJavaElement == null);
768         
769         setSearchFor(initData.getSearchFor());
770         int limitToVal= setLimitTo(initData.getSearchFor(), initData.getLimitTo());
771         setIncludeMask(initData.getIncludeMask(), limitToVal);
772
773         fPattern.setText(initData.getPattern());
774     }
775
776     private void updateUseJRE() {
777         setIncludeMask(getIncludeMask(), getLimitTo());
778     }
779
780     private static boolean forceIncludeAll(int limitTo, IJavaElement elem) {
781         return elem != null && (limitTo == DECLARATIONS || limitTo == IMPLEMENTORS);
782     }
783
784     private SearchPatternData tryStructuredSelection(IStructuredSelection selection) {
785         if (selection == null || selection.size() > 1)
786             return null;
787
788         Object JavaDoc o= selection.getFirstElement();
789         SearchPatternData res= null;
790         if (o instanceof IJavaElement) {
791             res= determineInitValuesFrom((IJavaElement) o);
792         } else if (o instanceof LogicalPackage) {
793             LogicalPackage lp= (LogicalPackage)o;
794             return new SearchPatternData(PACKAGE, REFERENCES, fIsCaseSensitive, lp.getElementName(), null, getLastIncludeMask());
795         } else if (o instanceof IAdaptable) {
796             IJavaElement element= (IJavaElement) ((IAdaptable) o).getAdapter(IJavaElement.class);
797             if (element != null) {
798                 res= determineInitValuesFrom(element);
799             }
800         }
801         if (res == null && o instanceof IAdaptable) {
802             IWorkbenchAdapter adapter= (IWorkbenchAdapter)((IAdaptable)o).getAdapter(IWorkbenchAdapter.class);
803             if (adapter != null) {
804                 return new SearchPatternData(TYPE, REFERENCES, fIsCaseSensitive, adapter.getLabel(o), null, getLastIncludeMask());
805             }
806         }
807         return res;
808     }
809     
810     final static boolean isSearchableType(IJavaElement element) {
811         switch (element.getElementType()) {
812             case IJavaElement.PACKAGE_FRAGMENT:
813             case IJavaElement.PACKAGE_DECLARATION:
814             case IJavaElement.IMPORT_DECLARATION:
815             case IJavaElement.TYPE:
816             case IJavaElement.FIELD:
817             case IJavaElement.METHOD:
818                 return true;
819         }
820         return false;
821     }
822
823     private SearchPatternData determineInitValuesFrom(IJavaElement element) {
824         try {
825             //JavaSearchScopeFactory factory= JavaSearchScopeFactory.getInstance();
826
//boolean isInsideJRE= factory.isInsideJRE(element);
827
int includeMask= getLastIncludeMask();
828             
829             switch (element.getElementType()) {
830                 case IJavaElement.PACKAGE_FRAGMENT:
831                 case IJavaElement.PACKAGE_DECLARATION:
832                     return new SearchPatternData(PACKAGE, REFERENCES, true, element.getElementName(), element, includeMask);
833                 case IJavaElement.IMPORT_DECLARATION: {
834                     IImportDeclaration declaration= (IImportDeclaration) element;
835                     if (declaration.isOnDemand()) {
836                         String JavaDoc name= Signature.getQualifier(declaration.getElementName());
837                         return new SearchPatternData(PACKAGE, DECLARATIONS, true, name, element, JavaSearchScopeFactory.ALL);
838                     }
839                     return new SearchPatternData(TYPE, DECLARATIONS, true, element.getElementName(), element, JavaSearchScopeFactory.ALL);
840                 }
841                 case IJavaElement.TYPE:
842                     return new SearchPatternData(TYPE, REFERENCES, true, PatternStrings.getTypeSignature((IType) element), element, includeMask);
843                 case IJavaElement.COMPILATION_UNIT: {
844                     IType mainType= ((ICompilationUnit) element).findPrimaryType();
845                     if (mainType != null) {
846                         return new SearchPatternData(TYPE, REFERENCES, true, PatternStrings.getTypeSignature(mainType), mainType, includeMask);
847                     }
848                     break;
849                 }
850                 case IJavaElement.CLASS_FILE: {
851                     IType mainType= ((IClassFile) element).getType();
852                     if (mainType.exists()) {
853                         return new SearchPatternData(TYPE, REFERENCES, true, PatternStrings.getTypeSignature(mainType), mainType, includeMask);
854                     }
855                     break;
856                 }
857                 case IJavaElement.FIELD:
858                     return new SearchPatternData(FIELD, REFERENCES, true, PatternStrings.getFieldSignature((IField) element), element, includeMask);
859                 case IJavaElement.METHOD:
860                     IMethod method= (IMethod) element;
861                     int searchFor= method.isConstructor() ? CONSTRUCTOR : METHOD;
862                     return new SearchPatternData(searchFor, REFERENCES, true, PatternStrings.getMethodSignature(method), element, includeMask);
863             }
864             
865         } catch (JavaModelException e) {
866             if (!e.isDoesNotExist()) {
867                 ExceptionHandler.handle(e, SearchMessages.Search_Error_javaElementAccess_title, SearchMessages.Search_Error_javaElementAccess_message);
868             }
869             // element might not exist
870
}
871         return null;
872     }
873     
874     private SearchPatternData trySimpleTextSelection(ITextSelection selection) {
875         String JavaDoc selectedText= selection.getText();
876         if (selectedText != null && selectedText.length() > 0) {
877             int i= 0;
878             while (i < selectedText.length() && !IndentManipulation.isLineDelimiterChar(selectedText.charAt(i))) {
879                 i++;
880             }
881             if (i > 0) {
882                 return new SearchPatternData(TYPE, REFERENCES, fIsCaseSensitive, selectedText.substring(0, i), null, JavaSearchScopeFactory.ALL);
883             }
884         }
885         return null;
886     }
887     
888     private SearchPatternData getDefaultInitValues() {
889         if (!fPreviousSearchPatterns.isEmpty()) {
890             return (SearchPatternData) fPreviousSearchPatterns.get(0);
891         }
892
893         return new SearchPatternData(TYPE, REFERENCES, fIsCaseSensitive, "", null, getLastIncludeMask()); //$NON-NLS-1$
894
}
895     
896     private int getLastIncludeMask() {
897         try {
898             return getDialogSettings().getInt(STORE_INCLUDE_MASK);
899         } catch (NumberFormatException JavaDoc e) {
900             return JavaSearchScopeFactory.NO_JRE;
901         }
902     }
903
904     /*
905      * Implements method from ISearchPage
906      */

907     public void setContainer(ISearchPageContainer container) {
908         fContainer= container;
909     }
910     
911     /**
912      * Returns the search page's container.
913      * @return the search page container
914      */

915     private ISearchPageContainer getContainer() {
916         return fContainer;
917     }
918         
919     private IEditorPart getActiveEditor() {
920         IWorkbenchPage activePage= JavaPlugin.getActivePage();
921         if (activePage != null) {
922             return activePage.getActiveEditor();
923         }
924         return null;
925     }
926     
927     //--------------- Configuration handling --------------
928

929     /**
930      * Returns the page settings for this Java search page.
931      *
932      * @return the page settings to be used
933      */

934     private IDialogSettings getDialogSettings() {
935         if (fDialogSettings == null) {
936             fDialogSettings= JavaPlugin.getDefault().getDialogSettingsSection(PAGE_NAME);
937         }
938         return fDialogSettings;
939     }
940     
941     /**
942      * Initializes itself from the stored page settings.
943      */

944     private void readConfiguration() {
945         IDialogSettings s= getDialogSettings();
946         fIsCaseSensitive= s.getBoolean(STORE_CASE_SENSITIVE);
947         
948         try {
949             int historySize= s.getInt(STORE_HISTORY_SIZE);
950             for (int i= 0; i < historySize; i++) {
951                 IDialogSettings histSettings= s.getSection(STORE_HISTORY + i);
952                 if (histSettings != null) {
953                     SearchPatternData data= SearchPatternData.create(histSettings);
954                     if (data != null) {
955                         fPreviousSearchPatterns.add(data);
956                     }
957                 }
958             }
959         } catch (NumberFormatException JavaDoc e) {
960             // ignore
961
}
962     }
963     
964     /**
965      * Stores the current configuration in the dialog store.
966      */

967     private void writeConfiguration() {
968         IDialogSettings s= getDialogSettings();
969         s.put(STORE_CASE_SENSITIVE, fIsCaseSensitive);
970         s.put(STORE_INCLUDE_MASK, getIncludeMask());
971         
972         int historySize= Math.min(fPreviousSearchPatterns.size(), HISTORY_SIZE);
973         s.put(STORE_HISTORY_SIZE, historySize);
974         for (int i= 0; i < historySize; i++) {
975             IDialogSettings histSettings= s.addNewSection(STORE_HISTORY + i);
976             SearchPatternData data= ((SearchPatternData) fPreviousSearchPatterns.get(i));
977             data.store(histSettings);
978         }
979     }
980 }
981
Popular Tags