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