KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.jdt.internal.ui.search;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Platform;
20
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.jface.dialogs.IDialogConstants;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.operation.IRunnableContext;
27
28 import org.eclipse.ui.IWorkingSet;
29 import org.eclipse.ui.PlatformUI;
30
31 import org.eclipse.search.ui.ISearchQuery;
32 import org.eclipse.search.ui.NewSearchUI;
33
34 import org.eclipse.jdt.core.Flags;
35 import org.eclipse.jdt.core.ICompilationUnit;
36 import org.eclipse.jdt.core.IField;
37 import org.eclipse.jdt.core.IJavaElement;
38 import org.eclipse.jdt.core.JavaModelException;
39 import org.eclipse.jdt.core.Signature;
40
41 import org.eclipse.jdt.internal.corext.util.Messages;
42
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44 import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
45
46 import org.osgi.framework.Bundle;
47
48 /**
49  * This class contains some utility methods for J Search.
50  */

51 public class SearchUtil {
52
53     // LRU working sets
54
public static final int LRU_WORKINGSET_LIST_SIZE= 3;
55     private static LRUWorkingSetsList fgLRUWorkingSets;
56
57     // Settings store
58
private static final String JavaDoc DIALOG_SETTINGS_KEY= "JavaElementSearchActions"; //$NON-NLS-1$
59
private static final String JavaDoc STORE_LRU_WORKING_SET_NAMES= "lastUsedWorkingSetNames"; //$NON-NLS-1$
60

61     private static final String JavaDoc BIN_PRIM_CONST_WARN_DIALOG_ID= "BinaryPrimitiveConstantWarningDialog"; //$NON-NLS-1$
62

63     public static boolean isSearchPlugInActivated() {
64         return Platform.getBundle("org.eclipse.search").getState() == Bundle.ACTIVE; //$NON-NLS-1$
65
}
66
67     
68     /**
69      * This helper method with Object as parameter is needed to prevent the loading
70      * of the Search plug-in: the VM verifies the method call and hence loads the
71      * types used in the method signature, eventually triggering the loading of
72      * a plug-in (in this case ISearchQuery results in Search plug-in being loaded).
73      */

74     public static void runQueryInBackground(Object JavaDoc query) {
75         NewSearchUI.runQueryInBackground((ISearchQuery)query);
76     }
77     
78     /**
79      * This helper method with Object as parameter is needed to prevent the loading
80      * of the Search plug-in: the VM verifies the method call and hence loads the
81      * types used in the method signature, eventually triggering the loading of
82      * a plug-in (in this case ISearchQuery results in Search plug-in being loaded).
83      */

84     public static IStatus runQueryInForeground(IRunnableContext context, Object JavaDoc query) {
85         return NewSearchUI.runQueryInForeground(context, (ISearchQuery)query);
86     }
87     
88     /**
89      * Returns the compilation unit for the given java element.
90      *
91      * @param element the java element whose compilation unit is searched for
92      * @return the compilation unit of the given java element
93      */

94     static ICompilationUnit findCompilationUnit(IJavaElement element) {
95         if (element == null)
96             return null;
97         return (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
98     }
99
100
101     public static String JavaDoc toString(IWorkingSet[] workingSets) {
102         Arrays.sort(workingSets, new WorkingSetComparator());
103         String JavaDoc result= ""; //$NON-NLS-1$
104
if (workingSets != null && workingSets.length > 0) {
105             boolean firstFound= false;
106             for (int i= 0; i < workingSets.length; i++) {
107                 String JavaDoc workingSetLabel= workingSets[i].getLabel();
108                 if (firstFound)
109                     result= Messages.format(SearchMessages.SearchUtil_workingSetConcatenation, new String JavaDoc[] {result, workingSetLabel});
110                 else {
111                     result= workingSetLabel;
112                     firstFound= true;
113                 }
114             }
115         }
116         return result;
117     }
118
119     // ---------- LRU working set handling ----------
120

121     /**
122      * Updates the LRU list of working sets.
123      *
124      * @param workingSets the workings sets to be added to the LRU list
125      */

126     public static void updateLRUWorkingSets(IWorkingSet[] workingSets) {
127         if (workingSets == null || workingSets.length < 1)
128             return;
129         
130         getLRUWorkingSets().add(workingSets);
131         saveState(getDialogStoreSection());
132     }
133
134     private static void saveState(IDialogSettings settingsStore) {
135         IWorkingSet[] workingSets;
136         Iterator JavaDoc iter= fgLRUWorkingSets.iterator();
137         int i= 0;
138         while (iter.hasNext()) {
139             workingSets= (IWorkingSet[])iter.next();
140             String JavaDoc[] names= new String JavaDoc[workingSets.length];
141             for (int j= 0; j < workingSets.length; j++)
142                 names[j]= workingSets[j].getName();
143             settingsStore.put(STORE_LRU_WORKING_SET_NAMES + i, names);
144             i++;
145         }
146     }
147
148     public static LRUWorkingSetsList getLRUWorkingSets() {
149         if (fgLRUWorkingSets == null) {
150             restoreState();
151         }
152         return fgLRUWorkingSets;
153     }
154
155     private static void restoreState() {
156         fgLRUWorkingSets= new LRUWorkingSetsList(LRU_WORKINGSET_LIST_SIZE);
157         IDialogSettings settingsStore= getDialogStoreSection();
158         
159         boolean foundLRU= false;
160         for (int i= LRU_WORKINGSET_LIST_SIZE - 1; i >= 0; i--) {
161             String JavaDoc[] lruWorkingSetNames= settingsStore.getArray(STORE_LRU_WORKING_SET_NAMES + i);
162             if (lruWorkingSetNames != null) {
163                 Set JavaDoc workingSets= new HashSet JavaDoc(2);
164                 for (int j= 0; j < lruWorkingSetNames.length; j++) {
165                     IWorkingSet workingSet= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(lruWorkingSetNames[j]);
166                     if (workingSet != null) {
167                         workingSets.add(workingSet);
168                     }
169                 }
170                 foundLRU= true;
171                 if (!workingSets.isEmpty())
172                     fgLRUWorkingSets.add((IWorkingSet[])workingSets.toArray(new IWorkingSet[workingSets.size()]));
173             }
174         }
175         if (!foundLRU)
176             // try old preference format
177
restoreFromOldFormat();
178     }
179
180     private static IDialogSettings getDialogStoreSection() {
181         IDialogSettings settingsStore= JavaPlugin.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS_KEY);
182         if (settingsStore == null)
183             settingsStore= JavaPlugin.getDefault().getDialogSettings().addNewSection(DIALOG_SETTINGS_KEY);
184         return settingsStore;
185     }
186
187     private static void restoreFromOldFormat() {
188         fgLRUWorkingSets= new LRUWorkingSetsList(LRU_WORKINGSET_LIST_SIZE);
189         IDialogSettings settingsStore= getDialogStoreSection();
190
191         boolean foundLRU= false;
192         String JavaDoc[] lruWorkingSetNames= settingsStore.getArray(STORE_LRU_WORKING_SET_NAMES);
193         if (lruWorkingSetNames != null) {
194             for (int i= lruWorkingSetNames.length - 1; i >= 0; i--) {
195                 IWorkingSet workingSet= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(lruWorkingSetNames[i]);
196                 if (workingSet != null) {
197                     foundLRU= true;
198                     fgLRUWorkingSets.add(new IWorkingSet[]{workingSet});
199                 }
200             }
201         }
202         if (foundLRU)
203             // save in new format
204
saveState(settingsStore);
205     }
206
207     public static void warnIfBinaryConstant(IJavaElement element, Shell shell) {
208         if (isBinaryPrimitiveConstantOrString(element))
209             OptionalMessageDialog.open(
210                 BIN_PRIM_CONST_WARN_DIALOG_ID,
211                 shell,
212                 SearchMessages.Search_FindReferencesAction_BinPrimConstWarnDialog_title,
213                 null,
214                 SearchMessages.Search_FindReferencesAction_BinPrimConstWarnDialog_message,
215                 MessageDialog.INFORMATION,
216                 new String JavaDoc[] { IDialogConstants.OK_LABEL },
217                 0);
218     }
219     
220     private static boolean isBinaryPrimitiveConstantOrString(IJavaElement element) {
221         if (element != null && element.getElementType() == IJavaElement.FIELD) {
222             IField field= (IField)element;
223             int flags;
224             try {
225                 flags= field.getFlags();
226             } catch (JavaModelException ex) {
227                 return false;
228             }
229             return field.isBinary() && Flags.isStatic(flags) && Flags.isFinal(flags) && isPrimitiveOrString(field);
230         }
231         return false;
232     }
233
234     private static boolean isPrimitiveOrString(IField field) {
235         String JavaDoc fieldType;
236         try {
237             fieldType= field.getTypeSignature();
238         } catch (JavaModelException ex) {
239             return false;
240         }
241         char first= fieldType.charAt(0);
242         return (first != Signature.C_RESOLVED && first != Signature.C_UNRESOLVED && first != Signature.C_ARRAY)
243             || (first == Signature.C_RESOLVED && fieldType.substring(1, fieldType.length() - 1).equals(String JavaDoc.class.getName()));
244     }
245 }
246
Popular Tags