co.turnus.ui.profiling.wizard.page.StoreOperatorCostsFilePage.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.ui.profiling.wizard.page.StoreOperatorCostsFilePage.java

Source

/* 
 * TURNUS, the co-exploration framework
 * 
 * Copyright (C) 2015 EPFL SCI STI MM
 *
 * This file is part of TURNUS.
 *
 * TURNUS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TURNUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TURNUS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Additional permission under GNU GPL version 3 section 7
 * 
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or 
 * an Eclipse library), containing parts covered by the terms of the 
 * Eclipse Public License (EPL), the licensors of this Program grant you 
 * additional permission to convey the resulting work.  Corresponding Source 
 * for a non-source form of such a combination shall include the source code 
 * for the parts of Eclipse libraries used as well as that of the  covered work.
 * 
 */
package co.turnus.ui.profiling.wizard.page;

import java.io.File;
import java.util.Map;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import co.turnus.TurnusException;
import co.turnus.common.Operator;
import co.turnus.profiling.io.XmlOperatorsWeightsWriter;
import co.turnus.ui.util.EclipseHelper;
import co.turnus.ui.widgets.SelectFile;
import co.turnus.util.TurnusLogger;

public class StoreOperatorCostsFilePage extends TitleAreaDialog implements SelectionListener {

    private SelectFile outputChooser;
    private Map<Operator, Double> operatorCostsMap;
    private File file;

    public StoreOperatorCostsFilePage(Map<Operator, Double> operatorCostsMap, Shell parentShell) {
        super(parentShell);
        this.operatorCostsMap = operatorCostsMap;
    }

    @Override
    public void create() {
        super.create();
        setTitle("Store the costs configuration to an XML file");
        setMessage("Select an XML file where this configuration will be saved", IMessageProvider.INFORMATION);
        getButton(IDialogConstants.OK_ID).setEnabled(false);
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite area = (Composite) super.createDialogArea(parent);
        Composite container = new Composite(area, SWT.NONE);
        container.setLayoutData(new GridData(GridData.FILL_BOTH));
        GridLayout layout = new GridLayout(1, false);
        container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        container.setLayout(layout);

        GridData dataFirstName = new GridData();
        dataFirstName.grabExcessHorizontalSpace = true;
        dataFirstName.horizontalAlignment = GridData.FILL;

        outputChooser = new SelectFile(container, "Output", "Output File", new String[] { "*.xml" }, SWT.SAVE);
        outputChooser.setLayoutData(dataFirstName);
        outputChooser.addSelectionListener(this);

        setErrorMessage("no output file selected");

        return container;
    }

    @Override
    protected boolean isResizable() {
        return true;
    }

    private void saveFile() {
        EclipseHelper.openDefaultConsole();

        try {
            new XmlOperatorsWeightsWriter().write(operatorCostsMap, file);
            TurnusLogger.info("XML file written in: " + file);
        } catch (TurnusException e) {
            TurnusLogger.error("XML file cannot be written in: " + file);
        }
    }

    @Override
    protected void okPressed() {
        saveFile();
        super.okPressed();
    }

    @Override
    public void widgetSelected(SelectionEvent e) {
        String value = outputChooser.getText();
        if (value != null && !value.isEmpty()) {
            //TODO check if parent exists
            file = new File(value);
            setErrorMessage(null);
            getButton(IDialogConstants.OK_ID).setEnabled(true);
        } else {
            setErrorMessage("no output file selected");
            getButton(IDialogConstants.OK_ID).setEnabled(false);
        }
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
    }

}