misc
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / externaltools / internal / ui / FileSelectionDialog.java
1 package net.sourceforge.phpdt.externaltools.internal.ui;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
26
27 /**
28  * Dialog for selecting a file in the workspace. Derived from 
29  * org.eclipse.ui.dialogs.ResourceSelectionDialog
30  */
31 public class FileSelectionDialog extends MessageDialog {
32         // the root element to populate the viewer with
33         private IAdaptable                                      root;
34
35         // the visual selection widget group
36         private TreeAndListGroup        selectionGroup;
37         // constants
38         private final static int                        SIZING_SELECTION_WIDGET_WIDTH = 400;
39         private final static int                        SIZING_SELECTION_WIDGET_HEIGHT = 300;
40         /**
41          * The file selected by the user.
42          */
43         private IFile result= null;
44         /**
45          * String matcher used to filter content
46          */
47         private StringMatcher stringMatcher= null;
48 /**
49  * Creates a resource selection dialog rooted at the given element.
50  *
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
55  */
56 public FileSelectionDialog(Shell parentShell, IAdaptable rootElement, String message) {
57         super(parentShell, "Add Build File", null, message, MessageDialog.NONE, new String[] {"Ok", "Cancel"}, 0);
58         root = rootElement;
59         setShellStyle(getShellStyle() | SWT.RESIZE);
60 }
61 /**
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.
65  * 
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.
71  */
72 public void setFileFilter(String pattern, boolean ignoreCase) {
73         if (pattern != null) {
74                 stringMatcher= new StringMatcher(pattern, ignoreCase, false);
75         } else {
76                 stringMatcher= null;
77         }
78 }
79 /* (non-Javadoc)
80  * Method declared in Window.
81  */
82 protected void configureShell(Shell shell) {
83         super.configureShell(shell);
84         //WorkbenchHelp.setHelp(shell, IHelpContextIds.RESOURCE_SELECTION_DIALOG);
85 }
86
87 protected void createButtonsForButtonBar(Composite parent) {
88         super.createButtonsForButtonBar(parent);
89         initializeDialog();
90 }
91 /* (non-Javadoc)
92  * Method declared on Dialog.
93  */
94 protected Control createDialogArea(Composite parent) {
95         // page group
96         Composite composite = (Composite) super.createDialogArea(parent);
97
98         //create the input element, which has the root resource
99         //as its only child
100
101         selectionGroup =
102                 new TreeAndListGroup(
103                         composite,
104                         root,
105                         getResourceProvider(IResource.FOLDER | IResource.PROJECT | IResource.ROOT),
106                         new WorkbenchLabelProvider(),
107                         getResourceProvider(IResource.FILE),
108                         new WorkbenchLabelProvider(),
109                         SWT.NONE,
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);
115
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++) {
122                                 columns[i].pack();
123                         }
124                 }
125         });
126
127         return composite;
128 }
129 /**
130  * Returns a content provider for <code>IResource</code>s that returns
131  * only children of the given resource type.
132  */
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;
138                                 try {
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);
145                                                 }
146                                         }
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];
151                                 }
152
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())) {
161                                                         continue;
162                                                 }
163                                                 results.add(members[i]);
164                                         }
165                                 }
166                                 return results.toArray();
167                         } else {
168                                 return new Object[0];
169                         }
170                 }
171         };
172 }
173 /**
174  * Initializes this dialog's controls.
175  */
176 private void initializeDialog() {
177         selectionGroup.addSelectionChangedListener(new ISelectionChangedListener() {
178                 public void selectionChanged(SelectionChangedEvent event) {
179                         getOkButton().setEnabled(!selectionGroup.getListTableSelection().isEmpty());
180                 }
181         });
182
183         getOkButton().setEnabled(false);
184 }
185
186 /**
187  * Returns this dialog's OK button.
188  */
189 protected Button getOkButton() {
190         return getButton(0);
191 }
192 /**
193  * Returns the file the user chose or <code>null</code> if none.
194  */
195 public IFile getResult() {
196         return result;
197 }
198
199 protected void buttonPressed(int buttonId) {
200         if (buttonId == 0) {
201                 Object resource= selectionGroup.getListTableSelection().getFirstElement();
202                 if (resource instanceof IFile) {
203                         result = (IFile) resource;
204                 }
205         }
206         super.buttonPressed(buttonId);
207 }
208
209 }