Changeset 17099 in josm for trunk/src/org


Ignore:
Timestamp:
2020-10-07T19:52:23+02:00 (4 years ago)
Author:
simon04
Message:

fix #19745 - Preferences: add quick filter for styles/presets/rules

File:
1 edited

Legend:

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

    r16964 r17099  
    99import java.awt.Component;
    1010import java.awt.Dimension;
    11 import java.awt.Font;
    1211import java.awt.GridBagConstraints;
    1312import java.awt.GridBagLayout;
     
    4039import java.util.stream.Collectors;
    4140import java.util.stream.IntStream;
     41import java.util.stream.Stream;
    4242
    4343import javax.swing.AbstractAction;
    4444import javax.swing.BorderFactory;
    4545import javax.swing.Box;
    46 import javax.swing.DefaultListModel;
    4746import javax.swing.DefaultListSelectionModel;
    4847import javax.swing.JButton;
     
    5150import javax.swing.JFileChooser;
    5251import javax.swing.JLabel;
    53 import javax.swing.JList;
    5452import javax.swing.JOptionPane;
    5553import javax.swing.JPanel;
     
    5957import javax.swing.JToolBar;
    6058import javax.swing.KeyStroke;
    61 import javax.swing.ListCellRenderer;
    6259import javax.swing.ListSelectionModel;
    6360import javax.swing.UIManager;
     
    9491import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
    9592import org.openstreetmap.josm.gui.widgets.FileChooserManager;
     93import org.openstreetmap.josm.gui.widgets.FilterField;
    9694import org.openstreetmap.josm.gui.widgets.JosmTextField;
    9795import org.openstreetmap.josm.io.CachedFile;
     
    125123    protected final ActiveSourcesModel activeSourcesModel;
    126124    /** the list of available sources **/
    127     protected final JList<ExtendedSourceEntry> lstAvailableSources;
     125    protected final JTable tblAvailableSources;
    128126    /** the underlying model of available sources **/
    129     protected final AvailableSourcesListModel availableSourcesModel;
     127    protected final AvailableSourcesModel availableSourcesModel;
    130128    /** the URL from which the available sources are fetched **/
    131129    protected final String availableSourcesUrl;
     
    152150
    153151        DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
    154         this.availableSourcesModel = new AvailableSourcesListModel(selectionModel);
    155         this.lstAvailableSources = new JList<>(availableSourcesModel);
    156         this.lstAvailableSources.setSelectionModel(selectionModel);
    157         final SourceEntryListCellRenderer listCellRenderer = new SourceEntryListCellRenderer();
    158         this.lstAvailableSources.setCellRenderer(listCellRenderer);
    159         GuiHelper.extendTooltipDelay(lstAvailableSources);
     152        this.availableSourcesModel = new AvailableSourcesModel();
     153        this.tblAvailableSources = new ScrollHackTable(availableSourcesModel);
     154        this.tblAvailableSources.setAutoCreateRowSorter(true);
     155        this.tblAvailableSources.setSelectionModel(selectionModel);
     156        final FancySourceEntryTableCellRenderer availableSourcesEntryRenderer = new FancySourceEntryTableCellRenderer();
     157        this.tblAvailableSources.getColumnModel().getColumn(0).setCellRenderer(availableSourcesEntryRenderer);
     158        GuiHelper.extendTooltipDelay(tblAvailableSources);
    160159        this.availableSourcesUrl = availableSourcesUrl;
    161160        this.sourceProviders = sourceProviders;
     
    166165        tblActiveSources.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
    167166        tblActiveSources.setSelectionModel(selectionModel);
    168         tblActiveSources.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    169         tblActiveSources.setShowGrid(false);
    170         tblActiveSources.setIntercellSpacing(new Dimension(0, 0));
    171         tblActiveSources.setTableHeader(null);
    172         tblActiveSources.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
     167        Stream.of(tblAvailableSources, tblActiveSources).forEach(t -> {
     168            t.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
     169            t.setShowGrid(false);
     170            t.setIntercellSpacing(new Dimension(0, 0));
     171            t.setTableHeader(null);
     172            t.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
     173        });
    173174        SourceEntryTableCellRenderer sourceEntryRenderer = new SourceEntryTableCellRenderer();
    174175        if (canEnable) {
     
    181182
    182183        activeSourcesModel.addTableModelListener(e -> {
    183             listCellRenderer.updateSources(activeSourcesModel.getSources());
    184             lstAvailableSources.repaint();
     184            availableSourcesEntryRenderer.updateSources(activeSourcesModel.getSources());
     185            tblAvailableSources.repaint();
    185186        });
    186187        tblActiveSources.addPropertyChangeListener(evt -> {
    187             listCellRenderer.updateSources(activeSourcesModel.getSources());
    188             lstAvailableSources.repaint();
     188            availableSourcesEntryRenderer.updateSources(activeSourcesModel.getSources());
     189            tblAvailableSources.repaint();
    189190        });
    190191        // Force Swing to show horizontal scrollbars for the JTable
    191192        // Yes, this is a little ugly, but should work
     193        availableSourcesModel.addTableModelListener(e -> TableHelper.adjustColumnWidth(tblAvailableSources, 0, 800));
    192194        activeSourcesModel.addTableModelListener(e -> TableHelper.adjustColumnWidth(tblActiveSources, canEnable ? 1 : 0, 800));
    193195        activeSourcesModel.setActiveSources(getInitialSourcesList());
     
    227229
    228230        ActivateSourcesAction activateSourcesAction = new ActivateSourcesAction();
    229         lstAvailableSources.addListSelectionListener(activateSourcesAction);
     231        tblAvailableSources.getSelectionModel().addListSelectionListener(activateSourcesAction);
    230232        JButton activate = new JButton(activateSourcesAction);
    231233
     
    256258        gbc.insets = new Insets(0, 11, 0, 0);
    257259
    258         JScrollPane sp1 = new JScrollPane(lstAvailableSources);
    259         add(sp1, gbc);
     260        FilterField availableSourcesFilter = new FilterField().filter(tblAvailableSources, availableSourcesModel);
     261        JPanel defaultPane = new JPanel(new GridBagLayout());
     262        JScrollPane sp1 = new JScrollPane(tblAvailableSources);
     263        defaultPane.add(availableSourcesFilter, GBC.eol().insets(0, 0, 0, 0).fill(GridBagConstraints.HORIZONTAL));
     264        defaultPane.add(sp1, GBC.eol().insets(0, 0, 0, 0).fill(GridBagConstraints.BOTH));
     265        add(defaultPane, gbc);
    260266
    261267        gbc.gridx = 1;
     
    561567     * List model of available sources.
    562568     */
    563     protected static class AvailableSourcesListModel extends DefaultListModel<ExtendedSourceEntry> {
     569    protected static class AvailableSourcesModel extends AbstractTableModel {
    564570        private final transient List<ExtendedSourceEntry> data;
    565         private final DefaultListSelectionModel selectionModel;
    566571
    567572        /**
    568573         * Constructs a new {@code AvailableSourcesListModel}
    569          * @param selectionModel selection model
    570          */
    571         public AvailableSourcesListModel(DefaultListSelectionModel selectionModel) {
     574         */
     575        public AvailableSourcesModel() {
    572576            data = new ArrayList<>();
    573             this.selectionModel = selectionModel;
    574577        }
    575578
     
    583586                data.addAll(sources);
    584587            }
    585             fireContentsChanged(this, 0, data.size());
    586         }
    587 
    588         @Override
    589         public ExtendedSourceEntry getElementAt(int index) {
    590             return data.get(index);
    591         }
    592 
    593         @Override
    594         public int getSize() {
     588            fireTableDataChanged();
     589        }
     590
     591        public ExtendedSourceEntry getValueAt(int rowIndex) {
     592            return data.get(rowIndex);
     593        }
     594
     595        @Override
     596        public ExtendedSourceEntry getValueAt(int rowIndex, int ignored) {
     597            return getValueAt(rowIndex);
     598        }
     599
     600        @Override
     601        public int getRowCount() {
    595602            if (data == null) return 0;
    596603            return data.size();
    597604        }
    598605
    599         /**
    600          * Deletes the selected sources.
    601          */
    602         public void deleteSelected() {
    603             Iterator<ExtendedSourceEntry> it = data.iterator();
    604             int i = 0;
    605             while (it.hasNext()) {
    606                 it.next();
    607                 if (selectionModel.isSelectedIndex(i)) {
    608                     it.remove();
    609                 }
    610                 i++;
    611             }
    612             fireContentsChanged(this, 0, data.size());
    613         }
    614 
    615         /**
    616          * Returns the selected sources.
    617          * @return the selected sources
    618          */
    619         public List<ExtendedSourceEntry> getSelected() {
    620             return IntStream.range(0, data.size())
    621                     .filter(selectionModel::isSelectedIndex)
    622                     .mapToObj(data::get)
    623                     .collect(Collectors.toList());
     606        @Override
     607        public int getColumnCount() {
     608            return 1;
    624609        }
    625610    }
     
    10661051
    10671052        protected final void updateEnabledState() {
    1068             setEnabled(lstAvailableSources.getSelectedIndices().length > 0);
     1053            setEnabled(tblAvailableSources.getSelectedRowCount() > 0);
    10691054        }
    10701055
     
    10761061        @Override
    10771062        public void actionPerformed(ActionEvent e) {
    1078             List<ExtendedSourceEntry> sources = availableSourcesModel.getSelected();
     1063            List<ExtendedSourceEntry> sources = Arrays.stream(tblAvailableSources.getSelectedRows())
     1064                    .map(tblAvailableSources::convertRowIndexToModel)
     1065                    .mapToObj(availableSourcesModel::getValueAt)
     1066                    .collect(Collectors.toList());
     1067
    10791068            int josmVersion = Version.getInstance().getVersion();
    10801069            if (josmVersion != Version.JOSM_UNKNOWN_VERSION) {
     
    13301319    }
    13311320
    1332     static class SourceEntryListCellRenderer extends JLabel implements ListCellRenderer<ExtendedSourceEntry> {
     1321    static class FancySourceEntryTableCellRenderer extends DefaultTableCellRenderer {
    13331322
    13341323        private static final NamedColorProperty SOURCE_ENTRY_ACTIVE_BACKGROUND_COLOR = new NamedColorProperty(
     
    13421331
    13431332        @Override
    1344         public Component getListCellRendererComponent(JList<? extends ExtendedSourceEntry> list, ExtendedSourceEntry value,
    1345                 int index, boolean isSelected, boolean cellHasFocus) {
     1333        public Component getTableCellRendererComponent(JTable list, Object object, boolean isSelected, boolean hasFocus, int row, int column) {
     1334            super.getTableCellRendererComponent(list, object, isSelected, hasFocus, row, column);
     1335            final ExtendedSourceEntry value = (ExtendedSourceEntry) object;
    13461336            String s = value.toString();
    13471337            setText(s);
    1348             if (isSelected) {
    1349                 setBackground(list.getSelectionBackground());
    1350                 setForeground(list.getSelectionForeground());
    1351             } else {
    1352                 setBackground(list.getBackground());
    1353                 setForeground(list.getForeground());
    1354             }
    1355             setEnabled(list.isEnabled());
    1356             setFont(list.getFont());
    1357             setFont(getFont().deriveFont(Font.PLAIN));
    1358             setOpaque(true);
    13591338            setToolTipText(value.getTooltip());
    13601339            if (!isSelected) {
Note: See TracChangeset for help on using the changeset viewer.