diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/search/PluginSearchPage.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/search/PluginSearchPage.java index 39f1b1f..a7f07f0 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/search/PluginSearchPage.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/search/PluginSearchPage.java @@ -14,14 +14,13 @@ import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.dialogs.*; import org.eclipse.jface.dialogs.Dialog; -import org.eclipse.jface.dialogs.DialogPage; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.pde.internal.core.search.*; -import org.eclipse.pde.internal.ui.IHelpContextIds; -import org.eclipse.pde.internal.ui.PDEUIMessages; +import org.eclipse.pde.internal.ui.*; import org.eclipse.search.ui.*; import org.eclipse.swt.SWT; import org.eclipse.swt.events.*; @@ -32,7 +31,12 @@ public class PluginSearchPage extends DialogPage implements ISearchPage { + private static final String DIALOG_SETTINGS = "org.eclipse.pde.internal.ui.search.PluginSearchPage"; //$NON-NLS-1$ + private static final String PREVIOUS_QUERIES_SETTINGS_KEY = "previousQueries"; //$NON-NLS-1$ + class QueryData { + protected static final String SEPARATOR = "\n"; //$NON-NLS-1$ + public String text; public boolean isCaseSensitive; public int searchElement; @@ -40,6 +44,69 @@ public int externalScope; public int workspaceScope; public IWorkingSet[] workingSets; + + /** + * Returns the ordered fields of the object separated by newlines. + * Newlines were chosen because the query text can not contain newlines + * which means it is a safe character to split by. + * + * The names of selected working sets are added after the normal properties. + */ + public String toString() { + StringBuilder out = new StringBuilder(); + out.append(text); + out.append(SEPARATOR); + out.append(isCaseSensitive); + out.append(SEPARATOR); + out.append(searchElement); + out.append(SEPARATOR); + out.append(limit); + out.append(SEPARATOR); + out.append(externalScope); + out.append(SEPARATOR); + out.append(workspaceScope); + // add working sets to the end if there is any + if (workingSets != null) { + for (int i = 0; i < workingSets.length; i++) { + out.append(SEPARATOR); + out.append(workingSets[i].getName()); + } + } + return out.toString(); + } + + /** + * Reads the string representation of the fields created + * by the toString method. + * @param data + * @returns the success status of the read. false if the data was invalid. + */ + public boolean readString(String data) { + String[] parts = data.split(SEPARATOR); + if (parts.length < 6) { + return false; + } + text = parts[0]; + isCaseSensitive = Boolean.parseBoolean(parts[1]); + searchElement = Integer.parseInt(parts[2]); + limit = Integer.parseInt(parts[3]); + externalScope = Integer.parseInt(parts[4]); + workspaceScope = Integer.parseInt(parts[5]); + // if there are more than 6 they are selected working sets + // look them up by name and add them to the array + if (parts.length > 6) { + IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager(); + workingSets = new IWorkingSet[parts.length - 6]; + // offset i by six to start after normal properties + for (int i = 6; i < parts.length; i++) { + IWorkingSet set = manager.getWorkingSet(parts[i]); + if (set != null) { + workingSets[i - 6] = set; + } + } + } + return true; + } public boolean equals(Object obj) { if (obj instanceof QueryData) { @@ -72,6 +139,7 @@ createPatternSection(result); createSettingsSection(result); + restorePreviousQueries(); // load from dialog settings hookListeners(); setControl(result); @@ -247,6 +315,8 @@ public boolean performAction() { saveQueryData(); + // Save the queries array to persistent storage + savePreviousQueries(); NewSearchUI.activateSearchResultView(); NewSearchUI.runQueryInBackground(new PluginSearchQuery(getInput())); return true; @@ -292,6 +362,53 @@ previousQueries.remove(0); } + /** + * Saves the previousQueries array to IDialogSettings. + * Serializes the QueryData objects into an array of strings. + */ + private void savePreviousQueries() { + IDialogSettings settings = getDialogSettings(); + String[] items = new String[previousQueries.size()]; + // build up the items array by serializing each query + for (int i = 0; i < previousQueries.size(); i++) { + QueryData item = (QueryData) previousQueries.get(i); + items[i] = item.toString(); + } + + settings.put(PREVIOUS_QUERIES_SETTINGS_KEY, items); + } + + /** + * Restores the dialog from IDialogSettings. + * Destroys the previousQueries array and rebuilds it from persistent storage. + */ + private void restorePreviousQueries() { + IDialogSettings settings = getDialogSettings(); + String[] items = settings.getArray(PREVIOUS_QUERIES_SETTINGS_KEY); + if (items == null) { + return; // no previous data to load + } + previousQueries.clear(); + // build up the previousQueries array by serializing each query + for (int i = 0; i < items.length; i++) { + String itemdata = items[i]; + QueryData newData = new QueryData(); + boolean success = newData.readString(itemdata); + if (success) + previousQueries.add(newData); + } + } + + protected IDialogSettings getDialogSettings() { + IDialogSettings settings = PDEPlugin.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS); + + if (settings == null) { + settings = PDEPlugin.getDefault().getDialogSettings().addNewSection(DIALOG_SETTINGS); + } + + return settings; + } + public void setContainer(ISearchPageContainer container) { this.container = container; }