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