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