001package jmri.jmrit.operations.routes.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.routes.*; 011import jmri.jmrit.operations.routes.gui.RouteEditFrame; 012import jmri.jmrit.operations.setup.Control; 013import jmri.util.swing.JmriJOptionPane; 014 015/** 016 * Frame for copying a route for operations. 017 * 018 * @author Bob Jacobsen Copyright (C) 2001 019 * @author Daniel Boudreau Copyright (C) 2008, 2010 020 */ 021public class RouteCopyFrame extends OperationsFrame { 022 023 RouteManager routeManager = InstanceManager.getDefault(RouteManager.class); 024 025 // labels 026 javax.swing.JLabel textCopyRoute = new javax.swing.JLabel(Bundle.getMessage("CopyRoute")); 027 javax.swing.JLabel textRouteName = new javax.swing.JLabel(Bundle.getMessage("RouteName")); 028 029 // text field 030 javax.swing.JTextField routeNameTextField = new javax.swing.JTextField(Control.max_len_string_route_name); 031 032 // check boxes 033 javax.swing.JCheckBox invertCheckBox = new javax.swing.JCheckBox(Bundle.getMessage("Invert")); 034 035 // major buttons 036 javax.swing.JButton copyButton = new javax.swing.JButton(Bundle.getMessage("ButtonCopy")); 037 038 // combo boxes 039 JComboBox<Route> routeBox = InstanceManager.getDefault(RouteManager.class).getComboBox(); 040 041 public RouteCopyFrame(Route route) { 042 super(Bundle.getMessage("TitleRouteCopy")); 043 // general GUI config 044 045 getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 046 047 // Set up the panels 048 JPanel p1 = new JPanel(); 049 p1.setLayout(new GridBagLayout()); 050 051 // Layout the panel by rows 052 // row 1 textRouteName 053 addItem(p1, textRouteName, 0, 1); 054 addItemWidth(p1, routeNameTextField, 3, 1, 1); 055 056 // row 2 057 addItem(p1, textCopyRoute, 0, 2); 058 addItemWidth(p1, routeBox, 3, 1, 2); 059 060 // row 4 061 addItem(p1, invertCheckBox, 0, 4); 062 addItem(p1, copyButton, 1, 4); 063 064 getContentPane().add(p1); 065 066 // add help menu to window 067 addHelpMenu("package.jmri.jmrit.operations.Operations_CopyRoute", true); // NOI18N 068 069 initMinimumSize(new Dimension(Control.panelWidth600, Control.panelHeight200)); 070 071 // setup buttons 072 addButtonAction(copyButton); 073 074 routeBox.setSelectedItem(route); 075 } 076 077 @Override 078 public void buttonActionPerformed(java.awt.event.ActionEvent ae) { 079 if (ae.getSource() == copyButton) { 080 log.debug("copy route button activated"); 081 if (!checkName()) { 082 return; 083 } 084 085 Route newRoute = routeManager.getRouteByName(routeNameTextField.getText()); 086 if (newRoute != null) { 087 reportRouteExists(Bundle.getMessage("add")); 088 return; 089 } 090 if (routeBox.getSelectedItem() == null) { 091 reportRouteDoesNotExist(); 092 return; 093 } 094 095 // now copy 096 Route oldRoute = (Route) routeBox.getSelectedItem(); 097 newRoute = routeManager.copyRoute(oldRoute, routeNameTextField.getText(), 098 invertCheckBox.isSelected()); 099 100 RouteEditFrame f = new RouteEditFrame(); 101 f.initComponents(newRoute); 102 } 103 } 104 105 private void reportRouteExists(String s) { 106 JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("ReportExists"), 107 Bundle.getMessage("CanNotRoute", s), 108 JmriJOptionPane.ERROR_MESSAGE); 109 } 110 111 private void reportRouteDoesNotExist() { 112 JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("CopyRoute"), 113 Bundle.getMessage("CopyRoute"), JmriJOptionPane.ERROR_MESSAGE); 114 } 115 116 /** 117 * @return true if name length is okay 118 */ 119 private boolean checkName() { 120 if (routeNameTextField.getText().trim().isEmpty()) { 121 JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("EnterRouteName"), 122 Bundle.getMessage("EnterRouteName"), JmriJOptionPane.ERROR_MESSAGE); 123 return false; 124 } 125 if (routeNameTextField.getText().length() > Control.max_len_string_route_name) { 126 JmriJOptionPane.showMessageDialog(this, 127 Bundle.getMessage("RouteNameLess", 128 Control.max_len_string_route_name + 1), 129 Bundle 130 .getMessage("CanNotAddRoute"), 131 JmriJOptionPane.ERROR_MESSAGE); 132 return false; 133 } 134 return true; 135 } 136 137 @Override 138 public void dispose() { 139 super.dispose(); 140 } 141 142 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RouteCopyFrame.class); 143}