1 package net.sourceforge.phpdt.internal.ui.util;
3 import net.sourceforge.phpdt.internal.ui.dialogs.ElementListSelectionDialog;
4 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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.IAdaptable;
11 import org.eclipse.core.runtime.IPath;
12 import org.eclipse.core.runtime.Path;
13 import org.eclipse.jface.util.Assert;
14 import org.eclipse.jface.viewers.LabelProvider;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.ui.model.IWorkbenchAdapter;
18 public class PHPFileSelector extends ResourceSelector {
19 static class FileLabelProvider extends LabelProvider {
21 * Returns the implementation of IWorkbenchAdapter for the given object.
24 * the object to look up.
25 * @return IWorkbenchAdapter or <code>null</code> if the adapter is not defined or the object is not adaptable.
27 protected final IWorkbenchAdapter getAdapter(Object o) {
28 if (!(o instanceof IAdaptable)) {
31 return (IWorkbenchAdapter) ((IAdaptable) o).getAdapter(IWorkbenchAdapter.class);
35 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
37 public String getText(Object element) {
38 if (element instanceof IFile) {
39 // query the element for its label
40 IWorkbenchAdapter adapter = getAdapter(element);
41 if (adapter == null) {
42 return ""; //$NON-NLS-1$
44 String filename = adapter.getLabel(element);
45 IPath path = ((IFile) element).getFullPath();
46 String filePathname = path != null ? path.toString() : ""; //$NON-NLS-1$
47 return filename + " (" + filePathname + ")";
49 return super.getText(element);
53 protected PHPProjectSelector phpProjectSelector;
55 public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
57 Assert.isNotNull(aProjectSelector);
58 phpProjectSelector = aProjectSelector;
60 browseDialogTitle = "File Selection";
63 protected Object[] getPHPFiles() {
64 IProject phpProject = phpProjectSelector.getSelection();
65 if (phpProject == null)
68 PHPElementVisitor visitor = new PHPElementVisitor();
70 phpProject.accept(visitor);
71 } catch (CoreException e) {
72 PHPeclipsePlugin.log(e);
74 return visitor.getCollectedPHPFiles();
77 public IFile getSelection() {
78 String fileName = getSelectionText();
79 if (fileName != null && !fileName.equals("")) {
80 IPath filePath = new Path(fileName);
81 IProject project = phpProjectSelector.getSelection();
82 if (project != null && project.exists(filePath))
83 return project.getFile(filePath);
89 protected void handleBrowseSelected() {
90 // ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
91 ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new FileLabelProvider());
93 dialog.setTitle(browseDialogTitle);
94 dialog.setMessage(browseDialogMessage);
95 dialog.setElements(getPHPFiles());
97 if (dialog.open() == ElementListSelectionDialog.OK) {
98 textField.setText(((IResource) dialog.getFirstResult()).getProjectRelativePath().toString());
102 protected String validateResourceSelection() {
103 IFile selection = getSelection();
104 return selection == null ? EMPTY_STRING : selection.getProjectRelativePath().toString();