1 13 package org.eclipse.ui.internal.ide.misc; 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IContainer; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.jface.dialogs.Dialog; 23 import org.eclipse.jface.viewers.DoubleClickEvent; 24 import org.eclipse.jface.viewers.IDoubleClickListener; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.ISelectionChangedListener; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.SelectionChangedEvent; 29 import org.eclipse.jface.viewers.StructuredSelection; 30 import org.eclipse.jface.viewers.TreeViewer; 31 import org.eclipse.jface.viewers.ViewerComparator; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.layout.GridData; 34 import org.eclipse.swt.layout.GridLayout; 35 import org.eclipse.swt.widgets.Composite; 36 import org.eclipse.swt.widgets.Event; 37 import org.eclipse.swt.widgets.Label; 38 import org.eclipse.swt.widgets.Listener; 39 import org.eclipse.swt.widgets.Text; 40 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 41 import org.eclipse.ui.model.WorkbenchLabelProvider; 42 import org.eclipse.ui.part.DrillDownComposite; 43 44 47 public class ContainerSelectionGroup extends Composite { 48 private Listener listener; 50 51 private boolean allowNewContainerName = true; 53 54 private boolean showClosedProjects = true; 56 57 private IContainer selectedContainer; 59 60 private Text containerNameField; 62 63 TreeViewer treeViewer; 64 65 private static final String DEFAULT_MSG_NEW_ALLOWED = IDEWorkbenchMessages.ContainerGroup_message; 67 68 private static final String DEFAULT_MSG_SELECT_ONLY = IDEWorkbenchMessages.ContainerGroup_selectFolder; 69 70 private static final int SIZING_SELECTION_PANE_WIDTH = 320; 72 73 private static final int SIZING_SELECTION_PANE_HEIGHT = 300; 74 75 87 public ContainerSelectionGroup(Composite parent, Listener listener, 88 boolean allowNewContainerName) { 89 this(parent, listener, allowNewContainerName, null); 90 } 91 92 106 public ContainerSelectionGroup(Composite parent, Listener listener, 107 boolean allowNewContainerName, String message) { 108 this(parent, listener, allowNewContainerName, message, true); 109 } 110 111 127 public ContainerSelectionGroup(Composite parent, Listener listener, 128 boolean allowNewContainerName, String message, 129 boolean showClosedProjects) { 130 this(parent, listener, allowNewContainerName, message, 131 showClosedProjects, SIZING_SELECTION_PANE_HEIGHT, 132 SIZING_SELECTION_PANE_WIDTH); 133 } 134 135 155 public ContainerSelectionGroup(Composite parent, Listener listener, 156 boolean allowNewContainerName, String message, 157 boolean showClosedProjects, int heightHint, int widthHint) { 158 super(parent, SWT.NONE); 159 this.listener = listener; 160 this.allowNewContainerName = allowNewContainerName; 161 this.showClosedProjects = showClosedProjects; 162 if (message != null) { 163 createContents(message, heightHint, widthHint); 164 } else if (allowNewContainerName) { 165 createContents(DEFAULT_MSG_NEW_ALLOWED, heightHint, widthHint); 166 } else { 167 createContents(DEFAULT_MSG_SELECT_ONLY, heightHint, widthHint); 168 } 169 } 170 171 178 public void containerSelectionChanged(IContainer container) { 179 selectedContainer = container; 180 181 if (allowNewContainerName) { 182 if (container == null) { 183 containerNameField.setText(""); } else { 185 String text = container.getFullPath().makeRelative().toString(); 186 containerNameField.setText(text); 187 containerNameField.setToolTipText(text); 188 } 189 } 190 191 if (listener != null) { 193 Event changeEvent = new Event(); 194 changeEvent.type = SWT.Selection; 195 changeEvent.widget = this; 196 listener.handleEvent(changeEvent); 197 } 198 } 199 200 205 public void createContents(String message) { 206 createContents(message, SIZING_SELECTION_PANE_HEIGHT, 207 SIZING_SELECTION_PANE_WIDTH); 208 } 209 210 217 public void createContents(String message, int heightHint, int widthHint) { 218 GridLayout layout = new GridLayout(); 219 layout.marginWidth = 0; 220 setLayout(layout); 221 setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 222 223 Label label = new Label(this, SWT.WRAP); 224 label.setText(message); 225 label.setFont(this.getFont()); 226 227 if (allowNewContainerName) { 228 containerNameField = new Text(this, SWT.SINGLE | SWT.BORDER); 229 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 230 gd.widthHint = widthHint; 231 containerNameField.setLayoutData(gd); 232 containerNameField.addListener(SWT.Modify, listener); 233 containerNameField.setFont(this.getFont()); 234 } else { 235 new Label(this, SWT.NONE); 237 } 238 239 createTreeViewer(heightHint); 240 Dialog.applyDialogFont(this); 241 } 242 243 249 protected void createTreeViewer(int heightHint) { 250 DrillDownComposite drillDown = new DrillDownComposite(this, SWT.BORDER); 252 GridData spec = new GridData(SWT.FILL, SWT.FILL, true, true); 253 spec.widthHint = SIZING_SELECTION_PANE_WIDTH; 254 spec.heightHint = heightHint; 255 drillDown.setLayoutData(spec); 256 257 treeViewer = new TreeViewer(drillDown, SWT.NONE); 259 drillDown.setChildTree(treeViewer); 260 ContainerContentProvider cp = new ContainerContentProvider(); 261 cp.showClosedProjects(showClosedProjects); 262 treeViewer.setContentProvider(cp); 263 treeViewer.setLabelProvider(WorkbenchLabelProvider 264 .getDecoratingWorkbenchLabelProvider()); 265 treeViewer.setComparator(new ViewerComparator()); 266 treeViewer.setUseHashlookup(true); 267 treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { 268 public void selectionChanged(SelectionChangedEvent event) { 269 IStructuredSelection selection = (IStructuredSelection) event 270 .getSelection(); 271 containerSelectionChanged((IContainer) selection 272 .getFirstElement()); } 274 }); 275 treeViewer.addDoubleClickListener(new IDoubleClickListener() { 276 public void doubleClick(DoubleClickEvent event) { 277 ISelection selection = event.getSelection(); 278 if (selection instanceof IStructuredSelection) { 279 Object item = ((IStructuredSelection) selection) 280 .getFirstElement(); 281 if (item == null) { 282 return; 283 } 284 if (treeViewer.getExpandedState(item)) { 285 treeViewer.collapseToLevel(item, 1); 286 } else { 287 treeViewer.expandToLevel(item, 1); 288 } 289 } 290 } 291 }); 292 293 treeViewer.setInput(ResourcesPlugin.getWorkspace()); 295 } 296 297 303 public IPath getContainerFullPath() { 304 if (allowNewContainerName) { 305 String pathName = containerNameField.getText(); 306 if (pathName == null || pathName.length() < 1) { 307 return null; 308 } 309 return (new Path(pathName)).makeAbsolute(); 311 312 } 313 if (selectedContainer == null) 314 return null; 315 return selectedContainer.getFullPath(); 316 317 } 318 319 323 public void setInitialFocus() { 324 if (allowNewContainerName) { 325 containerNameField.setFocus(); 326 } else { 327 treeViewer.getTree().setFocus(); 328 } 329 } 330 331 335 public void setSelectedContainer(IContainer container) { 336 selectedContainer = container; 337 338 List itemsToExpand = new ArrayList (); 340 IContainer parent = container.getParent(); 341 while (parent != null) { 342 itemsToExpand.add(0, parent); 343 parent = parent.getParent(); 344 } 345 treeViewer.setExpandedElements(itemsToExpand.toArray()); 346 treeViewer.setSelection(new StructuredSelection(container), true); 347 } 348 } 349 | Popular Tags |