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