KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > ProjectReferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide.dialogs;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IProjectDescription;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jface.dialogs.ErrorDialog;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26 import org.eclipse.jface.preference.PreferencePage;
27 import org.eclipse.jface.viewers.CheckStateChangedEvent;
28 import org.eclipse.jface.viewers.CheckboxTableViewer;
29 import org.eclipse.jface.viewers.ICheckStateListener;
30 import org.eclipse.jface.viewers.IStructuredContentProvider;
31 import org.eclipse.jface.viewers.ViewerComparator;
32 import org.eclipse.osgi.util.NLS;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.graphics.Font;
35 import org.eclipse.swt.graphics.FontData;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.dialogs.PropertyPage;
43 import org.eclipse.ui.internal.ide.DialogUtil;
44 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
45 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
46 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
47 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
48 import org.eclipse.ui.model.WorkbenchContentProvider;
49 import org.eclipse.ui.model.WorkbenchLabelProvider;
50
51 /**
52  * A property page for viewing and modifying the set
53  * of projects referenced by a given project.
54  */

55 public class ProjectReferencePage extends PropertyPage {
56     private IProject project;
57
58     private boolean modified = false;
59
60     //widgets
61
private CheckboxTableViewer listViewer;
62
63     private static final int PROJECT_LIST_MULTIPLIER = 30;
64
65     /**
66      * Creates a new ProjectReferencePage.
67      */

68     public ProjectReferencePage() {
69         //Do nothing on creation
70
}
71
72     /**
73      * @see PreferencePage#createContents
74      */

75     protected Control createContents(Composite parent) {
76
77         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
78                 IIDEHelpContextIds.PROJECT_REFERENCE_PROPERTY_PAGE);
79         Font font = parent.getFont();
80
81         Composite composite = new Composite(parent, SWT.NONE);
82         GridLayout layout = new GridLayout();
83         composite.setLayout(layout);
84         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
85         composite.setFont(font);
86
87         initialize();
88
89         Label description = createDescriptionLabel(composite);
90         description.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
91
92         listViewer = CheckboxTableViewer.newCheckList(composite, SWT.TOP
93                 | SWT.BORDER);
94         listViewer.getTable().setFont(font);
95         GridData data = new GridData(GridData.FILL_BOTH);
96         data.grabExcessHorizontalSpace = true;
97         
98         if(!project.isOpen())
99             listViewer.getControl().setEnabled(false);
100
101         //Only set a height hint if it will not result in a cut off dialog
102
if (DialogUtil.inRegularFontMode(parent)) {
103             data.heightHint = getDefaultFontHeight(listViewer.getTable(),
104                     PROJECT_LIST_MULTIPLIER);
105         }
106         listViewer.getTable().setLayoutData(data);
107         listViewer.getTable().setFont(font);
108
109         listViewer.setLabelProvider(WorkbenchLabelProvider
110                 .getDecoratingWorkbenchLabelProvider());
111         listViewer.setContentProvider(getContentProvider(project));
112         listViewer.setComparator(new ViewerComparator());
113         listViewer.setInput(project.getWorkspace());
114         try {
115             listViewer.setCheckedElements(project.getDescription()
116                     .getReferencedProjects());
117         } catch (CoreException e) {
118             //don't initial-check anything
119
}
120
121         //check for initial modification to avoid work if no changes are made
122
listViewer.addCheckStateListener(new ICheckStateListener() {
123             public void checkStateChanged(CheckStateChangedEvent event) {
124                 modified = true;
125             }
126         });
127
128         return composite;
129     }
130
131     /**
132      * Returns a content provider for the list dialog. It
133      * will return all projects in the workspace except
134      * the given project, plus any projects referenced
135      * by the given project which do no exist in the
136      * workspace.
137      * @param project the project to provide content for
138      * @return the content provider that shows the project content
139      */

