1 package net.sourceforge.phpdt.externaltools.internal.ui;
3 import java.util.ArrayList;
6 import net.sourceforge.phpdt.externaltools.model.StringMatcher;
8 import org.eclipse.core.resources.IContainer;
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.resources.IResource;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IAdaptable;
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.ISelectionChangedListener;
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16 import org.eclipse.jface.viewers.SelectionChangedEvent;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.ControlEvent;
19 import org.eclipse.swt.events.ControlListener;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.swt.widgets.TableColumn;
25 import org.eclipse.ui.model.WorkbenchContentProvider;
26 import org.eclipse.ui.model.WorkbenchLabelProvider;
29 * Dialog for selecting a file in the workspace. Derived from
30 * org.eclipse.ui.dialogs.ResourceSelectionDialog
32 public class FileSelectionDialog extends MessageDialog {
33 // the root element to populate the viewer with
34 private IAdaptable root;
36 // the visual selection widget group
37 private TreeAndListGroup selectionGroup;
40 private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
42 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300;
45 * The file selected by the user.
47 private IFile result = null;
50 * String matcher used to filter content
52 private StringMatcher stringMatcher = null;
55 * Creates a resource selection dialog rooted at the given element.
60 * the root element to populate this dialog with
62 * the message to be displayed at the top of this dialog, or
63 * <code>null</code> to display a default message
65 public FileSelectionDialog(Shell parentShell, IAdaptable rootElement,
67 super(parentShell, "Add Build File", null, message, MessageDialog.NONE,
68 new String[] { "Ok", "Cancel" }, 0);
70 setShellStyle(getShellStyle() | SWT.RESIZE);
74 * Limits the files displayed in this dialog to files matching the given
75 * pattern. The string can be a filename or a regular expression containing
76 * '*' for any series of characters or '?' for any single character.
79 * a pattern used to filter the displayed files or
80 * <code>null</code> to display all files. If a pattern is
81 * supplied, only files whose names match the given pattern will
82 * be available for selection.
84 * if true, case is ignored. If the pattern argument is
85 * <code>null</code>, this argument is ignored.
87 public void setFileFilter(String pattern, boolean ignoreCase) {
88 if (pattern != null) {
89 stringMatcher = new StringMatcher(pattern, ignoreCase, false);
96 * (non-Javadoc) Method declared in Window.
98 protected void configureShell(Shell shell) {
99 super.configureShell(shell);
100 // WorkbenchHelp.setHelp(shell,
101 // IHelpContextIds.RESOURCE_SELECTION_DIALOG);
104 protected void createButtonsForButtonBar(Composite parent) {
105 super.createButtonsForButtonBar(parent);
110 * (non-Javadoc) Method declared on Dialog.
112 protected Control createDialogArea(Composite parent) {
114 Composite composite = (Composite) super.createDialogArea(parent);
116 // create the input element, which has the root resource
119 selectionGroup = new TreeAndListGroup(composite, root,
120 getResourceProvider(IResource.FOLDER | IResource.PROJECT
121 | IResource.ROOT), new WorkbenchLabelProvider(),
122 getResourceProvider(IResource.FILE),
123 new WorkbenchLabelProvider(), SWT.NONE,
124 // since this page has no other significantly-sized
125 // widgets we need to hardcode the combined widget's
126 // size, otherwise it will open too small
127 SIZING_SELECTION_WIDGET_WIDTH, SIZING_SELECTION_WIDGET_HEIGHT);
129 composite.addControlListener(new ControlListener() {
130 public void controlMoved(ControlEvent e) {
133 public void controlResized(ControlEvent e) {
134 // Also try and reset the size of the columns as appropriate
135 TableColumn[] columns = selectionGroup.getListTable()
137 for (int i = 0; i < columns.length; i++) {
147 * Returns a content provider for <code>IResource</code>s that returns
148 * only children of the given resource type.
150 private ITreeContentProvider getResourceProvider(final int resourceType) {
151 return new WorkbenchContentProvider() {
152 public Object[] getChildren(Object o) {
153 if (o instanceof IContainer) {
154 IResource[] members = null;
156 members = ((IContainer) o).members();
157 List accessibleMembers = new ArrayList(members.length);
158 for (int i = 0; i < members.length; i++) {
159 IResource resource = members[i];
160 if (resource.isAccessible()) {
161 accessibleMembers.add(resource);
164 members = (IResource[]) accessibleMembers
165 .toArray(new IResource[accessibleMembers.size()]);
166 } catch (CoreException e) {
167 // just return an empty set of children
168 return new Object[0];
171 // filter out the desired resource types
172 ArrayList results = new ArrayList();
173 for (int i = 0; i < members.length; i++) {
174 // And the test bits with the resource types to see if
175 // they are what we want
176 if ((members[i].getType() & resourceType) > 0) {
177 if (members[i].getType() == IResource.FILE
178 && stringMatcher != null
179 && !stringMatcher.match(members[i]
183 results.add(members[i]);
186 return results.toArray();
188 return new Object[0];
195 * Initializes this dialog's controls.
197 private void initializeDialog() {
199 .addSelectionChangedListener(new ISelectionChangedListener() {
200 public void selectionChanged(SelectionChangedEvent event) {
201 getOkButton().setEnabled(
202 !selectionGroup.getListTableSelection()
207 getOkButton().setEnabled(false);
211 * Returns this dialog's OK button.
213 protected Button getOkButton() {
218 * Returns the file the user chose or <code>null</code> if none.
220 public IFile getResult() {
224 protected void buttonPressed(int buttonId) {
226 Object resource = selectionGroup.getListTableSelection()
228 if (resource instanceof IFile) {
229 result = (IFile) resource;
232 super.buttonPressed(buttonId);