~meier-philipp/humigraf/development

« back to all changes in this revision

Viewing changes to P-Raeber Humigraf/src/ch/praeber/humigraf/DeviceSettingsDialog.java

  • Committer: Philipp Meier
  • Date: 2012-01-27 19:31:13 UTC
  • Revision ID: meier.phi@gmail.com-20120127193113-qo95p0n6z9g6p131
renamed project

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) Philipp Meier 2011
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License version 3 as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 */
16
 
package ch.praeber.humigraf;
17
 
 
18
 
import ch.praeber.humigraf.system.Controller;
19
 
import java.awt.BorderLayout;
20
 
import java.awt.FlowLayout;
21
 
import java.awt.GridLayout;
22
 
import java.awt.event.ActionEvent;
23
 
import java.awt.event.ActionListener;
24
 
import java.util.ResourceBundle;
25
 
import javax.swing.BorderFactory;
26
 
import javax.swing.JButton;
27
 
import javax.swing.JComboBox;
28
 
import javax.swing.JDialog;
29
 
import javax.swing.JLabel;
30
 
import javax.swing.JOptionPane;
31
 
import javax.swing.JPanel;
32
 
import javax.swing.JSpinner;
33
 
import javax.swing.SpinnerListModel;
34
 
import javax.swing.SpinnerNumberModel;
35
 
import javax.swing.border.Border;
36
 
import javax.swing.event.ChangeEvent;
37
 
import javax.swing.event.ChangeListener;
38
 
 
39
 
/**
40
 
 * DeviceSettingsDialog
41
 
 * Configuration Dialog to graphicaly adjust settings of Humigraf
42
 
 * @author Meier Philipp
43
 
 */
44
 
public class DeviceSettingsDialog extends JDialog {
45
 
 
46
 
    private static final ResourceBundle languages =
47
 
            ResourceBundle.getBundle("ch/praeber/humigraf/resources/languages/Languages");
48
 
    //Recording Panel
49
 
    private JPanel recordingpanel;
50
 
    private Border recordingborder;
51
 
    private JLabel IntervallLabel;
52
 
    private JComboBox IntervallCombo;
53
 
    private JLabel lblpaperfeed;
54
 
    private JComboBox cbpaperfeed;
55
 
    private JLabel lblsiunit;
56
 
    private JComboBox cbsiunit;
57
 
    //Alarm Panel
58
 
    private JPanel alarmpanel;
59
 
    private Border alarmborder;
60
 
    private JLabel lblalarm;
61
 
    private JSpinner spnalarm;
62
 
    private SpinnerListModel modelalarm;
63
 
    private JLabel lblminhum;
64
 
    private JSpinner spminhum;
65
 
    private SpinnerNumberModel modelminhum;
66
 
    private JLabel lblmaxhum;
67
 
    private SpinnerNumberModel modelmaxhum;
68
 
    private JSpinner spmaxhum;
69
 
    private JLabel lblmintemp;
70
 
    private SpinnerNumberModel modelmintemp;
71
 
    private JSpinner spmintemp;
72
 
    private JLabel lblmaxtemp;
73
 
    private SpinnerNumberModel modelmaxtemp;
74
 
    private JSpinner spmaxtemp;
75
 
    //Buttons
76
 
    private JButton CancelButton;
77
 
    private JButton SaveButton;
78
 
    private JPanel ButtonPanel;
79
 
    //Controller
80
 
    private Controller control;
81
 
    private final String[] alarmvalues = {languages.getString("OFF"), "1", "2", "3", "4", "5",
82
 
        "6", "7", "8", "9", languages.getString("ENDLESS (MANUAL STOP)")};
83
 
    //settings
84
 
    private long activeintervall;
85
 
    private int paperfeed;
86
 
    private int siunit;
87
 
    private int alarm;
88
 
    private int minhum;
89
 
    private int maxhum;
90
 
    private int mintemp;
91
 
    private int maxtemp;
92
 
    //changed settings
93
 
    private long comboint;
94
 
    private int newpaperfeed;
95
 
    private int newsiunit;
96
 
    private int newalarm;
97
 
    private int newminhum;
98
 
    private int newmaxhum;
99
 
    private int newmintemp;
100
 
    private int newmaxtemp;
101
 
    private boolean intervalchange = false;
102
 
    //tooltips
103
 
    private String tipinterval;
104
 
    private String tippaperfeed;
105
 
    private String tipunit;
106
 
    private String tipalarm;
107
 
