1 11 package org.eclipse.ui.internal.editors.text; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.custom.BusyIndicator; 23 import org.eclipse.swt.events.SelectionAdapter; 24 import org.eclipse.swt.events.SelectionEvent; 25 import org.eclipse.swt.events.SelectionListener; 26 import org.eclipse.swt.graphics.Font; 27 import org.eclipse.swt.layout.GridData; 28 import org.eclipse.swt.layout.GridLayout; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Label; 33 import org.eclipse.swt.widgets.Shell; 34 35 import org.eclipse.core.runtime.CoreException; 36 37 import org.eclipse.core.resources.IContainer; 38 import org.eclipse.core.resources.IResource; 39 import org.eclipse.core.resources.IWorkspaceRoot; 40 import org.eclipse.core.resources.ResourcesPlugin; 41 42 import org.eclipse.jface.dialogs.Dialog; 43 import org.eclipse.jface.dialogs.IDialogConstants; 44 import org.eclipse.jface.viewers.CheckStateChangedEvent; 45 import org.eclipse.jface.viewers.ICheckStateListener; 46 import org.eclipse.jface.viewers.ITreeContentProvider; 47 48 import org.eclipse.ui.dialogs.TypeFilteringDialog; 49 import org.eclipse.ui.model.WorkbenchContentProvider; 50 import org.eclipse.ui.model.WorkbenchLabelProvider; 51 52 53 58 class SelectResourcesDialog extends Dialog { 59 60 interface IFilter { 61 boolean accept(IResource resource); 62 } 63 64 private SelectResourcesBlock fResourceGroup; 65 private List fAcceptedFileTypes = new ArrayList (); 66 private IResource[] fInput; 67 private String fTitle; 68 private String fInstruction; 69 private Label fCountIndication; 70 private IFilter fAcceptableLocationsFilter; 71 72 73 public SelectResourcesDialog(Shell parentShell, String title, String instruction, IFilter acceptableLocationsFilter) { 74 super(parentShell); 75 setShellStyle(getShellStyle() | SWT.RESIZE); 76 fTitle= title; 77 fInstruction= instruction; 78 fAcceptableLocationsFilter= acceptableLocationsFilter; 79 } 80 81 public void setInput(IResource[] input) { 82 fInput= input; 83 } 84 85 public IResource[] getSelectedResources() { 86 List items= fResourceGroup.getAllCheckedListItems(); 87 return (IResource[]) items.toArray(new IResource[items.size()]); 88 } 89 90 93 protected void configureShell(Shell newShell) { 94 super.configureShell(newShell); 95 96 if (fTitle != null) 97 newShell.setText(fTitle); 98 } 99 100 103 protected Control createDialogArea(Composite parent) { 104 Composite composite= (Composite) super.createDialogArea(parent); 105 Label label= new Label(composite, SWT.LEFT); 106 label.setText(fInstruction); 107 label.setLayoutData(new GridData(GridData.FILL_BOTH)); 108 109 fResourceGroup= new SelectResourcesBlock(composite, ResourcesPlugin.getWorkspace().getRoot(), getResourceProvider(IResource.FOLDER | IResource.PROJECT), WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider(), getResourceProvider(IResource.FILE), WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider(), SWT.NONE, useHeightHint(parent)); 110 fResourceGroup.addCheckStateListener(new ICheckStateListener() { 111 public void checkStateChanged(CheckStateChangedEvent event) { 112 updateSelectionCount(); 113 } 114 }); 115 116 fCountIndication= new Label(composite, SWT.LEFT); 117 fCountIndication.setLayoutData(new GridData(GridData.FILL_BOTH)); 118 119 createSelectionButtonGroup(composite); 120 121 setInitialSelection(); 122 return composite; 123 } 124 125 private boolean useHeightHint(Composite parent) { 126 int fontHeight= (parent.getFont().getFontData())[0].getHeight(); 127 int displayHeight= parent.getDisplay().getClientArea().height; 128 return (displayHeight / fontHeight) > 50; 129 } 130 131 private ITreeContentProvider getResourceProvider(final int resourceType) { 132 return new WorkbenchContentProvider() { 133 public Object [] getChildren(Object o) { 134 if (o instanceof IWorkspaceRoot) { 135 HashSet projects= new HashSet (); 136 for (int i= 0; i < fInput.length; i++) { 137 IResource project= fInput[i].getProject(); 138 if ((project.getType() & resourceType) > 0) 139 projects.add(project); 140 } 141 return projects.toArray(); 142 } 143 144 if (o instanceof IContainer) { 145 IResource[] members = null; 146 try { 147 members = ((IContainer) o).members(); 148 } catch (CoreException e) { 149 return new Object [0]; 151 } 152 153 ArrayList results = new ArrayList (); 155 for (int i = 0; i < members.length; i++) { 156 if ((members[i].getType() & resourceType) > 0 && (resourceType != IResource.FILE || fAcceptableLocationsFilter == null || fAcceptableLocationsFilter.accept(members[i]))) { 158 results.add(members[i]); 159 } 160 } 161 return results.toArray(); 162 } 163 164 if (o instanceof ArrayList ) 166 return ((ArrayList ) o).toArray(); 167 168 return new Object [0]; 169 } 170 }; 171 } 172 173 protected final void createSelectionButtonGroup(Composite parent) { 174 175 Font font= parent.getFont(); 176 177 Composite buttonComposite= new Composite(parent, SWT.NONE); 179 buttonComposite.setFont(parent.getFont()); 180 181 GridLayout layout= new GridLayout(); 182 layout.numColumns= 3; 183 layout.makeColumnsEqualWidth= true; 184 buttonComposite.setLayout(layout); 185 buttonComposite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL)); 186 187 Button selectButton= createButton(buttonComposite, IDialogConstants.SELECT_ALL_ID, TextEditorMessages.SelectResourcesDialog_selectAll, false); 188 189 SelectionListener listener= new SelectionAdapter() { 190 public void widgetSelected(SelectionEvent e) { 191 fResourceGroup.setAllSelections(true); 192 updateSelectionCount(); 193 } 194 }; 195 selectButton.addSelectionListener(listener); 196 selectButton.setFont(font); 197 setButtonLayoutData(selectButton); 198 199 Button deselectButton= createButton(buttonComposite, IDialogConstants.DESELECT_ALL_ID, TextEditorMessages.SelectResourcesDialog_deselectAll, false); 200 201 listener= new SelectionAdapter() { 202 public void widgetSelected(SelectionEvent e) { 203 fResourceGroup.setAllSelections(false); 204 updateSelectionCount(); 205 } 206 }; 207 deselectButton.addSelectionListener(listener); 208 deselectButton.setFont(font); 209 setButtonLayoutData(deselectButton); 210 211 Button selectTypesButton= createButton(buttonComposite, IDialogConstants.SELECT_TYPES_ID, TextEditorMessages.SelectResourcesDialog_filterSelection, false); 213 214 listener= new SelectionAdapter() { 215 public void widgetSelected(SelectionEvent e) { 216 handleSelectFileTypes(); 217 } 218 }; 219 selectTypesButton.addSelectionListener(listener); 220 selectTypesButton.setFont(font); 221 setButtonLayoutData(selectTypesButton); 222 } 223 224 protected void handleSelectFileTypes() { 225 Object [] acceptedFileTypes= queryFileTypes(); 226 if (acceptedFileTypes != null) { 227 fAcceptedFileTypes= Arrays.asList(acceptedFileTypes); 228 filterSelection(); 229 } 230 } 231 232 protected Object [] queryFileTypes() { 233 TypeFilteringDialog dialog= new TypeFilteringDialog(getShell(), fAcceptedFileTypes); 234 dialog.open(); 235 return dialog.getResult(); 236 } 237 238 private void filterSelection() { 239 240 final IFilter filter= new IFilter() { 241 public boolean accept(IResource resource) { 242 return hasAcceptedFileType(resource); 243 } 244 }; 245 246 List list= fResourceGroup.getAllWhiteCheckedItems(); 247 final IResource[] resources= (IResource[]) list.toArray(new IResource[list.size()]); 248 249 Runnable runnable = new Runnable () { 250 public void run() { 251 setSelection(resources, filter); 252 } 253 }; 254 255 BusyIndicator.showWhile(getShell().getDisplay(), runnable); 256 } 257 258 protected boolean hasAcceptedFileType(IResource resource) { 259 if (fAcceptedFileTypes == null) 260 return true; 261 262 String resourceName= resource.getName(); 263 int separatorIndex= resourceName.lastIndexOf("."); if (separatorIndex == -1) 265 return false; 266 267 String extension= resourceName.substring(separatorIndex + 1); 268 269 Iterator e= fAcceptedFileTypes.iterator(); 270 while (e.hasNext()) { 271 if (extension.equalsIgnoreCase((String ) e.next())) 272 return true; 273 } 274 275 return false; 276 } 277 278 protected void setInitialSelection() { 279 IFilter filter= new IFilter() { 280 public boolean accept(IResource resource) { 281 return true; 282 } 283 }; 284 setSelection(fInput, filter); 285 selectAndReveal(fInput[0]); 286 } 287 288 protected void setSelection(IResource[] input, IFilter filter) { 289 Map selectionMap= new Hashtable (); 290 for (int i= 0; i < input.length; i++) { 291 IResource resource= input[i]; 292 if ((resource.getType() & IResource.FILE) > 0) { 293 if (filter.accept(resource)) { 294 List files= null; 295 IContainer parent= resource.getParent(); 296 if (selectionMap.containsKey(parent)) 297 files= (List ) selectionMap.get(parent); 298 else 299 files= new ArrayList (); 300 301 files.add(resource); 302 selectionMap.put(parent, files); 303 } 304 } else 305 setSelection(selectionMap, (IContainer) resource, filter); 306 } 307 fResourceGroup.updateSelections(selectionMap); 308 updateSelectionCount(); 309 } 310 311 private void setSelection(Map selectionMap, IContainer parent, IFilter filter) { 312 try { 313 314 IResource[] resources= parent.members(); 315 List selections= new ArrayList (); 316 317 for (int i= 0; i < resources.length; i++) { 318 IResource resource= resources[i]; 319 if ((resource.getType() & IResource.FILE) > 0) { 320 if (filter.accept(resource)) 321 selections.add(resource); 322 } else { 323 setSelection(selectionMap, (IContainer) resource, filter); 324 } 325 } 326 327 if (!selections.isEmpty()) 328 selectionMap.put(parent, selections); 329 330 } catch (CoreException x) { 331 return; 333 } 334 } 335 336 private void selectAndReveal(IResource resource) { 337 IContainer container= null; 338 if ((IResource.FILE & resource.getType()) > 0) 339 container= resource.getParent(); 340 else 341 container= (IContainer) resource; 342 fResourceGroup.selectAndReveal(container); 343 } 344 345 private void updateSelectionCount() { 346 List listItems= fResourceGroup.getAllCheckedListItems(); 347 int checkedFiles= listItems == null ? 0 : listItems.size(); 348 StringBuffer buffer= new StringBuffer (); 349 switch (checkedFiles) { 350 case 0: 351 buffer.append(TextEditorMessages.SelectResourcesDialog_noFilesSelected); 352 break; 353 case 1: 354 buffer.append(TextEditorMessages.SelectResourcesDialog_oneFileSelected); 355 break; 356 default: 357 buffer.append(NLSUtility.format(TextEditorMessages.SelectResourcesDialog_nFilesSelected, new Integer (checkedFiles))); 358 } 359 fCountIndication.setText(buffer.toString()); 360 361 Button okButton= getButton(IDialogConstants.OK_ID); 362 if (okButton != null) 363 okButton.setEnabled(checkedFiles > 0); 364 } 365 } 366 | Popular Tags |