1) Fixed issue #705: Take the global 'Undo history size' to overwrite the default...
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / 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
39         // constants
40         private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
41
42         private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300;
43
44         /**
45          * The file selected by the user.
46          */
47         private IFile result = null;
48
49         /**
50          * String matcher used to filter content
51          */
52         private StringMatcher stringMatcher = null;
53
54         /**
55          * Creates a resource selection dialog rooted at the given element.
56          * 
57          * @param parentShell
58          *            the parent shell
59          * @param rootElement
60          *            the root element to populate this dialog with
61          * @param message
62          *            the message to be displayed at the top of this dialog, or
63          *            <code>null</code> to display a default message
64          */
65         public FileSelectionDialog(Shell parentShell, IAdaptable rootElement,
66                         String message) {
67                 super(parentShell, "Add Build File", null, message, MessageDialog.NONE,
68                                 new String[] { "Ok", "Cancel" }, 0);
69                 root = rootElement;
70                 setShellStyle(getShellStyle() | SWT.RESIZE);
71         }
72
73         /**
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.
77          * 
78          * @param pattern
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.
83          * @param ignoreCase
84          *            if true, case is ignored. If the pattern argument is
85          *            <code>null</code>, this argument is ignored.
86          */
87         public void setFileFilter(String pattern, boolean ignoreCase) {
88                 if (pattern != null) {
89                         stringMatcher = new StringMatcher(pattern, ignoreCase, false);
90                 } else {
91                         stringMatcher = null;
92                 }
93         }
94
95         /*
96          * (non-Javadoc) Method declared in Window.
97          */
98         protected void configureShell(Shell shell) {
99                 super.configureShell(shell);
100                 // WorkbenchHelp.setHelp(shell,
101                 // IHelpContextIds.RESOURCE_SELECTION_DIALOG);
102         }
103
104         protected void createButtonsForButtonBar(Composite parent) {
105                 super.createButtonsForButtonBar(parent);
106                 initializeDialog();
107         }
108
109         /*
110          * (non-Javadoc) Method declared on Dialog.
111          */
112         protected Control createDialogArea(Composite parent) {
113                 // page group
114                 Composite composite = (Composite) super.createDialogArea(parent);
115
116                 // create the input element, which has the root resource
117                 // as its only child
118
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);
128
129                 composite.addControlListener(new ControlListener() {
130                         public void controlMoved(ControlEvent e) {
131                         };
132
133                         public void controlResized(ControlEvent e) {
134                                 // Also try and reset the size of the columns as appropriate
135                                 TableColumn[] columns = selectionGroup.getListTable()
136                                                 .getColumns();
137                                 for (int i = 0; i < columns.length; i++) {
138                                         columns[i].pack();
139                                 }
140                         }
141                 });
142
143                 return composite;
144         }
145
146         /**
147          * Returns a content provider for <code>IResource</code>s that returns
148          * only children of the given resource type.
149          */
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;
155                                         try {
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);
162                                                         }
163                                                 }
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];
169                                         }
170
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]
180                                                                                         .getName())) {
181                                                                 continue;
182                                                         }
183                                                         results.add(members[i]);
184                                                 }
185                                         }
186                                         return results.toArray();
187                                 } else {
188                                         return new Object[0];
189                                 }
190                         }
191                 };
192         }
193
194         /**
195          * Initializes this dialog's controls.
196          */
197         private void initializeDialog() {
198                 selectionGroup
199                                 .addSelectionChangedListener(new ISelectionChangedListener() {
200                                         public void selectionChanged(SelectionChangedEvent event) {
201                                                 getOkButton().setEnabled(
202                                                                 !selectionGroup.getListTableSelection()
203                                                                                 .isEmpty());
204                                         }
205                                 });
206
207                 getOkButton().setEnabled(false);
208         }
209
210         /**
211          * Returns this dialog's OK button.
212          */
213         protected Button getOkButton() {
214                 return getButton(0);
215         }
216
217         /**
218          * Returns the file the user chose or <code>null</code> if none.
219          */
220         public IFile getResult() {
221                 return result;
222         }
223
224         protected void buttonPressed(int buttonId) {
225                 if (buttonId == 0) {
226                         Object resource = selectionGroup.getListTableSelection()
227                                         .getFirstElement();
228                         if (resource instanceof IFile) {
229                                 result = (IFile) resource;
230                         }
231                 }
232                 super.buttonPressed(buttonId);
233         }
234
235 }