1 13 14 package org.eclipse.ui.dialogs; 15 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 19 import org.eclipse.jface.viewers.CheckStateChangedEvent; 20 import org.eclipse.jface.viewers.ICheckStateListener; 21 import org.eclipse.jface.viewers.ITreeContentProvider; 22 import org.eclipse.swt.SWT; 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.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.Control; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.ui.PlatformUI; 33 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 34 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 35 import org.eclipse.ui.internal.ide.misc.CheckboxTreeAndListGroup; 36 import org.eclipse.ui.model.WorkbenchContentProvider; 37 import org.eclipse.ui.model.WorkbenchLabelProvider; 38 import org.eclipse.ui.model.WorkbenchViewerComparator; 39 40 58 public class FileSelectionDialog extends SelectionDialog { 59 private FileSystemElement root; 61 62 CheckboxTreeAndListGroup selectionGroup; 64 65 private boolean expandAllOnOpen = false; 67 68 private static final int SIZING_SELECTION_WIDGET_WIDTH = 500; 70 71 private static final int SIZING_SELECTION_WIDGET_HEIGHT = 250; 72 73 81 public FileSelectionDialog(Shell parentShell, 82 FileSystemElement fileSystemElement, String message) { 83 super(parentShell); 84 setTitle(IDEWorkbenchMessages.FileSelectionDialog_title); 85 root = fileSystemElement; 86 if (message != null) { 87 setMessage(message); 88 } else { 89 setMessage(IDEWorkbenchMessages.FileSelectionDialog_message); 90 } 91 } 92 93 97 private void addSelectionButtons(Composite composite) { 98 99 Composite buttonComposite = new Composite(composite, SWT.RIGHT); 100 GridLayout layout = new GridLayout(); 101 layout.numColumns = 2; 102 buttonComposite.setLayout(layout); 103 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_END); 104 composite.setData(data); 105 106 Button selectButton = new Button(buttonComposite, SWT.PUSH); 107 selectButton.setText(SELECT_ALL_TITLE); 108 SelectionListener listener = new SelectionAdapter() { 109 public void widgetSelected(SelectionEvent e) { 110 selectionGroup.setAllSelections(true); 111 } 112 }; 113 selectButton.addSelectionListener(listener); 114 115 Button deselectButton = new Button(buttonComposite, SWT.PUSH); 116 deselectButton.setText(DESELECT_ALL_TITLE); 117 listener = new SelectionAdapter() { 118 public void widgetSelected(SelectionEvent e) { 119 selectionGroup.setAllSelections(false); 120 121 } 122 }; 123 deselectButton.addSelectionListener(listener); 124 125 } 126 127 131 private void checkInitialSelections() { 132 Iterator itemsToCheck = getInitialElementSelections().iterator(); 133 134 while (itemsToCheck.hasNext()) { 135 FileSystemElement currentElement = (FileSystemElement) itemsToCheck 136 .next(); 137 138 if (currentElement.isDirectory()) { 139 selectionGroup.initialCheckTreeItem(currentElement); 140 } else { 141 selectionGroup.initialCheckListItem(currentElement); 142 } 143 } 144 } 145 146 149 protected void configureShell(Shell shell) { 150 super.configureShell(shell); 151 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 152 IIDEHelpContextIds.FILE_SELECTION_DIALOG); 153 } 154 155 public void create() { 156 super.create(); 157 initializeDialog(); 158 } 159 160 163 protected Control createDialogArea(Composite parent) { 164 Composite composite = (Composite) super.createDialogArea(parent); 166 167 createMessageArea(composite); 168 169 FileSystemElement input = new FileSystemElement("", null, true); input.addChild(root); 174 root.setParent(input); 175 176 selectionGroup = new CheckboxTreeAndListGroup(composite, input, 177 getFolderProvider(), new WorkbenchLabelProvider(), 178 getFileProvider(), new WorkbenchLabelProvider(), SWT.NONE, 179 SIZING_SELECTION_WIDGET_WIDTH, SIZING_SELECTION_WIDGET_HEIGHT); 183 ICheckStateListener listener = new ICheckStateListener() { 184 public void checkStateChanged(CheckStateChangedEvent event) { 185 getOkButton().setEnabled( 186 selectionGroup.getCheckedElementCount() > 0); 187 } 188 }; 189 190 WorkbenchViewerComparator comparator = new WorkbenchViewerComparator(); 191 selectionGroup.setTreeComparator(comparator); 192 selectionGroup.setListComparator(comparator); 193 selectionGroup.addCheckStateListener(listener); 194 195 addSelectionButtons(composite); 196 197 return composite; 198 } 199 200 206 public boolean getExpandAllOnOpen() { 207 return expandAllOnOpen; 208 } 209 210 214 private ITreeContentProvider getFileProvider() { 215 return new WorkbenchContentProvider() { 216 public Object [] getChildren(Object o) { 217 if (o instanceof FileSystemElement) { 218 return ((FileSystemElement) o).getFiles().getChildren(o); 219 } 220 return new Object [0]; 221 } 222 }; 223 } 224 225 229 private ITreeContentProvider getFolderProvider() { 230 return new WorkbenchContentProvider() { 231 public Object [] getChildren(Object o) { 232 if (o instanceof FileSystemElement) { 233 return ((FileSystemElement) o).getFolders().getChildren(o); 234 } 235 return new Object [0]; 236 } 237 }; 238 } 239 240 243 private void initializeDialog() { 244 if (getInitialElementSelections().isEmpty()) { 246 getOkButton().setEnabled(false); 247 } else { 248 checkInitialSelections(); 249 } 250 selectionGroup.aboutToOpen(); 251 if (expandAllOnOpen) { 252 selectionGroup.expandAll(); 253 } 254 } 255 256 261 protected void okPressed() { 262 Iterator resultEnum = selectionGroup.getAllCheckedListItems(); 263 ArrayList list = new ArrayList (); 264 while (resultEnum.hasNext()) { 265 list.add(resultEnum.next()); 266 } 267 setResult(list); 268 super.okPressed(); 269 } 270 271 277 public void setExpandAllOnOpen(boolean expandAll) { 278 expandAllOnOpen = expandAll; 279 } 280 } 281 | Popular Tags |