Delete association of htmledit.gif for the PHPUnitEditor
[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.dialogs.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.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;
17
18 public class PHPFileSelector extends ResourceSelector {
19   static class FileLabelProvider extends LabelProvider {
20     /**
21      * Returns the implementation of IWorkbenchAdapter for the given object.
22      * 
23      * @param o
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.
26      */
27     protected final IWorkbenchAdapter getAdapter(Object o) {
28       if (!(o instanceof IAdaptable)) {
29         return null;
30       }
31       return (IWorkbenchAdapter) ((IAdaptable) o).getAdapter(IWorkbenchAdapter.class);
32     }
33
34     /*
35      * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
36      */
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$
43         }
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 + ")";
48       }
49       return super.getText(element);
50     }
51   }
52
53   protected PHPProjectSelector phpProjectSelector;
54
55   public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
56     super(parent);
57     Assert.isNotNull(aProjectSelector);
58     phpProjectSelector = aProjectSelector;
59
60     browseDialogTitle = "File Selection";
61   }
62
63   protected Object[] getPHPFiles() {
64     IProject phpProject = phpProjectSelector.getSelection();
65     if (phpProject == null)
66       return new Object[0];
67
68     PHPElementVisitor visitor = new PHPElementVisitor();
69     try {
70       phpProject.accept(visitor);
71     } catch (CoreException e) {
72       PHPeclipsePlugin.log(e);
73     }
74     return visitor.getCollectedPHPFiles();
75   }
76
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);
84     }
85
86     return null;
87   }
88
89   protected void handleBrowseSelected() {
90 //    ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
91     ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), new FileLabelProvider());
92
93     dialog.setTitle(browseDialogTitle);
94     dialog.setMessage(browseDialogMessage);
95     dialog.setElements(getPHPFiles());
96
97     if (dialog.open() == ElementListSelectionDialog.OK) {
98       textField.setText(((IResource) dialog.getFirstResult()).getProjectRelativePath().toString());
99     }
100   }
101
102   protected String validateResourceSelection() {
103     IFile selection = getSelection();
104     return selection == null ? EMPTY_STRING : selection.getProjectRelativePath().toString();
105   }
106 }