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;
39 private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
40 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300;
42 * The file selected by the user.
44 private IFile result= null;
46 * String matcher used to filter content
48 private StringMatcher stringMatcher= null;
50 * Creates a resource selection dialog rooted at the given element.
52 * @param parentShell the parent shell
53 * @param rootElement the root element to populate this dialog with
54 * @param message the message to be displayed at the top of this dialog, or
55 * <code>null</code> to display a default message
57 public FileSelectionDialog(Shell parentShell, IAdaptable rootElement, String message) {
58 super(parentShell, "Add Build File", null, message, MessageDialog.NONE, new String[] {"Ok", "Cancel"}, 0);
60 setShellStyle(getShellStyle() | SWT.RESIZE);
63 * Limits the files displayed in this dialog to files matching the given
64 * pattern. The string can be a filename or a regular expression containing
65 * '*' for any series of characters or '?' for any single character.
67 * @param pattern a pattern used to filter the displayed files or
68 * <code>null</code> to display all files. If a pattern is supplied, only files
69 * whose names match the given pattern will be available for selection.
70 * @param ignoreCase if true, case is ignored. If the pattern argument is
71 * <code>null</code>, this argument is ignored.
73 public void setFileFilter(String pattern, boolean ignoreCase) {
74 if (pattern != null) {
75 stringMatcher= new StringMatcher(pattern, ignoreCase, false);
81 * Method declared in Window.
83 protected void configureShell(Shell shell) {
84 super.configureShell(shell);
85 //WorkbenchHelp.setHelp(shell, IHelpContextIds.RESOURCE_SELECTION_DIALOG);
88 protected void createButtonsForButtonBar(Composite parent) {
89 super.createButtonsForButtonBar(parent);
93 * Method declared on Dialog.
95 protected Control createDialogArea(Composite parent) {
97 Composite composite = (Composite) super.createDialogArea(parent);
99 //create the input element, which has the root resource
103 new TreeAndListGroup(
106 getResourceProvider(IResource.FOLDER | IResource.PROJECT | IResource.ROOT),
107 new WorkbenchLabelProvider(),
108 getResourceProvider(IResource.FILE),
109 new WorkbenchLabelProvider(),
111 // since this page has no other significantly-sized
112 // widgets we need to hardcode the combined widget's
113 // size, otherwise it will open too small
114 SIZING_SELECTION_WIDGET_WIDTH,
115 SIZING_SELECTION_WIDGET_HEIGHT);
117 composite.addControlListener(new ControlListener() {
118 public void controlMoved(ControlEvent e) {};
119 public void controlResized(ControlEvent e) {
120 //Also try and reset the size of the columns as appropriate
121 TableColumn[] columns = selectionGroup.getListTable().getColumns();
122 for (int i = 0; i < columns.length; i++) {
131 * Returns a content provider for <code>IResource</code>s that returns
132 * only children of the given resource type.
134 private ITreeContentProvider getResourceProvider(final int resourceType) {
135 return new WorkbenchContentProvider() {
136 public Object[] getChildren(Object o) {
137 if (o instanceof IContainer) {
138 IResource[] members = null;
140 members = ((IContainer)o).members();
141 List accessibleMembers= new ArrayList(members.length);
142 for (int i = 0; i < members.length; i++) {
143 IResource resource = members[i];
144 if (resource.isAccessible()) {
145 accessibleMembers.add(resource);
148 members= (IResource[])accessibleMembers.toArray(new IResource[accessibleMembers.size()]);
149 } catch (CoreException e) {
150 //just return an empty set of children
151 return new Object[0];
154 //filter out the desired resource types
155 ArrayList results = new ArrayList();
156 for (int i = 0; i < members.length; i++) {
157 //And the test bits with the resource types to see if they are what we want
158 if ((members[i].getType() & resourceType) > 0) {
159 if (members[i].getType() == IResource.FILE &&
160 stringMatcher != null &&
161 !stringMatcher.match(members[i].getName())) {
164 results.add(members[i]);
167 return results.toArray();
169 return new Object[0];
175 * Initializes this dialog's controls.
177 private void initializeDialog() {
178 selectionGroup.addSelectionChangedListener(new ISelectionChangedListener() {
179 public void selectionChanged(SelectionChangedEvent event) {
180 getOkButton().setEnabled(!selectionGroup.getListTableSelection().isEmpty());
184 getOkButton().setEnabled(false);
188 * Returns this dialog's OK button.
190 protected Button getOkButton() {
194 * Returns the file the user chose or <code>null</code> if none.
196 public IFile getResult() {
200 protected void buttonPressed(int buttonId) {
202 Object resource= selectionGroup.getListTableSelection().getFirstElement();
203 if (resource instanceof IFile) {
204 result = (IFile) resource;
207 super.buttonPressed(buttonId);