1 11 package org.eclipse.search.internal.ui; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.accessibility.AccessibleAdapter; 23 import org.eclipse.swt.accessibility.AccessibleEvent; 24 import org.eclipse.swt.events.SelectionAdapter; 25 import org.eclipse.swt.events.SelectionEvent; 26 import org.eclipse.swt.layout.GridData; 27 import org.eclipse.swt.layout.GridLayout; 28 import org.eclipse.swt.widgets.Button; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Group; 31 import org.eclipse.swt.widgets.Text; 32 33 import org.eclipse.jface.dialogs.IDialogSettings; 34 import org.eclipse.jface.viewers.ISelection; 35 import org.eclipse.jface.viewers.IStructuredSelection; 36 import org.eclipse.jface.window.Window; 37 38 import org.eclipse.ui.IWorkbenchPage; 39 import org.eclipse.ui.IWorkingSet; 40 import org.eclipse.ui.IWorkingSetManager; 41 import org.eclipse.ui.PlatformUI; 42 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog; 43 44 import org.eclipse.search.ui.ISearchPageContainer; 45 import org.eclipse.search.ui.NewSearchUI; 46 47 import org.eclipse.search.internal.ui.util.PixelConverter; 48 import org.eclipse.search.internal.ui.util.SWTUtil; 49 50 public class ScopePart { 51 52 private static final String DIALOG_SETTINGS_KEY= "SearchDialog.ScopePart"; private static final String STORE_SCOPE= "scope"; private static final String STORE_LRU_WORKING_SET_NAME= "lastUsedWorkingSetName"; private static final String STORE_LRU_WORKING_SET_NAMES= "lastUsedWorkingSetNames"; 58 private IDialogSettings fSettingsStore; 59 private Group fPart; 60 61 private Button fUseWorkspace; 63 private Button fUseSelection; 64 private Button fUseProject; 65 private Button fUseWorkingSet; 66 67 private int fScope; 68 private boolean fCanSearchEnclosingProjects; 69 private Text fWorkingSetText; 70 private IWorkingSet[] fWorkingSets; 71 72 private SearchDialog fSearchDialog; 74 75 81 public ScopePart(SearchDialog searchDialog, boolean searchEnclosingProjects) { 82 fSearchDialog= searchDialog; 83 fCanSearchEnclosingProjects= searchEnclosingProjects; 84 85 fSettingsStore= SearchPlugin.getDefault().getDialogSettingsSection(DIALOG_SETTINGS_KEY); 86 fScope= getStoredScope(fSettingsStore, searchEnclosingProjects); 87 88 fWorkingSets= getStoredWorkingSets(); 89 } 90 91 private static int getStoredScope(IDialogSettings settingsStore, boolean canSearchEnclosingProjects) { 92 int scope; 93 try { 94 scope= settingsStore.getInt(STORE_SCOPE); 95 } catch (NumberFormatException ex) { 96 scope= ISearchPageContainer.WORKSPACE_SCOPE; 97 } 98 if (scope != ISearchPageContainer.WORKING_SET_SCOPE 99 && scope != ISearchPageContainer.SELECTION_SCOPE 100 && scope != ISearchPageContainer.SELECTED_PROJECTS_SCOPE 101 && scope != ISearchPageContainer.WORKSPACE_SCOPE) 102 scope= ISearchPageContainer.WORKSPACE_SCOPE; 103 104 if (!canSearchEnclosingProjects && scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE) 105 scope= ISearchPageContainer.WORKSPACE_SCOPE; 106 107 return scope; 108 } 109 110 private IWorkingSet getWorkingSet(IWorkingSetManager workingSetManager, String storedName) { 111 if (storedName.length() == 0) { 112 IWorkbenchPage page= fSearchDialog.getWorkbenchWindow().getActivePage(); 113 if (page != null) { 114 return page.getAggregateWorkingSet(); 115 } 116 return null; 117 } 118 return workingSetManager.getWorkingSet(storedName); 119 } 120 121 122 private IWorkingSet[] getStoredWorkingSets() { 123 String [] lruWorkingSetNames= fSettingsStore.getArray(STORE_LRU_WORKING_SET_NAMES); 124 125 IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager(); 126 if (lruWorkingSetNames != null) { 127 Set existingWorkingSets= new HashSet (lruWorkingSetNames.length); 128 for (int i= 0; i < lruWorkingSetNames.length; i++) { 129 IWorkingSet workingSet= getWorkingSet(workingSetManager, lruWorkingSetNames[i]); 130 if (workingSet != null) { 131 existingWorkingSets.add(workingSet); 132 } 133 } 134 if (!existingWorkingSets.isEmpty()) { 135 return (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]); 136 } 137 } else { 138 String workingSetName= fSettingsStore.get(STORE_LRU_WORKING_SET_NAME); 140 if (workingSetName != null) { 141 IWorkingSet workingSet= getWorkingSet(workingSetManager, workingSetName); 142 if (workingSet != null) { 143 return new IWorkingSet[] { workingSet }; 144 } 145 } 146 } 147 return null; 148 } 149 150 151 156 public int getSelectedScope() { 157 return fScope; 158 } 159 160 166 public void setSelectedScope(int scope) { 167 Assert.isLegal(scope >= 0 && scope <= 3); 168 Assert.isNotNull(fUseWorkspace); 169 Assert.isNotNull(fUseSelection); 170 Assert.isNotNull(fUseWorkingSet); 171 Assert.isNotNull(fUseProject); 172 173 if (scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE) { 174 if (!fCanSearchEnclosingProjects) { 175 SearchPlugin.log(new Status(IStatus.WARNING, NewSearchUI.PLUGIN_ID, IStatus.WARNING, "Enclosing projects scope set on search page that does not support it", null)); scope= ISearchPageContainer.WORKSPACE_SCOPE; 177 } else if (!fUseProject.isEnabled()) { 178 scope= ISearchPageContainer.WORKSPACE_SCOPE; 179 } 180 } else if (scope == ISearchPageContainer.SELECTION_SCOPE) { 181 if (!fUseSelection.isEnabled()) { 182 scope= ISearchPageContainer.WORKSPACE_SCOPE; 183 } 184 } 185 fScope= scope; 186 187 fUseWorkspace.setSelection(scope == ISearchPageContainer.WORKSPACE_SCOPE); 188 fUseSelection.setSelection(scope == ISearchPageContainer.SELECTION_SCOPE); 189 fUseProject.setSelection(scope == ISearchPageContainer.SELECTED_PROJECTS_SCOPE); 190 fUseWorkingSet.setSelection(scope == ISearchPageContainer.WORKING_SET_SCOPE); 191 192 updateSearchPageContainerActionPerformedEnablement(); 193 fSettingsStore.put(STORE_SCOPE, scope); 194 195 } 196 197 private void updateSearchPageContainerActionPerformedEnablement() { 198 fSearchDialog.notifyScopeSelectionChanged(); 199 } 200 201 208 public IWorkingSet[] getSelectedWorkingSets() { 209 if (getSelectedScope() == ISearchPageContainer.WORKING_SET_SCOPE) 210 return fWorkingSets; 211 return null; 212 } 213 214 220 public void setSelectedWorkingSets(IWorkingSet[] workingSets) { 221 Assert.isNotNull(workingSets); 222 setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE); 223 fWorkingSets= null; 224 Set existingWorkingSets= new HashSet (workingSets.length); 225 for (int i= 0; i < workingSets.length; i++) { 226 String name= workingSets[i].getName(); 227 IWorkingSet workingSet= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name); 228 if (workingSet != null) 229 existingWorkingSets.add(workingSet); 230 } 231 if (!existingWorkingSets.isEmpty()) 232 fWorkingSets= (IWorkingSet[]) existingWorkingSets.toArray(new IWorkingSet[existingWorkingSets.size()]); 233 234 saveState(); 235 236 if (fWorkingSetText != null) 237 fWorkingSetText.setText(toString(fWorkingSets)); 238 } 239 240 244 private void saveState() { 245 if (fWorkingSets != null && fWorkingSets.length > 0) { 246 String [] existingWorkingSetNames= new String [fWorkingSets.length]; 247 for (int i= 0; i < fWorkingSets.length; i++) { 248 IWorkingSet curr= fWorkingSets[i]; 249 existingWorkingSetNames[i]= curr.isAggregateWorkingSet() ? "" : curr.getName(); } 252 fSettingsStore.put(STORE_LRU_WORKING_SET_NAMES, existingWorkingSetNames); 253 } 254 } 255 256 262 public Composite createPart(Composite parent) { 263 fPart= new Group(parent, SWT.NONE); 264 fPart.setText(SearchMessages.ScopePart_group_text); 265 266 GridLayout layout= new GridLayout(); 267 layout.numColumns= 4; 268 fPart.setLayout(layout); 269 fPart.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 270 271 fUseWorkspace= new Button(fPart, SWT.RADIO); 272 fUseWorkspace.setData(new Integer (ISearchPageContainer.WORKSPACE_SCOPE)); 273 fUseWorkspace.setText(SearchMessages.ScopePart_workspaceScope_text); 274 275 fUseSelection= new Button(fPart, SWT.RADIO); 276 fUseSelection.setData(new Integer (ISearchPageContainer.SELECTION_SCOPE)); 277 fUseSelection.setText(SearchMessages.ScopePart_selectedResourcesScope_text); 278 279 boolean canSearchInSelection= canSearchInSelection(); 280 fUseSelection.setEnabled(canSearchInSelection); 281 282 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 283 gd.horizontalIndent= 8; 284 fUseSelection.setLayoutData(gd); 285 286 fUseProject= new Button(fPart, SWT.RADIO); 287 fUseProject.setData(new Integer (ISearchPageContainer.SELECTED_PROJECTS_SCOPE)); 288 fUseProject.setText(SearchMessages.ScopePart_enclosingProjectsScope_text); 289 fUseProject.setEnabled(fSearchDialog.getEnclosingProjectNames().length > 0); 290 291 gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 292 gd.horizontalSpan= 2; 293 gd.horizontalIndent= 8; 294 fUseProject.setLayoutData(gd); 295 if (!fCanSearchEnclosingProjects) 296 fUseProject.setVisible(false); 297 298 fUseWorkingSet= new Button(fPart, SWT.RADIO); 299 fUseWorkingSet.setData(new Integer (ISearchPageContainer.WORKING_SET_SCOPE)); 300 fUseWorkingSet.setText(SearchMessages.ScopePart_workingSetScope_text); 301 fWorkingSetText= new Text(fPart, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); 302 fWorkingSetText.getAccessible().addAccessibleListener(new AccessibleAdapter() { 303 public void getName(AccessibleEvent e) { 304 e.result= SearchMessages.ScopePart_workingSetText_accessible_label; 305 } 306 }); 307 308 Button chooseWorkingSet= new Button(fPart, SWT.PUSH); 309 chooseWorkingSet.setLayoutData(new GridData()); 310 chooseWorkingSet.setText(SearchMessages.ScopePart_workingSetChooseButton_text); 311 SWTUtil.setButtonDimensionHint(chooseWorkingSet); 312 chooseWorkingSet.addSelectionListener(new SelectionAdapter() { 313 public void widgetSelected(SelectionEvent e) { 314 if (handleChooseWorkingSet()) { 315 setSelectedScope(ISearchPageContainer.WORKING_SET_SCOPE); 316 } 317 } 318 }); 319 gd= new GridData(GridData.FILL_HORIZONTAL); 320 gd.horizontalIndent= 8; 321 gd.horizontalSpan= 2; 322 gd.widthHint= new PixelConverter(fWorkingSetText).convertWidthInCharsToPixels(30); 323 fWorkingSetText.setLayoutData(gd); 324 325 SelectionAdapter scopeChangedLister= new SelectionAdapter() { 327 public void widgetSelected(SelectionEvent e) { 328 handleScopeChanged(e); 329 } 330 }; 331 fUseWorkspace.addSelectionListener(scopeChangedLister); 332 fUseSelection.addSelectionListener(scopeChangedLister); 333 fUseProject.addSelectionListener(scopeChangedLister); 334 fUseWorkingSet.addSelectionListener(scopeChangedLister); 335 336 setSelectedScope(fScope); 338 339 if (fWorkingSets != null) 341 fWorkingSetText.setText(toString(fWorkingSets)); 342 343 return fPart; 344 } 345 346 347 private boolean canSearchInSelection() { 348 ISelection selection= fSearchDialog.getSelection(); 349 return selection instanceof IStructuredSelection && !selection.isEmpty(); 350 } 351 352 private void handleScopeChanged(SelectionEvent e) { 353 Object source= e.getSource(); 354 if (source instanceof Button) { 355 Button button= (Button) source; 356 if (button.getSelection()) 357 setSelectedScope(((Integer ) button.getData()).intValue()); 358 } 359 } 360 361 private boolean handleChooseWorkingSet() { 362 IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager(); 363 IWorkingSetSelectionDialog dialog= workingSetManager.createWorkingSetSelectionDialog(fUseSelection.getShell(), true); 364 365 if (fWorkingSets != null) 366 dialog.setSelection(fWorkingSets); 367 if (dialog.open() == Window.OK) { 368 Object [] result= dialog.getSelection(); 369 if (result.length > 0) { 370 setSelectedWorkingSets((IWorkingSet[]) result); 371 return true; 372 } 373 fWorkingSetText.setText(""); fWorkingSets= null; 375 if (fScope == ISearchPageContainer.WORKING_SET_SCOPE) 376 setSelectedScope(ISearchPageContainer.WORKSPACE_SCOPE); 377 return false; 378 } 379 if (fWorkingSets != null) { 380 int i= 0; 382 while (i < fWorkingSets.length) { 383 IWorkingSet workingSet= fWorkingSets[i]; 384 if (!workingSet.isAggregateWorkingSet() && workingSetManager.getWorkingSet(workingSet.getName()) == null) 385 break; 386 i++; 387 } 388 if (i < fWorkingSets.length) { 389 fWorkingSetText.setText(""); fWorkingSets= null; 391 updateSearchPageContainerActionPerformedEnablement(); 392 } 393 } 394 return false; 395 } 396 397 void setVisible(boolean state) { 398 fPart.setVisible(state); 399 } 400 401 public static String toString(IWorkingSet[] workingSets) { 402 String result= ""; if (workingSets != null && workingSets.length > 0) { 404 Arrays.sort(workingSets, new WorkingSetComparator()); 405 boolean firstFound= false; 406 for (int i= 0; i < workingSets.length; i++) { 407 String workingSetName= workingSets[i].getLabel(); 408 if (firstFound) 409 result= Messages.format(SearchMessages.ScopePart_workingSetConcatenation, new String [] { result, workingSetName }); 410 else { 411 result= workingSetName; 412 firstFound= true; 413 } 414 } 415 } 416 return result; 417 } 418 } 419 | Popular Tags |