001: /*******************************************************************************
002: * Copyright (c) 2005, 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.exports;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.Path;
018: import org.eclipse.jdt.core.IJavaProject;
019: import org.eclipse.jface.dialogs.Dialog;
020: import org.eclipse.jface.dialogs.IDialogSettings;
021: import org.eclipse.jface.dialogs.IMessageProvider;
022: import org.eclipse.jface.viewers.IStructuredContentProvider;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.TableViewer;
025: import org.eclipse.jface.window.Window;
026: import org.eclipse.pde.core.IModel;
027: import org.eclipse.pde.internal.ui.PDEPlugin;
028: import org.eclipse.pde.internal.ui.PDEUIMessages;
029: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
030: import org.eclipse.pde.internal.ui.parts.WizardCheckboxTablePart;
031: import org.eclipse.pde.internal.ui.wizards.ListUtil;
032: import org.eclipse.swt.SWT;
033: import org.eclipse.swt.layout.GridData;
034: import org.eclipse.swt.layout.GridLayout;
035: import org.eclipse.swt.widgets.Button;
036: import org.eclipse.swt.widgets.Composite;
037: import org.eclipse.swt.widgets.Control;
038: import org.eclipse.swt.widgets.TabFolder;
039: import org.eclipse.swt.widgets.TabItem;
040: import org.eclipse.ui.IWorkingSet;
041: import org.eclipse.ui.IWorkingSetManager;
042: import org.eclipse.ui.PlatformUI;
043: import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
044:
045: public abstract class BaseExportWizardPage extends
046: AbstractExportWizardPage {
047:
048: protected ExportPart fExportPart;
049: private IStructuredSelection fSelection;
050: protected ExportDestinationTab fDestinationTab;
051: protected ExportOptionsTab fOptionsTab;
052: protected JARSigningTab fJARSiginingTab;
053: protected TabFolder fTabFolder;
054:
055: class ExportListProvider extends DefaultContentProvider implements
056: IStructuredContentProvider {
057: public Object[] getElements(Object parent) {
058: return getListElements();
059: }
060: }
061:
062: class ExportPart extends WizardCheckboxTablePart {
063: public ExportPart(String label, String[] buttonLabels) {
064: super (label, buttonLabels);
065: }
066:
067: public void updateCounter(int count) {
068: super .updateCounter(count);
069: pageChanged();
070: }
071:
072: protected void buttonSelected(Button button, int index) {
073: switch (index) {
074: case 0:
075: handleSelectAll(true);
076: break;
077: case 1:
078: handleSelectAll(false);
079: break;
080: case 2:
081: handleWorkingSets();
082: }
083: }
084: }
085:
086: public BaseExportWizardPage(IStructuredSelection selection,
087: String name, String choiceLabel) {
088: super (name);
089: fSelection = selection;
090: fExportPart = new ExportPart(choiceLabel, new String[] {
091: PDEUIMessages.WizardCheckboxTablePart_selectAll,
092: PDEUIMessages.WizardCheckboxTablePart_deselectAll,
093: PDEUIMessages.ExportWizard_workingSet });
094: setDescription(PDEUIMessages.ExportWizard_Plugin_description);
095: }
096:
097: public void createControl(Composite parent) {
098: Composite container = new Composite(parent, SWT.NULL);
099: GridLayout layout = new GridLayout();
100: layout.verticalSpacing = 10;
101: container.setLayout(layout);
102:
103: createViewer(container);
104:
105: fTabFolder = new TabFolder(container, SWT.NONE);
106: fTabFolder
107: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
108: createTabs(fTabFolder, getDialogSettings());
109:
110: initializeViewer();
111: if (getErrorMessage() != null) {
112: setMessage(getErrorMessage());
113: setErrorMessage(null);
114: }
115: setControl(container);
116: hookHelpContext(container);
117: Dialog.applyDialogFont(container);
118: }
119:
120: protected void createTabs(TabFolder folder, IDialogSettings settings) {
121: createDestinationTab(folder);
122: fDestinationTab.initialize(settings);
123:
124: createOptionsTab(folder);
125: fOptionsTab.initialize(settings);
126:
127: if (fOptionsTab.useJARFormat()) {
128: createJARSigningTab(folder);
129: fJARSiginingTab.initialize(settings);
130: }
131: }
132:
133: protected void createDestinationTab(TabFolder folder) {
134: fDestinationTab = new ExportDestinationTab(this );
135: TabItem item = new TabItem(folder, SWT.NONE);
136: item.setControl(fDestinationTab.createControl(folder));
137: item.setText(PDEUIMessages.ExportWizard_destination);
138: }
139:
140: protected void createOptionsTab(TabFolder folder) {
141: fOptionsTab = new ExportOptionsTab(this );
142: TabItem item = new TabItem(folder, SWT.NONE);
143: item.setControl(fOptionsTab.createControl(folder));
144: item.setText(PDEUIMessages.ExportWizard_options);
145: }
146:
147: protected void createJARSigningTab(TabFolder folder) {
148: fJARSiginingTab = new JARSigningTab(this );
149: TabItem item = new TabItem(folder, SWT.NONE);
150: item.setControl(fJARSiginingTab.createControl(folder));
151: item.setText(PDEUIMessages.AdvancedPluginExportPage_signJar);
152: }
153:
154: protected void createViewer(Composite parent) {
155: Composite composite = new Composite(parent, SWT.NONE);
156: GridLayout layout = new GridLayout();
157: layout.numColumns = 3;
158: layout.marginHeight = 0;
159: layout.marginWidth = 0;
160: composite.setLayout(layout);
161: composite.setLayoutData(new GridData(GridData.FILL_BOTH));
162:
163: fExportPart.createControl(composite);
164: GridData gd = (GridData) fExportPart.getControl()
165: .getLayoutData();
166: gd.heightHint = 125;
167: gd.widthHint = 150;
168: gd.horizontalSpan = 2;
169:
170: TableViewer viewer = fExportPart.getTableViewer();
171: viewer.setContentProvider(new ExportListProvider());
172: viewer.setLabelProvider(PDEPlugin.getDefault()
173: .getLabelProvider());
174: viewer.setComparator(ListUtil.PLUGIN_COMPARATOR);
175: fExportPart.getTableViewer().setInput(getInput());
176: }
177:
178: protected abstract Object getInput();
179:
180: protected void initializeViewer() {
181: Object[] elems = fSelection.toArray();
182: ArrayList checked = new ArrayList(elems.length);
183:
184: for (int i = 0; i < elems.length; i++) {
185: Object elem = elems[i];
186: IProject project = null;
187:
188: if (elem instanceof IFile) {
189: IFile file = (IFile) elem;
190: project = file.getProject();
191: } else if (elem instanceof IProject) {
192: project = (IProject) elem;
193: } else if (elem instanceof IJavaProject) {
194: project = ((IJavaProject) elem).getProject();
195: }
196: if (project != null) {
197: IModel model = findModelFor(project);
198: if (model != null && !checked.contains(model)) {
199: checked.add(model);
200: }
201: }
202: }
203: fExportPart.setSelection(checked.toArray());
204: if (checked.size() > 0)
205: fExportPart.getTableViewer().reveal(checked.get(0));
206: }
207:
208: private void handleWorkingSets() {
209: IWorkingSetManager manager = PlatformUI.getWorkbench()
210: .getWorkingSetManager();
211: IWorkingSetSelectionDialog dialog = manager
212: .createWorkingSetSelectionDialog(getShell(), true);
213: if (dialog.open() == Window.OK) {
214: ArrayList models = new ArrayList();
215: IWorkingSet[] workingSets = dialog.getSelection();
216: for (int i = 0; i < workingSets.length; i++) {
217: IAdaptable[] elements = workingSets[i].getElements();
218: for (int j = 0; j < elements.length; j++) {
219: IModel model = findModelFor(elements[j]);
220: if (isValidModel(model)) {
221: models.add(model);
222: }
223: }
224: }
225: fExportPart.setSelection(models.toArray());
226: }
227: }
228:
229: public Object[] getSelectedItems() {
230: return fExportPart.getSelection();
231: }
232:
233: protected void pageChanged() {
234: if (getMessage() != null)
235: setMessage(null);
236: if (fOptionsTab != null) {
237: String path = fOptionsTab.getAntBuildFileName();
238: String warningMessage = null;
239: if (path != null && path.length() > 0
240: && "build.xml".equals(new Path(path).lastSegment())) //$NON-NLS-1$
241: warningMessage = PDEUIMessages.ExportOptionsTab_antReservedMessage;
242: setMessage(warningMessage, IMessageProvider.WARNING);
243: }
244: String error = fExportPart.getSelectionCount() > 0 ? null
245: : PDEUIMessages.ExportWizard_status_noselection;
246: if (error == null)
247: error = validateTabs();
248: setErrorMessage(error);
249: setPageComplete(error == null);
250: }
251:
252: protected String validateTabs() {
253: String message = fDestinationTab.validate();
254: if (message == null)
255: message = fOptionsTab.validate();
256: if (message == null && fTabFolder.getItemCount() > 2)
257: message = fJARSiginingTab.validate();
258: return message;
259: }
260:
261: protected abstract void hookHelpContext(Control control);
262:
263: protected abstract boolean isValidModel(IModel model);
264:
265: public abstract Object[] getListElements();
266:
267: protected abstract IModel findModelFor(IAdaptable object);
268:
269: protected void saveSettings(IDialogSettings settings) {
270: fDestinationTab.saveSettings(settings);
271: fOptionsTab.saveSettings(settings);
272: if (fJARSiginingTab != null)
273: fJARSiginingTab.saveSettings(settings);
274: }
275:
276: protected boolean doExportToDirectory() {
277: return fDestinationTab.doExportToDirectory();
278: }
279:
280: protected String getFileName() {
281: return fDestinationTab.getFileName();
282: }
283:
284: protected String getDestination() {
285: return fDestinationTab.getDestination();
286: }
287:
288: protected boolean doExportSource() {
289: return fOptionsTab.doExportSource();
290: }
291:
292: protected boolean useJARFormat() {
293: return fOptionsTab.useJARFormat();
294: }
295:
296: protected boolean doGenerateAntFile() {
297: return fOptionsTab.doGenerateAntFile();
298: }
299:
300: protected String getAntBuildFileName() {
301: return fOptionsTab.getAntBuildFileName();
302: }
303:
304: protected String getQualifier() {
305: return fOptionsTab.getQualifier();
306: }
307:
308: protected String[] getSigningInfo() {
309: if (fJARSiginingTab == null || fTabFolder.getItemCount() < 3)
310: return null;
311: return fJARSiginingTab.getSigningInfo();
312: }
313:
314: protected abstract void adjustAdvancedTabsVisibility();
315:
316: protected void adjustJARSigningTabVisibility() {
317: IDialogSettings settings = getDialogSettings();
318: if (useJARFormat()) {
319: if (fTabFolder.getItemCount() < 3) {
320: createJARSigningTab(fTabFolder);
321: fJARSiginingTab.initialize(settings);
322: }
323: } else {
324: if (fTabFolder.getItemCount() >= 3) {
325: fJARSiginingTab.saveSettings(settings);
326: fTabFolder.getItem(2).dispose();
327: }
328: }
329: }
330:
331: }
|