| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Igor Fedorenko <igorfie@yahoo.com> - |
| 11 | * Fix for Bug 136921 [IDE] New File dialog locks for 20 seconds |
| 12 | *******************************************************************************/ |
| 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.osgi.util.TextProcessor; |
| 33 | import org.eclipse.swt.SWT; |
| 34 | import org.eclipse.swt.layout.GridData; |
| 35 | import org.eclipse.swt.layout.GridLayout; |
| 36 | import org.eclipse.swt.widgets.Composite; |
| 37 | import org.eclipse.swt.widgets.Event; |
| 38 | import org.eclipse.swt.widgets.Label; |
| 39 | import org.eclipse.swt.widgets.Listener; |
| 40 | import org.eclipse.swt.widgets.Text; |
| 41 | import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
| 42 | import org.eclipse.ui.model.WorkbenchLabelProvider; |
| 43 | import org.eclipse.ui.part.DrillDownComposite; |
| 44 | |
| 45 | /** |
| 46 | * Workbench-level composite for choosing a container. |
| 47 | */ |
| 48 | public class ContainerSelectionGroup extends Composite { |
| 49 | // The listener to notify of events |
| 50 | private Listener listener; |
| 51 | |
| 52 | // Enable user to type in new container name |
| 53 | private boolean allowNewContainerName = true; |
| 54 | |
| 55 | // show all projects by default |
| 56 | private boolean showClosedProjects = true; |
| 57 | |
| 58 | // Last selection made by user |
| 59 | private IContainer selectedContainer; |
| 60 | |
| 61 | // handle on parts |
| 62 | private Text containerNameField; |
| 63 | |
| 64 | TreeViewer treeViewer; |
| 65 | |
| 66 | // the message to display at the top of this dialog |
| 67 | private static final String DEFAULT_MSG_NEW_ALLOWED = IDEWorkbenchMessages.ContainerGroup_message; |
| 68 | |
| 69 | private static final String DEFAULT_MSG_SELECT_ONLY = IDEWorkbenchMessages.ContainerGroup_selectFolder; |
| 70 | |
| 71 | // sizing constants |
| 72 | private static final int SIZING_SELECTION_PANE_WIDTH = 320; |
| 73 | |
| 74 | private static final int SIZING_SELECTION_PANE_HEIGHT = 300; |
| 75 | |
| 76 | /** |
| 77 | * Creates a new instance of the widget. |
| 78 | * |
| 79 | * @param parent |
| 80 | * The parent widget of the group. |
| 81 | * @param listener |
| 82 | * A listener to forward events to. Can be null if no listener is |
| 83 | * required. |
| 84 | * @param allowNewContainerName |
| 85 | * Enable the user to type in a new container name instead of |
| 86 | * just selecting from the existing ones. |
| 87 | */ |
| 88 | public ContainerSelectionGroup(Composite parent, Listener listener, |
| 89 | boolean allowNewContainerName) { |
| 90 | this(parent, listener, allowNewContainerName, null); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Creates a new instance of the widget. |
| 95 | * |
| 96 | * @param parent |
| 97 | * The parent widget of the group. |
| 98 | * @param listener |
| 99 | * A listener to forward events to. Can be null if no listener is |
| 100 | * required. |
| 101 | * @param allowNewContainerName |
| 102 | * Enable the user to type in a new container name instead of |
| 103 | * just selecting from the existing ones. |
| 104 | * @param message |
| 105 | * The text to present to the user. |
| 106 | */ |
| 107 | public ContainerSelectionGroup(Composite parent, Listener listener, |
| 108 | boolean allowNewContainerName, String message) { |
| 109 | this(parent, listener, allowNewContainerName, message, true); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Creates a new instance of the widget. |
| 114 | * |
| 115 | * @param parent |
| 116 | * The parent widget of the group. |
| 117 | * @param listener |
| 118 | * A listener to forward events to. Can be null if no listener is |
| 119 | * required. |
| 120 | * @param allowNewContainerName |
| 121 | * Enable the user to type in a new container name instead of |
| 122 | * just selecting from the existing ones. |
| 123 | * @param message |
| 124 | * The text to present to the user. |
| 125 | * @param showClosedProjects |
| 126 | * Whether or not to show closed projects. |
| 127 | */ |
| 128 | public ContainerSelectionGroup(Composite parent, Listener listener, |
| 129 | boolean allowNewContainerName, String message, |
| 130 | boolean showClosedProjects) { |
| 131 | this(parent, listener, allowNewContainerName, message, |
| 132 | showClosedProjects, SIZING_SELECTION_PANE_HEIGHT, |
| 133 | SIZING_SELECTION_PANE_WIDTH); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates a new instance of the widget. |
| 138 | * |
| 139 | * @param parent |
| 140 | * The parent widget of the group. |
| 141 | * @param listener |
| 142 | * A listener to forward events to. Can be null if no listener is |
| 143 | * required. |
| 144 | * @param allowNewContainerName |
| 145 | * Enable the user to type in a new container name instead of |
| 146 | * just selecting from the existing ones. |
| 147 | * @param message |
| 148 | * The text to present to the user. |
| 149 | * @param showClosedProjects |
| 150 | * Whether or not to show closed projects. |
| 151 | * @param heightHint |
| 152 | * height hint for the drill down composite |
| 153 | * @param widthHint |
| 154 | * width hint for the drill down composite |
| 155 | */ |
| 156 | public ContainerSelectionGroup(Composite parent, Listener listener, |
| 157 | boolean allowNewContainerName, String message, |
| 158 | boolean showClosedProjects, int heightHint, int widthHint) { |
| 159 | super(parent, SWT.NONE); |
| 160 | this.listener = listener; |
| 161 | this.allowNewContainerName = allowNewContainerName; |
| 162 | this.showClosedProjects = showClosedProjects; |
| 163 | if (message != null) { |
| 164 | createContents(message, heightHint, widthHint); |
| 165 | } else if (allowNewContainerName) { |
| 166 | createContents(DEFAULT_MSG_NEW_ALLOWED, heightHint, widthHint); |
| 167 | } else { |
| 168 | createContents(DEFAULT_MSG_SELECT_ONLY, heightHint, widthHint); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * The container selection has changed in the tree view. Update the |
| 174 | * container name field value and notify all listeners. |
| 175 | * |
| 176 | * @param container |
| 177 | * The container that changed |
| 178 | */ |
| 179 | public void containerSelectionChanged(IContainer container) { |
| 180 | selectedContainer = container; |
| 181 | |
| 182 | if (allowNewContainerName) { |
| 183 | if (container == null) { |
| 184 | containerNameField.setText("");//$NON-NLS-1$ |
| 185 | } else { |
| 186 | String text = TextProcessor.process(container.getFullPath() |
| 187 | .makeRelative().toString()); |
| 188 | containerNameField.setText(text); |
| 189 | containerNameField.setToolTipText(text); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // fire an event so the parent can update its controls |
| 194 | if (listener != null) { |
| 195 | Event changeEvent = new Event(); |
| 196 | changeEvent.type = SWT.Selection; |
| 197 | changeEvent.widget = this; |
| 198 | listener.handleEvent(changeEvent); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Creates the contents of the composite. |
| 204 | * |
| 205 | * @param message |
| 206 | */ |
| 207 | public void createContents(String message) { |
| 208 | createContents(message, SIZING_SELECTION_PANE_HEIGHT, |
| 209 | SIZING_SELECTION_PANE_WIDTH); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Creates the contents of the composite. |
| 214 | * |
| 215 | * @param message |
| 216 | * @param heightHint |
| 217 | * @param widthHint |
| 218 | */ |
| 219 | public void createContents(String message, int heightHint, int widthHint) { |
| 220 | GridLayout layout = new GridLayout(); |
| 221 | layout.marginWidth = 0; |
| 222 | setLayout(layout); |
| 223 | setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
| 224 | |
| 225 | Label label = new Label(this, SWT.WRAP); |
| 226 | label.setText(message); |
| 227 | label.setFont(this.getFont()); |
| 228 | |
| 229 | if (allowNewContainerName) { |
| 230 | containerNameField = new Text(this, SWT.SINGLE | SWT.BORDER); |
| 231 | GridData gd = new GridData(GridData.FILL_HORIZONTAL); |
| 232 | gd.widthHint = widthHint; |
| 233 | containerNameField.setLayoutData(gd); |
| 234 | containerNameField.addListener(SWT.Modify, listener); |
| 235 | containerNameField.setFont(this.getFont()); |
| 236 | } else { |
| 237 | // filler... |
| 238 | new Label(this, SWT.NONE); |
| 239 | } |
| 240 | |
| 241 | createTreeViewer(heightHint); |
| 242 | Dialog.applyDialogFont(this); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Returns a new drill down viewer for this dialog. |
| 247 | * |
| 248 | * @param heightHint |
| 249 | * height hint for the drill down composite |
| 250 | */ |
| 251 | protected void createTreeViewer(int heightHint) { |
| 252 | // Create drill down. |
| 253 | DrillDownComposite drillDown = new DrillDownComposite(this, SWT.BORDER); |
| 254 | GridData spec = new GridData(SWT.FILL, SWT.FILL, true, true); |
| 255 | spec.widthHint = SIZING_SELECTION_PANE_WIDTH; |
| 256 | spec.heightHint = heightHint; |
| 257 | drillDown.setLayoutData(spec); |
| 258 | |
| 259 | // Create tree viewer inside drill down. |
| 260 | treeViewer = new TreeViewer(drillDown, SWT.NONE); |
| 261 | drillDown.setChildTree(treeViewer); |
| 262 | ContainerContentProvider cp = new ContainerContentProvider(); |
| 263 | cp.showClosedProjects(showClosedProjects); |
| 264 | treeViewer.setContentProvider(cp); |
| 265 | treeViewer.setLabelProvider(WorkbenchLabelProvider |
| 266 | .getDecoratingWorkbenchLabelProvider()); |
| 267 | treeViewer.setComparator(new ViewerComparator()); |
| 268 | treeViewer.setUseHashlookup(true); |
| 269 | treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { |
| 270 | public void selectionChanged(SelectionChangedEvent event) { |
| 271 | IStructuredSelection selection = (IStructuredSelection) event |
| 272 | .getSelection(); |
| 273 | containerSelectionChanged((IContainer) selection |
| 274 | .getFirstElement()); // allow null |
| 275 | } |
| 276 | }); |
| 277 | treeViewer.addDoubleClickListener(new IDoubleClickListener() { |
| 278 | public void doubleClick(DoubleClickEvent event) { |
| 279 | ISelection selection = event.getSelection(); |
| 280 | if (selection instanceof IStructuredSelection) { |
| 281 | Object item = ((IStructuredSelection) selection) |
| 282 | .getFirstElement(); |
| 283 | if (item == null) { |
| 284 | return; |
| 285 | } |
| 286 | if (treeViewer.getExpandedState(item)) { |
| 287 | treeViewer.collapseToLevel(item, 1); |
| 288 | } else { |
| 289 | treeViewer.expandToLevel(item, 1); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | }); |
| 294 | |
| 295 | // This has to be done after the viewer has been laid out |
| 296 | treeViewer.setInput(ResourcesPlugin.getWorkspace()); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns the currently entered container name. Null if the field is empty. |
| 301 | * Note that the container may not exist yet if the user entered a new |
| 302 | * container name in the field. |
| 303 | * |
| 304 | * @return IPath |
| 305 | */ |
| 306 | public IPath getContainerFullPath() { |
| 307 | if (allowNewContainerName) { |
| 308 | String pathName = containerNameField.getText(); |
| 309 | if (pathName == null || pathName.length() < 1) { |
| 310 | return null; |
| 311 | } |
| 312 | // The user may not have made this absolute so do it for them |
| 313 | return (new Path(TextProcessor.deprocess(pathName))).makeAbsolute(); |
| 314 | |
| 315 | } |
| 316 | if (selectedContainer == null) |
| 317 | return null; |
| 318 | return selectedContainer.getFullPath(); |
| 319 | |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Gives focus to one of the widgets in the group, as determined by the |
| 324 | * group. |
| 325 | */ |
| 326 | public void setInitialFocus() { |
| 327 | if (allowNewContainerName) { |
| 328 | containerNameField.setFocus(); |
| 329 | } else { |
| 330 | treeViewer.getTree().setFocus(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Sets the selected existing container. |
| 336 | * |
| 337 | * @param container |
| 338 | */ |
| 339 | public void setSelectedContainer(IContainer container) { |
| 340 | selectedContainer = container; |
| 341 | |
| 342 | // expand to and select the specified container |
| 343 | List itemsToExpand = new ArrayList(); |
| 344 | IContainer parent = container.getParent(); |
| 345 | while (parent != null) { |
| 346 | itemsToExpand.add(0, parent); |
| 347 | parent = parent.getParent(); |
| 348 | } |
| 349 | treeViewer.setExpandedElements(itemsToExpand.toArray()); |
| 350 | treeViewer.setSelection(new StructuredSelection(container), true); |
| 351 | } |
| 352 | } |