1 package net.sourceforge.phpeclipse.preferences;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
8 import net.sourceforge.phpdt.internal.core.JavaProject;
9 import net.sourceforge.phpdt.internal.ui.preferences.PHPEditorPreferencePage;
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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.ISharedImages;
31 import org.eclipse.ui.IWorkbench;
33 public class PHPProjectLibraryPage {
34 protected JavaProject workingProject;
36 protected PHPProjectLibraryPage(JavaProject theWorkingProject) {
38 workingProject = theWorkingProject;
41 protected Control getControl(Composite parent) {
42 Composite composite = new Composite(parent, SWT.NONE);
43 composite.setLayout(new FillLayout());
45 Table projectsTable = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
46 projectsTable.setHeaderVisible(false);
47 projectsTable.setLinesVisible(false);
48 projectsTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
50 TableColumn tableColumn = new TableColumn(projectsTable, SWT.NONE);
51 tableColumn.setWidth(200);
52 tableColumn.setText(PHPPreferencesMessages.getString("PHPEditorPreferencePageLibraryPage.project")); //$NON-NLS-1$
54 CheckboxTableViewer projectsTableViewer = new CheckboxTableViewer(projectsTable);
55 projectsTableViewer.addCheckStateListener(new ICheckStateListener() {
56 public void checkStateChanged(CheckStateChangedEvent event) {
57 projectCheckedUnchecked(event);
61 projectsTableViewer.setContentProvider(getContentProvider());
62 projectsTableViewer.setLabelProvider(getLabelProvider());
64 projectsTableViewer.setInput(getWorkspaceProjects());
65 projectsTableViewer.setCheckedElements(workingProject.getReferencedProjects().toArray());
70 protected void projectCheckedUnchecked(CheckStateChangedEvent event) {
71 IProject checkEventProject = (IProject) event.getElement();
72 if (event.getChecked())
73 getWorkingProject().addLoadPathEntry(checkEventProject);
75 getWorkingProject().removeLoadPathEntry(checkEventProject);
78 protected JavaProject getWorkingProject() {
79 return workingProject;
82 protected List getWorkspaceProjects() {
83 IWorkspaceRoot root = PHPeclipsePlugin.getWorkspace().getRoot();
84 return Arrays.asList(root.getProjects());
87 protected ITableLabelProvider getLabelProvider() {
88 ITableLabelProvider labelProvider = new ITableLabelProvider() {
89 public Image getColumnImage(Object element, int columnIndex) {
90 IWorkbench workbench = PHPeclipsePlugin.getDefault().getWorkbench();
91 return workbench.getSharedImages().getImage(ISharedImages.IMG_OBJ_PROJECT);
94 public String getColumnText(Object element, int columnIndex) {
95 if (element instanceof IProject)
96 return ((IProject) element).getName();
98 return PHPPreferencesMessages.getString("PHPEditorPreferencePageLibraryPage.elementNotIProject"); //$NON-NLS-1$
101 public void addListener(ILabelProviderListener listener) {
104 public void dispose() {
107 public boolean isLabelProperty(Object element, String property) {
111 public void removeListener(ILabelProviderListener listener) {
115 return labelProvider;
118 protected IContentProvider getContentProvider() {
119 IStructuredContentProvider contentProvider = new IStructuredContentProvider() {
120 protected List PHPEditorPreferencePages;
122 public Object[] getElements(Object inputElement) {
123 return PHPEditorPreferencePages.toArray();
126 public void dispose() {
129 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
130 PHPEditorPreferencePages = new ArrayList();
132 if (!(newInput instanceof List))
135 Iterator workspaceProjectsIterator = ((List) newInput).iterator();
136 while (workspaceProjectsIterator.hasNext()) {
137 Object anObject = workspaceProjectsIterator.next();
138 if (anObject instanceof IProject) {
139 IProject project = (IProject) anObject;
140 if (project.getName() != workingProject.getProject().getName()) {
142 if (project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID))
143 PHPEditorPreferencePages.add(project);
144 } catch (CoreException e) {
152 return contentProvider;