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