001package jmri.jmrit.display.palette; 002 003import java.awt.FlowLayout; 004 005import javax.swing.JButton; 006import javax.swing.JPanel; 007 008import jmri.jmrit.catalog.NamedIcon; 009import jmri.util.swing.JmriJOptionPane; 010 011/** 012 * Icons may be added or deleted from a family. 013 * 014 * @author Pete Cressman Copyright (c) 2010 015 */ 016public class MultiSensorIconDialog extends IconDialog { 017 018 /* 019 * Constructor for existing family to change icons, add/delete icons, or to 020 * delete the family. 021 */ 022 public MultiSensorIconDialog(String type, String family, FamilyItemPanel parent) { 023 super(type, family, parent); 024 } 025 026 protected String getIconName() { 027 return MultiSensorItemPanel.POSITION[_iconMap.size() - 3]; 028 } 029 030 /* 031 * Add/delete icon. For Multisensor, it adds another sensor position. 032 */ 033 @Override 034 protected void makeDoneButtonPanel(JPanel buttonPanel, String text) { 035 super.makeDoneButtonPanel(buttonPanel, text); 036 JPanel panel2 = new JPanel(); 037 panel2.setLayout(new FlowLayout()); 038 JButton addSensor = new JButton(Bundle.getMessage("addIcon")); 039 addSensor.addActionListener(a -> { 040 if (addNewIcon(getIconName())) { 041 finishAddDelete(); 042 } 043 }); 044 addSensor.setToolTipText(Bundle.getMessage("ToolTipAddPosition")); 045 panel2.add(addSensor); 046 047 JButton deleteSensor = new JButton(Bundle.getMessage("deleteIcon")); 048 deleteSensor.addActionListener(a -> { 049 if (deleteIcon()) { 050 finishAddDelete(); 051 } 052 }); 053 deleteSensor.setToolTipText(Bundle.getMessage("ToolTipDeletePosition")); 054 panel2.add(deleteSensor); 055 buttonPanel.add(panel2); 056 } 057 058 private void finishAddDelete() { 059 _iconEditPanel.removeAll(); 060 _parent.addIconsToPanel(_iconMap, _iconEditPanel, true); 061 pack(); 062 } 063 064 @Override 065 protected boolean doDoneAction() { 066 MultiSensorItemPanel parent = (MultiSensorItemPanel) _parent; 067 068 if (_iconMap.size() != parent.getIconMap().size()) { 069 parent.setSelections(); 070 } 071 return super.doDoneAction(); 072 } 073 074 /** 075 * Action item for makeAddIconButtonPanel. 076 * @param name icon name 077 * @return true if name is OK 078 */ 079 private boolean addNewIcon(String name) { 080 if (log.isDebugEnabled()) { 081 log.debug("addNewIcon Action: iconMap.size()= {}", _iconMap.size()); 082 } 083 if (name == null || name.length() == 0) { 084 JmriJOptionPane.showMessageDialog(_parent._frame, Bundle.getMessage("NoIconName"), 085 Bundle.getMessage("WarningTitle"), JmriJOptionPane.WARNING_MESSAGE); 086 return false; 087 } else if (_iconMap.get(name) != null) { 088 JmriJOptionPane.showMessageDialog(_parent._frame, 089 Bundle.getMessage("DuplicateIconName", name), 090 Bundle.getMessage("WarningTitle"), JmriJOptionPane.WARNING_MESSAGE); 091 return false; 092 } 093 String fileName = "resources/icons/misc/X-red.gif"; 094 NamedIcon icon = new jmri.jmrit.catalog.NamedIcon(fileName, fileName); 095 _iconMap.put(name, icon); 096 return true; 097 } 098 099 /** 100 * Action item for makeAddIconButtonPanel. 101 */ 102 private boolean deleteIcon() { 103 if (log.isDebugEnabled()) { 104 log.debug("deleteSensor Action: iconMap.size()= {}", _iconMap.size()); 105 } 106 if (_iconMap.size() < 4) { 107 return false; 108 } 109 String name = MultiSensorItemPanel.POSITION[_iconMap.size() - 4]; 110 _iconMap.remove(name); 111 return true; 112 } 113 114 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MultiSensorIconDialog.class); 115 116}