1 11 package org.eclipse.search2.internal.ui; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 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 fInput; 72 private final List 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 fCurrentList; 87 private final List fCurrentRemoves; 88 89 public HistoryConfigurationDialog(Shell parent, List currentList, List 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 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 133 protected void createButtonsForButtonBar(Composite parent) { 134 createButton(parent, DEFAULT_ID, SearchMessages.SearchHistorySelectionDialog_restore_default_button, false); 135 super.createButtonsForButtonBar(parent); 136 } 137 138 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 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 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 (), null); 166 } 167 updateStatus(status); 168 return !status.matches(IStatus.ERROR); 169 } 170 171 174 protected void okPressed() { 175 IPreferenceStore store= SearchPlugin.getDefault().getPreferenceStore(); 176 store.setValue(SearchPreferencePage.LIMIT_HISTORY, fHistorySize); 177 178 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 fImages= new ArrayList (); 191 192 public String getText(Object element) { 193 return ((ISearchResult)element).getLabel(); 194 } 195 196 public Image getImage(Object 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 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 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 (); 224 setHelpAvailable(false); 225 } 226 227 230 public boolean isOpenInNewView() { 231 return fIsOpenInNewView; 232 } 233 234 237 protected IDialogSettings getDialogBoundsSettings() { 238 return SearchPlugin.getDefault().getDialogSettingsSection("DialogBounds_SearchHistorySelectionDialog"); } 240 241 244 protected int getDialogBoundsStrategy() { 245 return DIALOG_PERSISTSIZE; 246 } 247 248 249 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 label.setLayoutData(gd); 266 267 268 applyDialogFont(label); 269 return label; 270 } 271 272 273 274 277 public void create() { 278 super.create(); 279 280 List initialSelection= getInitialElementSelections(); 281 if (initialSelection != null) 282 fViewer.setSelection(new StructuredSelection(initialSelection)); 283 284 validateDialogState(); 285 } 286 287 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 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 (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 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 searchResults= selection.iterator(); 392 while (searchResults.hasNext()) { 393 Object 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 413 protected void okPressed() { 414 ISelection selection= fViewer.getSelection(); 416 if (selection instanceof IStructuredSelection) 417 setResult(((IStructuredSelection) fViewer.getSelection()).toList()); 418 419 for (Iterator iter= fRemovedEntries.iterator(); iter.hasNext();) { 421 ISearchResult result= (ISearchResult) iter.next(); 422 ISearchQuery query= result.getQuery(); 423 if (query != null) { InternalSearchUI.getInstance().removeQuery(query); 425 } 426 } 427 super.okPressed(); 428 } 429 } 430 431 432 | Popular Tags |