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