    private String tiplowerhum;
108
 
    private String tipupperhum;
109
 
    private String tiplowertemp;
110
 
    private String tipuppertemp;
111
 
 
112
 
    /**
113
 
     * Start the Settings Dialog. Allows to configure Humigraf
114
 
     * @param mainFrame Frame which opens a Configure Dialog
115
 
     * @param control Controller used to configure Humigraf
116
 
     */
117
 
    public DeviceSettingsDialog(AnalyticsGUI parent, Controller control) {
118
 
        super(parent);
119
 
        this.control = control;
120
 
        setTitle(languages.getString("HUMIGRAF SETTINGS"));
121
 
        setModal(true);
122
 
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
123
 
        //setSize(420, 400);
124
 
        initToolTipStrings();
125
 
 
126
 
        setLayout(new BorderLayout());
127
 
        loadConfigFromHumigraf();
128
 
        initialise();
129
 
        setupListeners();
130
 
        pack();
131
 
        setLocationRelativeTo(parent);
132
 
        setMinimumSize(getSize());
133
 
 
134
 
        //  setResizable(false);
135
 
 
136
 
        setVisible(true);
137
 
    }
138
 
 
139
 
    /**
140
 
     * initialies ToolTipStrings
141
 
     */
142
 
    private void initToolTipStrings() {
143
 
        tipinterval = languages.getString("INTERVAL BETWEEN TWO RECORDS. CHANGING THIS VALUE WILL ERASE ALL RECORDS FROM HUMIGRAF.");
144
 
        tippaperfeed = languages.getString("CONFIGURE THE PAPER FEED SPEED.");
145
 
        tipunit = languages.getString("ADJUST THE PHYSICAL UNITS WHICH ARE USED ON PAPER AND DISPLAY.");
146
 
        tipalarm = languages.getString("ADJUST THE NUMBER OF SOUNDS PLAYED WHEN ONE OF THE ALARM LEVELS HAS BEEN CROSSED.");
147
 
        tiplowerhum = languages.getString("CONFIGURE THE LOWER ALARM LEVEL FOR HUMIDITY.");
148
 
        tipupperhum = languages.getString("CONFIGURE THE UPPER ALARM LEVEL FOR HUMIDITY.");
149
 
        tiplowertemp = languages.getString("CONFIGURE THE LOWER ALARM LEVEL FOR TEMPERATURE.");
150
 
        tipuppertemp = languages.getString("CONFIGURE THE UPPER ALARM LEVEL FOR TEMPERATURE.");
151
 
    }
152
 
 
153
 
    /**
154
 
     * Initialises Buttons, Panels and constructs the Window
155
 
     */
156
 
