/** * */ package ie.dcu.apps.ist.actions; import ie.dcu.apps.ist.exp.*; import java.io.*; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; /** * Open an experiment file and switch to experiment mode. * * @author Kevin McGuinness */ public class OpenExperimentAction extends AppAction { private FileDialog dialog; public OpenExperimentAction(ActionManager m) { super(m); } @Override public void run() { File file = getFile(); if (file != null) { open(file); } } private void open(File file) { try { Experiment ex = ExperimentFactory.getInstance().load(file); window.setExperiment(ex); } catch (IOException ex) { handleError(file, ex); } catch (FormatException ex) { handleError(file, ex); } } private void handleError(File file, Exception ex) { // Get appropriate message String mesg = ex.getMessage(); // Log warning log.warning(String.format( "Unable to open experiment file %s\n Problem: %s", file, mesg )); // Show warning dialog warning("Unable to open experiment file %s:\n%s", file.getName(), mesg); } private File getFile() { createOpenDialog(); String path = dialog.open(); if (path != null) { return new File(path); } return null; } private void createOpenDialog() { // Create dialog if necessary if (dialog == null) { dialog = new FileDialog(window.getShell(), SWT.OPEN | SWT.SHEET); dialog.setText(property("OpenExperimentAction.dialog.text")); dialog.setFilterExtensions(getFilters()); dialog.setFilterNames(getFilterNames()); } } private String[] getFilters() { return new String[] { property("OpenExperimentAction.dialog.filter.exts") }; } private String[] getFilterNames() { return new String[] { property("OpenExperimentAction.dialog.filter.text") }; } }