| 1 | package tmcsim.simulationmanager.actions; |
|---|
| 2 | |
|---|
| 3 | import java.awt.event.ActionEvent; |
|---|
| 4 | import java.util.logging.Level; |
|---|
| 5 | import java.util.logging.Logger; |
|---|
| 6 | |
|---|
| 7 | import javax.swing.AbstractAction; |
|---|
| 8 | import javax.swing.JComboBox; |
|---|
| 9 | |
|---|
| 10 | import tmcsim.cadmodels.CMSDiversion; |
|---|
| 11 | import tmcsim.common.SimulationException; |
|---|
| 12 | import tmcsim.simulationmanager.SimulationManagerView; |
|---|
| 13 | import tmcsim.simulationmanager.dialogs.CMSDiversionDialog; |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * ApplyDiversionAction is an AbstractAction that is used for settings traffic |
|---|
| 18 | * diversions in the simulation. When the action is performed, the action gets |
|---|
| 19 | * the selected CMS ID from the CMSIDComboBox in the SimulationManagerView. |
|---|
| 20 | * The action then calls the SimulationManagerModel to get the current CMSInfo |
|---|
| 21 | * for this id. The CMSDiversionDialog is then shown with this CMSInfo. If |
|---|
| 22 | * the user updates the diversions at this CMS, the new info is sent to the |
|---|
| 23 | * SimulationManagerModel to update the current diversions. |
|---|
| 24 | * <br/> |
|---|
| 25 | * <br/> |
|---|
| 26 | * The CMS_ID_COMBO_BOX key should be assigned the CMSID JComboBox object |
|---|
| 27 | * from the SimulationManagerView object. |
|---|
| 28 | */ |
|---|
| 29 | @SuppressWarnings("serial") |
|---|
| 30 | public class ApplyDiversionAction extends AbstractAction { |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * This key string is used to reference the CMSID JComboBox object |
|---|
| 34 | * from the SimulationManagerView object. |
|---|
| 35 | */ |
|---|
| 36 | public static final String CMS_ID_COMBO_BOX = "CMS_ID_COMBO_BOX"; |
|---|
| 37 | |
|---|
| 38 | /** Reference to the SimulationManagerView object. */ |
|---|
| 39 | private SimulationManagerView theSimManagerView = null; |
|---|
| 40 | |
|---|
| 41 | /** CMSDiversionDialog used for updating diversions in the simulation. */ |
|---|
| 42 | private CMSDiversionDialog theCMSDiversionDialog; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Constructor. |
|---|
| 46 | * @param view View class object for the Simulation Manager. |
|---|
| 47 | */ |
|---|
| 48 | public ApplyDiversionAction(SimulationManagerView view) { |
|---|
| 49 | super("Divert Traffic"); |
|---|
| 50 | |
|---|
| 51 | theSimManagerView = view; |
|---|
| 52 | theCMSDiversionDialog = new CMSDiversionDialog(theSimManagerView); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public void actionPerformed(ActionEvent evt) { |
|---|
| 56 | if(getValue(CMS_ID_COMBO_BOX) == null) { |
|---|
| 57 | Logger.getLogger("tmcsim.simulationmanager.actions").logp( |
|---|
| 58 | Level.WARNING, "ApplyDiversionAction", |
|---|
| 59 | "actionPerformed", |
|---|
| 60 | "Object for CMS_ID_COMBO_BOX is null."); |
|---|
| 61 | return; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | showDiversionDialog((String)((JComboBox)getValue(CMS_ID_COMBO_BOX)).getSelectedItem()); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public void showDiversionDialog(final String cms_id) { |
|---|
| 69 | |
|---|
| 70 | Runnable divertRunnable = new Runnable(){ |
|---|
| 71 | public void run() { |
|---|
| 72 | |
|---|
| 73 | try{ |
|---|
| 74 | |
|---|
| 75 | theCMSDiversionDialog.showDialog( |
|---|
| 76 | theSimManagerView.getModel().getCMSDiversionInfo(cms_id)); |
|---|
| 77 | |
|---|
| 78 | long currentSimTime = theSimManagerView.getCurrentSimTime(); |
|---|
| 79 | |
|---|
| 80 | if(theCMSDiversionDialog.theCMSInfo.isUpdated()) { |
|---|
| 81 | |
|---|
| 82 | for(CMSDiversion div : theCMSDiversionDialog.theCMSInfo.possibleDiversions) { |
|---|
| 83 | if(div.isUpdated()) { |
|---|
| 84 | div.timeApplied = currentSimTime; |
|---|
| 85 | |
|---|
| 86 | theSimManagerView.removeDiversion(theCMSDiversionDialog.theCMSInfo, div); |
|---|
| 87 | |
|---|
| 88 | if(!div.isCleared()) { |
|---|
| 89 | theSimManagerView.addDiversion(theCMSDiversionDialog.theCMSInfo, div); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | theSimManagerView.getModel().applyDiversions(theCMSDiversionDialog.theCMSInfo); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | } |
|---|
| 98 | catch (SimulationException se) { |
|---|
| 99 | theSimManagerView.SimulationExceptionHandler(se); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | Thread theThread = new Thread(divertRunnable); |
|---|
| 105 | theThread.start(); |
|---|
| 106 | |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | } |
|---|