Java tutorial
/** * Copyright (c) 2011 - 2015, Lunifera GmbH (Gross Enzersdorf), Loetz KG (Heidelberg) * 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: * Florian Pirchner - Initial implementation */ package org.lunifera.vaaclipse.ui.preferences.addon.internal.impexp; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; import org.eclipse.core.internal.preferences.PreferencesService; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.IPreferenceFilter; import org.lunifera.vaaclipse.ui.preferences.model.PreferencesPage; import org.semanticsoft.vaadin.optiondialog.OptionDialog; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.vaadin.server.FileDownloader; import com.vaadin.server.StreamResource; import com.vaadin.server.StreamResource.StreamSource; import com.vaadin.ui.Button; import com.vaadin.ui.Component; import com.vaadin.ui.CssLayout; import com.vaadin.ui.Label; import com.vaadin.ui.themes.BaseTheme; /** * @author rushan * */ public class ExportPreferences extends BasicImpExp { Logger logger = LoggerFactory.getLogger(ExportPreferences.class); private CssLayout layout; private Button downloadButton; private byte[] preferencesBytes; @Override public Component getComponent(OptionDialog optionDialog) { layout = new CssLayout(); layout.addStyleName("export"); layout.addComponent(new Label("Select preferences to export")); createPreferencesTable(layout); createStatusLabel(layout, "Press Export to export preferences"); return layout; } @SuppressWarnings("serial") private StreamResource createResource() { return new StreamResource(new StreamSource() { @Override public InputStream getStream() { if (preferencesBytes != null) { ByteArrayInputStream is = new ByteArrayInputStream(preferencesBytes); return is; } else return null; } }, "preferences.epf"); } @SuppressWarnings("restriction") @Override protected void doAction() { // dlg.close(); List<PreferencesPage> selectedPages = getSelectedPages(); if (selectedPages.isEmpty()) { setStatusText("Preferences doesn't selected. Please select preferences above."); return; } IEclipsePreferences root = PreferencesService.getDefault().getRootNode(); ByteArrayOutputStream baos = new ByteArrayOutputStream(200); try { PreferencesService.getDefault().exportPreferences(root, new IPreferenceFilter[] { createFilter(selectedPages) }, baos); } catch (CoreException e) { logger.error("Exception when export preferences", e); return; } preferencesBytes = baos.toByteArray(); StringBuffer exportedPrefNames = toTextWithCatName(selectedPages); setStatusText("Preferences was exported: " + exportedPrefNames.toString()); if (downloadButton == null) { downloadButton = new Button(BaseTheme.BUTTON_LINK); downloadButton.addStyleName("download-button"); downloadButton.setCaption("Download preferences.epf"); layout.addComponent(downloadButton); FileDownloader fileDownloader = new FileDownloader(createResource()); fileDownloader.extend(downloadButton); } } @Override protected String getActionName() { return "Export"; } }