Ignore:
Timestamp:
2015-09-13T22:45:59+02:00 (5 years ago)
Author:
simon04
Message:

fix #10023 - Add history of Overpass queries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OverpassDownloadAction.java

    r8744 r8756  
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.awt.BorderLayout;
    78import java.awt.Component;
     9import java.awt.GridLayout;
     10import java.awt.Rectangle;
    811import java.awt.event.ActionEvent;
     12import java.awt.event.ActionListener;
    913import java.awt.event.KeyEvent;
    1014import java.util.ArrayList;
    1115import java.util.Arrays;
     16import java.util.Collection;
    1217import java.util.Collections;
     18import java.util.Deque;
     19import java.util.LinkedList;
    1320import java.util.concurrent.Future;
    1421
    1522import javax.swing.AbstractAction;
    1623import javax.swing.JButton;
     24import javax.swing.JComponent;
    1725import javax.swing.JLabel;
     26import javax.swing.JMenuItem;
    1827import javax.swing.JOptionPane;
    1928import javax.swing.JPanel;
     29import javax.swing.JPopupMenu;
    2030import javax.swing.JScrollPane;
     31import javax.swing.plaf.basic.BasicArrowButton;
    2132
    2233import org.openstreetmap.josm.Main;
     
    2536import org.openstreetmap.josm.data.Bounds;
    2637import org.openstreetmap.josm.data.preferences.CollectionProperty;
     38import org.openstreetmap.josm.data.preferences.IntegerProperty;
    2739import org.openstreetmap.josm.data.preferences.StringProperty;
    2840import org.openstreetmap.josm.gui.HelpAwareOptionPane;
     
    132144            overpassQuery.setFont(GuiHelper.getMonospacedFont(overpassQuery));
    133145            JScrollPane scrollPane = new JScrollPane(overpassQuery);
     146            final JPanel pane = new JPanel(new BorderLayout());
     147            final BasicArrowButton arrowButton = new BasicArrowButton(BasicArrowButton.SOUTH);
     148            arrowButton.addActionListener(new AbstractAction() {
     149                @Override
     150                public void actionPerformed(ActionEvent e) {
     151                    OverpassQueryHistoryPopup.show(arrowButton, OverpassDownloadDialog.this);
     152                }
     153            });
     154            pane.add(scrollPane, BorderLayout.CENTER);
     155            pane.add(arrowButton, BorderLayout.EAST);
    134156            pnl.add(new JLabel(tr("Overpass query: ")), GBC.std().insets(5, 5, 5, 5));
    135157            GBC gbc = GBC.eol().fill(GBC.HORIZONTAL);
    136158            gbc.ipady = 200;
    137             pnl.add(scrollPane, gbc);
     159            pnl.add(pane, gbc);
    138160
    139161            overpassServer = new HistoryComboBox();
     
    149171        public String getOverpassQuery() {
    150172            return overpassQuery.getText();
     173        }
     174
     175        public void setOverpassQuery(String text) {
     176            overpassQuery.setText(text);
    151177        }
    152178
     
    166192            OVERPASS_SERVER_HISTORY.put(overpassServer.getHistory());
    167193            OVERPASS_WIZARD_HISTORY.put(overpassWizard.getHistory());
    168         }
    169 
     194            OverpassQueryHistoryPopup.addToHistory(getOverpassQuery());
     195        }
     196
     197    }
     198
     199    static class OverpassQueryHistoryPopup extends JPopupMenu {
     200
     201        static final CollectionProperty OVERPASS_QUERY_HISTORY = new CollectionProperty("download.overpass.query", new ArrayList<String>());
     202        static final IntegerProperty OVERPASS_QUERY_HISTORY_SIZE = new IntegerProperty("download.overpass.query.size", 12);
     203
     204        OverpassQueryHistoryPopup(final OverpassDownloadDialog dialog) {
     205            final Collection<String> history = OVERPASS_QUERY_HISTORY.get();
     206            setLayout(new GridLayout((int) Math.ceil(history.size() / 2.), 2));
     207            for (final String i : history) {
     208                add(new OverpassQueryHistoryItem(i, dialog));
     209            }
     210        }
     211
     212        static void show(final JComponent parent, final OverpassDownloadDialog dialog) {
     213            final OverpassQueryHistoryPopup menu = new OverpassQueryHistoryPopup(dialog);
     214            final Rectangle r = parent.getBounds();
     215            menu.show(parent.getParent(), r.x + r.width - (int) menu.getPreferredSize().getWidth(), r.y + r.height);
     216        }
     217
     218        static void addToHistory(final String query) {
     219            final Deque<String> history = new LinkedList<>(OVERPASS_QUERY_HISTORY.get());
     220            if (!history.contains(query)) {
     221                history.add(query);
     222            }
     223            while (history.size() > OVERPASS_QUERY_HISTORY_SIZE.get()) {
     224                history.removeFirst();
     225            }
     226            OVERPASS_QUERY_HISTORY.put(history);
     227        }
     228    }
     229
     230    static class OverpassQueryHistoryItem extends JMenuItem implements ActionListener {
     231
     232        final String query;
     233        final OverpassDownloadDialog dialog;
     234
     235        OverpassQueryHistoryItem(final String query, final OverpassDownloadDialog dialog) {
     236            this.query = query;
     237            this.dialog = dialog;
     238            setText("<html><pre style='width:300px;'>" +
     239                    Utils.escapeReservedCharactersHTML(Utils.restrictStringLines(query, 7)));
     240            addActionListener(this);
     241        }
     242
     243        @Override
     244        public void actionPerformed(ActionEvent e) {
     245            dialog.setOverpassQuery(query);
     246        }
    170247    }
    171248
Note: See TracChangeset for help on using the changeset viewer.