7b83cd058d0cdf528d8eafcedb793f3061a330e6
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / util / PHPFileSelector.java
1 package net.sourceforge.phpdt.internal.ui.util;
2
3 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
4
5 import net.sourceforge.phpeclipse.ui.WebUI;
6
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 //incastrix
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;
21
22 public class PHPFileSelector extends ResourceSelector {
23         static class FileLabelProvider extends LabelProvider {
24                 /**
25                  * Returns the implementation of IWorkbenchAdapter for the given object.
26                  * 
27                  * @param o
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.
31                  */
32                 protected final IWorkbenchAdapter getAdapter(Object o) {
33                         if (!(o instanceof IAdaptable)) {
34                                 return null;
35                         }
36                         return (IWorkbenchAdapter) ((IAdaptable) o)
37                                         .getAdapter(IWorkbenchAdapter.class);
38                 }
39
40                 /*
41                  * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
42                  */
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$
49                                 }
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 + ")";
54                         }
55                         return super.getText(element);
56                 }
57         }
58
59         protected PHPProjectSelector phpProjectSelector;
60
61         public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
62                 super(parent);
63                 Assert.isNotNull(aProjectSelector);
64                 phpProjectSelector = aProjectSelector;
65
66                 browseDialogTitle = "File Selection";
67         }
68
69         protected Object[] getPHPFiles() {
70                 IProject phpProject = phpProjectSelector.getSelection();
71                 if (phpProject == null)
72                         return new Object[0];
73
74                 PHPElementVisitor visitor = new PHPElementVisitor();
75                 try {
76                         phpProject.accept(visitor);
77                 } catch (CoreException e) {
78                         WebUI.log(e);
79                 }
80                 return visitor.getCollectedPHPFiles();
81         }
82
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);
90                 }
91
92                 return null;
93         }
94
95         protected void handleBrowseSelected() {
96                 // ElementListSelectionDialog dialog = new
97                 // ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
98                 ElementListSelectionDialog dialog = new ElementListSelectionDialog(
99                                 getShell(), new FileLabelProvider());
100
101                 dialog.setTitle(browseDialogTitle);
102                 dialog.setMessage(browseDialogMessage);
103                 dialog.setElements(getPHPFiles());
104
105                 if (dialog.open() == ElementListSelectionDialog.OK) {
106                         textField.setText(((IResource) dialog.getFirstResult())
107                                         .getProjectRelativePath().toString());
108                 }
109         }
110
111         protected String validateResourceSelection() {
112                 IFile selection = getSelection();
113                 return selection == null ? EMPTY_STRING : selection
114                                 .getProjectRelativePath().toString();
115         }
116 }