1 11 package org.eclipse.pde.internal.ui.wizards; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.jface.viewers.ILabelProvider; 15 import org.eclipse.jface.viewers.ISelectionChangedListener; 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 import org.eclipse.jface.viewers.ITreeContentProvider; 18 import org.eclipse.jface.viewers.SelectionChangedEvent; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 import org.eclipse.jface.viewers.TreeViewer; 21 import org.eclipse.jface.window.Window; 22 import org.eclipse.pde.internal.ui.PDEUIMessages; 23 import org.eclipse.pde.internal.ui.util.SWTUtil; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.SelectionAdapter; 26 import org.eclipse.swt.events.SelectionEvent; 27 import org.eclipse.swt.layout.GridData; 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.dialogs.ElementTreeSelectionDialog; 33 import org.eclipse.ui.dialogs.NewFolderDialog; 34 import org.eclipse.ui.views.navigator.ResourceComparator; 35 36 public class FolderSelectionDialog extends ElementTreeSelectionDialog implements ISelectionChangedListener { 37 38 private Button fNewFolderButton; 39 private IContainer fSelectedContainer; 40 41 public FolderSelectionDialog(Shell parent, ILabelProvider labelProvider, ITreeContentProvider contentProvider) { 42 super(parent, labelProvider, contentProvider); 43 setComparator(new ResourceComparator(ResourceComparator.NAME)); 44 } 45 46 49 protected Control createDialogArea(Composite parent) { 50 Composite result= (Composite)super.createDialogArea(parent); 51 52 getTreeViewer().addSelectionChangedListener(this); 53 getTreeViewer().expandToLevel(2); 54 fNewFolderButton = new Button(result, SWT.PUSH); 55 fNewFolderButton.setText(PDEUIMessages.BuildEditor_SourceFolderSelectionDialog_button); 56 fNewFolderButton.addSelectionListener(new SelectionAdapter() { 57 public void widgetSelected(SelectionEvent event) { 58 newFolderButtonPressed(); 59 } 60 }); 61 fNewFolderButton.setFont(parent.getFont()); 62 fNewFolderButton.setLayoutData(new GridData()); 63 SWTUtil.setButtonDimensionHint(fNewFolderButton); 64 65 applyDialogFont(result); 66 return result; 67 } 68 69 private void updateNewFolderButtonState() { 70 IStructuredSelection selection= (IStructuredSelection) getTreeViewer().getSelection(); 71 fSelectedContainer= null; 72 if (selection.size() == 1) { 73 Object first= selection.getFirstElement(); 74 if (first instanceof IContainer) { 75 fSelectedContainer= (IContainer) first; 76 } 77 } 78 fNewFolderButton.setEnabled(fSelectedContainer != null); 79 } 80 81 protected void newFolderButtonPressed() { 82 NewFolderDialog dialog= new NewFolderDialog(getShell(), fSelectedContainer); 83 if (dialog.open() == Window.OK) { 84 TreeViewer treeViewer= getTreeViewer(); 85 treeViewer.refresh(fSelectedContainer); 86 Object createdFolder; 87 if (dialog.getResult()!=null){ 88 createdFolder= dialog.getResult()[0]; 89 treeViewer.reveal(createdFolder); 90 treeViewer.setSelection(new StructuredSelection(createdFolder)); 91 } 92 } 93 } 94 95 98 public void selectionChanged(SelectionChangedEvent event) { 99 updateNewFolderButtonState(); 100 } 101 102 103 104 105 } 106 | Popular Tags |