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.swt.SWT; 18 import org.eclipse.swt.events.MouseAdapter; 19 import org.eclipse.swt.events.MouseEvent; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Button; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Label; 29 import org.eclipse.swt.widgets.Shell; 30 import org.eclipse.swt.widgets.Table; 31 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.jface.resource.ImageDescriptor; 34 import org.eclipse.jface.viewers.ISelection; 35 import org.eclipse.jface.viewers.ISelectionChangedListener; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 import org.eclipse.jface.viewers.LabelProvider; 38 import org.eclipse.jface.viewers.SelectionChangedEvent; 39 import org.eclipse.jface.viewers.StructuredSelection; 40 import org.eclipse.jface.viewers.TableViewer; 41 42 import org.eclipse.ui.dialogs.SelectionDialog; 43 44 import org.eclipse.search.ui.ISearchResult; 45 46 import org.eclipse.search.internal.ui.util.ListContentProvider; 47 import org.eclipse.search.internal.ui.util.SWTUtil; 48 49 52 public class SearchesDialog extends SelectionDialog { 53 54 private static final int REMOVE_ID= IDialogConstants.CLIENT_ID+1; 55 private static final int WIDTH_IN_CHARACTERS= 55; 56 57 private List fInput; 58 private TableViewer fViewer; 59 private Button fRemoveButton; 60 61 private static final class SearchesLabelProvider extends LabelProvider { 62 63 private ArrayList fImages= new ArrayList (); 64 65 public String getText(Object element) { 66 return ((ISearchResult)element).getLabel(); 67 } 68 69 public Image getImage(Object element) { 70 71 ImageDescriptor imageDescriptor= ((ISearchResult)element).getImageDescriptor(); 72 if (imageDescriptor == null) 73 return null; 74 75 Image image= imageDescriptor.createImage(); 76 fImages.add(image); 77 78 return image; 79 } 80 81 public void dispose() { 82 Iterator iter= fImages.iterator(); 83 while (iter.hasNext()) 84 ((Image)iter.next()).dispose(); 85 86 fImages= null; 87 } 88 } 89 90 public SearchesDialog(Shell parent, List input) { 91 super(parent); 92 setTitle(SearchMessages.SearchesDialog_title); 93 setMessage(SearchMessages.SearchesDialog_message); 94 fInput= input; 95 } 96 97 100 protected Label createMessageArea(Composite composite) { 101 Label label = new Label(composite,SWT.WRAP); 102 label.setText(getMessage()); 103 GridData gd= new GridData(GridData.FILL_BOTH); 104 gd.widthHint= convertWidthInCharsToPixels(WIDTH_IN_CHARACTERS); 105 label.setLayoutData(gd); 106 applyDialogFont(label); 107 return label; 108 } 109 110 111 112 115 public void create() { 116 super.create(); 117 118 List initialSelection= getInitialElementSelections(); 119 if (initialSelection != null) 120 fViewer.setSelection(new StructuredSelection(initialSelection)); 121 122 validateDialogState(); 123 } 124 125 128 protected Control createDialogArea(Composite container) { 129 Composite ancestor= (Composite) super.createDialogArea(container); 130 131 createMessageArea(ancestor); 132 133 Composite parent= new Composite(ancestor, SWT.NONE); 134 135 GridLayout layout= new GridLayout(); 136 layout.numColumns= 2; 137 layout.marginHeight= 0; 138 layout.marginWidth= 0; 139 parent.setLayout(layout); 140 141 142 fViewer= new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER | SWT.FULL_SELECTION); 143 fViewer.setContentProvider(new ListContentProvider()); 144 145 final Table table= fViewer.getTable(); 146 table.addMouseListener(new MouseAdapter() { 147 public void mouseDoubleClick(MouseEvent e) { 148 okPressed(); 149 } 150 }); 151 fViewer.setLabelProvider(new SearchesLabelProvider()); 152 GridData gd= new GridData(GridData.FILL_BOTH); 153 gd.heightHint= convertHeightInCharsToPixels(15); 154 gd.widthHint= convertWidthInCharsToPixels(WIDTH_IN_CHARACTERS); 155 table.setLayoutData(gd); 156 157 158 fRemoveButton= new Button(parent, SWT.PUSH); 159 fRemoveButton.setText(SearchMessages.SearchesDialog_remove_label); 160 fRemoveButton.addSelectionListener(new SelectionAdapter() { 161 public void widgetSelected(SelectionEvent event) { 162 buttonPressed(REMOVE_ID); 163 } 164 }); 165 fRemoveButton.setLayoutData(new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false)); 166 SWTUtil.setButtonDimensionHint(fRemoveButton); 167 168 fViewer.addSelectionChangedListener(new ISelectionChangedListener() { 169 public void selectionChanged(SelectionChangedEvent event) { 170 validateDialogState(); 171 } 172 }); 173 174 applyDialogFont(ancestor); 175 176 fViewer.setInput(fInput); 178 return table; 179 } 180 181 protected final void validateDialogState() { 182 IStructuredSelection sel= (IStructuredSelection) fViewer.getSelection(); 183 int elementsSelected= sel.toList().size(); 184 185 fRemoveButton.setEnabled(elementsSelected > 0); 186 Button okButton= getOkButton(); 187 if (okButton != null) { 188 okButton.setEnabled(elementsSelected == 1); 189 } 190 } 191 192 193 protected void buttonPressed(int buttonId) { 194 if (buttonId == REMOVE_ID) { 195 IStructuredSelection selection= (IStructuredSelection) fViewer.getSelection(); 196 Iterator searchResults= selection.iterator(); 197 while (searchResults.hasNext()) { 198 ISearchResult result= (ISearchResult)searchResults.next(); 199 InternalSearchUI.getInstance().removeQuery(result.getQuery()); 200 fInput.remove(result); 201 fViewer.remove(result); 202 } 203 if (fViewer.getSelection().isEmpty() && !fInput.isEmpty()) { 204 fViewer.setSelection(new StructuredSelection(fInput.get(0))); 205 } 206 return; 207 } 208 super.buttonPressed(buttonId); 209 } 210 211 214 protected void okPressed() { 215 ISelection selection= fViewer.getSelection(); 217 if (selection instanceof IStructuredSelection) 218 setResult(((IStructuredSelection)fViewer.getSelection()).toList()); 219 super.okPressed(); 220 } 221 } 222 223 224 | Popular Tags |