KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > ui > FileSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.externaltools.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.regex.Pattern JavaDoc;
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.viewers.DoubleClickEvent;
23 import org.eclipse.jface.viewers.IDoubleClickListener;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.ITreeContentProvider;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ControlEvent;
30 import org.eclipse.swt.events.ControlListener;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.TableColumn;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.externaltools.internal.model.IExternalToolsHelpContextIds;
37 import org.eclipse.ui.model.WorkbenchContentProvider;
38 import org.eclipse.ui.model.WorkbenchLabelProvider;
39
40 /**
41  * Dialog for selecting a file in the workspace. Derived from
42  * org.eclipse.ui.dialogs.ResourceSelectionDialog
43  */

44 public class FileSelectionDialog extends MessageDialog {
45     // the root element to populate the viewer with
46
private IAdaptable root;
47
48     // the visual selection widget group
49
private TreeAndListGroup selectionGroup;
50     // constants
51
private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
52     private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300;
53     /**
54      * The file(s) selected by the user.
55      */

56     private IStructuredSelection result = null;
57
58     private boolean allowMultiselection= false;
59
60     private Pattern JavaDoc fPattern;
61     /**
62      * Creates a resource selection dialog rooted at the given element.
63      *
64      * @param parentShell
65      * the parent shell
66      * @param rootElement
67      * the root element to populate this dialog with
68      * @param message
69      * the message to be displayed at the top of this dialog, or
70      * <code>null</code> to display a default message
71      */

72     public FileSelectionDialog(Shell parentShell, IAdaptable rootElement, String JavaDoc message) {
73         super(parentShell, ExternalToolsUIMessages.FileSelectionDialog_Choose_Location_1, null, message, MessageDialog.NONE, new String JavaDoc[] { ExternalToolsUIMessages.FileSelectionDialog_Ok_2, ExternalToolsUIMessages.FileSelectionDialog_Cancel_3}, 0);
74         root = rootElement;
75         setShellStyle(getShellStyle() | SWT.RESIZE);
76     }
77     
78     /**
79      * Limits the files displayed in this dialog to files matching the given
80      * pattern. The string can be a filename or a regular expression containing
81      * '*' for any series of characters or '?' for any single character.
82      *
83      * @param pattern
84      * a pattern used to filter the displayed files or <code>null</code>
85      * to display all files. If a pattern is supplied, only files
86      * whose names match the given pattern will be available for
87      * selection.
88      * @param ignoreCase
89      * if true, case is ignored. If the pattern argument is <code>null</code>,
90      * this argument is ignored.
91      */

92     public void setFileFilter(String JavaDoc pattern, boolean ignoreCase) {
93         if (pattern != null) {
94             if (ignoreCase) {
95                 fPattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
96             } else {
97                 fPattern = Pattern.compile(pattern);
98             }
99         } else {
100             fPattern = null;
101         }
102     }
103     
104     /*
105      * (non-Javadoc) Method declared in Window.
106      */

107     protected void configureShell(Shell shell) {
108         super.configureShell(shell);
109         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IExternalToolsHelpContextIds.FILE_SELECTION_DIALOG);
110     }
111
112     protected void createButtonsForButtonBar(Composite parent) {
113         super.createButtonsForButtonBar(parent);
114         initializeDialog();
115     }
116     
117     /*
118      * (non-Javadoc) Method declared on Dialog.
119      */

