Changeset 8756 in josm
- Timestamp:
- 2015-09-13T22:45:59+02:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/OverpassDownloadAction.java
r8744 r8756 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 6 7 import java.awt.BorderLayout; 7 8 import java.awt.Component; 9 import java.awt.GridLayout; 10 import java.awt.Rectangle; 8 11 import java.awt.event.ActionEvent; 12 import java.awt.event.ActionListener; 9 13 import java.awt.event.KeyEvent; 10 14 import java.util.ArrayList; 11 15 import java.util.Arrays; 16 import java.util.Collection; 12 17 import java.util.Collections; 18 import java.util.Deque; 19 import java.util.LinkedList; 13 20 import java.util.concurrent.Future; 14 21 15 22 import javax.swing.AbstractAction; 16 23 import javax.swing.JButton; 24 import javax.swing.JComponent; 17 25 import javax.swing.JLabel; 26 import javax.swing.JMenuItem; 18 27 import javax.swing.JOptionPane; 19 28 import javax.swing.JPanel; 29 import javax.swing.JPopupMenu; 20 30 import javax.swing.JScrollPane; 31 import javax.swing.plaf.basic.BasicArrowButton; 21 32 22 33 import org.openstreetmap.josm.Main; … … 25 36 import org.openstreetmap.josm.data.Bounds; 26 37 import org.openstreetmap.josm.data.preferences.CollectionProperty; 38 import org.openstreetmap.josm.data.preferences.IntegerProperty; 27 39 import org.openstreetmap.josm.data.preferences.StringProperty; 28 40 import org.openstreetmap.josm.gui.HelpAwareOptionPane; … … 132 144 overpassQuery.setFont(GuiHelper.getMonospacedFont(overpassQuery)); 133 145 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); 134 156 pnl.add(new JLabel(tr("Overpass query: ")), GBC.std().insets(5, 5, 5, 5)); 135 157 GBC gbc = GBC.eol().fill(GBC.HORIZONTAL); 136 158 gbc.ipady = 200; 137 pnl.add( scrollPane, gbc);159 pnl.add(pane, gbc); 138 160 139 161 overpassServer = new HistoryComboBox(); … … 149 171 public String getOverpassQuery() { 150 172 return overpassQuery.getText(); 173 } 174 175 public void setOverpassQuery(String text) { 176 overpassQuery.setText(text); 151 177 } 152 178 … … 166 192 OVERPASS_SERVER_HISTORY.put(overpassServer.getHistory()); 167 193 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 } 170 247 } 171 248 -
trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java
r8513 r8756 236 236 "<html>Uploading to the server <strong>failed</strong> because your current<br>" 237 237 + "dataset violates a precondition.<br>" + "The error message is:<br>" + "{0}" + "</html>", 238 escapeReservedCharactersHTML(e.getMessage())); 238 Utils.escapeReservedCharactersHTML(e.getMessage())); 239 239 } 240 240 } … … 424 424 } 425 425 Main.error(e); 426 return escapeReservedCharactersHTML(msg); 426 return Utils.escapeReservedCharactersHTML(msg); 427 427 } 428 428 … … 691 691 + "<br>" 692 692 + "The error message is:<br>" + "{0}" 693 + "</html>", escapeReservedCharactersHTML(e.getMessage())); 693 + "</html>", Utils.escapeReservedCharactersHTML(e.getMessage())); 694 694 } 695 695 … … 709 709 } 710 710 711 /**712 * Replaces some HTML reserved characters (<, > and &) by their equivalent entity (&lt;, &gt; and &amp;);713 * @param s The unescaped string714 * @return The escaped string715 */716 public static String escapeReservedCharactersHTML(String s) {717 return s == null ? "" : s.replace("&", "&").replace("<", "<").replace(">", ">");718 }719 711 } -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r8734 r8756 646 646 647 647 /** 648 * Replaces some HTML reserved characters (<, > and &) by their equivalent entity (&lt;, &gt; and &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("&", "&").replace("<", "<").replace(">", ">"); 654 } 655 656 /** 648 657 * Represents a function that can be applied to objects of {@code A} and 649 658 * returns objects of {@code B}. … … 1184 1193 } else { 1185 1194 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 } 1186 1214 } 1187 1215 } -
trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
r8510 r8756 130 130 assertThat(Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)), is("8 days 12 h")); 131 131 } 132 133 @Test 134 public void testEscapeReservedCharactersHTML() throws Exception { 135 assertThat(Utils.escapeReservedCharactersHTML("foo -> bar -> '&'"), is("foo -> bar -> '&'")); 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 } 132 144 }
Note:
See TracChangeset
for help on using the changeset viewer.