KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > SearchHistorySelectionDialog


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.search2.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.ModifyEvent;
22 import org.eclipse.swt.events.ModifyListener;
23 import org.eclipse.swt.events.MouseAdapter;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Link;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Table;
37 import org.eclipse.swt.widgets.Text;
38
39 import org.eclipse.jface.dialogs.IDialogConstants;
40 import org.eclipse.jface.dialogs.IDialogSettings;
41 import org.eclipse.jface.dialogs.StatusDialog;
42 import org.eclipse.jface.preference.IPreferenceStore;
43 import org.eclipse.jface.resource.ImageDescriptor;
44 import org.eclipse.jface.viewers.ArrayContentProvider;
45 import org.eclipse.jface.viewers.ISelection;
46 import org.eclipse.jface.viewers.ISelectionChangedListener;
47 import org.eclipse.jface.viewers.IStructuredSelection;
48 import org.eclipse.jface.viewers.LabelProvider;
49 import org.eclipse.jface.viewers.SelectionChangedEvent;
50 import org.eclipse.jface.viewers.StructuredSelection;
51 import org.eclipse.jface.viewers.TableViewer;
52 import org.eclipse.jface.window.Window;
53
54 import org.eclipse.ui.dialogs.SelectionDialog;
55
56 import org.eclipse.search.ui.ISearchQuery;
57 import org.eclipse.search.ui.ISearchResult;
58
59 import org.eclipse.search.internal.ui.SearchPlugin;
60 import org.eclipse.search.internal.ui.SearchPreferencePage;
61 import org.eclipse.search.internal.ui.util.SWTUtil;
62
63 /**
64  * Dialog that shows a list of items with icon and label.
65  */

