001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.dialogs;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.resources.IWorkspace;
014: import org.eclipse.core.resources.ResourcesPlugin;
015: import org.eclipse.jface.viewers.CheckboxTableViewer;
016: import org.eclipse.jface.viewers.IStructuredContentProvider;
017: import org.eclipse.jface.viewers.ViewerComparator;
018: import org.eclipse.jface.wizard.WizardPage;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.graphics.Font;
021: import org.eclipse.swt.graphics.FontData;
022: import org.eclipse.swt.layout.GridData;
023: import org.eclipse.swt.layout.GridLayout;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
029: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
030: import org.eclipse.ui.model.WorkbenchContentProvider;
031: import org.eclipse.ui.model.WorkbenchLabelProvider;
032:
033: /**
034: * Standard project reference page for a wizard that creates a
035: * project resource.
036: * <p>
037: * This page may be used by clients as-is; it may be also be
038: * subclassed to suit.
039: * </p>
040: * <p>
041: * Example usage:
042: * <pre>
043: * referencePage = new WizardNewProjectReferencePage("basicReferenceProjectPage");
044: * referencePage.setTitle("Project");
045: * referencePage.setDescription("Select referenced projects.");
046: * </pre>
047: * </p>
048: */
049: public class WizardNewProjectReferencePage extends WizardPage {
050: // widgets
051: private CheckboxTableViewer referenceProjectsViewer;
052:
053: private static final String REFERENCED_PROJECTS_TITLE = IDEWorkbenchMessages.WizardNewProjectReferences_title;
054:
055: private static final int PROJECT_LIST_MULTIPLIER = 15;
056:
057: /**
058: * Creates a new project reference wizard page.
059: *
060: * @param pageName the name of this page
061: */
062: public WizardNewProjectReferencePage(String pageName) {
063: super (pageName);
064: }
065:
066: /** (non-Javadoc)
067: * Method declared on IDialogPage.
068: */
069: public void createControl(Composite parent) {
070:
071: Font font = parent.getFont();
072:
073: Composite composite = new Composite(parent, SWT.NONE);
074: composite.setLayout(new GridLayout());
075: composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
076: composite.setFont(font);
077:
078: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
079: IIDEHelpContextIds.NEW_PROJECT_REFERENCE_WIZARD_PAGE);
080:
081: Label referenceLabel = new Label(composite, SWT.NONE);
082: referenceLabel.setText(REFERENCED_PROJECTS_TITLE);
083: referenceLabel.setFont(font);
084:
085: referenceProjectsViewer = CheckboxTableViewer.newCheckList(
086: composite, SWT.BORDER);
087: referenceProjectsViewer.getTable().setFont(composite.getFont());
088: GridData data = new GridData();
089: data.horizontalAlignment = GridData.FILL;
090: data.grabExcessHorizontalSpace = true;
091:
092: data.heightHint = getDefaultFontHeight(referenceProjectsViewer
093: .getTable(), PROJECT_LIST_MULTIPLIER);
094: referenceProjectsViewer.getTable().setLayoutData(data);
095: referenceProjectsViewer.setLabelProvider(WorkbenchLabelProvider
096: .getDecoratingWorkbenchLabelProvider());
097: referenceProjectsViewer
098: .setContentProvider(getContentProvider());
099: referenceProjectsViewer.setComparator(new ViewerComparator());
100: referenceProjectsViewer
101: .setInput(ResourcesPlugin.getWorkspace());
102:
103: setControl(composite);
104: }
105:
106: /**
107: * Returns a content provider for the reference project
108: * viewer. It will return all projects in the workspace.
109: *
110: * @return the content provider
111: */
112: protected IStructuredContentProvider getContentProvider() {
113: return new WorkbenchContentProvider() {
114: public Object[] getChildren(Object element) {
115: if (!(element instanceof IWorkspace)) {
116: return new Object[0];
117: }
118: IProject[] projects = ((IWorkspace) element).getRoot()
119: .getProjects();
120: return projects == null ? new Object[0] : projects;
121: }
122: };
123: }
124:
125: /**
126: * Get the defualt widget height for the supplied control.
127: * @return int
128: * @param control - the control being queried about fonts
129: * @param lines - the number of lines to be shown on the table.
130: */
131: private static int getDefaultFontHeight(Control control, int lines) {
132: FontData[] viewerFontData = control.getFont().getFontData();
133: int fontHeight = 10;
134:
135: //If we have no font data use our guess
136: if (viewerFontData.length > 0) {
137: fontHeight = viewerFontData[0].getHeight();
138: }
139: return lines * fontHeight;
140:
141: }
142:
143: /**
144: * Returns the referenced projects selected by the user.
145: *
146: * @return the referenced projects
147: */
148: public IProject[] getReferencedProjects() {
149: Object[] elements = referenceProjectsViewer
150: .getCheckedElements();
151: IProject[] projects = new IProject[elements.length];
152: System.arraycopy(elements, 0, projects, 0, elements.length);
153: return projects;
154: }
155: }
|