    private void initialise() {
157
 
        //Greate Button Space
158
 
        CancelButton = new JButton(languages.getString("CANCEL"));
159
 
        SaveButton = new JButton(languages.getString("SAVE"));
160
 
        ButtonPanel = new JPanel();
161
 
        ButtonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
162
 
        ButtonPanel.add(SaveButton);
163
 
        ButtonPanel.add(CancelButton);
164
 
 
165
 
 
166
 
        //Recording Panel
167
 
        recordingpanel = new JPanel();
168
 
        recordingborder = BorderFactory.createTitledBorder(languages.getString("RECORDING"));
169
 
        recordingpanel.setBorder(recordingborder);
170
 
        recordingpanel.setLayout(new GridLayout(3, 2));
171
 
        lblpaperfeed = new JLabel(languages.getString("<HTML>PAPER FEED</HTML>"));
172
 
        cbpaperfeed = new JComboBox();
173
 
        cbpaperfeed.setToolTipText(tippaperfeed);
174
 
        cbpaperfeed.addItem("0.5 mm / Min");
175
 
        cbpaperfeed.addItem("1.0 mm / Min");
176
 
        cbpaperfeed.addItem("2.0 mm / Min");
177
 
        cbpaperfeed.addItem("4.0 mm / Min");
178
 
        cbpaperfeed.setSelectedIndex(paperfeed);
179
 
 
180
 
        lblsiunit = new JLabel(languages.getString("<HTML>SI-UNIT ON HUMIGRAF</HTML>"));
181
 
        cbsiunit = new JComboBox();
182
 
        cbsiunit.setToolTipText(tipunit);
183
 
        cbsiunit.addItem(languages.getString("FAHRENHEIT"));
184
 
        cbsiunit.addItem(languages.getString("CELSIUS"));
185
 
        cbsiunit.setSelectedIndex(siunit);
186
 
 
187
 
        IntervallLabel = new JLabel(languages.getString("<HTML>RECORDING TIME INTERVALL</HTML>"));
188
 
        IntervallCombo = new JComboBox();
189
 
        IntervallCombo.setToolTipText(tipinterval);
190
 
        IntervallCombo.setModel(new javax.swing.DefaultComboBoxModel());
191
 
        IntervallCombo.addItem(languages.getString("OFF"));
192
 
        IntervallCombo.addItem("1 Min (45 " + languages.getString("DAYS") + ")");
193
 
        IntervallCombo.addItem("5 Min (227 " + languages.getString("DAYS") + ")");
194
 
        IntervallCombo.addItem("10 Min (454 " + languages.getString("DAYS") + ")");
195
 
        IntervallCombo.addItem("15 Min (682 " + languages.getString("DAYS") + ")");
196
 
        IntervallCombo.addItem("30 Min (1364 " + languages.getString("DAYS") + ")");
197
 
        IntervallCombo.addItem("60 Min (2738 " + languages.getString("DAYS") + ")");
198
 
        IntervallCombo.setEditable(false);
199
 
        IntervallCombo.setSelectedIndex((int) activeintervall);
200
 
 
201
 
        recordingpanel.add(IntervallLabel);
202
 
        recordingpanel.add(IntervallCombo);
203
 
        //recordingpanel.add(lblpaperfeed);
204
 
        //recordingpanel.add(cbpaperfeed);
205
 
        recordingpanel.add(lblsiunit);
206
 
        recordingpanel.add(cbsiunit);
207
 
 
208
 
        //Alarm panel
209
 
        alarmpanel = new JPanel();
210
 
        alarmborder = BorderFactory.createTitledBorder(languages.getString("ALARM"));
211
 
        alarmpanel.setBorder(alarmborder);
212
 
        alarmpanel.setLayout(new GridLayout(6, 2));
213
 
 
214
 
        lblalarm = new JLabel(languages.getString("ALARM (NUMBER OF BEEPS)"));
215
 
        modelalarm = new SpinnerListModel(alarmvalues);
216
 
        modelalarm.setValue(alarmvalues[alarm]);
217
 
        spnalarm = new JSpinner(modelalarm);
218
 
        spnalarm.setToolTipText(tipalarm);
219
 
        alarmpanel.add(lblalarm);
220
 
        alarmpanel.add(spnalarm);
221
 
 
222
 
        modelminhum = new SpinnerNumberModel(minhum, 0, 100, 1);
223
 
        lblminhum = new JLabel(languages.getString("LOWER HUMIDITY LIMIT"));
224
 
        spminhum = new JSpinner(modelminhum);
225
 
        spminhum.setToolTipText(tiplowerhum);
226
 
        alarmpanel.add(lblminhum);
227
 
        alarmpanel.add(spminhum);
228
 
 
229
 
        lblmaxhum = new JLabel(languages.getString("UPPER HUMIDITY LIMIT"));
230
 
        modelmaxhum = new SpinnerNumberModel(maxhum, 0, 100, 1);
231
 
        spmaxhum = new JSpinner(modelmaxhum);
232
 
        spmaxhum.setToolTipText(tipupperhum);
233
 
        alarmpanel.add(lblmaxhum);
234
 
        alarmpanel.add(spmaxhum);
235
 
 
236
 
 
237
 
        modelmintemp = new SpinnerNumberModel(mintemp, -5, 45, 1);
238
 
        lblmintemp = new JLabel(languages.getString("LOWER TEMPERATURE LIMIT"));
239
 
        spmintemp = new JSpinner(modelmintemp);
240
 
        spmintemp.setToolTipText(tiplowertemp);
241
 
        alarmpanel.add(lblmintemp);
242
 
        alarmpanel.add(spmintemp);
243
 
 
244
 
        lblmaxtemp = new JLabel(languages.getString("UPPER TEMPERATURE LIMIT"));
245
 
        modelmaxtemp = new SpinnerNumberModel(maxtemp, -5, 45, 1);
246
 
        spmaxtemp = new JSpinner(modelmaxtemp);
247
 
        spmaxtemp.setToolTipText(tipuppertemp);
248
 
        alarmpanel.add(lblmaxtemp);
249
 
        alarmpanel.add(spmaxtemp);
250
 
 
251
 
        //Add the Components to the Window
252
 
        add(recordingpanel, BorderLayout.NORTH);
253
 
        add(alarmpanel, BorderLayout.CENTER);
254
 
        add(ButtonPanel, BorderLayout.SOUTH);
255
 
    }
256
 
 
257
 
