Changeset 8756 in josm


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

fix #10023 - Add history of Overpass queries

Location:
trunk
Files:
4 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
  • trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java

    r8513 r8756  
    236236                    "<html>Uploading to the server <strong>failed</strong> because your current<br>"
    237237                    + "dataset violates a precondition.<br>" + "The error message is:<br>" + "{0}" + "</html>",
    238                     escapeReservedCharactersHTML(e.getMessage()));
     238                    Utils.escapeReservedCharactersHTML(e.getMessage()));
    239239        }
    240240    }
     
    424424        }
    425425        Main.error(e);
    426         return escapeReservedCharactersHTML(msg);
     426        return Utils.escapeReservedCharactersHTML(msg);
    427427    }
    428428
     
    691691                + "<br>"
    692692                + "The error message is:<br>" + "{0}"
    693                 + "</html>", escapeReservedCharactersHTML(e.getMessage()));
     693                + "</html>", Utils.escapeReservedCharactersHTML(e.getMessage()));
    694694    }
    695695
     
    709709    }
    710710
    711     /**
    712      * Replaces some HTML reserved characters (&lt;, &gt; and &amp;) by their equivalent entity (&amp;lt;, &amp;gt; and &amp;amp;);
    713      * @param s The unescaped string
    714      * @return The escaped string
    715      */
    716     public static String escapeReservedCharactersHTML(String s) {
    717         return s == null ? "" : s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
    718     }
    719711}
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r8734 r8756  
    646646
    647647    /**
     648     * Replaces some HTML reserved characters (&lt;, &gt; and &amp;) by their equivalent entity (&amp;lt;, &amp;gt; and &amp;amp;);
     649     * @param s The unescaped string
     650     * @return The escaped string
     651     */
     652    public static String escapeReservedCharactersHTML(String s) {
     653        return s == null ? "" : s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
     654    }
     655
     656    /**
    648657     * Represents a function that can be applied to objects of {@code A} and
    649658     * returns objects of {@code B}.
     
    11841193        } else {
    11851194            return s;
     1195        }
     1196    }
     1197
     1198    /**
     1199     * If the string {@code s} is longer than {@code maxLines} lines, the string is cut and a "..." line is appended.
     1200     * @param s String to shorten
     1201     * @param maxLines maximum number of lines to keep (including including the "..." line)
     1202     * @return the shortened string
     1203     */
     1204    public static String restrictStringLines(String s, int maxLines) {
     1205        if (s == null) {
     1206            return null;
     1207        } else {
     1208            final List<String> lines = Arrays.asList(s.split("\\n"));
     1209            if (lines.size() > maxLines) {
     1210                return join("\n", lines.subList(0, maxLines - 1)) + "\n" + "...";
     1211            } else {
     1212                return s;
     1213            }
    11861214        }
    11871215    }
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r8510 r8756  
    130130        assertThat(Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)), is("8 days 12 h"));
    131131    }
     132
     133    @Test
     134    public void testEscapeReservedCharactersHTML() throws Exception {
     135        assertThat(Utils.escapeReservedCharactersHTML("foo -> bar -> '&'"), is("foo -&gt; bar -&gt; '&amp;'"));
     136    }
     137
     138    @Test
     139    public void testRestrictStringLines() throws Exception {
     140        assertThat(Utils.restrictStringLines("1\n2\n3", 2), is("1\n..."));
     141        assertThat(Utils.restrictStringLines("1\n2\n3", 3), is("1\n2\n3"));
     142        assertThat(Utils.restrictStringLines("1\n2\n3", 4), is("1\n2\n3"));
     143    }
    132144}
Note: See TracChangeset for help on using the changeset viewer.