001package jmri.jmrit.display.palette;
002
003import java.awt.FlowLayout;
004import java.util.HashMap;
005import java.util.Map.Entry;
006import java.util.Set;
007
008import javax.swing.JButton;
009import javax.swing.JPanel;
010
011import jmri.jmrit.catalog.NamedIcon;
012import jmri.util.swing.JmriJOptionPane;
013
014/**
015 *
016 * @author Pete Cressman Copyright (c) 2010
017 */
018public class IndicatorTOIconDialog extends IconDialog {
019
020    private HashMap<String, HashMap<String, NamedIcon>> _iconGroupsMap;
021
022    public IndicatorTOIconDialog(String type, String family, IndicatorTOItemPanel parent) {
023        super(type, family, parent); // temporarily use key for family to set JL
024    }
025
026    protected void setMaps(HashMap<String, HashMap<String, NamedIcon>> iconMaps) {
027        IndicatorTOItemPanel p = (IndicatorTOItemPanel)_parent;
028        if (iconMaps != null) {
029            _iconGroupsMap = new HashMap<>();
030            for (Entry<String, HashMap<String, NamedIcon>> entry : iconMaps.entrySet()) {
031                _iconGroupsMap.put(entry.getKey(), IconDialog.clone(entry.getValue()));
032            }
033        } else {
034            _iconGroupsMap = p.makeNewIconMap();
035        }
036        p.addIcons2Panel(_iconGroupsMap, _iconEditPanel, true);
037        setLocationRelativeTo(p);
038        setVisible(true);
039        pack();
040        log.debug("setMaps: initialization done.");
041    }
042
043    /**
044     * Add/Delete icon family for types that may have more than 1 family.
045     */
046    @Override
047    protected void makeDoneButtonPanel(JPanel buttonPanel, String text) {
048        super.makeDoneButtonPanel(buttonPanel, text);
049        IndicatorTOItemPanel p = (IndicatorTOItemPanel)_parent;
050        if (!p._cntlDown) {
051            return;
052        }
053        JPanel panel1 = new JPanel();
054        panel1.setLayout(new FlowLayout());
055        JButton button = new JButton(Bundle.getMessage("addMissingStatus"));
056        button.addActionListener(a -> {
057            addMissingStatus();
058        });
059        button.setToolTipText(Bundle.getMessage("ToolTipMissingStatus"));
060        panel1.add(button);
061
062        button = new JButton(Bundle.getMessage("addStatus"));
063        button.addActionListener(a -> {
064            addStatusSet();
065        });
066        button.setToolTipText(Bundle.getMessage("addStatus"));
067        panel1.add(button);
068
069        button = new JButton(Bundle.getMessage("deleteStatus"));
070        button.addActionListener(a -> {
071            deleteStatusSet();
072        });
073        button.setToolTipText(Bundle.getMessage("ToolTipDeleteStatus"));
074        panel1.add(button);
075        
076        buttonPanel.add(panel1);
077    }
078
079    /**
080     * Action item to rename an icon family.
081     */@Override
082    protected void renameFamily() {
083         IndicatorTOItemPanel p = (IndicatorTOItemPanel)_parent;
084        String family = p.getValidFamily(null, _iconGroupsMap);
085        if (family != null) {
086            _family = family;
087            _nameLabel.setText(Bundle.getMessage("FamilyName", _family));
088            invalidate();
089        }
090    }
091    
092     /**
093      * Action item for makeDoneButtonPanel.
094      */
095     @Override
096     protected boolean doDoneAction() {
097         if (log.isDebugEnabled()) {
098             log.debug("doDoneAction: {} for {} family= {}", (_parent._update?"Update":""), _type, _family);
099         }
100         if (_family == null || _family.isEmpty()) {
101             JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("NoFamilyName"),
102                     Bundle.getMessage("MessageTitle"), JmriJOptionPane.INFORMATION_MESSAGE);
103             return false;
104         }
105         IndicatorTOItemPanel p = (IndicatorTOItemPanel)_parent;
106         HashMap<String, HashMap<String, HashMap<String, NamedIcon>>> families =
107                                         ItemPalette.getLevel4FamilyMaps(_type);
108         String family;
109         HashMap<String, HashMap<String, NamedIcon>> catalogSet = families.get(_family);
110         boolean nameUsed;
111         if (catalogSet == null) {
112             family = p.findFamilyOfMaps(null, _iconGroupsMap, families);
113             nameUsed = false;
114         } else {
115             family = p.findFamilyOfMaps(_family, _iconGroupsMap, families);
116             nameUsed = true;        // the map is found under another name than _family
117         }
118         if (family != null ) {  // "new" map is stored
119             boolean sameMap = p.familiesAreEqual(_iconGroupsMap, families.get(family));
120             if (!mapInCatalogOK(sameMap, nameUsed, _family, family)) {
121                 return false;
122             }
123         } else {
124             boolean sameMap;
125             if (catalogSet == null) {
126                 sameMap = false;
127             } else {
128                 sameMap = p.familiesAreEqual(catalogSet, _iconGroupsMap);
129             }
130             if (!mapNotInCatalogOK(sameMap, nameUsed, _family)) {
131                 return false;
132             }
133         }
134         p.dialogDone(_family, _iconGroupsMap);
135         return true;
136     }
137
138     /// CNTL A operations - So far no need to use these. - artifact of previous expansion plans
139     /// for 4.21 CNTL A added to simplify conventional GUI 
140    /**
141     * Action item for restoring status set in makeAddIconButtonPanel.
142     */
143    private void addMissingStatus() {
144        Set<String> set = _iconGroupsMap.keySet();
145        HashMap<String, String> options = new HashMap<>();
146        for (String status : ItemPanel.INDICATOR_TRACK) {
147            if (!set.contains(status)) {
148                options.put(ItemPalette.convertText(status), status);
149            }
150        }
151        if (!options.isEmpty()) {
152            Object[] selections = options.keySet().toArray();
153            String key = (String) JmriJOptionPane.showInputDialog(this,
154                    Bundle.getMessage("PickStatus"), Bundle.getMessage("QuestionTitle"), JmriJOptionPane.QUESTION_MESSAGE, null,
155                    selections, selections[0]);
156            if (key != null) {
157                addStatus(options.get(key));
158            }
159        } else {
160            JmriJOptionPane.showMessageDialog(this, Bundle.getMessage("AllStatus"),
161                    Bundle.getMessage("MessageTitle"), JmriJOptionPane.INFORMATION_MESSAGE);
162        }
163    }
164
165    /**
166     * NOT add a new family. Create a status family
167     */
168    private void addStatusSet() {
169        String status = JmriJOptionPane.showInputDialog(this,
170            Bundle.getMessage("StatusName"), Bundle.getMessage("createNewFamily"),
171            JmriJOptionPane.QUESTION_MESSAGE );
172        if (status != null) {
173            addStatus(status);
174        }
175    }
176
177    /**
178     * Action item for delete status set in makeAddIconButtonPanel.
179     */
180    private void deleteStatusSet() {
181        Set<String> set = _iconGroupsMap.keySet();
182        HashMap<String, String> options = new HashMap<>();
183        for (String status : set) {
184            options.put(ItemPalette.convertText(status), status);
185        }
186        Object[] selections = options.keySet().toArray();
187        String key = (String) JmriJOptionPane.showInputDialog(this,
188                Bundle.getMessage("PickDelete"), Bundle.getMessage("QuestionTitle"), JmriJOptionPane.QUESTION_MESSAGE, null,
189                selections, selections[0]);
190        if (key != null) {
191            _iconGroupsMap.remove(options.get(key));
192            IndicatorTOItemPanel p = (IndicatorTOItemPanel)_parent;
193            if (_iconGroupsMap.isEmpty()) {
194                p.deleteFamilySet();
195                dispose();
196            }
197            p.addIcons2Panel(_iconGroupsMap, _iconEditPanel, true);
198            _iconEditPanel.invalidate();
199            pack();
200        }
201    }
202
203    private void addStatus(String status) {
204        HashMap<String, NamedIcon> iconMap = _parent.makeNewIconMap("Turnout");
205        _iconGroupsMap.put(status, iconMap);
206        IndicatorTOItemPanel p = (IndicatorTOItemPanel)_parent;
207        p.addIcons2Panel(_iconGroupsMap, _iconEditPanel, true);
208        _iconEditPanel.invalidate();
209        pack();
210    }
211
212    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(IndicatorTOIconDialog.class);
213
214}