001 002package jmri.jmrit.operations.locations.tools; 003 004import java.awt.Dimension; 005import java.awt.GridBagLayout; 006 007import javax.swing.*; 008 009import org.slf4j.Logger; 010import org.slf4j.LoggerFactory; 011 012import jmri.jmrit.operations.OperationsFrame; 013import jmri.jmrit.operations.OperationsXml; 014import jmri.jmrit.operations.locations.Track; 015import jmri.jmrit.operations.setup.Control; 016import jmri.jmrit.operations.setup.Setup; 017 018/** 019 * Planned Pick ups. 020 * Frame to allow a user to define how much used track space is to be ignored 021 * by the program when placing new rolling stock onto a track. 022 * 023 * @author Daniel Boudreau Copyright (C) 2012, 2017 024 * 025 */ 026class IgnoreUsedTrackFrame extends OperationsFrame { 027 028 // radio buttons 029 JRadioButton zeroPercent = new JRadioButton(Bundle.getMessage("Disabled")); 030 JRadioButton twentyfivePercent = new JRadioButton("25%"); // NOI18N 031 JRadioButton fiftyPercent = new JRadioButton("50%"); // NOI18N 032 JRadioButton seventyfivePercent = new JRadioButton("75%"); // NOI18N 033 JRadioButton hundredPercent = new JRadioButton("100%"); // NOI18N 034 035 // major buttons 036 JButton saveButton = new JButton(Bundle.getMessage("ButtonSave")); 037 038 protected Track _track; 039 040 public IgnoreUsedTrackFrame(Track track) { 041 super(); 042 043 setTitle(Bundle.getMessage("MenuItemPlannedPickups")); 044 045 _track = track; 046 if (_track == null) { 047 log.debug("track is null!"); 048 return; 049 } 050 051 // load the panel 052 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 053 054 // row 1 055 JPanel p1 = new JPanel(); 056 p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS)); 057 p1.setMaximumSize(new Dimension(2000, 250)); 058 059 // row 1a 060 JPanel pTrackName = new JPanel(); 061 pTrackName.setLayout(new GridBagLayout()); 062 pTrackName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Track"))); 063 addItem(pTrackName, new JLabel(track.getName()), 0, 0); 064 065 // row 1b 066 JPanel pLocationName = new JPanel(); 067 pLocationName.setLayout(new GridBagLayout()); 068 pLocationName.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("Location"))); 069 addItem(pLocationName, new JLabel(track.getLocation().getName()), 0, 0); 070 071 p1.add(pTrackName); 072 p1.add(pLocationName); 073 074 JPanel p2 = new JPanel(); 075 p2.setBorder(BorderFactory.createTitledBorder(Bundle.getMessage("PrePlanedPickups"))); 076 077 p2.add(zeroPercent); 078 p2.add(twentyfivePercent); 079 p2.add(fiftyPercent); 080 p2.add(seventyfivePercent); 081 p2.add(hundredPercent); 082 083 ButtonGroup buttonGroup = new ButtonGroup(); 084 buttonGroup.add(zeroPercent); 085 buttonGroup.add(twentyfivePercent); 086 buttonGroup.add(fiftyPercent); 087 buttonGroup.add(seventyfivePercent); 088 buttonGroup.add(hundredPercent); 089 090 // select the correct radio button 091 int percentage = _track.getIgnoreUsedLengthPercentage(); 092 zeroPercent.setSelected(percentage >= 0); 093 twentyfivePercent.setSelected(percentage >= 25); 094 fiftyPercent.setSelected(percentage >= 50); 095 seventyfivePercent.setSelected(percentage >= 75); 096 hundredPercent.setSelected(percentage >= 100); 097 098 // warning text for planned pick ups. 099 JPanel p3 = new JPanel(); 100 p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS)); 101 p3.add(new JLabel(Bundle.getMessage("PPWarningMessage"))); 102 p3.add(new JLabel(Bundle.getMessage("PPWarningMessage2"))); 103 104 JPanel pW = new JPanel(); 105 pW.setLayout(new GridBagLayout()); 106 addItem(pW, p3, 0, 1); 107 addItem(pW, saveButton, 0, 2); 108 109 getContentPane().add(p1); 110 getContentPane().add(p2); 111 getContentPane().add(pW); 112 113 addButtonAction(saveButton); 114 115 addHelpMenu("package.jmri.jmrit.operations.Operations_PlannedPickUps", true); // NOI18N 116 117 initMinimumSize(new Dimension(Control.panelWidth600, Control.panelHeight300)); 118 } 119 120 @Override 121 public void buttonActionPerformed(java.awt.event.ActionEvent ae) { 122 if (ae.getSource() == saveButton) { 123 // save percentage selected 124 int percentage = 0; 125 if (twentyfivePercent.isSelected()) { 126 percentage = 25; 127 } else if (fiftyPercent.isSelected()) { 128 percentage = 50; 129 } else if (seventyfivePercent.isSelected()) { 130 percentage = 75; 131 } else if (hundredPercent.isSelected()) { 132 percentage = 100; 133 } 134 if (_track != null) { 135 _track.setIgnoreUsedLengthPercentage(percentage); 136 if (_track.getAlternateTrack() != null && percentage > 0) { 137 JOptionPane.showMessageDialog(null, Bundle.getMessage("PPWarningAlternate"), 138 Bundle.getMessage("PPWarningConfiguration"), 139 JOptionPane.ERROR_MESSAGE); 140 } 141 } 142 // save location file 143 OperationsXml.save(); 144 if (Setup.isCloseWindowOnSaveEnabled()) { 145 dispose(); 146 } 147 } 148 } 149 150 private final static Logger log = LoggerFactory.getLogger(IgnoreUsedTrackFrame.class); 151}