1 package net.sourceforge.phpdt.externaltools.internal.ui;
3 import java.util.ArrayList;
6 import org.eclipse.core.resources.IContainer;
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IResource;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IAdaptable;
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.jface.viewers.ISelectionChangedListener;
13 import org.eclipse.jface.viewers.ITreeContentProvider;
14 import org.eclipse.jface.viewers.SelectionChangedEvent;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.ControlEvent;
17 import org.eclipse.swt.events.ControlListener;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.swt.widgets.TableColumn;
23 import net.sourceforge.phpdt.externaltools.model.StringMatcher;
24 import org.eclipse.ui.model.WorkbenchContentProvider;
25 import org.eclipse.ui.model.WorkbenchLabelProvider;
28 * Dialog for selecting a file in the workspace. Derived from
29 * org.eclipse.ui.dialogs.ResourceSelectionDialog
31 public class FileSelectionDialog extends MessageDialog {
32 // the root element to populate the viewer with
33 private IAdaptable root;
35 // the visual selection widget group
36 private TreeAndListGroup selectionGroup;
38 private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
39 private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300;
41 * The file selected by the user.
43 private IFile result= null;
45 * String matcher used to filter content
47 private StringMatcher stringMatcher= null;
49 * Creates a resource selection dialog rooted at the given element.
51 * @param parentShell the parent shell
52 * @param rootElement the root element to populate this dialog with
53 * @param message the message to be displayed at the top of this dialog, or
54 * <code>null</code> to display a default message
56 public FileSelectionDialog(Shell parentShell, IAdaptable rootElement, String message) {
57 super(parentShell, "Add Build File", null, message, MessageDialog.NONE, new String[] {"Ok", "Cancel"}, 0);
59 setShellStyle(getShellStyle() | SWT.RESIZE);
62 * Limits the files displayed in this dialog to files matching the given
63 * pattern. The string can be a filename or a regular expression containing
64 * '*' for any series of characters or '?' for any single character.
66 * @param pattern a pattern used to filter the displayed files or
67 * <code>null</code> to display all files. If a pattern is supplied, only files
68 * whose names match the given pattern will be available for selection.
69 * @param ignoreCase if true, case is ignored. If the pattern argument is
70 * <code>null</code>, this argument is ignored.
72 public void setFileFilter(String pattern, boolean ignoreCase) {
73 if (pattern != null) {
74 stringMatcher= new StringMatcher(pattern, ignoreCase, false);
80 * Method declared in Window.
82 protected void configureShell(Shell shell) {
83 super.configureShell(shell);
84 //WorkbenchHelp.setHelp(shell, IHelpContextIds.RESOURCE_SELECTION_DIALOG);
87 protected void createButtonsForButtonBar(Composite parent) {
88 super.createButtonsForButtonBar(parent);
92 * Method declared on Dialog.
94 protected Control createDialogArea(Composite parent) {
96 Composite composite = (Composite) super.createDialogArea(parent);
98 //create the input element, which has the root resource
102 new TreeAndListGroup(
105 getResourceProvider(IResource.FOLDER | IResource.PROJECT | IResource.ROOT),
106 new WorkbenchLabelProvider(),
107 getResourceProvider(IResource.FILE),
108 new WorkbenchLabelProvider(),
110 // since this page has no other significantly-sized
111 // widgets we need to hardcode the combined widget's
112 // size, otherwise it will open too small
113 SIZING_SELECTION_WIDGET_WIDTH,
114 SIZING_SELECTION_WIDGET_HEIGHT);
116 composite.addControlListener(new ControlListener() {
117 public void controlMoved(ControlEvent e) {};
118 public void controlResized(ControlEvent e) {
119 //Also try and reset the size of the columns as appropriate
120 TableColumn[] columns = selectionGroup.getListTable().getColumns();
121 for (int i = 0; i < columns.length; i++) {
130 * Returns a content provider for <code>IResource</code>s that returns
131 * only children of the given resource type.
133 private ITreeContentProvider getResourceProvider(final int resourceType) {
134 return new WorkbenchContentProvider() {
135 public Object[] getChildren(Object o) {
136 if (o instanceof IContainer) {
137 IResource[] members = null;
139 members = ((IContainer)o).members();
140 List accessibleMembers= new ArrayList(members.length);
141 for (int i = 0; i < members.length; i++) {
142 IResource resource = members[i];
143 if (resource.isAccessible()) {
144 accessibleMembers.add(resource);
147 members= (IResource[])accessibleMembers.toArray(new IResource[accessibleMembers.size()]);
148 } catch (CoreException e) {
149 //just return an empty set of children
150 return new Object[0];
153 //filter out the desired resource types
154 ArrayList results = new ArrayList();
155 for (int i = 0; i < members.length; i++) {
156 //And the test bits with the resource types to see if they are what we want
157 if ((members[i].getType() & resourceType) > 0) {
158 if (members[i].getType() == IResource.FILE &&
159 stringMatcher != null &&
160 !stringMatcher.match(members[i].getName())) {
163 results.add(members[i]);
166 return results.toArray();
168 return new Object[0];
174 * Initializes this dialog's controls.
176 private void initializeDialog() {
177 selectionGroup.addSelectionChangedListener(new ISelectionChangedListener() {
178 public void selectionChanged(SelectionChangedEvent event) {
179 getOkButton().setEnabled(!selectionGroup.getListTableSelection().isEmpty());
183 getOkButton().setEnabled(false);
187 * Returns this dialog's OK button.
189 protected Button getOkButton() {
193 * Returns the file the user chose or <code>null</code> if none.
195 public IFile getResult() {
199 protected void buttonPressed(int buttonId) {
201 Object resource= selectionGroup.getListTableSelection().getFirstElement();
202 if (resource instanceof IFile) {
203 result = (IFile) resource;
206 super.buttonPressed(buttonId);