core/com.nokia.carbide.discovery.ui/src/com/nokia/carbide/internal/discovery/ui/wizard/ExportWizard.java
author dadubrow
Wed, 25 Aug 2010 20:02:15 -0500
changeset 1888 fc9d5d520518
parent 1885 988b4ab1b8c1
child 1889 7232c671a6c7
permissions -rw-r--r--
Write initial features file at startup to ensure it exists prior to any user installation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     1
/*
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     3
* All rights reserved.
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     4
* This component and the accompanying materials are made available
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     6
* which accompanies this distribution, and is available
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     8
*
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
     9
* Initial Contributors:
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    11
*
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    12
* Contributors:
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    13
*
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    14
* Description: 
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    15
*
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    16
*/
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    17
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    18
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    19
package com.nokia.carbide.internal.discovery.ui.wizard;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    20
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    21
import java.io.File;
1884
94c9fe3693ea Filter initial features from export wizard + sort wizards by feature id + version
dadubrow
parents: 1824
diff changeset
    22
import java.io.FileInputStream;
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    23
import java.io.FileOutputStream;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    24
import java.io.OutputStream;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    25
import java.text.MessageFormat;
1884
94c9fe3693ea Filter initial features from export wizard + sort wizards by feature id + version
dadubrow
parents: 1824
diff changeset
    26
import java.util.ArrayList;
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    27
import java.util.Collection;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    28
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    29
import org.eclipse.jface.dialogs.MessageDialog;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    30
import org.eclipse.jface.viewers.IStructuredSelection;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    31
import org.eclipse.jface.wizard.Wizard;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    32
import org.eclipse.ui.IExportWizard;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    33
import org.eclipse.ui.IWorkbench;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    34
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    35
import com.nokia.carbide.discovery.ui.Activator;
1824
7e34f628583a update import wizard and operation
dadubrow
parents: 1818
diff changeset
    36
import com.nokia.carbide.discovery.ui.Messages;
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    37
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    38
/**
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    39
 * Wizard for exporting installed features to a file
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    40
 */
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    41
public class ExportWizard extends Wizard implements IExportWizard {
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    42
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    43
	private ExportPage exportPage;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    44
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    45
	public ExportWizard() {
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    46
	}
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    47
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    48
	public boolean performFinish() {
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    49
		Collection<FeatureInfo> featureInfos = exportPage.getFeatureInfos();
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    50
		File file = exportPage.getFile();
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    51
		OutputStream os;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    52
		try {
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    53
			os = new FileOutputStream(file);
1818
bb528d06abcd initial commit of feature config import
dadubrow
parents: 1810
diff changeset
    54
			ImportExportData data = new ImportExportData(false, P2Utils.getKnownRepositories(), featureInfos);
bb528d06abcd initial commit of feature config import
dadubrow
parents: 1810
diff changeset
    55
			Streamer.writeToXML(os, data);
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    56
		} catch (Exception e) {
1824
7e34f628583a update import wizard and operation
dadubrow
parents: 1818
diff changeset
    57
			MessageDialog.openError(getShell(), Messages.ExportWizard_ErrorTitle, 
7e34f628583a update import wizard and operation
dadubrow
parents: 1818
diff changeset
    58
					MessageFormat.format(Messages.ExportWizard_WriteFileError, e.getMessage()));
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    59
		}
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    60
		return true;
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    61
	}
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    62
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    63
	public void init(IWorkbench workbench, IStructuredSelection selection) {
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    64
		setDefaultPageImageDescriptor(Activator.getImageDescriptor("icons\\install_wiz.gif")); //$NON-NLS-1$
1824
7e34f628583a update import wizard and operation
dadubrow
parents: 1818
diff changeset
    65
		setWindowTitle(Messages.ExportWizard_Title);
1888
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    66
		exportPage = new ExportPage(getInitialFeatureIds());
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    67
		addPage(exportPage);
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    68
	}
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    69
1888
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    70
	private Collection<String> getInitialFeatureIds() {
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    71
		// make sure it exists if not written already
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    72
		File file = P2Utils.getInitialFeaturesFile();
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    73
		if (!file.exists()) {
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    74
			P2Utils.writeFeaturesToFile(file);
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    75
		}
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    76
		
1884
94c9fe3693ea Filter initial features from export wizard + sort wizards by feature id + version
dadubrow
parents: 1824
diff changeset
    77
		Collection<FeatureInfo> installedFeatures = null;
1888
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    78
		try {
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    79
			ImportExportData data = Streamer.readFromXML(new FileInputStream(file));
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    80
			installedFeatures = data.getFeatureInfos();
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    81
		} catch (Exception e) {
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    82
			Activator.logError(Messages.ExportWizard_ReadInstalledFeaturesError + file, e);
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    83
		}
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    84
		
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    85
		Collection<String> featureIds = new ArrayList<String>();
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    86
		if (installedFeatures != null) {
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    87
			for (FeatureInfo featureInfo : installedFeatures) {
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    88
				featureIds.add(featureInfo.getId());
1884
94c9fe3693ea Filter initial features from export wizard + sort wizards by feature id + version
dadubrow
parents: 1824
diff changeset
    89
			}
94c9fe3693ea Filter initial features from export wizard + sort wizards by feature id + version
dadubrow
parents: 1824
diff changeset
    90
		}
1888
fc9d5d520518 Write initial features file at startup to ensure it exists prior to any user installation
dadubrow
parents: 1885
diff changeset
    91
		return featureIds;
1884
94c9fe3693ea Filter initial features from export wizard + sort wizards by feature id + version
dadubrow
parents: 1824
diff changeset
    92
	}
94c9fe3693ea Filter initial features from export wizard + sort wizards by feature id + version
dadubrow
parents: 1824
diff changeset
    93
1810
2ccd3660a736 feature config exporter + non-functional importer
dadubrow
parents:
diff changeset
    94
}