1 package net.sourceforge.phpdt.internal.ui.util;
3 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
5 import net.sourceforge.phpeclipse.ui.WebUI;
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IResource;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IAdaptable;
12 import org.eclipse.core.runtime.IPath;
13 import org.eclipse.core.runtime.Path;
14 import org.eclipse.jface.util.Assert;
15 import org.eclipse.jface.viewers.LabelProvider;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
18 import org.eclipse.ui.model.IWorkbenchAdapter;
20 public class PHPFileSelector extends ResourceSelector {
21 static class FileLabelProvider extends LabelProvider {
23 * Returns the implementation of IWorkbenchAdapter for the given object.
26 * the object to look up.
27 * @return IWorkbenchAdapter or <code>null</code> if the adapter is
28 * not defined or the object is not adaptable.
30 protected final IWorkbenchAdapter getAdapter(Object o) {
31 if (!(o instanceof IAdaptable)) {
34 return (IWorkbenchAdapter) ((IAdaptable) o)
35 .getAdapter(IWorkbenchAdapter.class);
39 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
41 public String getText(Object element) {
42 if (element instanceof IFile) {
43 // query the element for its label
44 IWorkbenchAdapter adapter = getAdapter(element);
45 if (adapter == null) {
46 return ""; //$NON-NLS-1$
48 String filename = adapter.getLabel(element);
49 IPath path = ((IFile) element).getFullPath();
50 String filePathname = path != null ? path.toString() : ""; //$NON-NLS-1$
51 return filename + " (" + filePathname + ")";
53 return super.getText(element);
57 protected PHPProjectSelector phpProjectSelector;
59 public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
61 Assert.isNotNull(aProjectSelector);
62 phpProjectSelector = aProjectSelector;
64 browseDialogTitle = "File Selection";
67 protected Object[] getPHPFiles() {
68 IProject phpProject = phpProjectSelector.getSelection();
69 if (phpProject == null)
72 PHPElementVisitor visitor = new PHPElementVisitor();
74 phpProject.accept(visitor);
75 } catch (CoreException e) {
78 return visitor.getCollectedPHPFiles();
81 public IFile getSelection() {
82 String fileName = getSelectionText();
83 if (fileName != null && !fileName.equals("")) {
84 IPath filePath = new Path(fileName);
85 IProject project = phpProjectSelector.getSelection();
86 if (project != null && project.exists(filePath))
87 return project.getFile(filePath);
93 protected void handleBrowseSelected() {
94 // ElementListSelectionDialog dialog = new
95 // ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
96 ElementListSelectionDialog dialog = new ElementListSelectionDialog(
97 getShell(), new FileLabelProvider());
99 dialog.setTitle(browseDialogTitle);
100 dialog.setMessage(browseDialogMessage);
101 dialog.setElements(getPHPFiles());
103 if (dialog.open() == ElementListSelectionDialog.OK) {
104 textField.setText(((IResource) dialog.getFirstResult())
105 .getProjectRelativePath().toString());
109 protected String validateResourceSelection() {
110 IFile selection = getSelection();
111 return selection == null ? EMPTY_STRING : selection
112 .getProjectRelativePath().toString();