Java tutorial
/* * 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.HashMap; 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.XmlOperatorsCostsReader; import co.turnus.ui.util.EclipseHelper; import co.turnus.ui.widgets.SelectFile; import co.turnus.util.TurnusLogger; public class LoadOperatorCostsFilePage extends TitleAreaDialog implements SelectionListener { private SelectFile outputChooser; private Map<Operator, Double> operatorCostsMap; private File file; public LoadOperatorCostsFilePage(Shell parentShell) { super(parentShell); } public Map<Operator, Double> getOperatorCostsMap() { if (operatorCostsMap != null) { return new HashMap<Operator, Double>(operatorCostsMap); } return null; } @Override public void create() { super.create(); setTitle("Load the costs configuration from an XML file"); setMessage("Select an XML from where the configuration will be loaded", 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 input file selected"); return container; } @Override protected boolean isResizable() { return true; } private void loadFile() { EclipseHelper.openDefaultConsole(); try { operatorCostsMap = new XmlOperatorsCostsReader().load(file); TurnusLogger.info("XML file loaded from: " + file); } catch (TurnusException e1) { operatorCostsMap = null; TurnusLogger.error("XML file cannot be load from: " + file); } } @Override protected void okPressed() { loadFile(); super.okPressed(); } @Override public void widgetSelected(SelectionEvent e) { String value = outputChooser.getText(); if (value != null && !value.isEmpty()) { File tmpFile = new File(value); if (tmpFile.exists()) { file = tmpFile; // reset the error message setErrorMessage(null); getButton(IDialogConstants.OK_ID).setEnabled(true); return; } } file = null; outputChooser.setFile(""); setErrorMessage("no input file selected"); getButton(IDialogConstants.OK_ID).setEnabled(false); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }