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