1 package net.sourceforge.phpeclipse.preferences;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
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;
31 public class PHPProjectLibraryPage {
32 protected PHPProject workingProject;
34 protected PHPProjectLibraryPage(PHPProject theWorkingProject) {
36 workingProject = theWorkingProject;
39 protected Control getControl(Composite parent) {
40 Composite composite = new Composite(parent, SWT.NONE);
41 composite.setLayout(new FillLayout());
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);
48 TableColumn tableColumn = new TableColumn(projectsTable, SWT.NONE);
49 tableColumn.setWidth(200);
50 tableColumn.setText(PHPPreferencesMessages.getString("PHPProjectLibraryPage.project")); //$NON-NLS-1$
52 CheckboxTableViewer projectsTableViewer = new CheckboxTableViewer(projectsTable);
53 projectsTableViewer.addCheckStateListener(new ICheckStateListener() {
54 public void checkStateChanged(CheckStateChangedEvent event) {
55 projectCheckedUnchecked(event);
59 projectsTableViewer.setContentProvider(getContentProvider());
60 projectsTableViewer.setLabelProvider(getLabelProvider());
62 projectsTableViewer.setInput(getWorkspaceProjects());
63 projectsTableViewer.setCheckedElements(workingProject.getReferencedProjects().toArray());
68 protected void projectCheckedUnchecked(CheckStateChangedEvent event) {
69 IProject checkEventProject = (IProject) event.getElement();
70 if (event.getChecked())
71 getWorkingProject().addLoadPathEntry(checkEventProject);
73 getWorkingProject().removeLoadPathEntry(checkEventProject);
76 protected PHPProject getWorkingProject() {
77 return workingProject;
80 protected List getWorkspaceProjects() {
81 IWorkspaceRoot root = PHPeclipsePlugin.getWorkspace().getRoot();
82 return Arrays.asList(root.getProjects());
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);
92 public String getColumnText(Object element, int columnIndex) {
93 if (element instanceof IProject)
94 return ((IProject) element).getName();
96 return PHPPreferencesMessages.getString("PHPProjectLibraryPage.elementNotIProject"); //$NON-NLS-1$
99 public void addListener(ILabelProviderListener listener) {
102 public void dispose() {
105 public boolean isLabelProperty(Object element, String property) {
109 public void removeListener(ILabelProviderListener listener) {
113 return labelProvider;
116 protected IContentProvider getContentProvider() {
117 IStructuredContentProvider contentProvider = new IStructuredContentProvider() {
118 protected List phpProjects;
120 public Object[] getElements(Object inputElement) {
121 return phpProjects.toArray();
124 public void dispose() {
127 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
128 phpProjects = new ArrayList();
130 if (!(newInput instanceof List))
133 Iterator workspaceProjectsIterator = ((List) newInput).iterator();
134 while (workspaceProjectsIterator.hasNext()) {
135 Object anObject = workspaceProjectsIterator.next();
136 if (anObject instanceof IProject) {
137 IProject project = (IProject) anObject;
138 if (project.getName() != workingProject.getProject().getName()) {
140 if (project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID))
141 phpProjects.add(project);
142 } catch (CoreException e) {
150 return contentProvider;