KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > FileSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ant.internal.ui.preferences;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.ant.internal.ui.AntUIPlugin;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
32 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
33 import org.eclipse.ui.model.WorkbenchContentProvider;
34 import org.eclipse.ui.model.WorkbenchLabelProvider;
35 import org.eclipse.ui.views.navigator.ResourceComparator;
36
37 public class FileSelectionDialog extends ElementTreeSelectionDialog {
38     
39     private FileFilter fFilter;
40     private String JavaDoc fFilterMessage;
41     private boolean fShowAll= false;
42     private final static String JavaDoc DIALOG_SETTING= "AntPropertiesFileSelectionDialog.showAll"; //$NON-NLS-1$
43
private final static String JavaDoc LAST_CONTAINER= "AntPropertiesFileSelectionDialog.lastContainer"; //$NON-NLS-1$
44

45     public FileSelectionDialog(Shell parent, List JavaDoc files, String JavaDoc title, String JavaDoc message, String JavaDoc filterExtension, String JavaDoc filterMessage) {
46         super(parent, new WorkbenchLabelProvider(), new WorkbenchContentProvider());
47         
48         setTitle(title);
49         setMessage(message);
50         fFilter= new FileFilter(files, filterExtension);
51         fFilterMessage= filterMessage;
52         setInput(ResourcesPlugin.getWorkspace().getRoot());
53         setComparator(new ResourceComparator(ResourceComparator.NAME));
54         
55         ISelectionStatusValidator validator= new ISelectionStatusValidator() {
56             public IStatus validate(Object JavaDoc[] selection) {
57                 if (selection.length == 0) {
58                     return new Status(IStatus.ERROR, AntUIPlugin.getUniqueIdentifier(), 0, "", null); //$NON-NLS-1$
59
}
60                 for (int i= 0; i < selection.length; i++) {
61                     if (!(selection[i] instanceof IFile)) {
62                         return new Status(IStatus.ERROR, AntUIPlugin.getUniqueIdentifier(), 0, "", null); //$NON-NLS-1$
63
}
64                 }
65                 return new Status(IStatus.OK, AntUIPlugin.getUniqueIdentifier(), 0, "", null); //$NON-NLS-1$
66
}
67         };
68         setValidator(validator);
69     }
70     
71     /* (non-Javadoc)
72      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
73      */

74     protected Control createDialogArea(Composite parent) {
75         
76         Composite result= (Composite)super.createDialogArea(parent);
77         final Button button = new Button(result, SWT.CHECK);
78         button.setText(fFilterMessage);
79         button.setFont(parent.getFont());
80         
81         IDialogSettings settings= AntUIPlugin.getDefault().getDialogSettings();
82         fShowAll= settings.getBoolean(DIALOG_SETTING);
83         
84         String JavaDoc lastPath= settings.get(LAST_CONTAINER);
85         if (lastPath != null) {
86             IPath path= Path.fromPortableString(lastPath);
87             IResource resource= ResourcesPlugin.getWorkspace().getRoot().findMember(path);
88             setInitialSelection(resource);
89         }
90         
91         fFilter.considerExtension(!fShowAll);
92         getTreeViewer().addFilter(fFilter);
93         if (!fShowAll) {
94             button.setSelection(true);
95         }
96         
97         button.addSelectionListener(new SelectionAdapter() {
98             public void widgetSelected(SelectionEvent event) {
99                 if (button.getSelection()) {
100                     fShowAll= false;
101                 } else {
102                     fShowAll= true;
103                 }
104                 fFilter.considerExtension(!fShowAll);
105                 getTreeViewer().refresh();
106             }
107         });
108         applyDialogFont(result);
109         return result;
110     }
111     
112     /* (non-Javadoc)
113      * @see org.eclipse.jface.window.Window#close()
114      */

115     public boolean close() {
116         IDialogSettings settings= AntUIPlugin.getDefault().getDialogSettings();
117         settings.put(DIALOG_SETTING, fShowAll);
118         
119         Object JavaDoc[] result= getResult();
120         if (result != null && result.length > 0) {
121             settings.put(LAST_CONTAINER, ((IResource)result[0]).getParent().getFullPath().toPortableString());
122         }
123         return super.close();
124     }
125 }
126
Popular Tags