001package jmri.jmrit.operations.locations.tools; 002 003import java.awt.Dimension; 004import java.awt.GridBagLayout; 005 006import javax.swing.*; 007 008import jmri.InstanceManager; 009import jmri.jmrit.operations.OperationsFrame; 010import jmri.jmrit.operations.OperationsXml; 011import jmri.jmrit.operations.locations.Location; 012import jmri.jmrit.operations.locations.LocationManager; 013import jmri.jmrit.operations.setup.Control; 014import jmri.jmrit.operations.setup.Setup; 015import jmri.util.PhysicalLocation; 016import jmri.util.PhysicalLocationPanel; 017import jmri.util.swing.JmriJOptionPane; 018 019/** 020 * Frame for setting train physical location coordinates for a location. 021 * 022 * @author Bob Jacobsen Copyright (C) 2001 023 * @author Daniel Boudreau Copyright (C) 2010 024 * @author Mark Underwood Copyright (C) 2011 025 */ 026public class SetPhysicalLocationFrame extends OperationsFrame { 027 028 LocationManager locationManager = InstanceManager.getDefault(LocationManager.class); 029 030 Location _location; 031 032 // labels 033 // text field 034 // check boxes 035 // major buttons 036 JButton saveButton = new JButton(Bundle.getMessage("ButtonSave")); 037 JButton closeButton = new JButton(Bundle.getMessage("Close")); 038 039 // combo boxes 040 JComboBox<Location> locationBox = InstanceManager.getDefault(LocationManager.class).getComboBox(); 041 042 // Spinners 043 PhysicalLocationPanel physicalLocation; 044 045 public SetPhysicalLocationFrame(Location l) { 046 super(Bundle.getMessage("MenuSetPhysicalLocation")); 047 048 // Store the location (null if called from the list view) 049 _location = l; 050 051 // general GUI config 052 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 053 054 // set tool tips 055 saveButton.setToolTipText(Bundle.getMessage("TipSaveButton")); 056 closeButton.setToolTipText(Bundle.getMessage("TipCloseButton")); 057 058 // Set up the panels 059 JPanel pLocation = new JPanel(); 060 pLocation.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Location"))); 061 pLocation.add(locationBox); 062 063 physicalLocation = new PhysicalLocationPanel(Bundle.getMessage("PhysicalLocation")); 064 physicalLocation.setToolTipText(Bundle.getMessage("PhysicalLocationToolTip")); 065 physicalLocation.setVisible(true); 066 067 JPanel pControl = new JPanel(); 068 pControl.setLayout(new GridBagLayout()); 069 pControl.setBorder(BorderFactory.createTitledBorder("")); 070 addItem(pControl, saveButton, 1, 0); 071 addItem(pControl, closeButton, 2, 0); 072 073 getContentPane().add(pLocation); 074 getContentPane().add(physicalLocation); 075 getContentPane().add(pControl); 076 077 // add help menu to window 078 addHelpMenu("package.jmri.jmrit.operations.Operations_SetTrainIconCoordinates", true); // fix this // NOI18N 079 // later 080 081 // setup buttons 082 saveButton.addActionListener(new java.awt.event.ActionListener() { 083 @Override 084 public void actionPerformed(java.awt.event.ActionEvent e) { 085 saveButtonActionPerformed(e); 086 } 087 }); 088 closeButton.addActionListener(new java.awt.event.ActionListener() { 089 @Override 090 public void actionPerformed(java.awt.event.ActionEvent e) { 091 closeButtonActionPerformed(e); 092 } 093 }); 094 095 // setup combo box 096 addComboBoxAction(locationBox); 097 098 if (_location != null) { 099 locationBox.setSelectedItem(_location); 100 } 101 102 initMinimumSize(new Dimension(Control.panelWidth400, Control.panelHeight250)); 103 } 104 105 /** 106 * Close button action 107 * 108 * @param ae The ActionEvent. 109 */ 110 public void closeButtonActionPerformed(java.awt.event.ActionEvent ae) { 111 dispose(); 112 } 113 114 /** 115 * Save button action {@literal ->} save this Reporter's location 116 * 117 * @param ae The ActionEvent. 118 */ 119 public void saveButtonActionPerformed(java.awt.event.ActionEvent ae) { 120 // check to see if a location has been selected 121 if (locationBox.getSelectedItem() == null) { 122 JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("SelectLocationToEdit"), 123 Bundle.getMessage("NoLocationSelected"), JmriJOptionPane.ERROR_MESSAGE); 124 return; 125 } 126 Location l = (Location) locationBox.getSelectedItem(); 127 if (l == null) { 128 return; 129 } 130 if (ae.getSource() == saveButton) { 131 int value = JmriJOptionPane.showConfirmDialog(this, 132 Bundle.getMessage("UpdatePhysicalLocation", l.getName()), 133 Bundle.getMessage("UpdateDefaults"), JmriJOptionPane.YES_NO_OPTION); 134 if (value == JmriJOptionPane.YES_OPTION) { 135 saveSpinnerValues(l); 136 } 137 OperationsXml.save(); 138 if (Setup.isCloseWindowOnSaveEnabled()) { 139 dispose(); 140 } 141 } 142 } 143 144 @Override 145 public void comboBoxActionPerformed(java.awt.event.ActionEvent ae) { 146 if (locationBox.getSelectedItem() == null) { 147 resetSpinners(); 148 } else { 149 Location l = (Location) locationBox.getSelectedItem(); 150 loadSpinners(l); 151 } 152 } 153 154 @Override 155 public void spinnerChangeEvent(javax.swing.event.ChangeEvent ae) { 156 if (ae.getSource() == physicalLocation) { 157 Location l = (Location) locationBox.getSelectedItem(); 158 if (l != null) { 159 l.setPhysicalLocation(physicalLocation.getValue()); 160 } 161 } 162 } 163 164 private void resetSpinners() { 165 // Reset spinners to zero. 166 physicalLocation.setValue(new PhysicalLocation()); 167 } 168 169 private void loadSpinners(Location l) { 170 log.debug("Load spinners location {}", l.getName()); 171 physicalLocation.setValue(l.getPhysicalLocation()); 172 } 173 174 /* 175 * private void spinnersEnable(boolean enable){ 176 * physicalLocation.setEnabled(enable); } 177 */ 178 private void saveSpinnerValues(Location l) { 179 log.debug("Save physical coordinates for location {}", l.getName()); 180 l.setPhysicalLocation(physicalLocation.getValue()); 181 } 182 183 @Override 184 public void dispose() { 185 super.dispose(); 186 } 187 188 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SetPhysicalLocationFrame.class); 189}