001package jmri.jmrix.nce.boosterprog;
002
003import java.awt.*;
004
005import javax.swing.*;
006
007import jmri.*;
008import jmri.jmrix.nce.NceSystemConnectionMemo;
009
010/**
011 * Panel for configuring an NCE booster.
012 *
013 * @author ken cameron Copyright (C) 2010 Derived from BoosterProgFrame by
014 * @author Bob Jacobsen Copyright (C) 2004
015 */
016public class BoosterProgPanel extends jmri.jmrix.nce.swing.NcePanel {
017
018    JTextField start = new JTextField(6);
019    JTextField length = new JTextField(12);
020    JLabel status = new JLabel();
021
022    public BoosterProgPanel() {
023        super();
024    }
025
026    /**
027     * {@inheritDoc}
028     */
029    @Override
030    public void initContext(Object context) {
031        if (context instanceof NceSystemConnectionMemo) {
032            try {
033                initComponents((NceSystemConnectionMemo) context);
034            } catch (Exception e) {
035                log.warn("BoosterProg initContext", e);
036            }
037        }
038    }
039
040    /**
041     * {@inheritDoc}
042     */
043    @Override
044    public String getHelpTarget() {
045        return "package.jmri.jmrix.nce.boosterprog.BoosterProgPanel";
046    }
047
048    /**
049     * {@inheritDoc}
050     */
051    @Override
052    public String getTitle() {
053        StringBuilder x = new StringBuilder();
054        if (memo != null) {
055            x.append(memo.getUserName());
056        } else {
057            x.append("NCE_");
058        }
059        x.append(": ");
060        x.append(Bundle.getMessage("TitleBoosterProg"));
061        return x.toString();
062    }
063
064    /**
065     * The minimum frame size for font size 16
066     */
067    @Override
068    public Dimension getMinimumDimension() {
069        return new Dimension(400, 200);
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public void initComponents(NceSystemConnectionMemo m) {
077        this.memo = m;
078
079        // general GUI config
080        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
081
082        // install items in GUI, one line at a time
083        // box of entries
084        JPanel box = new JPanel();
085        box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
086
087        JPanel p = new JPanel();
088        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
089        JLabel warn = new JLabel(Bundle.getMessage("Warn1"));
090        warn.setAlignmentX(Component.LEFT_ALIGNMENT);
091        p.add(warn);
092        warn = new JLabel(Bundle.getMessage("Warn2"));
093        warn.setAlignmentX(Component.LEFT_ALIGNMENT);
094        p.add(warn);
095        box.add(p);
096
097        box.add(Box.createRigidArea(new Dimension(0,5)));
098
099        p = new JPanel();
100        p.setLayout(new FlowLayout());
101        JButton b = new JButton(Bundle.getMessage("ButtonSet"));
102        p.add(new JLabel(Bundle.getMessage("LabelStart")));
103        start.setText("30");
104        p.add(start);
105        p.add(Box.createHorizontalGlue());
106        p.add(b);
107        b.addActionListener(e -> setStartPushed());
108        box.add(p);
109
110        p = new JPanel();
111        p.setLayout(new FlowLayout());
112        b = new JButton(Bundle.getMessage("ButtonSet"));
113        p.add(new JLabel(Bundle.getMessage("LabelDuration")));
114        length.setText("420");
115        p.add(length);
116        p.add(Box.createHorizontalGlue());
117        p.add(b);
118        b.addActionListener(e -> setDurationPushed());
119        box.add(p);
120
121        add(box);
122        add(Box.createVerticalGlue());
123
124        p = new JPanel();
125        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
126        p.add(new JLabel(Bundle.getMessage("StatusLabel") + " "));
127        p.add(status);
128        status.setText(Bundle.getMessage("StatusOK"));
129        p.setAlignmentX(Component.LEFT_ALIGNMENT);
130        add(p);
131    }
132
133    private AddressedProgrammer p = null;
134
135    private void getProgrammer() {
136        p = memo.getProgrammerManager().getAddressedProgrammer(true, 0);
137    }
138
139    private void releaseProgrammer() {
140        if (p != null) {
141            memo.getProgrammerManager().releaseAddressedProgrammer(p);
142        }
143        p = null;
144    }
145
146    void setStartPushed() {
147        getProgrammer();
148        status.setText(Bundle.getMessage("StatusProgramming"));
149        int val = Integer.parseInt(start.getText());
150
151        try {
152            p.writeCV("255", val, new ProgListener() {
153                @Override
154                public void programmingOpReply(int value, int retval) {
155                    status.setText(Bundle.getMessage("StatusOK"));
156                }
157            });
158        } catch (ProgrammerException e) {
159            status.setText(Bundle.getMessage("StatusError") + e);
160        } finally {
161            releaseProgrammer();
162        }
163    }
164
165    void setStart(int val) {
166        getProgrammer();
167
168        try {
169            p.writeCV("255", val, new ProgListener() {
170                @Override
171                public void programmingOpReply(int value, int retval) {
172                }
173            });
174        } catch (ProgrammerException ignored) {
175        } finally {
176            releaseProgrammer();
177        }
178    }
179
180    void setDuration(final int val) {
181        getProgrammer();
182
183        try {
184            p.writeCV("253", val / 256, new ProgListener() {
185                @Override
186                public void programmingOpReply(int value, int retval) {
187                    synchronized (this) {
188                        try {
189                            wait(1500);  // needed for booster to reset
190                        } catch (InterruptedException i) {
191                            Thread.currentThread().interrupt(); // retain if needed later
192                        }
193                    }
194                    try {
195                        p.writeCV("254", val % 256, new ProgListener() {
196                            @Override
197                            public void programmingOpReply(int value, int retval) {
198                            }
199                        });
200                    } catch (ProgrammerException ignored) {
201                    } finally {
202                        releaseProgrammer();
203                    }
204                }
205            });
206        } catch (ProgrammerException e) {
207            releaseProgrammer();
208        }
209    }
210
211    void setDurationPushed() {
212        getProgrammer();
213        status.setText(Bundle.getMessage("StatusProgramming"));
214        int val = Integer.parseInt(length.getText()) / 256;
215
216        try {
217            p.writeCV("253", val, new ProgListener() {
218                @Override
219                public void programmingOpReply(int value, int retval) {
220                    synchronized (this) {
221                        try {
222                            wait(1500);  // needed for booster to reset
223                        } catch (InterruptedException ignored) {
224                        }
225                    }
226                    durationPart2();
227                }
228            });
229        } catch (ProgrammerException e) {
230            status.setText(Bundle.getMessage("StatusError") + e);
231            releaseProgrammer();
232        }
233    }
234
235    void durationPart2() {
236        status.setText(Bundle.getMessage("StatusProgramming"));
237        int val = Integer.parseInt(length.getText()) % 256;
238
239        try {
240            p.writeCV("254", val, new ProgListener() {
241                @Override
242                public void programmingOpReply(int value, int retval) {
243                    status.setText(Bundle.getMessage("StatusOK"));
244                }
245            });
246        } catch (ProgrammerException e) {
247            status.setText(Bundle.getMessage("StatusError") + e);
248        } finally {
249            releaseProgrammer();
250        }
251    }
252
253    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BoosterProgPanel.class);
254
255}