1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 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 51 public class SearchUtil { 52 53 public static final int LRU_WORKINGSET_LIST_SIZE= 3; 55 private static LRUWorkingSetsList fgLRUWorkingSets; 56 57 private static final String DIALOG_SETTINGS_KEY= "JavaElementSearchActions"; private static final String STORE_LRU_WORKING_SET_NAMES= "lastUsedWorkingSetNames"; 61 private static final String BIN_PRIM_CONST_WARN_DIALOG_ID= "BinaryPrimitiveConstantWarningDialog"; 63 public static boolean isSearchPlugInActivated() { 64 return Platform.getBundle("org.eclipse.search").getState() == Bundle.ACTIVE; } 66 67 68 74 public static void runQueryInBackground(Object query) { 75 NewSearchUI.runQueryInBackground((ISearchQuery)query); 76 } 77 78 84 public static IStatus runQueryInForeground(IRunnableContext context, Object query) { 85 return NewSearchUI.runQueryInForeground(context, (ISearchQuery)query); 86 } 87 88 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 toString(IWorkingSet[] workingSets) { 102 Arrays.sort(workingSets, new WorkingSetComparator()); 103 String result= ""; if (workingSets != null && workingSets.length > 0) { 105 boolean firstFound= false; 106 for (int i= 0; i < workingSets.length; i++) { 107 String workingSetLabel= workingSets[i].getLabel(); 108 if (firstFound) 109 result= Messages.format(SearchMessages.SearchUtil_workingSetConcatenation, new String [] {result, workingSetLabel}); 110 else { 111 result= workingSetLabel; 112 firstFound= true; 113 } 114 } 115 } 116 return result; 117 } 118 119 121 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 iter= fgLRUWorkingSets.iterator(); 137 int i= 0; 138 while (iter.hasNext()) { 139 workingSets= (IWorkingSet[])iter.next(); 140 String [] names= new String [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 [] lruWorkingSetNames= settingsStore.getArray(STORE_LRU_WORKING_SET_NAMES + i); 162 if (lruWorkingSetNames != null) { 163 Set workingSets= new HashSet (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 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 [] 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 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 [] { 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 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 .class.getName())); 244 } 245 } 246 | Popular Tags |