KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > WizardNewProjectReferencePage


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.dialogs;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.IWorkspace;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.jface.viewers.CheckboxTableViewer;
17 import org.eclipse.jface.viewers.IStructuredContentProvider;
18 import org.eclipse.jface.viewers.ViewerComparator;
19 import org.eclipse.jface.wizard.WizardPage;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.FontData;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
30 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
31 import org.eclipse.ui.model.WorkbenchContentProvider;
32 import org.eclipse.ui.model.WorkbenchLabelProvider;
33
34 /**
35  * Standard project reference page for a wizard that creates a
36  * project resource.
37  * <p>
38  * This page may be used by clients as-is; it may be also be
39  * subclassed to suit.
40  * </p>
41  * <p>
42  * Example usage:
43  * <pre>
44  * referencePage = new WizardNewProjectReferencePage("basicReferenceProjectPage");
45  * referencePage.setTitle("Project");
46  * referencePage.setDescription("Select referenced projects.");
47  * </pre>
48  * </p>
49  */

50 public class WizardNewProjectReferencePage extends WizardPage {
51     // widgets
52
private CheckboxTableViewer referenceProjectsViewer;
53
54     private static final String JavaDoc REFERENCED_PROJECTS_TITLE = IDEWorkbenchMessages.WizardNewProjectReferences_title;
55
56     private static final int PROJECT_LIST_MULTIPLIER = 15;
57
58     /**
59      * Creates a new project reference wizard page.
60      *
61      * @param pageName the name of this page
62      */

63     public WizardNewProjectReferencePage(String JavaDoc pageName) {
64         super(pageName);
65     }
66
67     /** (non-Javadoc)
68      * Method declared on IDialogPage.
69      */

70     public void createControl(Composite parent) {
71
72         Font font = parent.getFont();
73
74         Composite composite = new Composite(parent, SWT.NONE);
75         composite.setLayout(new GridLayout());
76         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
77         composite.setFont(font);
78
79         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
80                 IIDEHelpContextIds.NEW_PROJECT_REFERENCE_WIZARD_PAGE);
81
82         Label referenceLabel = new Label(composite, SWT.NONE);
83         referenceLabel.setText(REFERENCED_PROJECTS_TITLE);
84         referenceLabel.setFont(font);
85
86         referenceProjectsViewer = CheckboxTableViewer.newCheckList(composite,
87                 SWT.BORDER);
88         referenceProjectsViewer.getTable().setFont(composite.getFont());
89         GridData data = new GridData();
90         data.horizontalAlignment = GridData.FILL;
91         data.grabExcessHorizontalSpace = true;
92
93         data.heightHint = getDefaultFontHeight(referenceProjectsViewer
94                 .getTable(), PROJECT_LIST_MULTIPLIER);
95         referenceProjectsViewer.getTable().setLayoutData(data);
96         referenceProjectsViewer.setLabelProvider(WorkbenchLabelProvider
97                 .getDecoratingWorkbenchLabelProvider());
98         referenceProjectsViewer.setContentProvider(getContentProvider());
99         referenceProjectsViewer.setComparator(new ViewerComparator());
100         referenceProjectsViewer.setInput(ResourcesPlugin.getWorkspace());
101
102         setControl(composite);
103     }
104
105     /**
106      * Returns a content provider for the reference project
107      * viewer. It will return all projects in the workspace.
108      *
109      * @return the content provider
110      */

111     protected IStructuredContentProvider getContentProvider() {
112         return new WorkbenchContentProvider() {
113             public Object JavaDoc[] getChildren(Object JavaDoc element) {
114                 if (!(element instanceof IWorkspace)) {
115                     return new Object JavaDoc[0];
116                 }
117                 IProject[] projects = ((IWorkspace) element).getRoot()
118                         .getProjects();
119                 return projects == null ? new Object JavaDoc[0] : projects;
120             }
121         };
122     }
123
124     /**
125      * Get the defualt widget height for the supplied control.
126      * @return int
127      * @param control - the control being queried about fonts
128      * @param lines - the number of lines to be shown on the table.
129      */

130     private static int getDefaultFontHeight(Control control, int lines) {
131         FontData[] viewerFontData = control.getFont().getFontData();
132         int fontHeight = 10;
133
134         //If we have no font data use our guess
135
if (viewerFontData.length > 0) {
136             fontHeight = viewerFontData[0].getHeight();
137         }
138         return lines * fontHeight;
139
140     }
141
142     /**
143      * Returns the referenced projects selected by the user.
144      *
145      * @return the referenced projects
146      */

147     public IProject[] getReferencedProjects() {
148         Object JavaDoc[] elements = referenceProjectsViewer.getCheckedElements();
149         IProject[] projects = new IProject[elements.length];
150         System.arraycopy(elements, 0, projects, 0, elements.length);
151         return projects;
152     }
153 }
154
Popular Tags