66 public class SearchHistorySelectionDialog extends SelectionDialog {
67     
68     private static final int REMOVE_ID= IDialogConstants.CLIENT_ID+1;
69     private static final int WIDTH_IN_CHARACTERS= 55;
70     
71     private List JavaDoc fInput;
72     private final List JavaDoc fRemovedEntries;
73     
74     private TableViewer fViewer;
75     private Button fRemoveButton;
76     
77     private boolean fIsOpenInNewView;
78     private Link fLink;
79     
80     private static class HistoryConfigurationDialog extends StatusDialog {
81         
82         private static final int DEFAULT_ID= 100;
83         
84         private int fHistorySize;
85         private Text fHistorySizeTextField;
86         private final List JavaDoc fCurrentList;
87         private final List JavaDoc fCurrentRemoves;
88         
89         public HistoryConfigurationDialog(Shell parent, List JavaDoc currentList, List JavaDoc removedEntries) {
90             super(parent);
91             fCurrentList= currentList;
92             fCurrentRemoves= removedEntries;
93             setShellStyle(getShellStyle() | SWT.RESIZE);
94             setTitle(SearchMessages.SearchHistorySelectionDialog_history_size_title);
95             fHistorySize= SearchPreferencePage.getHistoryLimit();
96             setHelpAvailable(false);
97         }
98         
99         /*
100          * Overrides method from Dialog
101          */

102         protected Control createDialogArea(Composite container) {
103             Composite ancestor= (Composite) super.createDialogArea(container);
104             GridLayout layout= (GridLayout) ancestor.getLayout();
105             layout.numColumns= 2;
106             ancestor.setLayout(layout);
107                         
108             Label limitText= new Label(ancestor, SWT.NONE);
109             limitText.setText(SearchMessages.SearchHistorySelectionDialog_history_size_description);
110             limitText.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
111             
112             fHistorySizeTextField= new Text(ancestor, SWT.BORDER | SWT.RIGHT);
113             fHistorySizeTextField.setTextLimit(2);
114             fHistorySizeTextField.setText(String.valueOf(fHistorySize));
115             fHistorySizeTextField.addModifyListener(new ModifyListener() {
116                 public void modifyText(ModifyEvent e) {
117                     validateDialogState();
118                 }
119             });
120                     
121             GridData gridData= new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
122             gridData.widthHint= convertWidthInCharsToPixels(6);
123             fHistorySizeTextField.setLayoutData(gridData);
124             fHistorySizeTextField.setSelection(0, fHistorySizeTextField.getText().length());
125             applyDialogFont(ancestor);
126
127             return ancestor;
128         }
129         
130         /* (non-Javadoc)
131          * @see org.eclipse.jface.dialogs.StatusDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
132          */

133         protected void createButtonsForButtonBar(Composite parent) {
134             createButton(parent, DEFAULT_ID, SearchMessages.SearchHistorySelectionDialog_restore_default_button, false);
135             super.createButtonsForButtonBar(parent);
136         }
137         
138         /* (non-Javadoc)
139          * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
140          */

141         protected void buttonPressed(int buttonId) {
142             if (buttonId == DEFAULT_ID) {
143                 IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
144                 fHistorySizeTextField.setText(store.getDefaultString(SearchPreferencePage.LIMIT_HISTORY));
145                 validateDialogState();
146             }
147             super.buttonPressed(buttonId);
148         }
149         
150
151         protected final boolean validateDialogState() {
152             IStatus status= null;
153             try {
154                 String JavaDoc historySize= fHistorySizeTextField.getText();
155                 int size= Integer.parseInt(historySize);
156                 if (size < 1) {
157                     status= new Status(IStatus.ERROR, SearchPlugin.getID(), IStatus.ERROR, SearchMessages.SearchHistorySelectionDialog_history_size_error, null);
158                 } else {
159                     fHistorySize= size;
160                 }
161             } catch (NumberFormatException JavaDoc e) {
162                 status= new Status(IStatus.ERROR, SearchPlugin.getID(), IStatus.ERROR, SearchMessages.SearchHistorySelectionDialog_history_size_error, null);
163             }
164             if (status == null) {
165                 status= new Status(IStatus.OK, SearchPlugin.getID(), IStatus.OK, new String JavaDoc(), null);
166             }
167             updateStatus(status);
168             return !status.matches(IStatus.ERROR);
169         }
170         
171         /* (non-Javadoc)
172          * @see org.eclipse.jface.dialogs.Dialog#okPressed()
173          */

174         protected void okPressed() {
175             IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore();
176             store.setValue(SearchPreferencePage.LIMIT_HISTORY, fHistorySize);
177             
178             // establish history size
179
for (int i= fCurrentList.size() - 1; i >= fHistorySize; i--) {
180                 fCurrentRemoves.add(fCurrentList.get(i));
181                 fCurrentList.remove(i);
182             }
183             super.okPressed();
184         }
185         
186     }
187     
188     private static final class SearchesLabelProvider extends LabelProvider {
189         
190         private ArrayList JavaDoc fImages= new ArrayList JavaDoc();
191         
192         public String JavaDoc getText(Object JavaDoc element) {
193             return ((ISearchResult)element).getLabel();
194         }
195         
196         public Image getImage(Object JavaDoc element) {
197
198             ImageDescriptor imageDescriptor= ((ISearchResult)element).getImageDescriptor();
199             if (imageDescriptor == null)
200                 return null;
201             
202             Image image= imageDescriptor.createImage();
203             fImages.add(image);
204
205             return image;
206         }
207         
208         public void dispose() {
209             Iterator JavaDoc iter= fImages.iterator();
210             while (iter.hasNext())
211                 ((Image)iter.next()).dispose();
212             
213             fImages= null;
214         }
215     }
216
217     public SearchHistorySelectionDialog(Shell parent, List JavaDoc input) {
218         super(parent);
219         setShellStyle(getShellStyle() | SWT.RESIZE);
220         setTitle(SearchMessages.SearchesDialog_title);
221         setMessage(SearchMessages.SearchesDialog_message);
222         fInput= input;
223         fRemovedEntries= new ArrayList JavaDoc();
224         setHelpAvailable(false);
225     }
226     
227     /**
228      * @return the isOpenInNewView
229      */

230     public boolean isOpenInNewView() {
231         return fIsOpenInNewView;
232     }
233     
234     /* (non-Javadoc)
235      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
236      */

237     protected IDialogSettings getDialogBoundsSettings() {
238         return SearchPlugin.getDefault().getDialogSettingsSection("DialogBounds_SearchHistorySelectionDialog"); //$NON-NLS-1$
239
}
240     
241     /* (non-Javadoc)
242      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsStrategy()
243      */

244     protected int getDialogBoundsStrategy() {
245         return DIALOG_PERSISTSIZE;
246     }
247     
248     
249     /*
250      * Overrides method from Dialog
251      */

252     protected Label createMessageArea(Composite composite) {
253         Composite parent= new Composite(composite, SWT.NONE);
254         GridLayout layout= new GridLayout();
255         layout.numColumns= 2;
256         layout.marginHeight= 0;
257         layout.marginWidth= 0;
258         parent.setLayout(layout);
259         parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
260         
261         Label label = new Label(parent,SWT.WRAP);
262         label.setText(getMessage());
263         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
264         //gd.widthHint= convertWidthInCharsToPixels(WIDTH_IN_CHARACTERS);
265
label.setLayoutData(gd);
266         
267
268         applyDialogFont(label);
269         return label;
270     }
271     
272     
273     
274     /* (non-Javadoc)
275      * @see org.eclipse.jface.window.Window#create()
276      */

277     public void create() {
278         super.create();
279         
280         List JavaDoc initialSelection= getInitialElementSelections();
281         if (initialSelection != null)
282             fViewer.setSelection(new StructuredSelection(initialSelection));
283
284         validateDialogState();
285     }
286     
287     /*
288      * Overrides method from Dialog
289      */

290     protected Control createDialogArea(Composite container) {
291         Composite ancestor= (Composite) super.createDialogArea(container);
292         
293         createMessageArea(ancestor);
294         
295         Composite parent= new Composite(ancestor, SWT.NONE);
296         
297         GridLayout layout= new GridLayout();
298         layout.numColumns= 2;
299         layout.marginHeight= 0;
300         layout.marginWidth= 0;
301         parent.setLayout(layout);
302         parent.setLayoutData(new GridData(GridData.FILL_BOTH));
303
304         fViewer= new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);
305         fViewer.setContentProvider(new ArrayContentProvider());
306         
307         final Table table= fViewer.getTable();
308         table.addMouseListener(new MouseAdapter() {
309             public void mouseDoubleClick(MouseEvent e) {
310                 okPressed();
311             }
312         });
313         fViewer.setLabelProvider(new SearchesLabelProvider());
314         GridData gd= new GridData(GridData.FILL_BOTH);
315         gd.heightHint= convertHeightInCharsToPixels(15);
316         gd.widthHint= convertWidthInCharsToPixels(WIDTH_IN_CHARACTERS);
317         table.setLayoutData(gd);
318         
319         
320         fRemoveButton= new Button(parent, SWT.PUSH);
321         fRemoveButton.setText(SearchMessages.SearchesDialog_remove_label);
322         fRemoveButton.addSelectionListener(new SelectionAdapter() {
323             public void widgetSelected(SelectionEvent event) {
324                 buttonPressed(REMOVE_ID);
325             }
326         });
327         fRemoveButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false));
328         SWTUtil.setButtonDimensionHint(fRemoveButton);
329         
330         fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
331             public void selectionChanged(SelectionChangedEvent event) {
332                 validateDialogState();
333             }
334         });
335         
336         fLink= new Link(parent, SWT.NONE);
337         configureHistoryLink();
338         fLink.addSelectionListener(new SelectionAdapter() {
339             public void widgetSelected(SelectionEvent e) {
340                 HistoryConfigurationDialog dialog= new HistoryConfigurationDialog(getShell(), fInput, fRemovedEntries);
341                 if (dialog.open() == Window.OK) {
342                     fViewer.refresh();
343                     configureHistoryLink();
344                 }
345             }
346         });
347         fLink.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
348         
349         
350         applyDialogFont(ancestor);
351
352         // set input & selections last, so all the widgets are created.
353
fViewer.setInput(fInput);
354         fViewer.getTable().setFocus();
355         return ancestor;
356     }
357
358     private void configureHistoryLink() {
359         int historyLimit= SearchPreferencePage.getHistoryLimit();
360         fLink.setText(Messages.format(SearchMessages.SearchHistorySelectionDialog_configure_link_label, new Integer JavaDoc(historyLimit)));
361     }
362     
363     protected final void validateDialogState() {
364         IStructuredSelection sel= (IStructuredSelection) fViewer.getSelection();
365         int elementsSelected= sel.toList().size();
366         
367         fRemoveButton.setEnabled(elementsSelected > 0);
368         Button okButton= getOkButton();
369         if (okButton != null) {
370             okButton.setEnabled(elementsSelected == 1);
371         }
372         Button openInNewButton= getButton(IDialogConstants.OPEN_ID);
373         if (openInNewButton != null) {
374             openInNewButton.setEnabled(elementsSelected == 1);
375         }
376     }
377     
378     /* (non-Javadoc)
379      * @see org.eclipse.ui.dialogs.SelectionDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
380      */

