Changeset 13515 in josm
- Timestamp:
- 2018-03-11T18:26:01+01:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/projection/CodeProjectionChoice.java
r13206 r13515 17 17 import java.util.regex.Pattern; 18 18 19 import javax.swing.AbstractListModel;20 import javax.swing.JList;21 19 import javax.swing.JPanel; 22 20 import javax.swing.JScrollPane; 21 import javax.swing.JTable; 23 22 import javax.swing.event.DocumentEvent; 24 23 import javax.swing.event.DocumentListener; 25 24 import javax.swing.event.ListSelectionEvent; 26 25 import javax.swing.event.ListSelectionListener; 26 import javax.swing.table.AbstractTableModel; 27 27 28 28 import org.openstreetmap.josm.data.projection.Projection; … … 33 33 /** 34 34 * Projection choice that lists all known projects by code. 35 * @since 5634 35 36 */ 36 37 public class CodeProjectionChoice extends AbstractProjectionChoice implements SubPrefsOptions { … … 47 48 private static class CodeSelectionPanel extends JPanel implements ListSelectionListener, DocumentListener { 48 49 49 p ublicfinal JosmTextField filter = new JosmTextField(30);50 private final ProjectionCode ListModel model = new ProjectionCodeListModel();51 p ublic JList<String> selectionList;50 private final JosmTextField filter = new JosmTextField(30); 51 private final ProjectionCodeModel model = new ProjectionCodeModel(); 52 private JTable table; 52 53 private final List<String> data; 53 54 private final List<String> filteredData; … … 63 64 build(); 64 65 setCode(initialCode != null ? initialCode : DEFAULT_CODE); 65 selectionList.addListSelectionListener(this);66 table.getSelectionModel().addListSelectionListener(this); 66 67 } 67 68 … … 69 70 * List model for the filtered view on the list of all codes. 70 71 */ 71 private class ProjectionCode ListModel extends AbstractListModel<String>{72 @Override 73 public int get Size() {72 private class ProjectionCodeModel extends AbstractTableModel { 73 @Override 74 public int getRowCount() { 74 75 return filteredData.size(); 75 76 } 76 77 77 78 @Override 78 public String getElementAt(int index) { 79 if (index >= 0 && index < filteredData.size()) 80 return filteredData.get(index); 81 else 82 return null; 83 } 84 85 public void fireContentsChanged() { 86 fireContentsChanged(this, 0, this.getSize()-1); 79 public String getValueAt(int index, int column) { 80 if (index >= 0 && index < filteredData.size()) { 81 String code = filteredData.get(index); 82 switch (column) { 83 case 0: return code; 84 case 1: return Projections.getProjectionByCode(code).toString(); 85 default: break; 86 } 87 } 88 return null; 89 } 90 91 @Override 92 public int getColumnCount() { 93 return 2; 94 } 95 96 @Override 97 public String getColumnName(int column) { 98 switch (column) { 99 case 0: return tr("Projection code"); 100 case 1: return tr("Projection name"); 101 default: return super.getColumnName(column); 102 } 87 103 } 88 104 } 89 105 90 106 private void build() { 91 filter.setColumns( 10);107 filter.setColumns(40); 92 108 filter.getDocument().addDocumentListener(this); 93 109 94 selectionList= new JList<>(data.toArray(new String[0]));95 selectionList.setModel(model);96 JScrollPane scroll = new JScrollPane( selectionList);110 table = new JTable(model); 111 table.setAutoCreateRowSorter(true); 112 JScrollPane scroll = new JScrollPane(table); 97 113 scroll.setPreferredSize(new Dimension(200, 214)); 98 114 99 115 this.setLayout(new GridBagLayout()); 100 116 this.add(filter, GBC.eol().weight(1.0, 0.0)); 101 this.add(scroll, GBC.eol()); 117 this.add(scroll, GBC.eol().fill(GBC.HORIZONTAL)); 102 118 } 103 119 104 120 public String getCode() { 105 int idx = selectionList.getSelectedIndex();121 int idx = table.getSelectedRow(); 106 122 if (idx == -1) 107 123 return lastCode; 108 return filteredData.get( selectionList.getSelectedIndex());124 return filteredData.get(table.convertRowIndexToModel(table.getSelectedRow())); 109 125 } 110 126 … … 112 128 int idx = filteredData.indexOf(code); 113 129 if (idx != -1) { 114 selectionList.setSelectedIndex(idx); 115 selectionList.ensureIndexIsVisible(idx); 116 } 130 selectRow(idx); 131 } 132 } 133 134 private void selectRow(int idx) { 135 table.setRowSelectionInterval(idx, idx); 136 ensureRowIsVisible(idx); 137 } 138 139 private void ensureRowIsVisible(int idx) { 140 table.scrollRectToVisible(table.getCellRect(idx, 0, true)); 117 141 } 118 142 … … 142 166 String filterTxt = filter.getText().trim().toLowerCase(Locale.ENGLISH); 143 167 for (String code : data) { 144 if (code.toLowerCase(Locale.ENGLISH).contains(filterTxt)) { 168 if (code.toLowerCase(Locale.ENGLISH).contains(filterTxt) 169 || Projections.getProjectionByCode(code).toString().toLowerCase(Locale.ENGLISH).contains(filterTxt)) { 145 170 filteredData.add(code); 146 171 } 147 172 } 148 model.fire ContentsChanged();173 model.fireTableDataChanged(); 149 174 int idx = filteredData.indexOf(lastCode); 150 175 if (idx == -1) { 151 selectionList.clearSelection();152 if ( selectionList.getModel().getSize() > 0) {153 selectionList.ensureIndexIsVisible(0);176 table.clearSelection(); 177 if (table.getModel().getRowCount() > 0) { 178 ensureRowIsVisible(0); 154 179 } 155 180 } else { 156 selectionList.setSelectedIndex(idx); 157 selectionList.ensureIndexIsVisible(idx); 181 selectRow(idx); 158 182 } 159 183 }
Note:
See TracChangeset
for help on using the changeset viewer.