1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.DefaultCellEditor;
|
---|
11 | import javax.swing.JComponent;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 | import javax.swing.JScrollPane;
|
---|
15 | import javax.swing.JTable;
|
---|
16 | import javax.swing.table.AbstractTableModel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.spi.preferences.ListSetting;
|
---|
19 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
20 | import org.openstreetmap.josm.tools.GBC;
|
---|
21 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Editor for List preference entries.
|
---|
25 | * @since 4634
|
---|
26 | */
|
---|
27 | public class ListEditor extends AbstractListEditor<String> {
|
---|
28 |
|
---|
29 | private final ListSettingTableModel model;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructs a new {@code ListEditor}.
|
---|
33 | * @param gui The parent component
|
---|
34 | * @param entry preference entry
|
---|
35 | * @param setting list setting
|
---|
36 | */
|
---|
37 | public ListEditor(final JComponent gui, PrefEntry entry, ListSetting setting) {
|
---|
38 | super(gui, tr("Change list setting"), entry);
|
---|
39 | model = new ListSettingTableModel(setting.getValue());
|
---|
40 | setContent(build(), false);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public List<String> getData() {
|
---|
45 | return new ArrayList<>(SubclassFilteredCollection.filter(model.getData(), object -> object != null && !object.isEmpty()));
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | protected final JPanel build() {
|
---|
50 | JPanel p = new JPanel(new GridBagLayout());
|
---|
51 | p.add(new JLabel(tr("Key: {0}", entry.getKey())), GBC.eol().insets(0, 0, 5, 0));
|
---|
52 | JTable table = new JTable(model);
|
---|
53 | table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
|
---|
54 | table.setTableHeader(null);
|
---|
55 |
|
---|
56 | DefaultCellEditor editor = new DefaultCellEditor(new JosmTextField());
|
---|
57 | editor.setClickCountToStart(1);
|
---|
58 | table.setDefaultEditor(table.getColumnClass(0), editor);
|
---|
59 |
|
---|
60 | JScrollPane pane = new JScrollPane(table);
|
---|
61 | p.add(pane, GBC.eol().insets(5, 10, 0, 0).fill());
|
---|
62 | return p;
|
---|
63 | }
|
---|
64 |
|
---|
65 | static class ListSettingTableModel extends AbstractTableModel {
|
---|
66 |
|
---|
67 | private final List<String> data;
|
---|
68 |
|
---|
69 | ListSettingTableModel(List<String> orig) {
|
---|
70 | if (orig != null) {
|
---|
71 | data = new ArrayList<>(orig);
|
---|
72 | } else {
|
---|
73 | data = new ArrayList<>();
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public List<String> getData() {
|
---|
78 | return data;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public int getRowCount() {
|
---|
83 | return data.size() + 1;
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public int getColumnCount() {
|
---|
88 | return 1;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public Object getValueAt(int row, int column) {
|
---|
93 | return data.size() == row ? "" : data.get(row);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void setValueAt(Object o, int row, int column) {
|
---|
98 | String s = (String) o;
|
---|
99 | if (row == data.size()) {
|
---|
100 | data.add(s);
|
---|
101 | fireTableRowsInserted(row+1, row+1);
|
---|
102 | } else {
|
---|
103 | data.set(row, s);
|
---|
104 | }
|
---|
105 | fireTableCellUpdated(row, column);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public boolean isCellEditable(int row, int column) {
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|