1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.AbstractListModel;
|
---|
15 | import javax.swing.DefaultCellEditor;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JList;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JScrollPane;
|
---|
20 | import javax.swing.JTable;
|
---|
21 | import javax.swing.JToolBar;
|
---|
22 | import javax.swing.event.ListSelectionEvent;
|
---|
23 | import javax.swing.event.ListSelectionListener;
|
---|
24 | import javax.swing.table.AbstractTableModel;
|
---|
25 | import javax.swing.table.TableCellEditor;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.data.Preferences.ListListSetting;
|
---|
28 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
30 | import org.openstreetmap.josm.gui.preferences.advanced.AdvancedPreference.PrefEntry;
|
---|
31 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
32 | import org.openstreetmap.josm.tools.GBC;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
35 |
|
---|
36 | public class ListListEditor extends ExtendedDialog {
|
---|
37 |
|
---|
38 | EntryListModel entryModel;
|
---|
39 | List<List<String>> data;
|
---|
40 | PrefEntry entry;
|
---|
41 |
|
---|
42 | JList entryList;
|
---|
43 | Integer entryIdx;
|
---|
44 | JTable table;
|
---|
45 |
|
---|
46 | ListTableModel tableModel;
|
---|
47 |
|
---|
48 | public ListListEditor(final PreferenceTabbedPane gui, PrefEntry entry, ListListSetting setting) {
|
---|
49 | super(gui, tr("Change list of lists setting"), new String[] {tr("OK"), tr("Cancel")});
|
---|
50 | this.entry = entry;
|
---|
51 | List<List<String>> orig = setting.getValue();
|
---|
52 | data = new ArrayList<List<String>>();
|
---|
53 | if (orig != null) {
|
---|
54 | for (List<String> l : orig) {
|
---|
55 | data.add(new ArrayList<String>(l));
|
---|
56 | }
|
---|
57 | }
|
---|
58 | setButtonIcons(new String[] {"ok.png", "cancel.png"});
|
---|
59 | setRememberWindowGeometry(getClass().getName() + ".geometry", WindowGeometry.centerInWindow(gui, new Dimension(500, 350)));
|
---|
60 | setContent(build(), false);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public List<List<String>> getData() {
|
---|
64 | return data;
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected JPanel build() {
|
---|
68 | JPanel p = new JPanel(new GridBagLayout());
|
---|
69 | p.add(new JLabel(tr("Key: {0}", entry.getKey())), GBC.std(0,0).span(2).weight(1, 0).insets(0,0,5,10));
|
---|
70 |
|
---|
71 | JPanel left = new JPanel(new GridBagLayout());
|
---|
72 |
|
---|
73 | entryModel = new EntryListModel();
|
---|
74 | entryList = new JList(entryModel);
|
---|
75 | entryList.getSelectionModel().addListSelectionListener(new EntryListener());
|
---|
76 | JScrollPane scroll = new JScrollPane(entryList);
|
---|
77 | left.add(scroll, GBC.eol().fill());
|
---|
78 |
|
---|
79 | JToolBar sideButtonTB = new JToolBar(JToolBar.HORIZONTAL);
|
---|
80 | sideButtonTB.setBorderPainted(false);
|
---|
81 | sideButtonTB.setOpaque(false);
|
---|
82 | sideButtonTB.add(new NewEntryAction());
|
---|
83 | RemoveEntryAction removeEntryAction = new RemoveEntryAction();
|
---|
84 | entryList.getSelectionModel().addListSelectionListener(removeEntryAction);
|
---|
85 | sideButtonTB.add(removeEntryAction);
|
---|
86 | left.add(sideButtonTB, GBC.eol());
|
---|
87 |
|
---|
88 | left.setPreferredSize(new Dimension(80, 0));
|
---|
89 |
|
---|
90 | p.add(left, GBC.std(0,1).fill().weight(0.3, 1.0));
|
---|
91 |
|
---|
92 | tableModel = new ListTableModel();
|
---|
93 | table = new JTable(tableModel);
|
---|
94 | table.putClientProperty("terminateEditOnFocusLost", true);
|
---|
95 | table.setTableHeader(null);
|
---|
96 |
|
---|
97 | DefaultCellEditor editor = new DefaultCellEditor(new JosmTextField());
|
---|
98 | editor.setClickCountToStart(1);
|
---|
99 | table.setDefaultEditor(table.getColumnClass(0), editor);
|
---|
100 |
|
---|
101 | JScrollPane pane = new JScrollPane(table);
|
---|
102 | pane.setPreferredSize(new Dimension(140, 0));
|
---|
103 | p.add(pane, GBC.std(1,1).insets(5,0,0,0).fill().weight(0.7, 1.0));
|
---|
104 | return p;
|
---|
105 | }
|
---|
106 |
|
---|
107 | class EntryListModel extends AbstractListModel {
|
---|
108 | public Object getElementAt(int index) {
|
---|
109 | return (index+1) + ": " + data.get(index).toString();
|
---|
110 | }
|
---|
111 |
|
---|
112 | public int getSize() {
|
---|
113 | return data.size();
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void add(List<String> l) {
|
---|
117 | data.add(l);
|
---|
118 | fireIntervalAdded(this, data.size() - 1, data.size() - 1);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public void remove(int idx) {
|
---|
122 | data.remove(idx);
|
---|
123 | fireIntervalRemoved(this, idx, idx);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | class NewEntryAction extends AbstractAction {
|
---|
128 | public NewEntryAction() {
|
---|
129 | putValue(NAME, tr("New"));
|
---|
130 | putValue(SHORT_DESCRIPTION, tr("add entry"));
|
---|
131 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "add"));
|
---|
132 | }
|
---|
133 |
|
---|
134 | public void actionPerformed(ActionEvent evt) {
|
---|
135 | entryModel.add(new ArrayList<String>());
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | class RemoveEntryAction extends AbstractAction implements ListSelectionListener {
|
---|
140 | public RemoveEntryAction() {
|
---|
141 | putValue(NAME, tr("Remove"));
|
---|
142 | putValue(SHORT_DESCRIPTION, tr("Remove the selected entry"));
|
---|
143 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
144 | updateEnabledState();
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected void updateEnabledState() {
|
---|
148 | setEnabled(entryList.getSelectedIndices().length == 1);
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void valueChanged(ListSelectionEvent e) {
|
---|
152 | updateEnabledState();
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void actionPerformed(ActionEvent e) {
|
---|
156 | int idx = entryList.getSelectedIndices()[0];
|
---|
157 | entryModel.remove(idx);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | class EntryListener implements ListSelectionListener {
|
---|
162 | public void valueChanged(ListSelectionEvent e) {
|
---|
163 | TableCellEditor editor = table.getCellEditor();
|
---|
164 | if (editor != null) {
|
---|
165 | ((DefaultCellEditor) editor).stopCellEditing();
|
---|
166 | }
|
---|
167 | if (entryList.getSelectedIndices().length != 1) {
|
---|
168 | entryIdx = null;
|
---|
169 | table.setEnabled(false);
|
---|
170 | } else {
|
---|
171 | entryIdx = entryList.getSelectedIndices()[0];
|
---|
172 | table.setEnabled(true);
|
---|
173 | }
|
---|
174 | tableModel.fireTableStructureChanged();
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | class ListTableModel extends AbstractTableModel {
|
---|
179 |
|
---|
180 | private List<String> data() {
|
---|
181 | if (entryIdx == null) return Collections.emptyList();
|
---|
182 | return data.get(entryIdx);
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public int getRowCount() {
|
---|
187 | return entryIdx == null ? 0 : data().size() + 1;
|
---|
188 | }
|
---|
189 |
|
---|
190 | @Override
|
---|
191 | public int getColumnCount() {
|
---|
192 | return 1;
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Override
|
---|
196 | public Object getValueAt(int row, int column) {
|
---|
197 | return data().size() == row ? "" : data().get(row);
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public void setValueAt(Object o, int row, int column) {
|
---|
202 | String s = (String)o;
|
---|
203 | if (row == data().size()) {
|
---|
204 | data().add(s);
|
---|
205 | fireTableRowsInserted(row+1, row+1);
|
---|
206 | } else {
|
---|
207 | data().set(row, s);
|
---|
208 | }
|
---|
209 | fireTableCellUpdated(row, column);
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | public boolean isCellEditable(int row, int column) {
|
---|
214 | return true;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|