fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[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.phpeclipse.PHPeclipsePlugin;
4
5 import org.eclipse.core.resources.IFile;
6 import org.eclipse.core.resources.IProject;
7 import org.eclipse.core.resources.IResource;
8 import org.eclipse.core.runtime.CoreException;
9 import org.eclipse.core.runtime.IAdaptable;
10 import org.eclipse.core.runtime.IPath;
11 import org.eclipse.core.runtime.Path;
12 import org.eclipse.jface.util.Assert;
13 import org.eclipse.jface.viewers.LabelProvider;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
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
26                  *         not defined or the object is not adaptable.
27                  */
28                 protected final IWorkbenchAdapter getAdapter(Object o) {
29                         if (!(o instanceof IAdaptable)) {
30                                 return null;
31                         }
32                         return (IWorkbenchAdapter) ((IAdaptable) o)
33                                         .getAdapter(IWorkbenchAdapter.class);
34                 }
35
36                 /*
37                  * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
38                  */
39                 public String getText(Object element) {
40                         if (element instanceof IFile) {
41                                 // query the element for its label
42                                 IWorkbenchAdapter adapter = getAdapter(element);
43                                 if (adapter == null) {
44                                         return ""; //$NON-NLS-1$
45                                 }
46                                 String filename = adapter.getLabel(element);
47                                 IPath path = ((IFile) element).getFullPath();
48                                 String filePathname = path != null ? path.toString() : ""; //$NON-NLS-1$
49                                 return filename + " (" + filePathname + ")";
50                         }
51                         return super.getText(element);
52                 }
53         }
54
55         protected PHPProjectSelector phpProjectSelector;
56
57         public PHPFileSelector(Composite parent, PHPProjectSelector aProjectSelector) {
58                 super(parent);
59                 Assert.isNotNull(aProjectSelector);
60                 phpProjectSelector = aProjectSelector;
61
62                 browseDialogTitle = "File Selection";
63         }
64
65         protected Object[] getPHPFiles() {
66                 IProject phpProject = phpProjectSelector.getSelection();
67                 if (phpProject == null)
68                         return new Object[0];
69
70                 PHPElementVisitor visitor = new PHPElementVisitor();
71                 try {
72                         phpProject.accept(visitor);
73                 } catch (CoreException e) {
74                         PHPeclipsePlugin.log(e);
75                 }
76                 return visitor.getCollectedPHPFiles();
77         }
78
79         public IFile getSelection() {
80                 String fileName = getSelectionText();
81                 if (fileName != null && !fileName.equals("")) {
82                         IPath filePath = new Path(fileName);
83                         IProject project = phpProjectSelector.getSelection();
84                         if (project != null && project.exists(filePath))
85                                 return project.getFile(filePath);
86                 }
87
88                 return null;
89         }
90
91         protected void handleBrowseSelected() {
92                 // ElementListSelectionDialog dialog = new
93                 // ElementListSelectionDialog(getShell(), new WorkbenchLabelProvider());
94                 ElementListSelectionDialog dialog = new ElementListSelectionDialog(
95                                 getShell(), new FileLabelProvider());
96
97                 dialog.setTitle(browseDialogTitle);
98                 dialog.setMessage(browseDialogMessage);
99                 dialog.setElements(getPHPFiles());
100
101                 if (dialog.open() == ElementListSelectionDialog.OK) {
102                         textField.setText(((IResource) dialog.getFirstResult())
103                                         .getProjectRelativePath().toString());
104                 }
105         }
106
107         protected String validateResourceSelection() {
108                 IFile selection = getSelection();
109                 return selection == null ? EMPTY_STRING : selection
110                                 .getProjectRelativePath().toString();
111         }
112 }