KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > search > HelpSearchPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.ui.internal.search;
12 import org.eclipse.core.runtime.*;
13 import org.eclipse.help.internal.base.*;
14 import org.eclipse.help.ui.internal.ide.*;
15 import org.eclipse.jface.dialogs.*;
16 import org.eclipse.jface.window.*;
17 import org.eclipse.search.ui.*;
18 import org.eclipse.swt.*;
19 import org.eclipse.swt.events.*;
20 import org.eclipse.swt.layout.*;
21 import org.eclipse.swt.widgets.*;
22 import org.eclipse.ui.*;
23 import org.eclipse.ui.dialogs.*;
24 import org.eclipse.ui.help.*;
25 /**
26  * HelpSearchPage
27  */

28 public class HelpSearchPage extends DialogPage implements ISearchPage {
29     protected Combo searchWordCombo = null;
30     protected static java.util.List JavaDoc previousSearchQueryData = new java.util.ArrayList JavaDoc(
31             20);
32     protected Button all;
33     protected Button selected;
34     protected Text selectedWorkingSetsText;
35     //private Button headingsButton = null;
36
private ISearchPageContainer scontainer = null;
37     private boolean firstTime = true;
38     // Search query based on the data entered in the UI
39
protected SearchQueryData searchQueryData;
40     protected Button includeDisabledActivities;
41     /**
42      * Search Page
43      */

44     public HelpSearchPage() {
45         super();
46         searchQueryData = new SearchQueryData();
47     }
48     public void createControl(Composite parent) {
49         Composite control = new Composite(parent, SWT.NULL);
50         GridLayout layout = new GridLayout();
51         layout.numColumns = 2;
52         control.setLayout(layout);
53         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
54         control.setLayoutData(gd);
55         // Search Expression
56
Label expressionLabel = new Label(control, SWT.LEFT);
57         expressionLabel.setText(HelpIdeResources.getString("expression")); //$NON-NLS-1$
58
// Pattern combo
59
searchWordCombo = new Combo(control, SWT.SINGLE | SWT.BORDER);
60         gd = new GridData(GridData.FILL_HORIZONTAL);
61         gd.widthHint = convertWidthInCharsToPixels(30);
62         searchWordCombo.setLayoutData(gd);
63         // Not done here to prevent page from resizing
64
// fPattern.setItems(getPreviousSearchPatterns());
65
searchWordCombo.addSelectionListener(new SelectionAdapter() {
66             public void widgetSelected(SelectionEvent e) {
67                 if (searchWordCombo.getSelectionIndex() < 0)
68                     return;
69                 int index = previousSearchQueryData.size() - 1
70                         - searchWordCombo.getSelectionIndex();
71                 searchQueryData = (SearchQueryData) previousSearchQueryData
72                         .get(index);
73                 searchWordCombo.setText(searchQueryData.getSearchWord());
74                 all.setSelection(!searchQueryData.isBookFiltering());
75                 selected.setSelection(searchQueryData.isBookFiltering());
76                 includeDisabledActivities.setSelection(!searchQueryData
77                         .isActivityFiltering());
78                 displaySelectedBooks();
79                 //headingsButton.setSelection(searchOperation.getQueryData().isFieldsSearch());
80
}
81         });
82         searchWordCombo.addModifyListener(new ModifyListener() {
83             public void modifyText(ModifyEvent e) {
84                 updateSearchButtonState();
85             }
86         });
87         // Space
88
new Label(control, SWT.NONE);
89         // Syntax description
90
Label label = new Label(control, SWT.LEFT);
91         gd = new GridData(GridData.FILL_HORIZONTAL);
92         gd.horizontalSpan = 1;
93         label.setLayoutData(gd);
94         label.setText(HelpIdeResources.getString("expression_label")); //$NON-NLS-1$
95
// Headings only button
96
// headingsButton = new Button(control, SWT.CHECK);
97
// gd = new GridData();
98
// gd.horizontalAlignment = gd.BEGINNING;
99
// gd = new GridData();
100
// gd.verticalAlignment = gd.VERTICAL_ALIGN_BEGINNING;
101
// headingsButton.setLayoutData(gd);
102
// headingsButton.setFont(font);
103
// headingsButton.setText(WorkbenchResources.getString("Search_headers_only"));
104
// Filtering group
105
Group filteringGroup = new Group(control, SWT.NONE);
106         filteringGroup.setLayout(layout);
107         gd = new GridData(GridData.FILL_BOTH);
108         gd.horizontalSpan = 2;
109         gd.grabExcessHorizontalSpace = true;
110         gd.grabExcessVerticalSpace = true;
111         filteringGroup.setLayoutData(gd);
112         filteringGroup.setText(HelpIdeResources.getString("limit_to")); //$NON-NLS-1$
113
layout = new GridLayout();
114         layout.numColumns = 3;
115         filteringGroup.setLayout(layout);
116         //
117
all = new Button(filteringGroup, SWT.RADIO);
118         all.setSelection(!searchQueryData.isBookFiltering());
119         all.setText(HelpIdeResources.getString("HelpSearchPage.allBooks")); //$NON-NLS-1$
120
gd = new GridData();
121         gd.horizontalSpan = 3;
122         all.setLayoutData(gd);
123         all.addSelectionListener(new SelectionAdapter() {
124             public void widgetSelected(SelectionEvent e) {
125                 searchQueryData.setBookFiltering(false);
126             }
127         });
128         //
129
selected = new Button(filteringGroup, SWT.RADIO);
130         selected.setSelection(searchQueryData.isBookFiltering());
131         selected.setText(HelpIdeResources
132                 .getString("HelpSearchPage.selectedBooks")); //$NON-NLS-1$
133
selected.addSelectionListener(new SelectionAdapter() {
134             public void widgetSelected(SelectionEvent e) {
135                 searchQueryData.setBookFiltering(true);
136                 updateSearchButtonState();
137             }
138         });
139         //
140
selectedWorkingSetsText = new Text(filteringGroup, SWT.SINGLE
141                 | SWT.BORDER | SWT.READ_ONLY);
142         displaySelectedBooks();
143         //
144
Button chooseWorkingSet = new Button(filteringGroup, SWT.PUSH);
145         org.eclipse.jface.dialogs.Dialog.applyDialogFont(chooseWorkingSet);
146         setButtonLayoutData(chooseWorkingSet);
147         chooseWorkingSet.setText(HelpIdeResources
148                 .getString("HelpSearchPage.choose")); //$NON-NLS-1$
149
SWTUtil.setButtonDimensionHint(chooseWorkingSet);
150         gd = new GridData(GridData.FILL_HORIZONTAL);
151         gd.horizontalIndent = 8;
152         gd.widthHint = SWTUtil.convertWidthInCharsToPixels(30,
153                 selectedWorkingSetsText);
154         selectedWorkingSetsText.setLayoutData(gd);
155         chooseWorkingSet.addSelectionListener(new SelectionAdapter() {
156             public void widgetSelected(SelectionEvent e) {
157                 IWorkingSetSelectionDialog dialog = PlatformUI.getWorkbench()
158                         .getWorkingSetManager()
159                         .createWorkingSetSelectionDialog(selected.getShell(),
160                                 false);
161                 // no multiple selection
162
if (dialog.open() == Window.OK) {
163                     all.setSelection(false);
164                     selected.setSelection(true);
165                     searchQueryData.setBookFiltering(true);
166                     searchQueryData.setSelectedWorkingSets(dialog
167                             .getSelection());
168                     displaySelectedBooks();
169                 }
170             }
171         });
172         includeDisabledActivities = new Button(filteringGroup, SWT.CHECK);
173         gd = new GridData();
174         gd.horizontalSpan = 3;
175         includeDisabledActivities.setLayoutData(gd);
176         includeDisabledActivities.setText(HelpIdeResources
177                 .getString("searchDisabledActivities")); //$NON-NLS-1$
178
includeDisabledActivities.addSelectionListener(new SelectionListener() {
179             public void widgetSelected(SelectionEvent e) {
180                 searchQueryData.setActivityFiltering(!includeDisabledActivities
181                         .getSelection());
182             }
183             public void widgetDefaultSelected(SelectionEvent e) {
184                 widgetSelected(e);
185             }
186         });
187         org.eclipse.jface.dialogs.Dialog.applyDialogFont(control);
188         setControl(control);
189         WorkbenchHelp.setHelp(control, IHelpIdeConstants.SEARCH_PAGE);
190     }
191     /**
192      * @see ISearchPage#performAction()
193      */

194     public boolean performAction() {
195         searchQueryData.setSearchWord(searchWordCombo.getText());
196         searchQueryData.setFieldsSearch(false);
197         // Save working set
198
Preferences prefs = HelpBasePlugin.getDefault().getPluginPreferences();
199         String JavaDoc lastWS = prefs.getString(BaseHelpSystem.WORKING_SET);
200         IWorkingSet[] iws = searchQueryData.getSelectedWorkingSets();
201         if (searchQueryData.isBookFiltering() && iws != null && iws.length > 0
202                 && !iws[0].getName().equals(lastWS)) {
203             prefs.setValue(BaseHelpSystem.WORKING_SET, iws[0].getName());
204             HelpBasePlugin.getDefault().savePluginPreferences();
205         } else if (!searchQueryData.isBookFiltering()
206                 && (lastWS != null || lastWS.length() > 0)) {
207             prefs.setValue(BaseHelpSystem.WORKING_SET, ""); //$NON-NLS-1$
208
HelpBasePlugin.getDefault().savePluginPreferences();
209         }
210         if (!previousSearchQueryData.contains(searchQueryData))
211             previousSearchQueryData.add(searchQueryData);
212         //
213
ISearchQuery searchUIQuery = new HelpSearchQuery(searchQueryData);
214         NewSearchUI.activateSearchResultView();
215         NewSearchUI.runQuery(searchUIQuery);
216         return true;
217     }
218     public void setContainer(ISearchPageContainer container) {
219         scontainer = container;
220     }
221     /*
222      * Implements method from IDialogPage
223      */

224     public void setVisible(boolean visible) {
225         if (visible && searchWordCombo != null) {
226             if (firstTime) {
227                 firstTime = false;
228                 // Set item and text here to prevent page from resizing
229
String JavaDoc[] patterns = new String JavaDoc[previousSearchQueryData.size()];
230                 for (int i = 0; i < previousSearchQueryData.size(); i++) {
231                     patterns[previousSearchQueryData.size() - 1 - i] = ((SearchQueryData) previousSearchQueryData
232                             .get(i)).getSearchWord();
233                 }
234                 searchWordCombo.setItems(patterns);
235             }
236             searchWordCombo.setFocus();
237             updateSearchButtonState();
238         }
239         super.setVisible(visible);
240     }
241     /**
242      * Lists selected books in the selection field. If filtering dissabled or
243      * all books are selected, it will display "All"
244      */

245     protected void displaySelectedBooks() {
246         String JavaDoc workingSetNames = ""; //$NON-NLS-1$
247
if (searchQueryData.isBookFiltering()) {
248             IWorkingSet[] workingSets = searchQueryData
249                     .getSelectedWorkingSets();
250             for (int i = 0; i < workingSets.length; i++) {
251                 String JavaDoc workingSet = workingSets[i].getName();
252                 if (workingSetNames.length() <= 0)
253                     workingSetNames = workingSet;
254                 else
255                     workingSetNames += HelpIdeResources
256                             .getString("HelpSearchPage.bookLabelSeparator") //$NON-NLS-1$
257
+ workingSet;
258             }
259         } else {
260             workingSetNames = ""; //$NON-NLS-1$
261
}
262         selectedWorkingSetsText.setText(workingSetNames);
263         updateSearchButtonState();
264     }
265     protected void updateSearchButtonState() {
266         boolean searchWordValid = searchWordCombo.getText() != null
267                 && searchWordCombo.getText().trim().length() > 0;
268         boolean workingSetValid = selectedWorkingSetsText.getText() != null
269                 && selectedWorkingSetsText.getText().length() > 0;
270         scontainer.setPerformActionEnabled(searchWordValid
271                 && (all.getSelection() || workingSetValid));
272     }
273 }
Popular Tags