1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Set ; 18 19 import org.eclipse.core.runtime.IStatus; 20 21 import org.eclipse.core.resources.IContainer; 22 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.custom.BusyIndicator; 25 import org.eclipse.swt.events.SelectionAdapter; 26 import org.eclipse.swt.events.SelectionEvent; 27 import org.eclipse.swt.layout.GridData; 28 import org.eclipse.swt.widgets.Button; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Control; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.swt.widgets.Tree; 33 34 import org.eclipse.jface.viewers.CheckStateChangedEvent; 35 import org.eclipse.jface.viewers.CheckboxTreeViewer; 36 import org.eclipse.jface.viewers.ICheckStateListener; 37 import org.eclipse.jface.viewers.ILabelProvider; 38 import org.eclipse.jface.viewers.ISelectionChangedListener; 39 import org.eclipse.jface.viewers.IStructuredSelection; 40 import org.eclipse.jface.viewers.ITreeContentProvider; 41 import org.eclipse.jface.viewers.SelectionChangedEvent; 42 import org.eclipse.jface.viewers.StructuredSelection; 43 import org.eclipse.jface.viewers.ViewerFilter; 44 import org.eclipse.jface.window.Window; 45 46 import org.eclipse.ui.dialogs.NewFolderDialog; 47 import org.eclipse.ui.dialogs.SelectionStatusDialog; 48 49 import org.eclipse.ui.views.navigator.ResourceComparator; 50 51 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 52 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 53 54 56 public class MultipleFolderSelectionDialog extends SelectionStatusDialog implements ISelectionChangedListener { 57 58 private CheckboxTreeViewer fViewer; 59 60 private ILabelProvider fLabelProvider; 61 private ITreeContentProvider fContentProvider; 62 private List fFilters; 63 64 private Object fInput; 65 private Button fNewFolderButton; 66 private IContainer fSelectedContainer; 67 private Set fExisting; 68 private Object fFocusElement; 69 70 public MultipleFolderSelectionDialog(Shell parent, ILabelProvider labelProvider, ITreeContentProvider contentProvider) { 71 super(parent); 72 fLabelProvider= labelProvider; 73 fContentProvider= contentProvider; 74 75 setSelectionResult(null); 76 setStatusLineAboveButtons(true); 77 78 int shellStyle = getShellStyle(); 79 setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE); 80 81 fExisting= null; 82 fFocusElement= null; 83 fFilters= null; 84 } 85 86 public void setExisting(Object [] existing) { 87 fExisting= new HashSet (); 88 for (int i= 0; i < existing.length; i++) { 89 fExisting.add(existing[i]); 90 } 91 } 92 93 97 public void setInput(Object input) { 98 fInput = input; 99 } 100 101 105 public void addFilter(ViewerFilter filter) { 106 if (fFilters == null) 107 fFilters = new ArrayList (4); 108 109 fFilters.add(filter); 110 } 111 112 115 protected void cancelPressed() { 116 setSelectionResult(null); 117 super.cancelPressed(); 118 } 119 120 123 protected void computeResult() { 124 Object [] checked= fViewer.getCheckedElements(); 125 if (fExisting == null) { 126 if (checked.length == 0) { 127 checked= null; 128 } 129 } else { 130 ArrayList res= new ArrayList (); 131 for (int i= 0; i < checked.length; i++) { 132 Object elem= checked[i]; 133 if (!fExisting.contains(elem)) { 134 res.add(elem); 135 } 136 } 137 if (!res.isEmpty()) { 138 checked= res.toArray(); 139 } else { 140 checked= null; 141 } 142 } 143 setSelectionResult(checked); 144 } 145 146 private void access$superCreate() { 147 super.create(); 148 } 149 150 153 public void create() { 154 155 BusyIndicator.showWhile(null, new Runnable () { 156 public void run() { 157 access$superCreate(); 158 159 fViewer.setCheckedElements( 160 getInitialElementSelections().toArray()); 161 162 fViewer.expandToLevel(2); 163 if (fExisting != null) { 164 for (Iterator iter= fExisting.iterator(); iter.hasNext();) { 165 fViewer.reveal(iter.next()); 166 } 167 } 168 169 updateOKStatus(); 170 } 171 }); 172 173 } 174 175 181 protected CheckboxTreeViewer createTreeViewer(Composite parent) { 182 fViewer = new CheckboxTreeViewer(parent, SWT.BORDER); 183 184 fViewer.setContentProvider(fContentProvider); 185 fViewer.setLabelProvider(fLabelProvider); 186 fViewer.addCheckStateListener(new ICheckStateListener() { 187 public void checkStateChanged(CheckStateChangedEvent event) { 188 updateOKStatus(); 189 } 190 }); 191 192 fViewer.setComparator(new ResourceComparator(ResourceComparator.NAME)); 193 if (fFilters != null) { 194 for (int i = 0; i != fFilters.size(); i++) 195 fViewer.addFilter((ViewerFilter) fFilters.get(i)); 196 } 197 198 fViewer.setInput(fInput); 199 200 return fViewer; 201 } 202 203 204 205 208 protected void updateOKStatus() { 209 computeResult(); 210 if (getResult() != null) { 211 updateStatus(new StatusInfo()); 212 } else { 213 updateStatus(new StatusInfo(IStatus.ERROR, "")); } 215 } 216 217 220 protected Control createDialogArea(Composite parent) { 221 Composite composite = (Composite) super.createDialogArea(parent); 222 223 createMessageArea(composite); 224 CheckboxTreeViewer treeViewer = createTreeViewer(composite); 225 226 GridData data = new GridData(GridData.FILL_BOTH); 227 data.widthHint = convertWidthInCharsToPixels(60); 228 data.heightHint = convertHeightInCharsToPixels(18); 229 230 Tree treeWidget = treeViewer.getTree(); 231 treeWidget.setLayoutData(data); 232 treeWidget.setFont(composite.getFont()); 233 234 Button button = new Button(composite, SWT.PUSH); 235 button.setText(NewWizardMessages.MultipleFolderSelectionDialog_button); 236 button.addSelectionListener(new SelectionAdapter() { 237 public void widgetSelected(SelectionEvent event) { 238 newFolderButtonPressed(); 239 } 240 }); 241 button.setFont(composite.getFont()); 242 243 fNewFolderButton= button; 244 245 treeViewer.addSelectionChangedListener(this); 246 if (fExisting != null) { 247 Object [] existing= fExisting.toArray(); 248 treeViewer.setGrayedElements(existing); 249 setInitialSelections(existing); 250 } 251 if (fFocusElement != null) { 252 treeViewer.setSelection(new StructuredSelection(fFocusElement), true); 253 } 254 treeViewer.addCheckStateListener(new ICheckStateListener() { 255 public void checkStateChanged(CheckStateChangedEvent event) { 256 forceExistingChecked(event); 257 } 258 }); 259 260 applyDialogFont(composite); 261 return composite; 262 } 263 264 protected void forceExistingChecked(CheckStateChangedEvent event) { 265 if (fExisting != null) { 266 Object elem= event.getElement(); 267 if (fExisting.contains(elem)) { 268 fViewer.setChecked(elem, true); 269 } 270 } 271 } 272 273 private void updateNewFolderButtonState() { 274 IStructuredSelection selection= (IStructuredSelection) fViewer.getSelection(); 275 fSelectedContainer= null; 276 if (selection.size() == 1) { 277 Object first= selection.getFirstElement(); 278 if (first instanceof IContainer) { 279 fSelectedContainer= (IContainer) first; 280 } 281 } 282 fNewFolderButton.setEnabled(fSelectedContainer != null); 283 } 284 285 protected void newFolderButtonPressed() { 286 Object createdFolder= createFolder(fSelectedContainer); 287 if (createdFolder != null) { 288 CheckboxTreeViewer treeViewer= fViewer; 289 treeViewer.refresh(fSelectedContainer); 290 treeViewer.reveal(createdFolder); 291 treeViewer.setChecked(createdFolder, true); 292 treeViewer.setSelection(new StructuredSelection(createdFolder)); 293 updateOKStatus(); 294 } 295 } 296 297 protected Object createFolder(IContainer container) { 298 NewFolderDialog dialog= new NewFolderDialog(getShell(), container); 299 if (dialog.open() == Window.OK) { 300 return dialog.getResult()[0]; 301 } 302 return null; 303 } 304 305 308 public void selectionChanged(SelectionChangedEvent event) { 309 updateNewFolderButtonState(); 310 } 311 312 public void setInitialFocus(Object focusElement) { 313 fFocusElement= focusElement; 314 } 315 316 317 318 } 319 | Popular Tags |