140     protected IStructuredContentProvider getContentProvider(
141             final IProject project) {
142         return new WorkbenchContentProvider() {
143             public Object JavaDoc[] getChildren(Object JavaDoc o) {
144                 if (!(o instanceof IWorkspace)) {
145                     return new Object JavaDoc[0];
146                 }
147
148                 // Collect all the projects in the workspace except the given project
149
IProject[] projects = ((IWorkspace) o).getRoot().getProjects();
150                 ArrayList JavaDoc referenced = new ArrayList JavaDoc(projects.length);
151                 boolean found = false;
152                 for (int i = 0; i < projects.length; i++) {
153                     if (!found && projects[i].equals(project)) {
154                         found = true;
155                         continue;
156                     }
157                     referenced.add(projects[i]);
158                 }
159
160                 // Add any referenced that do not exist in the workspace currently
161
try {
162                     projects = project.getDescription().getReferencedProjects();
163                     for (int i = 0; i < projects.length; i++) {
164                         if (!referenced.contains(projects[i])) {
165                             referenced.add(projects[i]);
166                         }
167                     }
168                 } catch (CoreException e) {
169                     //Ignore core exceptions
170
}
171
172                 return referenced.toArray();
173             }
174         };
175     }
176
177     /**
178      * Get the defualt widget height for the supplied control.
179      * @return int
180      * @param control - the control being queried about fonts
181      * @param lines - the number of lines to be shown on the table.
182      */

183     private static int getDefaultFontHeight(Control control, int lines) {
184         FontData[] viewerFontData = control.getFont().getFontData();
185         int fontHeight = 10;
186
187         //If we have no font data use our guess
188
if (viewerFontData.length > 0) {
189             fontHeight = viewerFontData[0].getHeight();
190         }
191         return lines * fontHeight;
192
193     }
194
195     /**
196      * Handle the exception thrown when saving.
197      * @param e the exception
198      */

199     protected void handle(InvocationTargetException JavaDoc e) {
200         IStatus error;
201         Throwable JavaDoc target = e.getTargetException();
202         if (target instanceof CoreException) {
203             error = ((CoreException) target).getStatus();
204         } else {
205             String JavaDoc msg = target.getMessage();
206             if (msg == null) {
207                 msg = IDEWorkbenchMessages.Internal_error;
208             }
209             error = new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH,
210                     1, msg, target);
211         }
212         ErrorDialog.openError(getControl().getShell(), null, null, error);
213     }
214
215     /**
216      * Initializes a ProjectReferencePage.
217      */

218     private void initialize() {
219         project = (IProject) getElement().getAdapter(IResource.class);
220         noDefaultAndApplyButton();
221         setDescription(NLS.bind(IDEWorkbenchMessages.ProjectReferencesPage_label, project.getName()));
222     }
223
224     /**
225      * @see PreferencePage#performOk
226      */

227     public boolean performOk() {
228         if (!modified) {
229             return true;
230         }
231         Object JavaDoc[] checked = listViewer.getCheckedElements();
232         final IProject[] refs = new IProject[checked.length];
233         System.arraycopy(checked, 0, refs, 0, checked.length);
234         IRunnableWithProgress runnable = new IRunnableWithProgress() {
235             public void run(IProgressMonitor monitor)
236                     throws InvocationTargetException JavaDoc {
237
238                 try {
239                     IProjectDescription description = project.getDescription();
240                     description.setReferencedProjects(refs);
241                     project.setDescription(description, monitor);
242                 } catch (CoreException e) {
243                     throw new InvocationTargetException JavaDoc(e);
244                 }
245             }
246         };
247         try {
248             new ProgressMonitorJobsDialog(getControl().getShell()).run(true,
249                     true, runnable);
250         } catch (InterruptedException JavaDoc e) {
251             //Ignore interrupted exceptions
252
} catch (InvocationTargetException JavaDoc e) {
253             handle(e);
254             return false;
255         }
256         return true;
257     }
258 }
259
Popular Tags