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
26 * not defined or the object is not adaptable.
28 protected final IWorkbenchAdapter getAdapter(Object o) {
29 if (!(o instanceof IAdaptable)) {
32 return (IWorkbenchAdapter) ((IAdaptable) o)
33 .getAdapter(IWorkbenchAdapter.class);
37 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
39 public String getText(Object element) {
40 if (element instanceof IFile) {
41 // query the element for its label
42 IWorkbenchAdapter adapter = getAdapter(element);
43 if (adapter == null) {
44 return ""; //$NON-NLS-1$
46 String filename = adapter.getLabel(element);
47 IPath path = ((IFile) element).getFullPath();
48 String filePathname = path != null ? path.toString() : ""; //$NON-NLS-1$
49 return filename + " (" + filePathname + ")";
51 return super.getText(element);
55 protected PHPProjectSelector phpProjectSelector;
57 public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
59 Assert.isNotNull(aProjectSelector);
60 phpProjectSelector = aProjectSelector;
62 browseDialogTitle = "File Selection";
65 protected Object[] getPHPFiles() {
66 IProject phpProject = phpProjectSelector.getSelection();
67 if (phpProject == null)
70 PHPElementVisitor visitor = new PHPElementVisitor();
72 phpProject.accept(visitor);
73 } catch (CoreException e) {
74 PHPeclipsePlugin.log(e);
76 return visitor.getCollectedPHPFiles();
79 public IFile getSelection() {
80 String fileName = getSelectionText();
81 if (fileName != null && !fileName.equals("")) {
82 IPath filePath = new Path(fileName);
83 IProject project = phpProjectSelector.getSelection();
84 if (project != null && project.exists(filePath))
85 return project.getFile(filePath);
91 protected void handleBrowseSelected() {
92 // ElementListSelectionDialog dialog = new
93 // ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
94 ElementListSelectionDialog dialog = new ElementListSelectionDialog(
95 getShell(), new FileLabelProvider());
97 dialog.setTitle(browseDialogTitle);
98 dialog.setMessage(browseDialogMessage);
99 dialog.setElements(getPHPFiles());
100 dialog.setInitialElementSelections(null);
102 if (dialog.open() == ElementListSelectionDialog.OK) {
103 textField.setText(((IResource) dialog.getFirstResult())
104 .getProjectRelativePath().toString());
108 protected String validateResourceSelection() {
109 IFile selection = getSelection();
110 return selection == null ? EMPTY_STRING : selection
111 .getProjectRelativePath().toString();