001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.search;
011:
012: import java.util.Arrays;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Set;
016:
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Platform;
019:
020: import org.eclipse.swt.widgets.Shell;
021:
022: import org.eclipse.jface.dialogs.IDialogConstants;
023: import org.eclipse.jface.dialogs.IDialogSettings;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.jface.operation.IRunnableContext;
026:
027: import org.eclipse.ui.IWorkingSet;
028: import org.eclipse.ui.PlatformUI;
029:
030: import org.eclipse.search.ui.ISearchQuery;
031: import org.eclipse.search.ui.NewSearchUI;
032:
033: import org.eclipse.jdt.core.Flags;
034: import org.eclipse.jdt.core.ICompilationUnit;
035: import org.eclipse.jdt.core.IField;
036: import org.eclipse.jdt.core.IJavaElement;
037: import org.eclipse.jdt.core.JavaModelException;
038: import org.eclipse.jdt.core.Signature;
039:
040: import org.eclipse.jdt.internal.corext.util.Messages;
041:
042: import org.eclipse.jdt.internal.ui.JavaPlugin;
043: import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
044:
045: import org.osgi.framework.Bundle;
046:
047: /**
048: * This class contains some utility methods for J Search.
049: */
050: public class SearchUtil {
051:
052: // LRU working sets
053: public static final int LRU_WORKINGSET_LIST_SIZE = 3;
054: private static LRUWorkingSetsList fgLRUWorkingSets;
055:
056: // Settings store
057: private static final String DIALOG_SETTINGS_KEY = "JavaElementSearchActions"; //$NON-NLS-1$
058: private static final String STORE_LRU_WORKING_SET_NAMES = "lastUsedWorkingSetNames"; //$NON-NLS-1$
059:
060: private static final String BIN_PRIM_CONST_WARN_DIALOG_ID = "BinaryPrimitiveConstantWarningDialog"; //$NON-NLS-1$
061:
062: public static boolean isSearchPlugInActivated() {
063: return Platform.getBundle("org.eclipse.search").getState() == Bundle.ACTIVE; //$NON-NLS-1$
064: }
065:
066: /**
067: * This helper method with Object as parameter is needed to prevent the loading
068: * of the Search plug-in: the VM verifies the method call and hence loads the
069: * types used in the method signature, eventually triggering the loading of
070: * a plug-in (in this case ISearchQuery results in Search plug-in being loaded).
071: */
072: public static void runQueryInBackground(Object query) {
073: NewSearchUI.runQueryInBackground((ISearchQuery) query);
074: }
075:
076: /**
077: * This helper method with Object as parameter is needed to prevent the loading
078: * of the Search plug-in: the VM verifies the method call and hence loads the
079: * types used in the method signature, eventually triggering the loading of
080: * a plug-in (in this case ISearchQuery results in Search plug-in being loaded).
081: */
082: public static IStatus runQueryInForeground(
083: IRunnableContext context, Object query) {
084: return NewSearchUI.runQueryInForeground(context,
085: (ISearchQuery) query);
086: }
087:
088: /**
089: * Returns the compilation unit for the given java element.
090: *
091: * @param element the java element whose compilation unit is searched for
092: * @return the compilation unit of the given java element
093: */
094: static ICompilationUnit findCompilationUnit(IJavaElement element) {
095: if (element == null)
096: return null;
097: return (ICompilationUnit) element
098: .getAncestor(IJavaElement.COMPILATION_UNIT);
099: }
100:
101: public static String toString(IWorkingSet[] workingSets) {
102: Arrays.sort(workingSets, new WorkingSetComparator());
103: String 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 workingSetLabel = workingSets[i].getLabel();
108: if (firstFound)
109: result = Messages
110: .format(
111: SearchMessages.SearchUtil_workingSetConcatenation,
112: new String[] { result,
113: workingSetLabel });
114: else {
115: result = workingSetLabel;
116: firstFound = true;
117: }
118: }
119: }
120: return result;
121: }
122:
123: // ---------- LRU working set handling ----------
124:
125: /**
126: * Updates the LRU list of working sets.
127: *
128: * @param workingSets the workings sets to be added to the LRU list
129: */
130: public static void updateLRUWorkingSets(IWorkingSet[] workingSets) {
131: if (workingSets == null || workingSets.length < 1)
132: return;
133:
134: getLRUWorkingSets().add(workingSets);
135: saveState(getDialogStoreSection());
136: }
137:
138: private static void saveState(IDialogSettings settingsStore) {
139: IWorkingSet[] workingSets;
140: Iterator iter = fgLRUWorkingSets.iterator();
141: int i = 0;
142: while (iter.hasNext()) {
143: workingSets = (IWorkingSet[]) iter.next();
144: String[] names = new String[workingSets.length];
145: for (int j = 0; j < workingSets.length; j++)
146: names[j] = workingSets[j].getName();
147: settingsStore.put(STORE_LRU_WORKING_SET_NAMES + i, names);
148: i++;
149: }
150: }
151:
152: public static LRUWorkingSetsList getLRUWorkingSets() {
153: if (fgLRUWorkingSets == null) {
154: restoreState();
155: }
156: return fgLRUWorkingSets;
157: }
158:
159: private static void restoreState() {
160: fgLRUWorkingSets = new LRUWorkingSetsList(
161: LRU_WORKINGSET_LIST_SIZE);
162: IDialogSettings settingsStore = getDialogStoreSection();
163:
164: boolean foundLRU = false;
165: for (int i = LRU_WORKINGSET_LIST_SIZE - 1; i >= 0; i--) {
166: String[] lruWorkingSetNames = settingsStore
167: .getArray(STORE_LRU_WORKING_SET_NAMES + i);
168: if (lruWorkingSetNames != null) {
169: Set workingSets = new HashSet(2);
170: for (int j = 0; j < lruWorkingSetNames.length; j++) {
171: IWorkingSet workingSet = PlatformUI.getWorkbench()
172: .getWorkingSetManager().getWorkingSet(
173: lruWorkingSetNames[j]);
174: if (workingSet != null) {
175: workingSets.add(workingSet);
176: }
177: }
178: foundLRU = true;
179: if (!workingSets.isEmpty())
180: fgLRUWorkingSets
181: .add((IWorkingSet[]) workingSets
182: .toArray(new IWorkingSet[workingSets
183: .size()]));
184: }
185: }
186: if (!foundLRU)
187: // try old preference format
188: restoreFromOldFormat();
189: }
190:
191: private static IDialogSettings getDialogStoreSection() {
192: IDialogSettings settingsStore = JavaPlugin.getDefault()
193: .getDialogSettings().getSection(DIALOG_SETTINGS_KEY);
194: if (settingsStore == null)
195: settingsStore = JavaPlugin.getDefault().getDialogSettings()
196: .addNewSection(DIALOG_SETTINGS_KEY);
197: return settingsStore;
198: }
199:
200: private static void restoreFromOldFormat() {
201: fgLRUWorkingSets = new LRUWorkingSetsList(
202: LRU_WORKINGSET_LIST_SIZE);
203: IDialogSettings settingsStore = getDialogStoreSection();
204:
205: boolean foundLRU = false;
206: String[] lruWorkingSetNames = settingsStore
207: .getArray(STORE_LRU_WORKING_SET_NAMES);
208: if (lruWorkingSetNames != null) {
209: for (int i = lruWorkingSetNames.length - 1; i >= 0; i--) {
210: IWorkingSet workingSet = PlatformUI.getWorkbench()
211: .getWorkingSetManager().getWorkingSet(
212: lruWorkingSetNames[i]);
213: if (workingSet != null) {
214: foundLRU = true;
215: fgLRUWorkingSets
216: .add(new IWorkingSet[] { workingSet });
217: }
218: }
219: }
220: if (foundLRU)
221: // save in new format
222: saveState(settingsStore);
223: }
224:
225: public static void warnIfBinaryConstant(IJavaElement element,
226: Shell shell) {
227: if (isBinaryPrimitiveConstantOrString(element))
228: OptionalMessageDialog
229: .open(
230: BIN_PRIM_CONST_WARN_DIALOG_ID,
231: shell,
232: SearchMessages.Search_FindReferencesAction_BinPrimConstWarnDialog_title,
233: null,
234: SearchMessages.Search_FindReferencesAction_BinPrimConstWarnDialog_message,
235: MessageDialog.INFORMATION,
236: new String[] { IDialogConstants.OK_LABEL },
237: 0);
238: }
239:
240: private static boolean isBinaryPrimitiveConstantOrString(
241: IJavaElement element) {
242: if (element != null
243: && element.getElementType() == IJavaElement.FIELD) {
244: IField field = (IField) element;
245: int flags;
246: try {
247: flags = field.getFlags();
248: } catch (JavaModelException ex) {
249: return false;
250: }
251: return field.isBinary() && Flags.isStatic(flags)
252: && Flags.isFinal(flags)
253: && isPrimitiveOrString(field);
254: }
255: return false;
256: }
257:
258: private static boolean isPrimitiveOrString(IField field) {
259: String fieldType;
260: try {
261: fieldType = field.getTypeSignature();
262: } catch (JavaModelException ex) {
263: return false;
264: }
265: char first = fieldType.charAt(0);
266: return (first != Signature.C_RESOLVED
267: && first != Signature.C_UNRESOLVED && first != Signature.C_ARRAY)
268: || (first == Signature.C_RESOLVED && fieldType
269: .substring(1, fieldType.length() - 1).equals(
270: String.class.getName()));
271: }
272: }
|