first scanner /parser copied from the jdt java version
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / preferences / PHPProjectLibraryPage.java
1 package net.sourceforge.phpeclipse.preferences;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
6 import java.util.List;
7
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IWorkspaceRoot;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.jface.viewers.CheckStateChangedEvent;
12 import org.eclipse.jface.viewers.CheckboxTableViewer;
13 import org.eclipse.jface.viewers.ICheckStateListener;
14 import org.eclipse.jface.viewers.IContentProvider;
15 import org.eclipse.jface.viewers.ILabelProviderListener;
16 import org.eclipse.jface.viewers.IStructuredContentProvider;
17 import org.eclipse.jface.viewers.ITableLabelProvider;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.layout.FillLayout;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Table;
25 import org.eclipse.swt.widgets.TableColumn;
26 import org.eclipse.ui.ISharedImages;
27 import org.eclipse.ui.IWorkbench; 
28 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
29 import net.sourceforge.phpeclipse.resourcesview.PHPProject; 
30
31 public class PHPProjectLibraryPage {
32         protected PHPProject workingProject;
33
34         protected PHPProjectLibraryPage(PHPProject theWorkingProject) {
35                 super();
36                 workingProject = theWorkingProject;
37         }
38
39         protected Control getControl(Composite parent) {
40                 Composite composite = new Composite(parent, SWT.NONE);
41                 composite.setLayout(new FillLayout());
42
43                 Table projectsTable = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
44                 projectsTable.setHeaderVisible(false);
45                 projectsTable.setLinesVisible(false);
46                 projectsTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
47
48                 TableColumn tableColumn = new TableColumn(projectsTable, SWT.NONE);
49                 tableColumn.setWidth(200);
50                 tableColumn.setText(PHPPreferencesMessages.getString("PHPProjectLibraryPage.project")); //$NON-NLS-1$
51
52                 CheckboxTableViewer projectsTableViewer = new CheckboxTableViewer(projectsTable);
53                 projectsTableViewer.addCheckStateListener(new ICheckStateListener() {
54                         public void checkStateChanged(CheckStateChangedEvent event) {
55                                 projectCheckedUnchecked(event);
56                         }
57                 });
58
59                 projectsTableViewer.setContentProvider(getContentProvider());
60                 projectsTableViewer.setLabelProvider(getLabelProvider());
61
62                 projectsTableViewer.setInput(getWorkspaceProjects());
63                 projectsTableViewer.setCheckedElements(workingProject.getReferencedProjects().toArray());
64
65                 return composite;
66         }
67
68         protected void projectCheckedUnchecked(CheckStateChangedEvent event) {
69                 IProject checkEventProject = (IProject) event.getElement();
70                 if (event.getChecked())
71                         getWorkingProject().addLoadPathEntry(checkEventProject);
72                 else
73                         getWorkingProject().removeLoadPathEntry(checkEventProject);
74         }
75
76         protected PHPProject getWorkingProject() {
77                 return workingProject;
78         }
79
80         protected List getWorkspaceProjects() {
81                 IWorkspaceRoot root = PHPeclipsePlugin.getWorkspace().getRoot();
82                 return Arrays.asList(root.getProjects());
83         }
84
85         protected ITableLabelProvider getLabelProvider() {
86                 ITableLabelProvider labelProvider = new ITableLabelProvider() {
87                         public Image getColumnImage(Object element, int columnIndex) {
88                                 IWorkbench workbench= PHPeclipsePlugin.getDefault().getWorkbench();
89                                 return workbench.getSharedImages().getImage(ISharedImages.IMG_OBJ_PROJECT);
90                         }
91
92                         public String getColumnText(Object element, int columnIndex) {
93                                 if (element instanceof IProject)
94                                         return ((IProject) element).getName();
95
96                                 return PHPPreferencesMessages.getString("PHPProjectLibraryPage.elementNotIProject"); //$NON-NLS-1$
97                         }
98
99                         public void addListener(ILabelProviderListener listener) {}
100
101                         public void dispose() {}
102
103                         public boolean isLabelProperty(Object element, String property) {
104                                 return false;
105                         }
106
107                         public void removeListener(ILabelProviderListener listener) {}
108                 };
109
110                 return labelProvider;
111         }
112
113         protected IContentProvider getContentProvider() {
114                 IStructuredContentProvider contentProvider = new IStructuredContentProvider() {
115                         protected List phpProjects;
116
117                         public Object[] getElements(Object inputElement) {
118                                 return phpProjects.toArray();
119                         }
120
121                         public void dispose() {}
122
123                         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
124                                 phpProjects = new ArrayList();
125
126                                 if (!(newInput instanceof List))
127                                         return;
128
129                                 Iterator workspaceProjectsIterator = ((List) newInput).iterator();
130                                 while (workspaceProjectsIterator.hasNext()) {
131                                         Object anObject = workspaceProjectsIterator.next();
132                                         if (anObject instanceof IProject) {
133                                                 IProject project = (IProject) anObject;
134                                                 if (project.getName() != workingProject.getProject().getName()) {
135                                                         try {
136                                                                 if (project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID))
137                                                                         phpProjects.add(project);
138                                                         } catch (CoreException e) {}
139                                                 }
140                                         }
141                                 }
142                         }
143                 };
144
145                 return contentProvider;
146         }
147 }