Changeset 13515 in josm


Ignore:
Timestamp:
2018-03-11T18:26:01+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #16081 - improve selection of projections by EPSG code:

  • display projection name in a new two-column table
  • allow to sort the table by code or name
  • allow to search by projection code and name
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/projection/CodeProjectionChoice.java

    r13206 r13515  
    1717import java.util.regex.Pattern;
    1818
    19 import javax.swing.AbstractListModel;
    20 import javax.swing.JList;
    2119import javax.swing.JPanel;
    2220import javax.swing.JScrollPane;
     21import javax.swing.JTable;
    2322import javax.swing.event.DocumentEvent;
    2423import javax.swing.event.DocumentListener;
    2524import javax.swing.event.ListSelectionEvent;
    2625import javax.swing.event.ListSelectionListener;
     26import javax.swing.table.AbstractTableModel;
    2727
    2828import org.openstreetmap.josm.data.projection.Projection;
     
    3333/**
    3434 * Projection choice that lists all known projects by code.
     35 * @since 5634
    3536 */
    3637public class CodeProjectionChoice extends AbstractProjectionChoice implements SubPrefsOptions {
     
    4748    private static class CodeSelectionPanel extends JPanel implements ListSelectionListener, DocumentListener {
    4849
    49         public final JosmTextField filter = new JosmTextField(30);
    50         private final ProjectionCodeListModel model = new ProjectionCodeListModel();
    51         public JList<String> selectionList;
     50        private final JosmTextField filter = new JosmTextField(30);
     51        private final ProjectionCodeModel model = new ProjectionCodeModel();
     52        private JTable table;
    5253        private final List<String> data;
    5354        private final List<String> filteredData;
     
    6364            build();
    6465            setCode(initialCode != null ? initialCode : DEFAULT_CODE);
    65             selectionList.addListSelectionListener(this);
     66            table.getSelectionModel().addListSelectionListener(this);
    6667        }
    6768
     
    6970         * List model for the filtered view on the list of all codes.
    7071         */
    71         private class ProjectionCodeListModel extends AbstractListModel<String> {
    72             @Override
    73             public int getSize() {
     72        private class ProjectionCodeModel extends AbstractTableModel {
     73            @Override
     74            public int getRowCount() {
    7475                return filteredData.size();
    7576            }
    7677
    7778            @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                }
    87103            }
    88104        }
    89105
    90106        private void build() {
    91             filter.setColumns(10);
     107            filter.setColumns(40);
    92108            filter.getDocument().addDocumentListener(this);
    93109
    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);
    97113            scroll.setPreferredSize(new Dimension(200, 214));
    98114
    99115            this.setLayout(new GridBagLayout());
    100116            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));
    102118        }
    103119
    104120        public String getCode() {
    105             int idx = selectionList.getSelectedIndex();
     121            int idx = table.getSelectedRow();
    106122            if (idx == -1)
    107123                return lastCode;
    108             return filteredData.get(selectionList.getSelectedIndex());
     124            return filteredData.get(table.convertRowIndexToModel(table.getSelectedRow()));
    109125        }
    110126
     
    112128            int idx = filteredData.indexOf(code);
    113129            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));
    117141        }
    118142
     
    142166            String filterTxt = filter.getText().trim().toLowerCase(Locale.ENGLISH);
    143167            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)) {
    145170                    filteredData.add(code);
    146171                }
    147172            }
    148             model.fireContentsChanged();
     173            model.fireTableDataChanged();
    149174            int idx = filteredData.indexOf(lastCode);
    150175            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);
    154179                }
    155180            } else {
    156                 selectionList.setSelectedIndex(idx);
    157                 selectionList.ensureIndexIsVisible(idx);
     181                selectRow(idx);
    158182            }
    159183        }
Note: See TracChangeset for help on using the changeset viewer.