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;
15 //import org.eclipse.jface.text.Assert;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jface.viewers.LabelProvider;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
20 import org.eclipse.ui.model.IWorkbenchAdapter;
22 public class PHPFileSelector extends ResourceSelector {
23 static class FileLabelProvider extends LabelProvider {
25 * Returns the implementation of IWorkbenchAdapter for the given object.
28 * the object to look up.
29 * @return IWorkbenchAdapter or <code>null</code> if the adapter is
30 * not defined or the object is not adaptable.
32 protected final IWorkbenchAdapter getAdapter(Object o) {
33 if (!(o instanceof IAdaptable)) {
36 return (IWorkbenchAdapter) ((IAdaptable) o)
37 .getAdapter(IWorkbenchAdapter.class);
41 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
43 public String getText(Object element) {
44 if (element instanceof IFile) {
45 // query the element for its label
46 IWorkbenchAdapter adapter = getAdapter(element);
47 if (adapter == null) {
48 return ""; //$NON-NLS-1$
50 String filename = adapter.getLabel(element);
51 IPath path = ((IFile) element).getFullPath();
52 String filePathname = path != null ? path.toString() : ""; //$NON-NLS-1$
53 return filename + " (" + filePathname + ")";
55 return super.getText(element);
59 protected PHPProjectSelector phpProjectSelector;
61 public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
63 Assert.isNotNull(aProjectSelector);
64 phpProjectSelector = aProjectSelector;
66 browseDialogTitle = "File Selection";
69 protected Object[] getPHPFiles() {
70 IProject phpProject = phpProjectSelector.getSelection();
71 if (phpProject == null)
74 PHPElementVisitor visitor = new PHPElementVisitor();
76 phpProject.accept(visitor);
77 } catch (CoreException e) {
80 return visitor.getCollectedPHPFiles();
83 public IFile getSelection() {
84 String fileName = getSelectionText();
85 if (fileName != null && !fileName.equals("")) {
86 IPath filePath = new Path(fileName);
87 IProject project = phpProjectSelector.getSelection();
88 if (project != null && project.exists(filePath))
89 return project.getFile(filePath);
95 protected void handleBrowseSelected() {
96 // ElementListSelectionDialog dialog = new
97 // ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
98 ElementListSelectionDialog dialog = new ElementListSelectionDialog(
99 getShell(), new FileLabelProvider());
101 dialog.setTitle(browseDialogTitle);
102 dialog.setMessage(browseDialogMessage);
103 dialog.setElements(getPHPFiles());
105 if (dialog.open() == ElementListSelectionDialog.OK) {
106 textField.setText(((IResource) dialog.getFirstResult())
107 .getProjectRelativePath().toString());
111 protected String validateResourceSelection() {
112 IFile selection = getSelection();
113 return selection == null ? EMPTY_STRING : selection
114 .getProjectRelativePath().toString();