001package jmri.jmrit.display.controlPanelEditor; 002 003import java.awt.event.ActionEvent; 004import java.awt.event.ActionListener; 005import java.util.HashMap; 006import java.util.List; 007 008import javax.swing.AbstractButton; 009import javax.swing.BorderFactory; 010import javax.swing.Box; 011import javax.swing.BoxLayout; 012import javax.swing.ButtonGroup; 013import javax.swing.JComponent; 014import javax.swing.JLabel; 015import javax.swing.JPanel; 016import javax.swing.JRadioButton; 017import javax.swing.JScrollPane; 018import javax.swing.event.ListSelectionEvent; 019import javax.swing.event.ListSelectionListener; 020 021import jmri.jmrit.catalog.NamedIcon; 022import jmri.jmrit.logix.OBlock; 023import jmri.jmrit.logix.Portal; 024import jmri.util.swing.JmriJOptionPane; 025 026/** 027 * 028 * @author Pete Cressman Copyright: Copyright (c) 2013 029 * 030 */ 031public class EditPortalDirection extends EditFrame implements ActionListener, ListSelectionListener { 032 033 private PortalIcon _icon; 034 private JRadioButton _toButton; 035 private JRadioButton _fromButton; 036 private HashMap<String, NamedIcon> _iconMap; 037 038 private PortalList _portalList; 039 040 public EditPortalDirection(String title, CircuitBuilder parent, OBlock block) { 041 super(title, parent, block); 042 checkCircuitIcons("DirectionArrow"); 043 pack(); 044 } 045 046 private JPanel makeArrowPanel() { 047 JPanel panel = new JPanel(); 048 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 049 panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(java.awt.Color.black), 050 Bundle.getMessage("ArrowIconsTitle"))); 051 panel.add(Box.createHorizontalStrut(200)); 052 053 ButtonGroup group = new ButtonGroup(); 054 _toButton = new JRadioButton(_iconMap.get(PortalIcon.TO_ARROW)); 055 _toButton.setActionCommand(PortalIcon.TO_ARROW); 056 _toButton.addActionListener(this); 057 group.add(_toButton); 058 panel.add(_toButton); 059 060 _fromButton = new JRadioButton(_iconMap.get(PortalIcon.FROM_ARROW)); 061 _fromButton.setActionCommand(PortalIcon.FROM_ARROW); 062 _fromButton.addActionListener(this); 063 group.add(_fromButton); 064 panel.add(_fromButton); 065 066 JRadioButton _noButton = new JRadioButton(Bundle.getMessage("noIcon"), _iconMap.get(PortalIcon.HIDDEN)); 067 _noButton.setVerticalTextPosition(AbstractButton.CENTER); 068 _noButton.setHorizontalTextPosition(AbstractButton.CENTER); 069 _noButton.setActionCommand(PortalIcon.HIDDEN); 070 _noButton.addActionListener(this); 071 group.add(_noButton); 072 panel.add(_noButton); 073 074 return panel; 075 } 076 077 @Override 078 protected JPanel makeContentPanel() { 079 _iconMap = PortalIcon.getPaletteMap(); 080 JPanel portalPanel = new JPanel(); 081 portalPanel.setLayout(new BoxLayout(portalPanel, BoxLayout.Y_AXIS)); 082 083 JPanel panel = new JPanel(); 084 panel.add(new JLabel(Bundle.getMessage("PortalTitle", _homeBlock.getDisplayName()))); 085 portalPanel.add(panel); 086 087 _portalList = new PortalList(_homeBlock, this); 088 _portalList.addListSelectionListener(this); 089 portalPanel.add(new JScrollPane(_portalList)); 090 091 portalPanel.add(Box.createVerticalStrut(STRUT_SIZE)); 092 093 panel = new JPanel(); 094 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 095 096 JLabel l = new JLabel(Bundle.getMessage("PortalDirection1", _homeBlock.getDisplayName())); 097 l.setAlignmentX(JComponent.LEFT_ALIGNMENT); 098 panel.add(l); 099 panel.add(Box.createVerticalStrut(STRUT_SIZE / 2)); 100 l = new JLabel(Bundle.getMessage("PortalDirection2")); 101 l.setAlignmentX(JComponent.LEFT_ALIGNMENT); 102 panel.add(l); 103 l = new JLabel(Bundle.getMessage("PortalDirection3")); 104 l.setAlignmentX(JComponent.LEFT_ALIGNMENT); 105 panel.add(l); 106 panel.add(Box.createVerticalStrut(STRUT_SIZE)); 107 l = new JLabel(Bundle.getMessage("PortalDirection4")); 108 l.setAlignmentX(JComponent.LEFT_ALIGNMENT); 109 panel.add(l); 110 l = new JLabel(Bundle.getMessage("PortalDirection5")); 111 l.setAlignmentX(JComponent.LEFT_ALIGNMENT); 112 panel.add(l); 113 portalPanel.add(panel); 114 115 portalPanel.add(Box.createVerticalStrut(STRUT_SIZE)); 116 panel = new JPanel(); 117 panel.add(makeArrowPanel()); 118 portalPanel.add(panel); 119 120 portalPanel.add(Box.createVerticalStrut(STRUT_SIZE)); 121 122 portalPanel.add(makeDoneButtonPanel()); 123 return portalPanel; 124 } 125 126 @Override 127 protected void clearListSelection() { 128 _portalList.clearSelection(); 129 _parent._editor.highlight(null); 130 } 131 132 /** 133 * *********************** end setup ************************* 134 */ 135 @Override 136 public void valueChanged(ListSelectionEvent e) { 137 Portal portal = _portalList.getSelectedValue(); 138 if (portal != null) { 139 List<PortalIcon> piArray = _parent.getPortalIcons(portal); 140 if (piArray.isEmpty()) { 141 JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("portalHasNoIcon", portal.getName()), 142 Bundle.getMessage("incompleteCircuit"), JmriJOptionPane.INFORMATION_MESSAGE); 143 clearListSelection(); 144 } else { 145 for (PortalIcon icon : piArray) { 146 setPortalIcon(icon, false); 147 } 148 } 149 } 150 } 151 152 @Override 153 public void actionPerformed(ActionEvent e) { 154 if (_icon == null) { 155 return; 156 } 157 if (PortalIcon.TO_ARROW.equals(e.getActionCommand())) { 158 _icon.setIcon(PortalIcon.TO_ARROW,_iconMap.get(PortalIcon.TO_ARROW)); 159 _icon.setIcon(PortalIcon.FROM_ARROW, _iconMap.get(PortalIcon.FROM_ARROW)); 160 _icon.setArrowOrientation(true); 161 _icon.setHideArrows(false); 162 } else if (PortalIcon.FROM_ARROW.equals(e.getActionCommand())) { 163 _icon.setIcon(PortalIcon.TO_ARROW, _iconMap.get(PortalIcon.FROM_ARROW)); 164 _icon.setIcon(PortalIcon.FROM_ARROW, _iconMap.get(PortalIcon.TO_ARROW)); 165 _icon.setArrowOrientation(false); 166 _icon.setHideArrows(false); 167 } else if (PortalIcon.HIDDEN.equals(e.getActionCommand())) { 168 _icon.setHideArrows(true); 169 _icon.setStatus(PortalIcon.HIDDEN); 170 return; 171 } 172 _icon.setStatus(PortalIcon.TO_ARROW); 173 } 174 175 protected void setPortalIcon(PortalIcon icon, boolean setValue) { 176 if (!canEdit()) { 177 return; 178 } 179 _parent._editor.highlight(icon); 180 if (_icon != null) { 181 _icon.setStatus(PortalIcon.VISIBLE); 182 } 183 _icon = icon; 184 if (_icon != null) { 185 if (_icon.getArrowHide()) { 186 _icon.setStatus(PortalIcon.HIDDEN); 187 } else { 188 OBlock toBlock = _icon.getPortal().getToBlock(); 189 if (_homeBlock.equals(toBlock)) { 190 _icon.setStatus(PortalIcon.TO_ARROW); 191 } else { 192 _icon.setStatus(PortalIcon.FROM_ARROW); 193 } 194 } 195 _toButton.setIcon(_icon.getIcon(PortalIcon.TO_ARROW)); 196 _fromButton.setIcon(_icon.getIcon(PortalIcon.FROM_ARROW)); 197 if (setValue) { 198 _portalList.setSelectedValue(_icon.getPortal(), true); 199 } 200 } 201 } 202 203 @Override 204 protected void closingEvent(boolean close) { 205 StringBuffer sb = new StringBuffer(); 206 String msg = _parent.checkForPortals(_homeBlock, "BlockPaths"); 207 if (msg.length() > 0) { 208 sb.append(msg); 209 sb.append("\n"); 210 close = true; 211 } 212 msg = _parent.checkForPortalIcons(_homeBlock, "DirectionArrow"); 213 if (msg.length() > 0) { 214 sb.append(msg); 215 sb.append("\n"); 216 close = true; 217 } 218 closingEvent(close, sb.toString()); 219 } 220 221}