Prepared for multiple Partitioners
[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.phpeclipse.PHPeclipsePlugin;
9 import net.sourceforge.phpeclipse.resourcesview.PHPProject;
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.ISharedImages;
30 import org.eclipse.ui.IWorkbench;
31
32 public class PHPProjectLibraryPage {
33   protected PHPProject workingProject;
34
35   protected PHPProjectLibraryPage(PHPProject 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 | SWT.MULTI | SWT.FULL_SELECTION);
45     projectsTable.setHeaderVisible(false);
46     projectsTable.setLinesVisible(false);
47     projectsTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
48
49     TableColumn tableColumn = new TableColumn(projectsTable, SWT.NONE);
50     tableColumn.setWidth(200);
51     tableColumn.setText(PHPPreferencesMessages.getString("PHPProjectLibraryPage.project")); //$NON-NLS-1$
52
53     CheckboxTableViewer projectsTableViewer = new CheckboxTableViewer(projectsTable);
54     projectsTableViewer.addCheckStateListener(new ICheckStateListener() {
55       public void checkStateChanged(CheckStateChangedEvent event) {
56         projectCheckedUnchecked(event);
57       }
58     });
59
60     projectsTableViewer.setContentProvider(getContentProvider());
61     projectsTableViewer.setLabelProvider(getLabelProvider());
62
63     projectsTableViewer.setInput(getWorkspaceProjects());
64     projectsTableViewer.setCheckedElements(workingProject.getReferencedProjects().toArray());
65
66     return composite;
67   }
68
69   protected void projectCheckedUnchecked(CheckStateChangedEvent event) {
70     IProject checkEventProject = (IProject) event.getElement();
71     if (event.getChecked())
72       getWorkingProject().addLoadPathEntry(checkEventProject);
73     else
74       getWorkingProject().removeLoadPathEntry(checkEventProject);
75   }
76
77   protected PHPProject getWorkingProject() {
78     return workingProject;
79   }
80
81   protected List getWorkspaceProjects() {
82     IWorkspaceRoot root = PHPeclipsePlugin.getWorkspace().getRoot();
83     return Arrays.asList(root.getProjects());
84   }
85
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);
91       }
92
93       public String getColumnText(Object element, int columnIndex) {
94         if (element instanceof IProject)
95           return ((IProject) element).getName();
96
97         return PHPPreferencesMessages.getString("PHPProjectLibraryPage.elementNotIProject"); //$NON-NLS-1$
98       }
99
100       public void addListener(ILabelProviderListener listener) {
101       }
102
103       public void dispose() {
104       }
105
106       public boolean isLabelProperty(Object element, String property) {
107         return false;
108       }
109
110       public void removeListener(ILabelProviderListener listener) {
111       }
112     };
113
114     return labelProvider;
115   }
116
117   protected IContentProvider getContentProvider() {
118     IStructuredContentProvider contentProvider = new IStructuredContentProvider() {
119       protected List phpProjects;
120
121       public Object[] getElements(Object inputElement) {
122         return phpProjects.toArray();
123       }
124
125       public void dispose() {
126       }
127
128       public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
129         phpProjects = new ArrayList();
130
131         if (!(newInput instanceof List))
132           return;
133
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()) {
140               try {
141                 if (project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID))
142                   phpProjects.add(project);
143               } catch (CoreException e) {
144               }
145             }
146           }
147         }
148       }
149     };
150
151     return contentProvider;
152   }
153 }