001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.pde.internal.ui.wizards.templates;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.jface.dialogs.Dialog;
015: import org.eclipse.jface.viewers.CheckboxTableViewer;
016: import org.eclipse.jface.viewers.ColumnWeightData;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.IStructuredContentProvider;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.ITableLabelProvider;
021: import org.eclipse.jface.viewers.LabelProvider;
022: import org.eclipse.jface.viewers.SelectionChangedEvent;
023: import org.eclipse.jface.viewers.StructuredViewer;
024: import org.eclipse.jface.viewers.TableLayout;
025: import org.eclipse.jface.wizard.IWizardPage;
026: import org.eclipse.jface.wizard.Wizard;
027: import org.eclipse.jface.wizard.WizardPage;
028: import org.eclipse.pde.internal.ui.IHelpContextIds;
029: import org.eclipse.pde.internal.ui.PDEPlugin;
030: import org.eclipse.pde.internal.ui.PDEPluginImages;
031: import org.eclipse.pde.internal.ui.PDEUIMessages;
032: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
033: import org.eclipse.pde.internal.ui.parts.FormBrowser;
034: import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
035: import org.eclipse.pde.ui.templates.ITemplateSection;
036: import org.eclipse.swt.SWT;
037: import org.eclipse.swt.graphics.Image;
038: import org.eclipse.swt.layout.GridData;
039: import org.eclipse.swt.layout.GridLayout;
040: import org.eclipse.swt.widgets.Composite;
041: import org.eclipse.swt.widgets.Control;
042: import org.eclipse.swt.widgets.Table;
043: import org.eclipse.swt.widgets.TableColumn;
044: import org.eclipse.ui.PlatformUI;
045: import org.eclipse.ui.forms.widgets.FormToolkit;
046:
047: public class TemplateSelectionPage extends WizardPage {
048: private ITemplateSection[] fCandidates;
049: private ArrayList fVisiblePages;
050: private WizardCheckboxTablePart fTablePart;
051: private FormBrowser fDescriptionBrowser;
052:
053: class TablePart extends WizardCheckboxTablePart {
054: public TablePart(String mainLabel) {
055: super (mainLabel);
056: }
057:
058: protected StructuredViewer createStructuredViewer(
059: Composite parent, int style, FormToolkit toolkit) {
060: return super .createStructuredViewer(parent, style
061: | SWT.FULL_SELECTION, toolkit);
062: }
063:
064: protected void updateCounter(int amount) {
065: super .updateCounter(amount);
066: if (getContainer() != null)
067: getContainer().updateButtons();
068: }
069: }
070:
071: class ListContentProvider extends DefaultContentProvider implements
072: IStructuredContentProvider {
073: public Object[] getElements(Object parent) {
074: return fCandidates;
075: }
076: }
077:
078: class ListLabelProvider extends LabelProvider implements
079: ITableLabelProvider {
080: public String getColumnText(Object obj, int index) {
081: ITemplateSection section = (ITemplateSection) obj;
082: if (index == 0)
083: return section.getLabel();
084: return section.getUsedExtensionPoint();
085: }
086:
087: public Image getColumnImage(Object obj, int index) {
088: if (index == 0)
089: return PDEPlugin.getDefault().getLabelProvider().get(
090: PDEPluginImages.DESC_EXTENSION_OBJ);
091: return PDEPlugin.getDefault().getLabelProvider().get(
092: PDEPluginImages.DESC_EXT_POINT_OBJ);
093: }
094: }
095:
096: /**
097: * Constructor for TemplateSelectionPage.
098: * @param pageName
099: */
100: public TemplateSelectionPage(ITemplateSection[] candidates) {
101: super ("templateSelection"); //$NON-NLS-1$
102: fCandidates = candidates;
103: setTitle(PDEUIMessages.TemplateSelectionPage_title);
104: setDescription(PDEUIMessages.TemplateSelectionPage_desc);
105: initializeTemplates();
106: }
107:
108: private void initializeTemplates() {
109: fTablePart = new TablePart(
110: PDEUIMessages.TemplateSelectionPage_table);
111: fDescriptionBrowser = new FormBrowser(SWT.BORDER | SWT.V_SCROLL);
112: fDescriptionBrowser.setText(""); //$NON-NLS-1$
113: PDEPlugin.getDefault().getLabelProvider().connect(this );
114: fVisiblePages = new ArrayList();
115: }
116:
117: public void dispose() {
118: super .dispose();
119: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
120: }
121:
122: /**
123: * @see IDialogPage#createControl(Composite)
124: */
125: public void createControl(Composite parent) {
126: Composite container = new Composite(parent, SWT.NULL);
127: GridLayout layout = new GridLayout();
128: container.setLayout(layout);
129: layout.numColumns = 2;
130: fTablePart.createControl(container);
131: CheckboxTableViewer viewer = fTablePart.getTableViewer();
132: viewer.setContentProvider(new ListContentProvider());
133: viewer.setLabelProvider(new ListLabelProvider());
134: initializeTable(viewer.getTable());
135:
136: viewer
137: .addSelectionChangedListener(new ISelectionChangedListener() {
138: public void selectionChanged(
139: SelectionChangedEvent event) {
140: IStructuredSelection sel = (IStructuredSelection) event
141: .getSelection();
142: handleSelectionChanged((ITemplateSection) sel
143: .getFirstElement());
144: }
145: });
146: fDescriptionBrowser.createControl(container);
147: Control c = fDescriptionBrowser.getControl();
148: GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
149: | GridData.FILL_VERTICAL);
150: gd.heightHint = 100;
151: //gd.horizontalSpan = 2;
152: c.setLayoutData(gd);
153: viewer.setInput(PDEPlugin.getDefault());
154: // add all wizard pages to wizard. Just don't iniatilize them right away (bug 174457)
155: initializeWizardPages();
156: fTablePart.selectAll(true);
157: setControl(container);
158: Dialog.applyDialogFont(container);
159: PlatformUI.getWorkbench().getHelpSystem().setHelp(container,
160: IHelpContextIds.TEMPLATE_SELECTION);
161: }
162:
163: private void initializeWizardPages() {
164: for (int i = 0; i < fCandidates.length; i++) {
165: ITemplateSection section = fCandidates[i];
166: if (section.getPagesAdded() == false)
167: section.addPages((Wizard) getWizard());
168: }
169: }
170:
171: public ITemplateSection[] getSelectedTemplates() {
172: Object[] elements = fTablePart.getTableViewer()
173: .getCheckedElements();
174: ITemplateSection[] result = new ITemplateSection[elements.length];
175: System.arraycopy(elements, 0, result, 0, elements.length);
176: return result;
177: }
178:
179: private void initializeTable(Table table) {
180: table.setHeaderVisible(true);
181: TableColumn column = new TableColumn(table, SWT.NULL);
182: column.setText(PDEUIMessages.TemplateSelectionPage_column_name);
183: column.setResizable(true);
184: column = new TableColumn(table, SWT.NULL);
185: column
186: .setText(PDEUIMessages.TemplateSelectionPage_column_point);
187: column.setResizable(true);
188:
189: TableLayout layout = new TableLayout();
190: layout.addColumnData(new ColumnWeightData(50));
191: layout.addColumnData(new ColumnWeightData(50));
192: table.setLayout(layout);
193: }
194:
195: private void handleSelectionChanged(ITemplateSection section) {
196: String text = section != null ? section.getDescription() : ""; //$NON-NLS-1$
197: if (text.length() > 0)
198: text = "<p>" + text + "</p>"; //$NON-NLS-1$ //$NON-NLS-2$
199: fDescriptionBrowser.setText(text);
200: }
201:
202: public boolean canFlipToNextPage() {
203: if (fTablePart.getSelectionCount() == 0)
204: return false;
205: return super .canFlipToNextPage();
206: }
207:
208: public IWizardPage getNextPage() {
209: ITemplateSection[] sections = getSelectedTemplates();
210: fVisiblePages.clear();
211:
212: for (int i = 0; i < sections.length; i++) {
213: ITemplateSection section = sections[i];
214:
215: for (int j = 0; j < section.getPageCount(); j++) {
216: fVisiblePages.add(section.getPage(j));
217: }
218: }
219: if (fVisiblePages.size() > 0)
220: return (IWizardPage) fVisiblePages.get(0);
221:
222: return null;
223: }
224:
225: public IWizardPage getNextVisiblePage(IWizardPage page) {
226: if (page == this )
227: return page.getNextPage();
228: int index = fVisiblePages.indexOf(page);
229: if (index >= 0 && index < fVisiblePages.size() - 1)
230: return (IWizardPage) fVisiblePages.get(index + 1);
231: return null;
232: }
233:
234: }
|