120     protected Control createDialogArea(Composite parent) {
121         // page group
122
Composite composite = (Composite) super.createDialogArea(parent);
123
124         //create the input element, which has the root resource
125
//as its only child
126
selectionGroup =
127             new TreeAndListGroup(
128                 composite,
129                 root,
130                 getResourceProvider(
131                     IResource.FOLDER | IResource.PROJECT | IResource.ROOT),
132                 new WorkbenchLabelProvider(),
133                 getResourceProvider(IResource.FILE),
134                 new WorkbenchLabelProvider(),
135                 SWT.NONE,
136                 // since this page has no other significantly-sized
137
// widgets we need to hardcode the combined widget's
138
// size, otherwise it will open too small
139
SIZING_SELECTION_WIDGET_WIDTH, SIZING_SELECTION_WIDGET_HEIGHT,
140                 allowMultiselection);
141         
142         composite.addControlListener(new ControlListener() {
143             public void controlMoved(ControlEvent e) {
144             }
145             public void controlResized(ControlEvent e) {
146                 //Also try and reset the size of the columns as appropriate
147
TableColumn[] columns =
148                     selectionGroup.getListTable().getColumns();
149                 for (int i = 0; i < columns.length; i++) {
150                     columns[i].pack();
151                 }
152             }
153         });
154
155         return composite;
156     }
157     /**
158      * Returns a content provider for <code>IResource</code> s that returns
159      * only children of the given resource type.
160      */

161     private ITreeContentProvider getResourceProvider(final int resourceType) {
162         return new WorkbenchContentProvider() {
163             public Object JavaDoc[] getChildren(Object JavaDoc o) {
164                 if (o instanceof IContainer) {
165                     IResource[] members = null;
166                     try {
167                         members = ((IContainer) o).members();
168                         List JavaDoc accessibleMembers = new ArrayList JavaDoc(members.length);
169                         for (int i = 0; i < members.length; i++) {
170                             IResource resource = members[i];
171                             if (resource.isAccessible()) {
172                                 accessibleMembers.add(resource);
173                             }
174                         }
175                         members =
176                             (IResource[]) accessibleMembers.toArray(
177                                 new IResource[accessibleMembers.size()]);
178                     } catch (CoreException e) {
179                         //just return an empty set of children
180
return new Object JavaDoc[0];
181                     }
182
183                     //filter out the desired resource types
184
ArrayList JavaDoc results = new ArrayList JavaDoc();
185                     for (int i = 0; i < members.length; i++) {
186                         //And the test bits with the resource types to see if
187
// they are what we want
188
if ((members[i].getType() & resourceType) > 0) {
189                             if (members[i].getType() == IResource.FILE
190                                 && fPattern != null
191                                 && !fPattern.matcher(members[i].getName()).find()) {
192                                 continue;
193                             }
194                             results.add(members[i]);
195                         }
196                     }
197                     return results.toArray();
198                 }
199                 
200                 return new Object JavaDoc[0];
201             }
202         };
203     }
204     /**
205      * Initializes this dialog's controls.
206      */

207     private void initializeDialog() {
208         selectionGroup
209             .addSelectionChangedListener(new ISelectionChangedListener() {
210             public void selectionChanged(SelectionChangedEvent event) {
211                 getButton(IDialogConstants.OK_ID).setEnabled(
212                     !selectionGroup.getListTableSelection().isEmpty());
213             }
214         });
215         selectionGroup.addDoubleClickListener(new IDoubleClickListener() {
216             public void doubleClick(DoubleClickEvent event) {
217                 buttonPressed(IDialogConstants.OK_ID);
218             }
219         });
220
221         getButton(IDialogConstants.OK_ID).setEnabled(false);
222     }
223
224     /**
225      * Returns the file the user chose or <code>null</code> if none.
226      */

227     public IStructuredSelection getResult() {
228         return result;
229     }
230
231     protected void buttonPressed(int buttonId) {
232         if (buttonId == IDialogConstants.OK_ID) {
233             result= selectionGroup.getListTableSelection();
234         }
235         super.buttonPressed(buttonId);
236     }
237     /**
238      * Sets whether this dialog will allow multi-selection.
239      * Must be called before <code>open</code>
240      * @param allowMultiselection whether to allow multi-selection in the dialog
241      */

242     public void setAllowMultiselection(boolean allowMultiselection) {
243         this.allowMultiselection= allowMultiselection;
244     }
245 }
246
Popular Tags