summaryrefslogtreecommitdiff
path: root/sources/smstrade/SPrefs.java
blob: a317785147a8cfadc1e3d874b0574d116426abe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* JSMSTrade v0.3
 * Copyright (c) 2008, 2009 Dipl.-Inform. (FH) Paul C. Buetow
 * jsmstrade@dev.buetow.org - https://github.com/snonux/jsmstrade
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * All icons of the icons/ folder are 	under a Creative Commons
 * Attribution-Noncommercial-Share Alike License a CC-by-nc-sa.
 */

package smstrade;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * The class SPrefs.
 *
 * @author Paul C. Buetow
 */
public class SPrefs extends SFrame {
    /** The serial version uid */
    private static final long serialVersionUID = 1L;

    /** The options map */
    private HashMap<String,String> options = null;

    /** The text area */
    private JTextArea textArea = new JTextArea();

    /** The button panel */
    private JPanel buttonPanel = new JPanel();

    /** The ok button */
    private JButton okButton = new JButton("OK");

    /** The save button */
    private JButton saveButton = new JButton("Save");

    /**
     * Instantiates a new SMain object.
     */
    public SPrefs(Component parent, HashMap<String,String> options) {
        super("Preferences", parent);
        this.options = options;

        disposeWithParent();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(300, 150);
        setResizable(false);

        fillContentPane();
        setVisible(true);
    }

    /**
     * Fills the content pane
     */
    private void fillContentPane() {
        JPanel contentPanel = (JPanel) getContentPane();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));

        if (options.containsKey("URL"))
            textArea.setText(options.get("URL"));
        else
            textArea.setText(SMain.DEFAULT_URL);

        textArea.setLineWrap(true);

        contentPanel.add(textArea);
        contentPanel.add(createButtonPanel());
    }

    /**
     * Creates the button panel
     *
     * @return The button panel
     */
    private JPanel createButtonPanel() {
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JButton source = (JButton) ae.getSource();
                String text = source.getText();

                if (text.equals("OK")) {
                    save();
                    dispose();

                } else if (text.equals("Save")) {
                    save();
                }
            }

            private void save() {
                options.put("URL", textArea.getText());

                try {
                    FileOutputStream fos =
                        new FileOutputStream(SMain.SAVE_FILE);
                    ObjectOutputStream oos =
                        new ObjectOutputStream(fos);

                    oos.writeObject(options);

                } catch (Exception e) {
                    System.err.println(e);
                }
            }
        };

        buttonPanel.add(okButton);
        buttonPanel.add(saveButton);

        okButton.addActionListener(listener);
        saveButton.addActionListener(listener);

        return buttonPanel;
    }
}