001package jmri.jmrit.beantable.routetable; 002 003import java.awt.*; 004import java.util.*; 005 006import javax.swing.*; 007 008import jmri.InstanceManager; 009import jmri.Route; 010import jmri.RouteManager; 011import jmri.util.swing.JmriJOptionPane; 012 013/** 014 * Add frame for the Route Table. 015 * 016 * Split from {@link jmri.jmrit.beantable.RouteTableAction} 017 * 018 * @author Dave Duchamp Copyright (C) 2004 019 * @author Bob Jacobsen Copyright (C) 2007 020 * @author Simon Reader Copyright (C) 2008 021 * @author Pete Cressman Copyright (C) 2009 022 * @author Egbert Broerse Copyright (C) 2016 023 * @author Paul Bender Copyright (C) 2020 024 */ 025public class RouteAddFrame extends AbstractRouteAddEditFrame { 026 027 028 public RouteAddFrame() { 029 this(Bundle.getMessage("TitleAddRoute")); 030 } 031 032 public RouteAddFrame(String name) { 033 this(name,false,true); 034 } 035 036 public RouteAddFrame(String name, boolean saveSize, boolean savePosition) { 037 super(name, saveSize, savePosition); 038 super.initComponents(); 039 } 040 041 @Override 042 protected JPanel getButtonPanel() { 043 final JButton copyButton = new JButton(Bundle.getMessage("ButtonCopyRoute")); 044 final JButton createButton = new JButton(Bundle.getMessage("ButtonCreate")); 045 final JButton editButton = new JButton(Bundle.getMessage("ButtonEdit")); 046 final JButton cancelButton = new JButton(Bundle.getMessage("ButtonCancel")); 047 final JButton updateButton = new JButton(Bundle.getMessage("ButtonUpdate")); 048 049 // add Buttons panel 050 JPanel pb = new JPanel(); 051 pb.setLayout(new BorderLayout()); 052 053 // Copy button 054 JPanel left = new JPanel(); 055 left.add(copyButton); 056 copyButton.addActionListener( e -> copyPressed()); 057 058 JPanel right = new JPanel(); 059 right.setLayout(new FlowLayout(FlowLayout.TRAILING)); 060 061 // Cancel (Add) button 062 right.add(cancelButton); 063 cancelButton.addActionListener( e -> cancelAddPressed()); 064 065 // Create button 066 right.add(createButton); 067 createButton.addActionListener( e -> createPressed()); 068 createButton.setToolTipText(Bundle.getMessage("TooltipCreateRoute")); 069 070 pb.add(left, BorderLayout.WEST); 071 pb.add(right, BorderLayout.EAST); 072 073 // Show the initial buttons, and hide the others 074 copyButton.setVisible(true); 075 cancelButton.setVisible(true); 076 updateButton.setVisible(false); 077 editButton.setVisible(false); 078 createButton.setVisible(true); 079 return pb; 080 } 081 082 /** 083 * Respond to the CancelAdd button. 084 */ 085 private void cancelAddPressed() { 086 if (routeDirty) { 087 showReminderMessage(); 088 } 089 curRoute = null; 090 finishUpdate(); 091 status1.setText(Bundle.getMessage("RouteAddStatusInitial1", Bundle.getMessage("ButtonCreate"))); // I18N to include original button name in help string 092 //status2.setText(Bundle.getMessage("RouteAddStatusInitial2", Bundle.getMessage("ButtonEdit"))); 093 routeDirty = false; 094 // hide addFrame 095 setVisible(false); 096 closeFrame(); 097 } 098 099 /** 100 * Respond to the Create button. 101 */ 102 private void createPressed() { 103 if ( !_autoSystemName.isSelected() && !checkNewNamesOK() ) { 104 return; 105 } 106 updatePressed(true); // close pane after creating 107 //status2.setText(Bundle.getMessage("RouteAddStatusInitial2", Bundle.getMessage("ButtonEdit"))); 108 pref.setSimplePreferenceState(systemNameAuto, _autoSystemName.isSelected()); 109 // activate the route 110 if (curRoute != null) { 111 curRoute.activateRoute(); 112 } 113 closeFrame(); 114 } 115 116 /** 117 * Respond to the Copy button. 118 * Request a source route for the copy and load the page if possible. 119 */ 120 private void copyPressed() { 121 var selectedRoute = selectSourceRoute(); 122 if (selectedRoute != null) { 123 var route = InstanceManager.getDefault(RouteManager.class).getRoute(selectedRoute); 124 if (route != null) { 125 setPageContent(route); 126 } 127 } 128 } 129 130 /** 131 * Display an input dialog with a combo box of existing routes. 132 * 133 * @return the selected route name or null if none. 134 */ 135 private String selectSourceRoute() { 136 var routes = InstanceManager.getDefault(RouteManager.class).getNamedBeanSet(); 137 var routeList = new ArrayList<String>(); 138 routes.forEach( r -> routeList.add(r.getDisplayName())); 139 Collections.sort(routeList); 140 141 var routeArray = new String[routeList.size()]; 142 routeList.toArray(routeArray); 143 144 try { 145 var icon = new ImageIcon(jmri.util.FileUtil.getProgramPath() + jmri.Application.getLogo()); 146 var choice = JmriJOptionPane.showInputDialog( 147 this, 148 Bundle.getMessage("LabelCopyRoute"), 149 Bundle.getMessage("TitleCopyRoute"), 150 JmriJOptionPane.QUESTION_MESSAGE, 151 icon, 152 routeArray, 153 null); 154 return (String) choice; 155 } catch (HeadlessException ex) { 156 return null; 157 } 158 } 159 160 /** 161 * Check name for a new Route object using the _systemName field on the addFrame pane. 162 * 163 * @return whether name entered is allowed 164 */ 165 private boolean checkNewNamesOK() { 166 // Get system name and user name from Add Route pane 167 String sName = _systemName.getText(); 168 String uName = _userName.getText(); 169 if (sName.length() == 0) { 170 status1.setText(Bundle.getMessage("AddBeanStatusEnter")); 171 status1.setForeground(Color.red); 172 return false; 173 } 174 Route g; 175 // check if a Route with the same user name exists 176 if (!uName.isEmpty()) { 177 g = routeManager.getByUserName(uName); 178 if (g != null) { 179 // Route already exists 180 status1.setText(Bundle.getMessage("LightError8")); 181 return false; 182 } 183 } 184 // check if a Route with this system name already exists 185 sName = routeManager.makeSystemName(sName); 186 g = routeManager.getBySystemName(sName); 187 if (g != null) { 188 // Route already exists 189 status1.setText(Bundle.getMessage("LightError1")); 190 return false; 191 } 192 return true; 193 } 194 195}