| 1 | package tmcsim.simulationmanager.actions; |
|---|
| 2 | |
|---|
| 3 | import java.awt.event.ActionEvent; |
|---|
| 4 | import java.io.File; |
|---|
| 5 | |
|---|
| 6 | import javax.swing.AbstractAction; |
|---|
| 7 | import javax.swing.JFileChooser; |
|---|
| 8 | import javax.swing.SwingUtilities; |
|---|
| 9 | |
|---|
| 10 | import tmcsim.common.ScriptException; |
|---|
| 11 | import tmcsim.common.SimulationException; |
|---|
| 12 | import tmcsim.simulationmanager.SimulationManager; |
|---|
| 13 | import tmcsim.simulationmanager.SimulationManagerView; |
|---|
| 14 | import tmcsim.simulationmanager.model.XmlFilter; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * LoadScriptAction is an AbstractAction that is used for loading a |
|---|
| 18 | * simulation script. When the action is performed, the action opens |
|---|
| 19 | * a file chooser dialog. If the user chooses a file, the action calls the |
|---|
| 20 | * SimulationMAnagerModel to load the script file. |
|---|
| 21 | * @author Matthew Cechini |
|---|
| 22 | */ |
|---|
| 23 | @SuppressWarnings("serial") |
|---|
| 24 | public class LoadScriptAction extends AbstractAction { |
|---|
| 25 | |
|---|
| 26 | /** Reference to the SimulationManagerView object. */ |
|---|
| 27 | private SimulationManagerView theSimManagerView = null; |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * Constructor. |
|---|
| 31 | * @param view View class object for the Simulation Manager. |
|---|
| 32 | */ |
|---|
| 33 | public LoadScriptAction(SimulationManagerView view) { |
|---|
| 34 | super("Load Script"); |
|---|
| 35 | |
|---|
| 36 | theSimManagerView = view; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | public void actionPerformed(ActionEvent evt) { |
|---|
| 40 | SwingUtilities.invokeLater(new Runnable(){ |
|---|
| 41 | public void run() { |
|---|
| 42 | JFileChooser chooser = new JFileChooser(SimulationManager.SCENARIOS_DIR); |
|---|
| 43 | |
|---|
| 44 | chooser.setDialogTitle("Open Simulation Scenario File"); |
|---|
| 45 | chooser.setMultiSelectionEnabled(false); |
|---|
| 46 | chooser.setFileFilter(new XmlFilter()); |
|---|
| 47 | |
|---|
| 48 | int result = chooser.showOpenDialog(null); |
|---|
| 49 | |
|---|
| 50 | File selectedFile = chooser.getSelectedFile(); |
|---|
| 51 | |
|---|
| 52 | if(result == JFileChooser.APPROVE_OPTION) { |
|---|
| 53 | |
|---|
| 54 | try{ |
|---|
| 55 | theSimManagerView.getModel().loadScript(selectedFile); |
|---|
| 56 | theSimManagerView.setTitle("Simulation Manager: " + selectedFile.getName()); |
|---|
| 57 | } |
|---|
| 58 | catch (ScriptException se) { |
|---|
| 59 | theSimManagerView.ScriptExceptionHandler(se); |
|---|
| 60 | } |
|---|
| 61 | catch (SimulationException se) { |
|---|
| 62 | theSimManagerView.SimulationExceptionHandler(se); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | }); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | } |
|---|