summaryrefslogtreecommitdiff
path: root/smartdashboard/src/edu/wpi/first/smartdashboard/gui/elements/RobotPreferences.java
blob: 9cb35b2dc83e443ff01d84b30c21d560a6068b5d (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package edu.wpi.first.smartdashboard.gui.elements;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;

import edu.wpi.first.smartdashboard.gui.*;
import edu.wpi.first.smartdashboard.properties.*;
import edu.wpi.first.smartdashboard.robot.Robot;
import edu.wpi.first.wpilibj.tables.*;

/**
 *
 * @author Joe Grinstead
 */
public class RobotPreferences extends StaticWidget implements ITableListener {

    public static final String DELETED_VALUE = "\"";
    public static final String NAME = "Robot Preferences";
    private JTable table;
    private PreferenceTableModel model;
    private Map<String, String> values;
    private JButton save;
    private JButton add;
    private JButton remove;

    @Override
    public void init() {
        add = new JButton("Add");
        add.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                NewPreferenceEntryDialog dialog = new NewPreferenceEntryDialog();
                dialog.show(remove.getLocationOnScreen());
                if (!dialog.isCanceled()) {
                    model.put(dialog.getKey(), dialog.getValue());
                }
            }
        });

        remove = new JButton("Remove");
        remove.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (table.isEditing()) {
                    table.getCellEditor().cancelCellEditing();
                }
                Map.Entry<String, String> entry = model.getRow(table.getSelectedRow());
                if (entry != null) {
                    model.delete(entry.getKey());
                }
            }
        });

        save = new JButton("Save");
        save.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Robot.getPreferences().putBoolean(Robot.PREF_SAVE_FIELD, true);
            }
        });

        values = new LinkedHashMap<String, String>();

        Robot.getPreferences().addTableListener(this, true);

        model = new PreferenceTableModel();

        table = new JTable(model);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);


        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(0, 2));
        buttonPanel.add(add);
        buttonPanel.add(remove);

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new BorderLayout());
        controlPanel.add(buttonPanel, BorderLayout.NORTH);
        controlPanel.add(save, BorderLayout.SOUTH);

        setLayout(new BorderLayout());
        JScrollPane tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(tableScrollPane, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.SOUTH);
        
        setPreferredSize(new Dimension(300, 200));
    }

    @Override
    public void disconnect() {
        Robot.getPreferences().removeTableListener(this);
    }

    @Override
    public void propertyChanged(Property property) {
    }

	@Override
	public void valueChanged(ITable source, String key, Object value, boolean isNew) {
        if (key.equals(Robot.PREF_SAVE_FIELD)) {
            save.setEnabled(!(Boolean) value);
        } else {
            if (DELETED_VALUE.equals(value.toString())) {
                values.remove(key);
            } else {
                values.put(key, value.toString());
            }
        }

        if (model != null) {
            model.fireTableDataChanged();
        }
    }


    private class PreferenceTableModel extends AbstractTableModel {

        public int getRowCount() {
            return values.size();
        }

        public int getColumnCount() {
            return 2;
        }

        @Override
        public String getColumnName(int i) {
            if (i == 0) {
                return "Key";
            } else if (i == 1) {
                return "Value";
            } else {
                return "ERROR";
            }
        }

        public Map.Entry<String, String> getRow(int rowIndex) {
            int row = 0;
            for (Map.Entry<String, String> entry : values.entrySet()) {
                if (row++ == rowIndex) {
                    return entry;
                }
            }
            return null;
        }

        public boolean put(String key, String value) {
            if (validateKey(key) && validateValue(value)) {
                Robot.getPreferences().putString(key, value);
                return true;
            }
            return false;
        }

        public void delete(String key) {
            Robot.getPreferences().putString(key, DELETED_VALUE);
        }

        public boolean validateKey(String key) {
            if (key.isEmpty()) {
                JOptionPane.showMessageDialog(RobotPreferences.this, "The key cannot be empty", "Bad Key", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            if (key.contains(" ") || key.contains("=") || key.contains("\t") || key.contains("\r") || key.contains("\n")) {
                JOptionPane.showMessageDialog(RobotPreferences.this, "The key cannot containt ' ', '=', tabs or newlines", "Bad Key", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            return true;
        }

        public boolean validateValue(String value) {
            if (value.contains("\"")) {
                JOptionPane.showMessageDialog(RobotPreferences.this, "The value cannot contain '\"'", "Bad Value", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            return true;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (columnIndex == 0) {//Key
                Map.Entry<String, String> entry = getRow(rowIndex);
                if (entry != null) {
                    String oldName = entry.getKey();
                    String value = entry.getValue();
                    if (!oldName.equals(aValue.toString())) {
                        if (!values.containsKey(aValue.toString())) {
                            if(put(aValue.toString(), value))
                                delete(oldName);
                        } else {
                            JOptionPane.showMessageDialog(RobotPreferences.this, "An entry with the key " + aValue + " already exists", "Duplicate Key", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            } else {//Value
                Map.Entry<String, String> entry = getRow(rowIndex);
                if (entry != null) {
                    put(entry.getKey(), aValue.toString());
                }
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return rowIndex >= 0;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Map.Entry<String, String> entry = getRow(rowIndex);
            if (entry != null) {
                return columnIndex == 0 ? entry.getKey() : entry.getValue();
            }
            return "ERROR";
        }
    }

    private class NewPreferenceEntryDialog extends JDialog {

        private JTextField keyField;
        private JTextField valueField;
        private JButton addButton;
        private JButton cancelButton;
        boolean canceled = true;

        public NewPreferenceEntryDialog() {
            setTitle("New Preference Entry");
            setModal(true);
            setResizable(false);
            ((JComponent) getContentPane()).setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));

            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();

            add(new JLabel("Key: "), c);
            c.gridx = 1;
            add(keyField = new JTextField(10), c);

            c.gridx = 0;
            c.gridy = 1;
            add(new JLabel("Value: "), c);
            c.gridx = 1;
            add(valueField = new JTextField(10), c);

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(0, 2));
            buttonPanel.add(addButton = new JButton("Add"), c);
            addButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if (!values.containsKey(getKey())) {
                        if (model.validateKey(getKey()) && model.validateValue(getValue())) {
                            canceled = false;
                            dispose();
                        }
                    } else {
                        JOptionPane.showMessageDialog(RobotPreferences.this, "An entry with the key " + getKey() + " already exists", "Duplicate Key", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
            getRootPane().setDefaultButton(addButton);
            
            buttonPanel.add(cancelButton = new JButton("Cancel"), c);
            cancelButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });

            c.gridx = 0;
            c.gridy = 2;
            c.gridwidth = 2;
            add(buttonPanel, c);

            pack();
        }

        public void show(Point center) {
            setLocation((int) (center.getX() - getWidth() / 2), (int) (center.getY() - getHeight() / 2));
            setVisible(true);
        }

        public boolean isCanceled() {
            return canceled;
        }

        public String getKey() {
            return keyField.getText();
        }

        public String getValue() {
            return valueField.getText();
        }
    }

}