1 11 package org.eclipse.ui.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.jface.viewers.CheckStateChangedEvent; 21 import org.eclipse.jface.viewers.ICheckStateListener; 22 import org.eclipse.jface.viewers.ITreeContentProvider; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.events.ControlEvent; 25 import org.eclipse.swt.events.ControlListener; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.swt.widgets.TableColumn; 30 import org.eclipse.ui.PlatformUI; 31 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 32 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 33 import org.eclipse.ui.internal.ide.misc.CheckboxTreeAndListGroup; 34 import org.eclipse.ui.model.WorkbenchContentProvider; 35 import org.eclipse.ui.model.WorkbenchLabelProvider; 36 37 54 public class ResourceSelectionDialog extends SelectionDialog { 55 private IAdaptable root; 57 58 private CheckboxTreeAndListGroup selectionGroup; 60 61 private final static int SIZING_SELECTION_WIDGET_WIDTH = 400; 63 64 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300; 65 66 74 public ResourceSelectionDialog(Shell parentShell, IAdaptable rootElement, 75 String message) { 76 super(parentShell); 77 setTitle(IDEWorkbenchMessages.ResourceSelectionDialog_title); 78 root = rootElement; 79 if (message != null) { 80 setMessage(message); 81 } else { 82 setMessage(IDEWorkbenchMessages.ResourceSelectionDialog_message); 83 } 84 setShellStyle(getShellStyle() | SWT.RESIZE); 85 } 86 87 91 private void checkInitialSelections() { 92 Iterator itemsToCheck = getInitialElementSelections().iterator(); 93 94 while (itemsToCheck.hasNext()) { 95 IResource currentElement = (IResource) itemsToCheck.next(); 96 97 if (currentElement.getType() == IResource.FILE) { 98 selectionGroup.initialCheckListItem(currentElement); 99 } else { 100 selectionGroup.initialCheckTreeItem(currentElement); 101 } 102 } 103 } 104 105 106 109 public void checkStateChanged(CheckStateChangedEvent event) { 110 getOkButton().setEnabled(selectionGroup.getCheckedElementCount() > 0); 111 } 112 113 116 protected void configureShell(Shell shell) { 117 super.configureShell(shell); 118 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 119 IIDEHelpContextIds.RESOURCE_SELECTION_DIALOG); 120 } 121 122 public void create() { 123 super.create(); 124 initializeDialog(); 125 } 126 127 130 protected Control createDialogArea(Composite parent) { 131 Composite composite = (Composite) super.createDialogArea(parent); 133 134 ArrayList input = new ArrayList (); 137 input.add(root); 138 139 createMessageArea(composite); 140 selectionGroup = new CheckboxTreeAndListGroup(composite, input, 141 getResourceProvider(IResource.FOLDER | IResource.PROJECT 142 | IResource.ROOT), WorkbenchLabelProvider 143 .getDecoratingWorkbenchLabelProvider(), 144 getResourceProvider(IResource.FILE), WorkbenchLabelProvider 145 .getDecoratingWorkbenchLabelProvider(), SWT.NONE, 146 SIZING_SELECTION_WIDGET_WIDTH, SIZING_SELECTION_WIDGET_HEIGHT); 150 151 composite.addControlListener(new ControlListener() { 152 public void controlMoved(ControlEvent e) { 153 } 154 155 public void controlResized(ControlEvent e) { 156 TableColumn[] columns = selectionGroup.getListTable() 158 .getColumns(); 159 for (int i = 0; i < columns.length; i++) { 160 columns[i].pack(); 161 } 162 } 163 }); 164 165 return composite; 166 } 167 168 172 private ITreeContentProvider getResourceProvider(final int resourceType) { 173 return new WorkbenchContentProvider() { 174 public Object [] getChildren(Object o) { 175 if (o instanceof IContainer) { 176 IResource[] members = null; 177 try { 178 members = ((IContainer) o).members(); 179 } catch (CoreException e) { 180 return new Object [0]; 182 } 183 184 ArrayList results = new ArrayList (); 186 for (int i = 0; i < members.length; i++) { 187 if ((members[i].getType() & resourceType) > 0) { 189 results.add(members[i]); 190 } 191 } 192 return results.toArray(); 193 } 194 if (o instanceof ArrayList ) { 196 return ((ArrayList ) o).toArray(); 197 } 198 return new Object [0]; 199 } 200 }; 201 } 202 203 206 private void initializeDialog() { 207 selectionGroup.addCheckStateListener(new ICheckStateListener() { 208 public void checkStateChanged(CheckStateChangedEvent event) { 209 getOkButton().setEnabled( 210 selectionGroup.getCheckedElementCount() > 0); 211 } 212 }); 213 214 if (getInitialElementSelections().isEmpty()) { 215 getOkButton().setEnabled(false); 216 } else { 217 checkInitialSelections(); 218 } 219 } 220 221 226 protected void okPressed() { 227 Iterator resultEnum = selectionGroup.getAllCheckedListItems(); 228 ArrayList list = new ArrayList (); 229 while (resultEnum.hasNext()) { 230 list.add(resultEnum.next()); 231 } 232 setResult(list); 233 super.okPressed(); 234 } 235 } 236 | Popular Tags |