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.phpeclipse.PHPeclipsePlugin;
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.ISharedImages;
30 import org.eclipse.ui.IWorkbench;
32 public class PHPProjectLibraryPage {
33 protected JavaProject workingProject;
35 protected PHPProjectLibraryPage(JavaProject theWorkingProject) {
37 workingProject = theWorkingProject;
40 protected Control getControl(Composite parent) {
41 Composite composite = new Composite(parent, SWT.NONE);
42 composite.setLayout(new FillLayout());
44 Table projectsTable = new Table(composite, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
45 projectsTable.setHeaderVisible(false);
46 projectsTable.setLinesVisible(false);
47 projectsTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
49 TableColumn tableColumn = new TableColumn(projectsTable, SWT.NONE);
50 tableColumn.setWidth(200);
51 tableColumn.setText(PHPPreferencesMessages.getString("PHPEditorPreferencePageLibraryPage.project")); //$NON-NLS-1$
53 CheckboxTableViewer projectsTableViewer = new CheckboxTableViewer(projectsTable);
54 projectsTableViewer.addCheckStateListener(new ICheckStateListener() {
55 public void checkStateChanged(CheckStateChangedEvent event) {
56 projectCheckedUnchecked(event);
60 projectsTableViewer.setContentProvider(getContentProvider());
61 projectsTableViewer.setLabelProvider(getLabelProvider());
63 projectsTableViewer.setInput(getWorkspaceProjects());
64 projectsTableViewer.setCheckedElements(workingProject.getReferencedProjects().toArray());
69 protected void projectCheckedUnchecked(CheckStateChangedEvent event) {
70 IProject checkEventProject = (IProject) event.getElement();
71 if (event.getChecked())
72 getWorkingProject().addLoadPathEntry(checkEventProject);
74 getWorkingProject().removeLoadPathEntry(checkEventProject);
77 protected JavaProject getWorkingProject() {
78 return workingProject;
81 protected List getWorkspaceProjects() {
82 IWorkspaceRoot root = PHPeclipsePlugin.getWorkspace().getRoot();
83 return Arrays.asList(root.getProjects());
86 protected ITableLabelProvider getLabelProvider() {
87 ITableLabelProvider labelProvider = new ITableLabelProvider() {
88 public Image getColumnImage(Object element, int columnIndex) {
89 IWorkbench workbench = PHPeclipsePlugin.getDefault().getWorkbench();
90 return workbench.getSharedImages().getImage(ISharedImages.IMG_OBJ_PROJECT);
93 public String getColumnText(Object element, int columnIndex) {
94 if (element instanceof IProject)
95 return ((IProject) element).getName();
97 return PHPPreferencesMessages.getString("PHPEditorPreferencePageLibraryPage.elementNotIProject"); //$NON-NLS-1$
100 public void addListener(ILabelProviderListener listener) {
103 public void dispose() {
106 public boolean isLabelProperty(Object element, String property) {
110 public void removeListener(ILabelProviderListener listener) {
114 return labelProvider;
117 protected IContentProvider getContentProvider() {
118 IStructuredContentProvider contentProvider = new IStructuredContentProvider() {
119 protected List PHPEditorPreferencePages;
121 public Object[] getElements(Object inputElement) {
122 return PHPEditorPreferencePages.toArray();
125 public void dispose() {
128 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
129 PHPEditorPreferencePages = new ArrayList();
131 if (!(newInput instanceof List))
134 Iterator workspaceProjectsIterator = ((List) newInput).iterator();
135 while (workspaceProjectsIterator.hasNext()) {
136 Object anObject = workspaceProjectsIterator.next();
137 if (anObject instanceof IProject) {
138 IProject project = (IProject) anObject;
139 if (project.getName() != workingProject.getProject().getName()) {
141 if (project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID))
142 PHPEditorPreferencePages.add(project);
143 } catch (CoreException e) {
151 return contentProvider;