f83ffd3d57cb26e3be2c229f5503dc810c79cef8
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / util / PHPFileSelector.java
1 package net.sourceforge.phpdt.internal.ui.util;
2
3 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
4 import org.eclipse.core.resources.IFile;
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.IPath;
9 import org.eclipse.core.runtime.Path;
10 import org.eclipse.jface.util.Assert;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.ui.model.WorkbenchLabelProvider;
13 import net.sourceforge.phpdt.internal.ui.dialog.ElementListSelectionDialog;
14
15 public class PHPFileSelector extends ResourceSelector {
16         protected PHPProjectSelector phpProjectSelector;
17
18         public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
19                 super(parent);
20                 Assert.isNotNull(aProjectSelector);
21                 phpProjectSelector = aProjectSelector;
22                 
23                 browseDialogTitle = "File Selection";
24         }
25
26         protected Object[] getPHPFiles() {
27                 IProject phpProject = phpProjectSelector.getSelection();
28                 if (phpProject == null)
29                         return new Object[0];
30
31                 PHPElementVisitor visitor = new PHPElementVisitor();
32                 try {
33                         phpProject.accept(visitor);
34                 } catch(CoreException e) {
35                         PHPeclipsePlugin.log(e);
36                 }
37                 return visitor.getCollectedPHPFiles();
38         }
39
40         public IFile getSelection() {
41                 String fileName = getSelectionText();
42                 if (fileName != null && !fileName.equals("")) {
43                         IPath filePath = new Path(fileName);
44                         IProject project = phpProjectSelector.getSelection();
45                         if (project != null && project.exists(filePath))
46                                 return project.getFile(filePath);
47                 }
48                         
49                 return null;
50         }
51
52         protected void handleBrowseSelected() {
53                 ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
54                 dialog.setTitle(browseDialogTitle);
55                 dialog.setMessage(browseDialogMessage);
56                 dialog.setElements(getPHPFiles());
57
58                 if (dialog.open() == dialog.OK) {
59                         textField.setText(((IResource) dialog.getFirstResult()).getProjectRelativePath().toString());
60                 }
61         }
62
63         protected String validateResourceSelection() {
64                 IFile selection = getSelection();
65                 return selection == null ? EMPTY_STRING : selection.getProjectRelativePath().toString();
66         }
67 }