Changeset 9507 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2016-01-17T18:25:47+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/preferences/advanced
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/advanced/ListEditor.java
r9239 r9507 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Dimension;7 6 import java.awt.GridBagLayout; 8 7 import java.util.ArrayList; … … 18 17 19 18 import org.openstreetmap.josm.data.Preferences.ListSetting; 20 import org.openstreetmap.josm.gui.ExtendedDialog;21 19 import org.openstreetmap.josm.gui.widgets.JosmTextField; 22 20 import org.openstreetmap.josm.tools.GBC; 23 21 import org.openstreetmap.josm.tools.Predicate; 24 22 import org.openstreetmap.josm.tools.Utils; 25 import org.openstreetmap.josm.tools.WindowGeometry;26 23 27 24 /** 28 25 * Editor for List preference entries. 26 * @since 4634 29 27 */ 30 public class ListEditor extends ExtendedDialog{28 public class ListEditor extends AbstractListEditor<String> { 31 29 32 private List<String> data; 33 private final transient PrefEntry entry; 30 private final List<String> data; 34 31 35 32 /** … … 40 37 */ 41 38 public ListEditor(final JComponent gui, PrefEntry entry, ListSetting setting) { 42 super(gui, tr("Change list setting"), new String[] {tr("OK"), tr("Cancel")}); 43 this.entry = entry; 39 super(gui, tr("Change list setting"), entry); 44 40 List<String> orig = setting.getValue(); 45 41 if (orig != null) { … … 48 44 data = new ArrayList<>(); 49 45 } 50 setButtonIcons(new String[] {"ok.png", "cancel.png"});51 setRememberWindowGeometry(getClass().getName() + ".geometry", WindowGeometry.centerInWindow(gui, new Dimension(300, 350)));52 46 setContent(build(), false); 53 47 } 54 48 55 /** 56 * Returns the list of values. 57 * @return The list of values. 58 */ 49 @Override 59 50 public List<String> getData() { 60 51 return new ArrayList<>(Utils.filter(data, new Predicate<String>() { … … 66 57 } 67 58 59 @Override 68 60 protected final JPanel build() { 69 61 JPanel p = new JPanel(new GridBagLayout()); -
trunk/src/org/openstreetmap/josm/gui/preferences/advanced/ListListEditor.java
r9239 r9507 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Dimension;7 import java.awt.GridBagLayout;8 import java.awt.event.ActionEvent;9 6 import java.util.ArrayList; 10 7 import java.util.Collections; 11 8 import java.util.List; 12 9 13 import javax.swing.AbstractAction;14 import javax.swing.AbstractListModel;15 import javax.swing.DefaultCellEditor;16 10 import javax.swing.JComponent; 17 import javax.swing.JLabel;18 import javax.swing.JList;19 11 import javax.swing.JPanel; 20 import javax.swing.JScrollPane;21 import javax.swing.JTable;22 import javax.swing.JToolBar;23 import javax.swing.event.ListSelectionEvent;24 import javax.swing.event.ListSelectionListener;25 12 import javax.swing.table.AbstractTableModel; 26 import javax.swing.table.TableCellEditor;27 13 28 14 import org.openstreetmap.josm.data.Preferences.ListListSetting; 29 import org.openstreetmap.josm.gui.ExtendedDialog;30 import org.openstreetmap.josm.gui.widgets.JosmTextField;31 import org.openstreetmap.josm.tools.GBC;32 import org.openstreetmap.josm.tools.ImageProvider;33 import org.openstreetmap.josm.tools.WindowGeometry;34 15 35 16 /** 36 17 * Editor for List of Lists preference entries. 18 * @since 4634 37 19 */ 38 public class ListListEditor extends ExtendedDialog{20 public class ListListEditor extends AbstractTableListEditor<List<String>> { 39 21 40 private EntryListModel entryModel; 41 private final List<List<String>> data; 42 private final transient PrefEntry entry; 43 44 private JList<String> entryList; 45 private Integer entryIdx; 46 private JTable table; 47 48 private ListTableModel tableModel; 22 private final transient List<List<String>> data; 49 23 50 24 /** … … 55 29 */ 56 30 public ListListEditor(final JComponent gui, PrefEntry entry, ListListSetting setting) { 57 super(gui, tr("Change list of lists setting"), new String[] {tr("OK"), tr("Cancel")}); 58 this.entry = entry; 31 super(gui, tr("Change list of lists setting"), entry); 59 32 List<List<String>> orig = setting.getValue(); 60 33 data = new ArrayList<>(); … … 64 37 } 65 38 } 66 setButtonIcons(new String[] {"ok.png", "cancel.png"});67 setRememberWindowGeometry(getClass().getName() + ".geometry", WindowGeometry.centerInWindow(gui, new Dimension(500, 350)));68 setContent(build(), false);69 39 } 70 40 71 /** 72 * Returns the data. 73 * @return the preference data 74 */ 41 @Override 75 42 public List<List<String>> getData() { 76 43 return data; 77 44 } 78 45 46 @Override 79 47 protected final JPanel build() { 80 JPanel p = new JPanel(new GridBagLayout());81 p.add(new JLabel(tr("Key: {0}", entry.getKey())), GBC.std(0, 0).span(2).weight(1, 0).insets(0, 0, 5, 10));82 83 JPanel left = new JPanel(new GridBagLayout());84 85 entryModel = new EntryListModel();86 entryList = new JList<>(entryModel);87 entryList.getSelectionModel().addListSelectionListener(new EntryListener());88 JScrollPane scroll = new JScrollPane(entryList);89 left.add(scroll, GBC.eol().fill());90 91 JToolBar sideButtonTB = new JToolBar(JToolBar.HORIZONTAL);92 sideButtonTB.setBorderPainted(false);93 sideButtonTB.setOpaque(false);94 sideButtonTB.add(new NewEntryAction());95 RemoveEntryAction removeEntryAction = new RemoveEntryAction();96 entryList.getSelectionModel().addListSelectionListener(removeEntryAction);97 sideButtonTB.add(removeEntryAction);98 left.add(sideButtonTB, GBC.eol());99 100 left.setPreferredSize(new Dimension(80, 0));101 102 p.add(left, GBC.std(0, 1).fill().weight(0.3, 1.0));103 104 tableModel = new ListTableModel();105 table = new JTable(tableModel);106 table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);107 48 table.setTableHeader(null); 108 109 DefaultCellEditor editor = new DefaultCellEditor(new JosmTextField()); 110 editor.setClickCountToStart(1); 111 table.setDefaultEditor(table.getColumnClass(0), editor); 112 113 JScrollPane pane = new JScrollPane(table); 114 pane.setPreferredSize(new Dimension(140, 0)); 115 p.add(pane, GBC.std(1, 1).insets(5, 0, 0, 0).fill().weight(0.7, 1.0)); 116 return p; 49 return super.build(); 117 50 } 118 51 119 class EntryListModel extends AbstractListModel<String> { 52 private class EntryListModel extends AbstractEntryListModel { 53 120 54 @Override 121 55 public String getElementAt(int index) { … … 128 62 } 129 63 130 public void add(List<String> l) { 131 data.add(l); 132 fireIntervalAdded(this, data.size() - 1, data.size() - 1); 64 @Override 65 public void add() { 66 data.add(new ArrayList<String>()); 67 fireIntervalAdded(this, getSize() - 1, getSize() - 1); 133 68 } 134 69 70 @Override 135 71 public void remove(int idx) { 136 72 data.remove(idx); … … 139 75 } 140 76 141 class NewEntryAction extends AbstractAction { 142 NewEntryAction() { 143 putValue(NAME, tr("New")); 144 putValue(SHORT_DESCRIPTION, tr("add entry")); 145 putValue(SMALL_ICON, ImageProvider.get("dialogs", "add")); 77 private class ListTableModel extends AbstractTableModel { 78 79 private List<String> data() { 80 return entryIdx == null ? Collections.<String>emptyList() : data.get(entryIdx); 146 81 } 147 82 148 @Override 149 public void actionPerformed(ActionEvent evt) { 150 entryModel.add(new ArrayList<String>()); 151 } 152 } 153 154 class RemoveEntryAction extends AbstractAction implements ListSelectionListener { 155 RemoveEntryAction() { 156 putValue(NAME, tr("Remove")); 157 putValue(SHORT_DESCRIPTION, tr("Remove the selected entry")); 158 putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete")); 159 updateEnabledState(); 160 } 161 162 protected final void updateEnabledState() { 163 setEnabled(entryList.getSelectedIndices().length == 1); 164 } 165 166 @Override 167 public void valueChanged(ListSelectionEvent e) { 168 updateEnabledState(); 169 } 170 171 @Override 172 public void actionPerformed(ActionEvent e) { 173 int idx = entryList.getSelectedIndices()[0]; 174 entryModel.remove(idx); 175 } 176 } 177 178 class EntryListener implements ListSelectionListener { 179 @Override 180 public void valueChanged(ListSelectionEvent e) { 181 TableCellEditor editor = table.getCellEditor(); 182 if (editor != null) { 183 ((DefaultCellEditor) editor).stopCellEditing(); 184 } 185 if (entryList.getSelectedIndices().length != 1) { 186 entryIdx = null; 187 table.setEnabled(false); 188 } else { 189 entryIdx = entryList.getSelectedIndices()[0]; 190 table.setEnabled(true); 191 } 192 tableModel.fireTableStructureChanged(); 193 } 194 } 195 196 class ListTableModel extends AbstractTableModel { 197 198 private List<String> data() { 199 if (entryIdx == null) return Collections.emptyList(); 200 return data.get(entryIdx); 83 private int size() { 84 return data().size(); 201 85 } 202 86 203 87 @Override 204 88 public int getRowCount() { 205 return entryIdx == null ? 0 : data().size() + 1;89 return entryIdx == null ? 0 : size() + 1; 206 90 } 207 91 … … 213 97 @Override 214 98 public Object getValueAt(int row, int column) { 215 return data().size() == row ? "" : data().get(row);99 return size() == row ? "" : data().get(row); 216 100 } 217 101 … … 219 103 public void setValueAt(Object o, int row, int column) { 220 104 String s = (String) o; 221 if (row == data().size()) {105 if (row == size()) { 222 106 data().add(s); 223 107 fireTableRowsInserted(row+1, row+1); 224 108 } else { 225 109 data().set(row, s); 110 fireTableCellUpdated(row, column); 226 111 } 227 fireTableCellUpdated(row, column);228 112 } 229 113 … … 233 117 } 234 118 } 119 120 @Override 121 protected AbstractEntryListModel newEntryListModel() { 122 return new EntryListModel(); 123 } 124 125 @Override 126 protected AbstractTableModel newTableModel() { 127 return new ListTableModel(); 128 } 235 129 } -
trunk/src/org/openstreetmap/josm/gui/preferences/advanced/MapListEditor.java
r9078 r9507 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Dimension;7 import java.awt.GridBagLayout;8 import java.awt.event.ActionEvent;9 6 import java.util.ArrayList; 10 7 import java.util.Arrays; … … 15 12 import java.util.Map.Entry; 16 13 17 import javax.swing.AbstractAction;18 import javax.swing.AbstractListModel;19 import javax.swing.DefaultCellEditor;20 14 import javax.swing.JComponent; 21 import javax.swing.JLabel;22 import javax.swing.JList;23 15 import javax.swing.JPanel; 24 import javax.swing.JScrollPane;25 import javax.swing.JTable;26 import javax.swing.JToolBar;27 import javax.swing.event.ListSelectionEvent;28 import javax.swing.event.ListSelectionListener;29 16 import javax.swing.table.AbstractTableModel; 30 import javax.swing.table.TableCellEditor;31 17 32 18 import org.openstreetmap.josm.data.Preferences.MapListSetting; 33 import org.openstreetmap.josm.gui.ExtendedDialog;34 import org.openstreetmap.josm.gui.widgets.JosmTextField;35 import org.openstreetmap.josm.tools.GBC;36 import org.openstreetmap.josm.tools.ImageProvider;37 import org.openstreetmap.josm.tools.WindowGeometry;38 19 39 20 /** 40 21 * Editor for List of Maps preference entries. 22 * @since 4634 41 23 */ 42 public class MapListEditor extends ExtendedDialog{24 public class MapListEditor extends AbstractTableListEditor<Map<String, String>> { 43 25 44 private EntryListModel entryModel;45 private final transient PrefEntry entry;26 private final transient List<List<String>> dataKeys; 27 private final transient List<List<String>> dataValues; 46 28 47 private JList<String> entryList; 48 private JTable table; 49 private MapTableModel tableModel; 50 51 private final List<List<String>> dataKeys; 52 private final List<List<String>> dataValues; 53 private Integer entryIdx; 54 29 /** 30 * Constructs a new {@code MapListEditor}. 31 * @param gui The parent component 32 * @param entry preference entry 33 * @param setting list of maps setting 34 */ 55 35 public MapListEditor(JComponent gui, PrefEntry entry, MapListSetting setting) { 56 super(gui, tr("Change list of maps setting"), new String[] {tr("OK"), tr("Cancel")}); 57 this.entry = entry; 36 super(gui, tr("Change list of maps setting"), entry); 58 37 List<Map<String, String>> orig = setting.getValue(); 59 38 … … 72 51 } 73 52 } 74 setButtonIcons(new String[] {"ok.png", "cancel.png"});75 setRememberWindowGeometry(getClass().getName() + ".geometry", WindowGeometry.centerInWindow(gui, new Dimension(500, 350)));76 setContent(build(), false);77 53 } 78 54 79 /** 80 * Returns the data. 81 * @return the preference data 82 */ 55 @Override 83 56 public List<Map<String, String>> getData() { 84 57 List<Map<String, String>> data = new ArrayList<>(); … … 93 66 } 94 67 68 @Override 95 69 protected final JPanel build() { 96 JPanel p = new JPanel(new GridBagLayout());97 p.add(new JLabel(tr("Key: {0}", entry.getKey())), GBC.std(0, 0).span(2).weight(1, 0).insets(0, 0, 5, 10));98 99 JPanel left = new JPanel(new GridBagLayout());100 101 entryModel = new EntryListModel();102 entryList = new JList<>(entryModel);103 entryList.getSelectionModel().addListSelectionListener(new EntryListener());104 JScrollPane scroll = new JScrollPane(entryList);105 left.add(scroll, GBC.eol().fill());106 107 JToolBar sideButtonTB = new JToolBar(JToolBar.HORIZONTAL);108 sideButtonTB.setBorderPainted(false);109 sideButtonTB.setOpaque(false);110 sideButtonTB.add(new NewEntryAction());111 RemoveEntryAction removeEntryAction = new RemoveEntryAction();112 entryList.getSelectionModel().addListSelectionListener(removeEntryAction);113 sideButtonTB.add(removeEntryAction);114 left.add(sideButtonTB, GBC.eol());115 116 left.setPreferredSize(new Dimension(80, 0));117 118 p.add(left, GBC.std(0, 1).fill().weight(0.3, 1.0));119 120 tableModel = new MapTableModel();121 table = new JTable(tableModel);122 table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);123 70 table.getTableHeader().getColumnModel().getColumn(0).setHeaderValue(tr("Key")); 124 71 table.getTableHeader().getColumnModel().getColumn(1).setHeaderValue(tr("Value")); 125 DefaultCellEditor editor = new DefaultCellEditor(new JosmTextField()); 126 editor.setClickCountToStart(1); 127 table.setDefaultEditor(table.getColumnClass(0), editor); 128 129 JScrollPane pane = new JScrollPane(table); 130 pane.setPreferredSize(new Dimension(140, 0)); 131 p.add(pane, GBC.std(1, 1).insets(5, 0, 0, 0).fill().weight(0.7, 1.0)); 132 return p; 72 return super.build(); 133 73 } 134 74 135 class EntryListModel extends AbstractListModel<String> { 75 private class EntryListModel extends AbstractEntryListModel { 76 136 77 @Override 137 78 public String getElementAt(int index) { … … 144 85 } 145 86 87 @Override 146 88 public void add() { 147 89 dataKeys.add(new ArrayList<String>()); … … 150 92 } 151 93 94 @Override 152 95 public void remove(int idx) { 153 96 dataKeys.remove(idx); … … 157 100 } 158 101 159 class NewEntryAction extends AbstractAction { 160 NewEntryAction() { 161 putValue(NAME, tr("New")); 162 putValue(SHORT_DESCRIPTION, tr("add entry")); 163 putValue(SMALL_ICON, ImageProvider.get("dialogs", "add")); 164 } 102 private class MapTableModel extends AbstractTableModel { 165 103 166 @Override167 public void actionPerformed(ActionEvent evt) {168 entryModel.add();169 }170 }171 172 class RemoveEntryAction extends AbstractAction implements ListSelectionListener {173 RemoveEntryAction() {174 putValue(NAME, tr("Remove"));175 putValue(SHORT_DESCRIPTION, tr("Remove the selected entry"));176 putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));177 updateEnabledState();178 }179 180 protected final void updateEnabledState() {181 setEnabled(entryList.getSelectedIndices().length == 1);182 }183 184 @Override185 public void valueChanged(ListSelectionEvent e) {186 updateEnabledState();187 }188 189 @Override190 public void actionPerformed(ActionEvent e) {191 int idx = entryList.getSelectedIndices()[0];192 entryModel.remove(idx);193 }194 }195 196 class EntryListener implements ListSelectionListener {197 @Override198 public void valueChanged(ListSelectionEvent e) {199 TableCellEditor editor = table.getCellEditor();200 if (editor != null) {201 ((DefaultCellEditor) editor).stopCellEditing();202 }203 if (entryList.getSelectedIndices().length != 1) {204 entryIdx = null;205 table.setEnabled(false);206 } else {207 entryIdx = entryList.getSelectedIndices()[0];208 table.setEnabled(true);209 }210 tableModel.fireTableDataChanged();211 }212 }213 214 class MapTableModel extends AbstractTableModel {215 104 private List<List<String>> data() { 216 if (entryIdx == null) return Collections.emptyList(); 217 return Arrays.asList(dataKeys.get(entryIdx), dataValues.get(entryIdx)); 105 return entryIdx == null ? Collections.<List<String>>emptyList() : Arrays.asList(dataKeys.get(entryIdx), dataValues.get(entryIdx)); 218 106 } 219 107 220 108 private int size() { 221 if (entryIdx == null) return 0; 222 return dataKeys.get(entryIdx).size(); 109 return entryIdx == null ? 0 : dataKeys.get(entryIdx).size(); 223 110 } 224 111 … … 263 150 } 264 151 152 @Override 153 protected final AbstractEntryListModel newEntryListModel() { 154 return new EntryListModel(); 155 } 156 157 @Override 158 protected final AbstractTableModel newTableModel() { 159 return new MapTableModel(); 160 } 265 161 }
Note:
See TracChangeset
for help on using the changeset viewer.