    /**
258
 
     * Setup all listener
259
 
     */
260
 
    private void setupListeners() {
261
 
        //Cancel_Button Listener
262
 
        CancelButton.addActionListener(new ActionListener() {
263
 
 
264
 
            @Override
265
 
            public void actionPerformed(ActionEvent e) {
266
 
                cancelAction();
267
 
            }
268
 
        });
269
 
 
270
 
        SaveButton.addActionListener(new ActionListener() {
271
 
 
272
 
            @Override
273
 
            public void actionPerformed(ActionEvent e) {
274
 
                saveAction();
275
 
            }
276
 
        });
277
 
        //Comboboxes
278
 
        IntervallCombo.addActionListener(new ActionListener() {
279
 
 
280
 
            @Override
281
 
            public void actionPerformed(ActionEvent e) {
282
 
                comboint = IntervallCombo.getSelectedIndex();
283
 
                intervalchange = true;
284
 
            }
285
 
        });
286
 
 
287
 
        cbsiunit.addActionListener(new ActionListener() {
288
 
 
289
 
            @Override
290
 
            public void actionPerformed(ActionEvent e) {
291
 
                newsiunit = cbsiunit.getSelectedIndex();
292
 
            }
293
 
        });
294
 
 
295
 
        cbpaperfeed.addActionListener(new ActionListener() {
296
 
 
297
 
            @Override
298
 
            public void actionPerformed(ActionEvent e) {
299
 
                newpaperfeed = cbpaperfeed.getSelectedIndex();
300
 
            }
301
 
        });
302
 
 
303
 
 
304
 
        //alarm event
305
 
        spnalarm.addChangeListener(new ChangeListener() {
306
 
 
307
 
            @Override
308
 
            public void stateChanged(ChangeEvent e) {
309
 
                String a = (String) modelalarm.getValue();
310
 
                if (a.equals(alarmvalues[0])) {
311
 
                    newalarm = 0;
312
 
                } else if (a.equals(alarmvalues[10])) {
313
 
                    newalarm = 10;
314
 
                } else {
315
 
                    newalarm = Integer.parseInt(a);
316
 
                }
317
 
            }
318
 
        });
319
 
 
320
 
        //Spinner Events
321
 
        spminhum.addChangeListener(new ChangeListener() {
322
 
 
323
 
            @Override
324
 
            public void stateChanged(ChangeEvent e) {
325
 
                newminhum = modelminhum.getNumber().intValue();
326
 
            }
327
 
        });
328
 
 
329
 
 
330
 
        spmaxhum.addChangeListener(new ChangeListener() {
331
 
 
332
 
            @Override
333
 
            public void stateChanged(ChangeEvent e) {
334
 
                newmaxhum = modelmaxhum.getNumber().intValue();
335
 
            }
336
 
        });
337
 
 
338
 
        spmintemp.addChangeListener(new ChangeListener() {
339
 
 
340
 
            @Override
341
 
            public void stateChanged(ChangeEvent e) {
342
 
                newmintemp = modelmintemp.getNumber().intValue();
343
 
            }
344
 
        });
345
 
 
346
 
 
347
 
        spmaxtemp.addChangeListener(new ChangeListener() {
348
 
 
349
 
            @Override
350
 
            public void stateChanged(ChangeEvent e) {
351
 
                newmaxtemp = modelmaxtemp.getNumber().intValue();
352
 
            }
353
 
        });
354
 
 
355
 
 
356
 
    }
357
 
 
358
 
    /**
359
 
     * Loads the configuration from Humigraf.
360
 
     */
361
 
