001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.jface.viewers.ILabelProvider;
014: import org.eclipse.jface.viewers.ISelectionChangedListener;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.viewers.ITreeContentProvider;
017: import org.eclipse.jface.viewers.SelectionChangedEvent;
018: import org.eclipse.jface.viewers.StructuredSelection;
019: import org.eclipse.jface.viewers.TreeViewer;
020: import org.eclipse.jface.window.Window;
021: import org.eclipse.pde.internal.ui.PDEUIMessages;
022: import org.eclipse.pde.internal.ui.util.SWTUtil;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.SelectionAdapter;
025: import org.eclipse.swt.events.SelectionEvent;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.widgets.Button;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.Shell;
031: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
032: import org.eclipse.ui.dialogs.NewFolderDialog;
033: import org.eclipse.ui.views.navigator.ResourceComparator;
034:
035: public class FolderSelectionDialog extends ElementTreeSelectionDialog
036: implements ISelectionChangedListener {
037:
038: private Button fNewFolderButton;
039: private IContainer fSelectedContainer;
040:
041: public FolderSelectionDialog(Shell parent,
042: ILabelProvider labelProvider,
043: ITreeContentProvider contentProvider) {
044: super (parent, labelProvider, contentProvider);
045: setComparator(new ResourceComparator(ResourceComparator.NAME));
046: }
047:
048: /* (non-Javadoc)
049: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
050: */
051: protected Control createDialogArea(Composite parent) {
052: Composite result = (Composite) super .createDialogArea(parent);
053:
054: getTreeViewer().addSelectionChangedListener(this );
055: getTreeViewer().expandToLevel(2);
056: fNewFolderButton = new Button(result, SWT.PUSH);
057: fNewFolderButton
058: .setText(PDEUIMessages.BuildEditor_SourceFolderSelectionDialog_button);
059: fNewFolderButton.addSelectionListener(new SelectionAdapter() {
060: public void widgetSelected(SelectionEvent event) {
061: newFolderButtonPressed();
062: }
063: });
064: fNewFolderButton.setFont(parent.getFont());
065: fNewFolderButton.setLayoutData(new GridData());
066: SWTUtil.setButtonDimensionHint(fNewFolderButton);
067:
068: applyDialogFont(result);
069: return result;
070: }
071:
072: private void updateNewFolderButtonState() {
073: IStructuredSelection selection = (IStructuredSelection) getTreeViewer()
074: .getSelection();
075: fSelectedContainer = null;
076: if (selection.size() == 1) {
077: Object first = selection.getFirstElement();
078: if (first instanceof IContainer) {
079: fSelectedContainer = (IContainer) first;
080: }
081: }
082: fNewFolderButton.setEnabled(fSelectedContainer != null);
083: }
084:
085: protected void newFolderButtonPressed() {
086: NewFolderDialog dialog = new NewFolderDialog(getShell(),
087: fSelectedContainer);
088: if (dialog.open() == Window.OK) {
089: TreeViewer treeViewer = getTreeViewer();
090: treeViewer.refresh(fSelectedContainer);
091: Object createdFolder;
092: if (dialog.getResult() != null) {
093: createdFolder = dialog.getResult()[0];
094: treeViewer.reveal(createdFolder);
095: treeViewer.setSelection(new StructuredSelection(
096: createdFolder));
097: }
098: }
099: }
100:
101: /* (non-Javadoc)
102: * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
103: */
104: public void selectionChanged(SelectionChangedEvent event) {
105: updateNewFolderButtonState();
106: }
107:
108: }
|