Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f08a673b0860663b8055c69ac629257bef35e5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2011 WindRiver Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     WindRiver Corporation - initial API and implementation
 *     Ericsson AB (Pascal Rapicault) - Bug 387115 - Allow to export everything
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.importexport.internal.wizard;

import java.io.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.importexport.internal.Messages;
import org.eclipse.equinox.internal.p2.ui.ProvUI;
import org.eclipse.equinox.internal.p2.ui.model.ProfileElement;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.PlatformUI;

public class ExportPage extends AbstractPage {
	public ExportPage(String pageName) {
		super(pageName);
		setTitle(Messages.ExportPage_Title);
		setDescription(Messages.ExportPage_Description);
	}

	@Override
	protected void createContents(Composite composite) {
		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
		if (getSelfProfile() == null) {
			label.setText(Messages.ExportPage_ERROR_CONFIG);
		} else {
			label.setText(Messages.ExportPage_Label);

			createInstallationTable(composite);
			createDestinationGroup(composite, true);
		}
	}

	@Override
	public void doFinish() throws Exception {
		finishException = null;
		if (viewer == null)
			return;
		// about to invoke the operation so save our state
		saveWidgetValues();
		final Object[] checked = viewer.getCheckedElements();
		final boolean includeAllEntries = includeAllButton.getSelection();
		OutputStream stream = null;
		try {
			File target = new File(ExportPage.this.destinationNameField.getText());
			if (!target.exists())
				target.createNewFile();
			stream = new BufferedOutputStream(new FileOutputStream(target));
			final OutputStream out = stream;
			getContainer().run(true, true, new IRunnableWithProgress() {

				public void run(IProgressMonitor monitor) throws InterruptedException {
					try {
						IInstallableUnit[] units = new IInstallableUnit[checked.length];
						for (int i = 0; i < units.length; i++)
							units[i] = ProvUI.getAdapter(checked[i], IInstallableUnit.class);
						IStatus status = importexportService.exportP2F(out, units, includeAllEntries, monitor);
						if (status.isMultiStatus()) {
							final StringBuilder sb = new StringBuilder();
							for (IStatus child : status.getChildren()) {
								if (child.isMultiStatus()) {
									for (IStatus grandchild : child.getChildren())
										sb.append("<li>").append(grandchild.getMessage()).append("</li>"); //$NON-NLS-1$ //$NON-NLS-2$
								} else if (child.isOK())
									sb.insert(0, Messages.ExportPage_SuccessWithProblems);
								else {
									sb.insert(0, Messages.ExportPage_Fail);
									sb.append(status.getMessage());
								}
							}
							sb.append(Messages.ExportPage_FixSuggestion);
							sb.append(Messages.ExportPage_TryAgainQuestion);
							Runnable runnable = new Runnable() {

								public void run() {
									String title = Messages.ExportPage_Title;
									tryAgain = StyledErrorDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title, sb.toString());
								}
							};
							Display.getDefault().syncExec(runnable);
							if (tryAgain)
								throw new OperationCanceledException("User chosed try again."); //$NON-NLS-1$
						}
					} catch (OperationCanceledException e) {
						throw new InterruptedException(e.getMessage());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				boolean tryAgain = false;
			});
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e) {
					// do nothing
				}
			}
			if (finishException != null)
				throw finishException;
		}
	}

	@Override
	protected String getDialogTitle() {
		return Messages.ExportPage_FILEDIALOG_TITLE;
	}

	@Override
	protected Object getInput() {
		ProfileElement element = new ProfileElement(null, getSelfProfile().getProfileId());
		return element;
	}

	@Override
	protected String getInvalidDestinationMessage() {
		return Messages.ExportPage_DEST_ERRORMESSAGE;
	}

	@Override
	protected void giveFocusToDestination() {
		if (viewer != null)
			viewer.getControl().setFocus();
	}

	@Override
	protected String getDestinationLabel() {
		return Messages.ExportPage_LABEL_EXPORTFILE;
	}

	@Override
	protected int getBrowseDialogStyle() {
		return SWT.SAVE;
	}
}

Back to the top