    private void loadConfigFromHumigraf() {
362
 
        //Load Interval
363
 
        activeintervall = control.getIntervall() / 60000;
364
 
        switch ((int) activeintervall) {
365
 
            case 0:
366
 
                activeintervall = (long) 0;
367
 
                break;
368
 
            case 1:
369
 
                activeintervall = 1;
370
 
                break;
371
 
            case 5:
372
 
                activeintervall = (long) 2;
373
 
                break;
374
 
            case 10:
375
 
                activeintervall = (long) 3;
376
 
                break;
377
 
            case 15:
378
 
                activeintervall = (long) 4;
379
 
                break;
380
 
            case 30:
381
 
                activeintervall = (long) 5;
382
 
                break;
383
 
            case 60:
384
 
                activeintervall = (long) 6;
385
 
                break;
386
 
            default:
387
 
                activeintervall = (long) -1;
388
 
        }
389
 
 
390
 
//        //load paperfeed
391
 
//        paperfeed = control.getPaperFeed();
392
 
//        if (paperfeed < 0) {
393
 
//            paperfeed = 0;
394
 
//        }
395
 
        newpaperfeed = paperfeed;
396
 
        siunit = control.getTemperatureUnit();
397
 
        if (siunit < 0) {
398
 
            siunit = 0;
399
 
        }
400
 
        newsiunit = siunit;
401
 
 
402
 
        alarm = control.getAlarm();
403
 
        if (alarm < 0 || alarm > 10) {
404
 
            alarm = 0;
405
 
        }
406
 
        newalarm = alarm;
407
 
 
408
 
        minhum = control.getMinHum();
409
 
        if (minhum < 0) {
410
 
            minhum = 0;
411
 
        }
412
 
        newminhum = minhum;
413
 
 
414
 
        maxhum = control.getMaxHum();
415
 
        if (maxhum < 0) {
416
 
            maxhum = 0;
417
 
        }
418
 
        newmaxhum = maxhum;
419
 
 
420
 
        mintemp = control.getMinTemp();
421
 
        if (mintemp < -5) {
422
 
            mintemp = 0;
423
 
        }
424
 
        newmintemp = mintemp;
425
 
 
426
 
        maxtemp = control.getMaxTemp();
427
 
        if (maxtemp < -5) {
428
 
            maxtemp = 0;
429
 
        }
430
 
        newmaxtemp = maxtemp;
431
 
    }
432
 
 
433
 
    /**
434
 
     * Clear all actions done.
435
 
     */
436
 
    private void cancelAction() {
437
 
        //Clear all necessery data from the devices etc
438
 
        setVisible(false);
439
 
        this.removeAll();
440
 
 
441
 
    }
442
 
 
443
 
    /**
444
 
     * Action if savebutton is pressed.
445
 
     */
446
 
    private void saveAction() {
447
 
        if (writeToHumigraf()) {
448
 
            setVisible(false);
449
 
            this.removeAll();
450
 
        }
451
 
    }
452
 
 
453
 
    /**
454
 
     * Writes the changed elements to the humigraf
455
 
     */
456
 
    private boolean writeToHumigraf() {
457
 
        boolean answ = true;
458
 
        if (comboint != activeintervall && intervalchange) {
459
 
            int response = 0;
460
 
            int value = (int)comboint;
461
 
            response = JOptionPane.showConfirmDialog(this,
462
 
                    languages.getString("SETTING THE RECORDING INTERVAL WILL REMOVE ALL RECORDS FROM")
463
 
                    + languages.getString("HUMIGRAF. WOULD YOU LIKE TO CONTINUE AND REMOVE ALL RECORDS")
464
 
                    + languages.getString("FROM HUMIGRAF BY SETTING THE INTERVAL?"),
465
 
                    languages.getString("CAUTION"), JOptionPane.YES_NO_OPTION,
466
 
                    JOptionPane.WARNING_MESSAGE);
467
 
            // Response value: Yes = 0, No = 1, Cancel = 2
468
 
            if (response == JOptionPane.YES_OPTION) {
469
 
                control.setInterval(value);
470
 
            } else {
471
 
                answ = false;
472
 
                //Maybe reset
473
 
            }
474
 
        }
475
 
 
476
 
//        if (newpaperfeed != paperfeed) {
477
 
//            control.setPaperFeed(newpaperfeed);
478
 
//        }
479
 
 
480
 
        if (newsiunit != siunit) {
481
 
            control.setTemperatureUnit(newsiunit);
482
 
        }
483
 
 
484
 
        if (newminhum != minhum) {
485
 
            control.setMinHum(newminhum);
486
 
        }
487
 
 
488
 
        if (newmaxhum != maxhum) {
489
 
            control.setMaxHum(newmaxhum);
490
 
        }
491
 
 
492
 
        if (newmintemp != mintemp) {
493
 
            control.setMinTemp(newmintemp);
494
 
        }
495
 
 
496
 
        if (newmaxtemp != maxtemp) {
497
 
            control.setMaxTemp(newmaxtemp);
498
 
        }
499
 
 
500
 
        if (newalarm != alarm) {
501
 
            control.setAlarm(newalarm);
502
 
        }
503
 
        return answ;
504
 
    }
505
 
}