1 11 package org.eclipse.ant.internal.ui.views.actions; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.regex.Matcher ; 18 import java.util.regex.Pattern ; 19 import org.eclipse.ant.internal.ui.AntUIPlugin; 20 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds; 21 import org.eclipse.ant.internal.ui.IAntUIPreferenceConstants; 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IResource; 24 import org.eclipse.core.resources.IResourceProxy; 25 import org.eclipse.core.resources.IResourceProxyVisitor; 26 import org.eclipse.core.resources.ResourcesPlugin; 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.IAdaptable; 29 import org.eclipse.jface.dialogs.IDialogSettings; 30 import org.eclipse.jface.dialogs.IInputValidator; 31 import org.eclipse.jface.dialogs.InputDialog; 32 import org.eclipse.jface.window.Window; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.SelectionAdapter; 35 import org.eclipse.swt.events.SelectionEvent; 36 import org.eclipse.swt.graphics.Font; 37 import org.eclipse.swt.layout.GridData; 38 import org.eclipse.swt.layout.GridLayout; 39 import org.eclipse.swt.widgets.Button; 40 import org.eclipse.swt.widgets.Composite; 41 import org.eclipse.swt.widgets.Control; 42 import org.eclipse.swt.widgets.Display; 43 import org.eclipse.swt.widgets.Group; 44 import org.eclipse.swt.widgets.Shell; 45 import org.eclipse.swt.widgets.Text; 46 import org.eclipse.ui.IWorkingSet; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog; 49 50 55 public class SearchForBuildFilesDialog extends InputDialog { 56 57 60 private List results = new ArrayList (); 61 68 private List searchScopes = null; 69 72 private Button workingSetScopeButton; 73 76 private Button workspaceScopeButton; 77 80 private Text workingSetText; 81 85 private Button includeErrorResultButton; 86 89 private static IDialogSettings settings= AntUIPlugin.getDefault().getDialogSettings(); 90 91 94 static { 95 if (settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING) == null) { 96 settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING, "build.xml"); } 98 if (settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE) == null) { 99 settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE, ""); } 101 } 102 103 106 public SearchForBuildFilesDialog() { 107 super(Display.getCurrent().getActiveShell(), AntViewActionMessages.SearchForBuildFilesDialog_Search_for_Build_Files_1, AntViewActionMessages.SearchForBuildFilesDialog__Input, 108 settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING), new IInputValidator() { 109 public String isValid(String newText) { 110 String trimmedText = newText.trim(); 111 if (trimmedText.length() == 0) { 112 return AntViewActionMessages.SearchForBuildFilesDialog_Build_name_cannot_be_empty_3; 113 } 114 return null; 115 } 116 }); 117 } 118 119 122 protected void createButtonsForButtonBar(Composite parent) { 123 super.createButtonsForButtonBar(parent); 124 getOkButton().setText(AntViewActionMessages.SearchForBuildFilesDialog__Search_4); 125 126 String workingSetName= settings.get(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE); 127 if (workingSetName.length() > 0) { 128 setWorkingSet(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(workingSetName)); 129 } 130 if (!settings.getBoolean(IAntUIPreferenceConstants.ANTVIEW_USE_WORKINGSET_SEARCH_SCOPE)) { 131 selectRadioButton(workspaceScopeButton); 132 handleRadioButtonPressed(); 133 } 134 } 135 136 139 protected Control createDialogArea(Composite parent) { 140 Font font = parent.getFont(); 141 142 Composite composite = (Composite) super.createDialogArea(parent); 143 createIncludeErrorResultButton(composite, font); 144 createScopeGroup(composite, font); 145 return composite; 146 } 147 148 private void createScopeGroup(Composite composite, Font font) { 149 Group scope= new Group(composite, SWT.NONE); 150 scope.setText(AntViewActionMessages.SearchForBuildFilesDialog_Scope_5); 151 GridData data= new GridData(GridData.FILL_BOTH); 152 scope.setLayoutData(data); 153 GridLayout layout= new GridLayout(3, false); 154 scope.setLayout(layout); 155 scope.setFont(font); 156 157 Composite radioComposite= new Composite(scope, SWT.NONE); 159 GridLayout radioLayout= new GridLayout(); 160 radioLayout.marginHeight= 0; 161 radioComposite.setLayout(radioLayout); 162 163 SelectionAdapter selectionListener= new SelectionAdapter() { 164 public void widgetSelected(SelectionEvent e) { 165 handleRadioButtonPressed(); 166 } 167 }; 168 169 workspaceScopeButton= new Button(radioComposite, SWT.RADIO); 170 workspaceScopeButton.setFont(font); 171 workspaceScopeButton.setText(AntViewActionMessages.SearchForBuildFilesDialog__Workspace_6); 172 workspaceScopeButton.addSelectionListener(selectionListener); 173 174 workingSetScopeButton=new Button(radioComposite, SWT.RADIO); 175 workingSetScopeButton.setFont(font); 176 workingSetScopeButton.setText(AntViewActionMessages.SearchForBuildFilesDialog_Wor_king_Set__7); 177 workingSetScopeButton.addSelectionListener(selectionListener); 178 179 selectRadioButton(workspaceScopeButton); 180 181 workingSetText= new Text(scope, SWT.BORDER); 182 workingSetText.setEditable(false); 183 data= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_END); 184 workingSetText.setLayoutData(data); 185 workingSetText.setFont(font); 186 187 Button chooseButton = new Button(scope, SWT.PUSH); 188 data= new GridData(GridData.VERTICAL_ALIGN_END); 189 chooseButton.setLayoutData(data); 190 chooseButton.setFont(font); 191 chooseButton.setText(AntViewActionMessages.SearchForBuildFilesDialog__Choose____8); 192 chooseButton.addSelectionListener(new SelectionAdapter() { 193 public void widgetSelected(SelectionEvent evt) { 194 handleChooseButtonPressed(); 195 } 196 }); 197 } 198 199 207 private void selectRadioButton(Button button) { 208 if (button == workingSetScopeButton) { 209 workingSetScopeButton.setSelection(true); 210 workspaceScopeButton.setSelection(false); 211 } else if (button == workspaceScopeButton) { 212 workspaceScopeButton.setSelection(true); 213 workingSetScopeButton.setSelection(false); 214 } 215 } 216 217 221 private void handleRadioButtonPressed() { 222 if (workingSetScopeButton.getSelection()) { 223 IWorkingSet set= PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(getWorkingSetName()); 224 if (set != null) { 225 setWorkingSet(set); 226 return; 227 } 228 } 229 setWorkingSet(null); 230 } 231 232 235 private String getWorkingSetName() { 236 return workingSetText.getText().trim(); 237 } 238 239 243 private void createIncludeErrorResultButton(Composite composite, Font font) { 244 includeErrorResultButton= new Button(composite, SWT.CHECK); 245 includeErrorResultButton.setFont(font); 246 includeErrorResultButton.setText(AntViewActionMessages.SearchForBuildFilesDialog_Include_errors); 247 includeErrorResultButton.setSelection(settings.getBoolean(IAntUIPreferenceConstants.ANTVIEW_INCLUDE_ERROR_SEARCH_RESULTS)); 248 } 249 250 258 private void updateForWorkingSetSettings() { 259 if (workingSetScopeButton.getSelection()) { 260 String error= null; 261 if (searchScopes == null) { 262 error= AntViewActionMessages.SearchForBuildFilesDialog_Must_select_a_working_set_10; 263 } else if (searchScopes.isEmpty()) { 264 error= AntViewActionMessages.SearchForBuildFilesDialog_No_searchable; 265 } 266 if (error != null) { 267 setErrorMessage(error); 268 getOkButton().setEnabled(false); 269 return; 270 } 271 } 272 getOkButton().setEnabled(true); 273 setErrorMessage(null); 274 } 275 276 280 private void handleChooseButtonPressed() { 281 IWorkingSetSelectionDialog dialog= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSetSelectionDialog(getShell(), false); 282 if (dialog.open() == Window.CANCEL) { 283 return; 284 } 285 IWorkingSet[] sets= dialog.getSelection(); 286 if (sets == null) { 287 return; 288 } 289 if (sets.length == 0) { 290 setWorkingSet(null); } else { 292 setWorkingSet(sets[0]); } 294 } 295 296 303 private void setWorkingSet(IWorkingSet set) { 304 if (set == null) { 305 searchScopes= null; 306 workingSetText.setText(""); validateInput(); 308 return; 309 } 310 IAdaptable[] elements= set.getElements(); 311 searchScopes= new ArrayList (); 312 for (int i = 0; i < elements.length; i++) { 313 IResource resource= null; 315 IAdaptable adaptable = elements[i]; 316 if (adaptable instanceof IResource) { 317 resource= (IResource) adaptable; 318 } else { 319 resource= (IResource) adaptable.getAdapter(IResource.class); 320 } 321 if (resource != null) { 322 searchScopes.add(resource); 323 } 324 } 325 workingSetText.setText(set.getName()); 326 selectRadioButton(workingSetScopeButton); 327 328 validateInput(); 329 } 330 331 334 private String getInput() { 335 return getText().getText().trim(); 336 } 337 338 341 public IFile[] getResults() { 342 return (IFile[]) results.toArray(new IFile[results.size()]); 343 } 344 345 349 protected boolean getIncludeErrorResults() { 350 return settings.getBoolean(IAntUIPreferenceConstants.ANTVIEW_INCLUDE_ERROR_SEARCH_RESULTS); 351 } 352 353 357 protected void okPressed() { 358 String input = getInput(); 359 settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_SEARCH_STRING, input); 360 settings.put(IAntUIPreferenceConstants.ANTVIEW_INCLUDE_ERROR_SEARCH_RESULTS, includeErrorResultButton.getSelection()); 361 settings.put(IAntUIPreferenceConstants.ANTVIEW_LAST_WORKINGSET_SEARCH_SCOPE, getWorkingSetName()); 362 settings.put(IAntUIPreferenceConstants.ANTVIEW_USE_WORKINGSET_SEARCH_SCOPE, workingSetScopeButton.getSelection()); 363 results = new ArrayList (); ResourceProxyVisitor visitor= new ResourceProxyVisitor(); 365 if (searchScopes == null || searchScopes.isEmpty()) { 366 try { 367 ResourcesPlugin.getWorkspace().getRoot().accept(visitor, IResource.NONE); 368 } catch (CoreException ce) { 369 } 371 } else { 372 Iterator iter= searchScopes.iterator(); 373 while(iter.hasNext()) { 374 try { 375 ((IResource) iter.next()).accept(visitor, IResource.NONE); 376 } catch (CoreException ce) { 377 } 379 } 380 } 381 super.okPressed(); 382 } 383 384 387 class ResourceProxyVisitor implements IResourceProxyVisitor { 388 Pattern pattern; 389 390 ResourceProxyVisitor() { 391 String input = getInput(); 394 input = input.replaceAll("\\.", "\\\\."); input = input.replaceAll("\\*", "\\.\\*"); input = input.replaceAll("\\?", "\\.\\?"); pattern = Pattern.compile(input); 401 } 402 403 406 public boolean visit(IResourceProxy proxy) { 407 if (proxy.getType() == IResource.FILE) { 408 Matcher matcher = pattern.matcher(proxy.getName()); 409 if (matcher.find()) { 410 results.add(proxy.requestResource()); 411 } 412 return false; 413 } 414 return true; 415 } 416 } 417 420 protected void configureShell(Shell shell) { 421 super.configureShell(shell); 422 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IAntUIHelpContextIds.SEARCH_FOR_BUILDFILES_DIALOG); 423 } 424 425 428 protected void validateInput() { 429 String errorMessage = null; 430 if (getValidator() != null) { 431 errorMessage = getValidator().isValid(getText().getText()); 432 } 433 434 setErrorMessage(errorMessage); 435 if (errorMessage == null) { 436 updateForWorkingSetSettings(); 437 } 438 } 439 } 440 | Popular Tags |