381     protected void createButtonsForButtonBar(Composite parent) {
382         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OPEN_LABEL, true);
383         createButton(parent, IDialogConstants.OPEN_ID, SearchMessages.SearchHistorySelectionDialog_open_in_new_button, false);
384         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
385     }
386     
387     
388     protected void buttonPressed(int buttonId) {
389         if (buttonId == REMOVE_ID) {
390             IStructuredSelection selection= (IStructuredSelection) fViewer.getSelection();
391             Iterator JavaDoc searchResults= selection.iterator();
392             while (searchResults.hasNext()) {
393                 Object JavaDoc curr= searchResults.next();
394                 fRemovedEntries.add(curr);
395                 fInput.remove(curr);
396                 fViewer.remove(curr);
397             }
398             if (fViewer.getSelection().isEmpty() && !fInput.isEmpty()) {
399                 fViewer.setSelection(new StructuredSelection(fInput.get(0)));
400             }
401             return;
402         }
403         if (buttonId == IDialogConstants.OPEN_ID) {
404             fIsOpenInNewView= true;
405             buttonId= IDialogConstants.OK_ID;
406         }
407         super.buttonPressed(buttonId);
408     }
409         
410     /*
411      * Overrides method from Dialog
412      */

413     protected void okPressed() {
414         // Build a list of selected children.
415
ISelection selection= fViewer.getSelection();
416         if (selection instanceof IStructuredSelection)
417             setResult(((IStructuredSelection) fViewer.getSelection()).toList());
418         
419         // remove queries
420
for (Iterator JavaDoc iter= fRemovedEntries.iterator(); iter.hasNext();) {
421             ISearchResult result= (ISearchResult) iter.next();
422             ISearchQuery query= result.getQuery();
423             if (query != null) { // must not be null: invalid implementation of a search query
424
InternalSearchUI.getInstance().removeQuery(query);
425             }
426         }
427         super.okPressed();
428     }
429 }
430
431
432
Popular Tags