Ticket #16343: overpass-wizard-buttons.patch
File overpass-wizard-buttons.patch, 12.0 KB (added by , 6 years ago) |
---|
-
src/org/openstreetmap/josm/gui/download/OverpassDownloadSource.java
diff --git a/src/org/openstreetmap/josm/gui/download/OverpassDownloadSource.java b/src/org/openstreetmap/josm/gui/download/OverpassDownloadSource.java index c43902a..bac3715 100644
a b import java.awt.event.ActionEvent; 10 10 import java.awt.event.FocusAdapter; 11 11 import java.awt.event.FocusEvent; 12 12 import java.util.Collection; 13 import java.util.Objects; 13 14 import java.util.concurrent.Future; 14 15 import java.util.function.Consumer; 15 16 … … import org.openstreetmap.josm.data.preferences.StringProperty; 34 35 import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; 35 36 import org.openstreetmap.josm.gui.MainApplication; 36 37 import org.openstreetmap.josm.gui.download.DownloadSourceSizingPolicy.AdjustableDownloadSizePolicy; 38 import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration; 39 import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassQueryWizard; 40 import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks; 37 41 import org.openstreetmap.josm.gui.util.GuiHelper; 38 42 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 39 43 import org.openstreetmap.josm.io.OverpassDownloadReader; … … public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo 80 84 * The GUI representation of the Overpass download source. 81 85 * @since 12652 82 86 */ 83 public static class OverpassDownloadSourcePanel extends AbstractDownloadSourcePanel<OverpassDownloadData> { 87 public static class OverpassDownloadSourcePanel extends AbstractDownloadSourcePanel<OverpassDownloadData> 88 implements OverpassWizardCallbacks { 84 89 85 90 private static final String SIMPLE_NAME = "overpassdownloadpanel"; 86 91 private static final AbstractProperty<Integer> PANEL_SIZE_PROPERTY = … … public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo 103 108 super(ds); 104 109 setLayout(new BorderLayout()); 105 110 106 String tooltip = tr("Build an Overpass query using the Overpass Turbo Query Wizard tool");107 108 JButton openQueryWizard = new JButton(tr("Query Wizard"));109 openQueryWizard.setToolTipText(tooltip);110 openQueryWizard.addActionListener(new AbstractAction() {111 @Override112 public void actionPerformed(ActionEvent e) {113 new OverpassQueryWizardDialog(OverpassDownloadSourcePanel.this).showDialog();114 }115 });116 117 111 this.overpassQuery = new JosmTextArea(DOWNLOAD_QUERY.get(), 8, 80); 118 112 this.overpassQuery.setFont(GuiHelper.getMonospacedFont(overpassQuery)); 119 113 this.overpassQuery.addFocusListener(new FocusAdapter() { … … public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo 163 157 JPanel leftPanel = new JPanel(new GridBagLayout()); 164 158 leftPanel.add(new JLabel(tr("Overpass query:")), GBC.eol().insets(5, 1, 5, 1).anchor(GBC.NORTHWEST)); 165 159 leftPanel.add(new JLabel(), GBC.eol().fill(GBC.VERTICAL)); 166 leftPanel.add(openQueryWizard, GBC.eol().anchor(GBC.CENTER)); 160 OverpassWizardRegistration.getWizards() 161 .stream() 162 .map(this::generateWizardButton) 163 .forEach(button -> leftPanel.add(button, GBC.eol().anchor(GBC.CENTER))); 167 164 leftPanel.add(new JLabel(), GBC.eol().fill(GBC.VERTICAL)); 168 165 169 166 add(leftPanel, BorderLayout.WEST); … … public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo 173 170 setMinimumSize(new Dimension(450, 240)); 174 171 } 175 172 173 private JButton generateWizardButton(OverpassQueryWizard wizard) { 174 JButton openQueryWizard = new JButton(wizard.getWizardName()); 175 openQueryWizard.setToolTipText(wizard.getWizardTooltip().orElse(null)); 176 openQueryWizard.addActionListener(new AbstractAction() { 177 @Override 178 public void actionPerformed(ActionEvent e) { 179 wizard.startWizard(OverpassDownloadSourcePanel.this); 180 } 181 }); 182 return openQueryWizard; 183 } 184 176 185 @Override 177 186 public OverpassDownloadData getData() { 178 187 String query = overpassQuery.getText(); … … public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo 258 267 * @param query The query to set. 259 268 */ 260 269 public void setOverpassQuery(String query) { 270 Objects.requireNonNull(query, "query"); 261 271 this.overpassQuery.setText(query); 262 272 } 263 273 … … public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo 359 369 checkEnabled(); 360 370 } 361 371 } 372 373 @Override 374 public void submitWizardResult(String resultingQuery) { 375 setOverpassQuery(resultingQuery); 376 } 362 377 } 363 378 364 379 /** … … public class OverpassDownloadSource implements DownloadSource<OverpassDownloadSo 381 396 return this.errorReporter; 382 397 } 383 398 } 399 384 400 } -
src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java
diff --git a/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java b/src/org/openstreetmap/josm/gui/download/OverpassQueryWizardDialog.java index 8682501..5432e15 100644
a b import javax.swing.text.JTextComponent; 22 22 import org.openstreetmap.josm.Main; 23 23 import org.openstreetmap.josm.data.preferences.ListProperty; 24 24 import org.openstreetmap.josm.gui.ExtendedDialog; 25 import org.openstreetmap.josm.gui.download.overpass.OverpassWizardRegistration.OverpassWizardCallbacks; 25 26 import org.openstreetmap.josm.gui.util.GuiHelper; 26 27 import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 27 28 import org.openstreetmap.josm.tools.GBC; … … public final class OverpassQueryWizardDialog extends ExtendedDialog { 64 65 + "#desc {width: 350px;}" 65 66 + "</style>\n"; 66 67 67 private final Overpass DownloadSource.OverpassDownloadSourcePaneldsPanel;68 private final OverpassWizardCallbacks dsPanel; 68 69 69 70 /** 70 71 * Create a new {@link OverpassQueryWizardDialog} 71 * @param dsPanelThe Overpass download source panel.72 * @param callbacks The Overpass download source panel. 72 73 */ 73 public OverpassQueryWizardDialog(Overpass DownloadSource.OverpassDownloadSourcePanel dsPanel) {74 super( dsPanel.getParent(), tr("Overpass Turbo Query Wizard"),74 public OverpassQueryWizardDialog(OverpassWizardCallbacks callbacks) { 75 super(callbacks.getParent(), tr("Overpass Turbo Query Wizard"), 75 76 tr("Build query"), tr("Build query and execute"), tr("Cancel")); 76 this.dsPanel = dsPanel;77 this.dsPanel = callbacks; 77 78 78 79 this.queryWizard = new HistoryComboBox(); 79 80 this.overpassQueryBuilder = OverpassTurboQueryWizard.getInstance(); … … public final class OverpassQueryWizardDialog extends ExtendedDialog { 167 168 final String wizardSearchTerm = this.queryWizard.getText(); 168 169 169 170 Optional<String> q = this.tryParseSearchTerm(wizardSearchTerm); 170 if (q.isPresent()) { 171 String query = q.get(); 172 dsPanel.setOverpassQuery(query); 173 174 return true; 175 } 176 177 return false; 171 q.ifPresent(dsPanel::submitWizardResult); 172 return q.isPresent(); 178 173 } 179 174 180 175 private static JTextComponent buildDescriptionSection() { -
new file src/org/openstreetmap/josm/gui/download/overpass/OverpassWizardRegistration.java
diff --git a/src/org/openstreetmap/josm/gui/download/overpass/OverpassWizardRegistration.java b/src/org/openstreetmap/josm/gui/download/overpass/OverpassWizardRegistration.java new file mode 100644 index 0000000..bf8b99a
- + 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.download.overpass; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 import java.awt.Component; 7 import java.util.ArrayList; 8 import java.util.Collections; 9 import java.util.List; 10 import java.util.Objects; 11 import java.util.Optional; 12 13 import org.openstreetmap.josm.gui.download.OverpassQueryWizardDialog; 14 15 /** 16 * Registers the overpass query wizards. 17 * @author Michael Zangl 18 * @since xxx 19 */ 20 public final class OverpassWizardRegistration { 21 /** 22 * A list of all reigstered wizards. Needs to be synchronized since plugin registration may happen outside main thread / asynchronously. 23 */ 24 private static List<OverpassQueryWizard> wizards = Collections.synchronizedList(new ArrayList<>()); 25 26 /** 27 * Registers a wizard to be added to the overpass download dialog 28 * <p> 29 * To be called by plugins during the JOSM boot process or at least before opening the download dialog for the first time. 30 * @param wizard The wizard to register 31 * @since xxx 32 */ 33 public static void registerWizard(OverpassQueryWizard wizard) { 34 Objects.requireNonNull(wizard, "wizard"); 35 wizards.add(wizard); 36 } 37 38 /** 39 * Gets all wizards that are currently registered. 40 * @return The list of wizards. 41 */ 42 public static List<OverpassQueryWizard> getWizards() { 43 return Collections.unmodifiableList(wizards); 44 } 45 46 static { 47 // Register the default wizard 48 registerWizard(new OverpassQueryWizard() { 49 @Override 50 public void startWizard(OverpassWizardCallbacks callbacks) { 51 new OverpassQueryWizardDialog(callbacks).showDialog(); 52 } 53 54 @Override 55 public Optional<String> getWizardTooltip() { 56 return Optional.of(tr("Build an Overpass query using the Overpass Turbo Query Wizard tool")); 57 } 58 59 @Override 60 public String getWizardName() { 61 return tr("Query Wizard"); 62 } 63 }); 64 } 65 66 private OverpassWizardRegistration() { 67 // hidden 68 } 69 70 /** 71 * Defines a query wizard that generates overpass queries. 72 * @author Michael Zangl 73 * @since xxx 74 */ 75 public interface OverpassQueryWizard { 76 /** 77 * Get the name of the wizard 78 * @return The name 79 */ 80 String getWizardName(); 81 82 /** 83 * Get the tooltip text to display when hovering the wizard button. 84 * @return The tooltip text or an empty optional to display no tooltip. 85 */ 86 Optional<String> getWizardTooltip(); 87 88 /** 89 * Start the wizard. 90 * @param callbacks The callbacks to use to send back wizard results. 91 */ 92 void startWizard(OverpassWizardCallbacks callbacks); 93 } 94 95 /** 96 * Wizard callbacks required by {@link OverpassQueryWizard#startWizard(OverpassWizardCallbacks)} 97 * @author Michael Zangl 98 * @since xxx 99 */ 100 public interface OverpassWizardCallbacks { 101 /** 102 * Send the resulting query 103 * @param resultingQuery The query that is used by the wizard 104 */ 105 void submitWizardResult(String resultingQuery); 106 107 /** 108 * Get the parent component to use when opening the wizard dialog. 109 * @return The component. 110 */ 111 Component getParent(); 112 } 113 } -
new file src/org/openstreetmap/josm/gui/download/overpass/package-info.java
diff --git a/src/org/openstreetmap/josm/gui/download/overpass/package-info.java b/src/org/openstreetmap/josm/gui/download/overpass/package-info.java new file mode 100644 index 0000000..ad059b9
- + 1 // License: GPL. For details, see LICENSE file. 2 /** 3 * This package contains all overpass query related classes. 4 * <p> 5 * Traditionally (and still now for compatibility), some of them are in the download package. 6 */ 7 package org.openstreetmap.josm.gui.download.overpass;