001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005import static org.openstreetmap.josm.tools.I18n.trc; 006 007import java.awt.Component; 008import java.awt.Dimension; 009import java.awt.event.KeyEvent; 010import java.awt.event.WindowEvent; 011import java.awt.event.WindowListener; 012import java.util.Arrays; 013import java.util.Collection; 014import java.util.Collections; 015import java.util.EnumSet; 016import java.util.List; 017import java.util.stream.Collectors; 018 019import javax.swing.BorderFactory; 020import javax.swing.GroupLayout; 021import javax.swing.JLabel; 022import javax.swing.JOptionPane; 023import javax.swing.JPanel; 024import javax.swing.KeyStroke; 025import javax.swing.border.EtchedBorder; 026import javax.swing.plaf.basic.BasicComboBoxEditor; 027 028import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 029import org.openstreetmap.josm.data.osm.PrimitiveId; 030import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 031import org.openstreetmap.josm.gui.ExtendedDialog; 032import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils; 033import org.openstreetmap.josm.gui.widgets.HistoryComboBox; 034import org.openstreetmap.josm.gui.widgets.HtmlPanel; 035import org.openstreetmap.josm.gui.widgets.JosmTextField; 036import org.openstreetmap.josm.gui.widgets.OsmIdTextField; 037import org.openstreetmap.josm.gui.widgets.OsmPrimitiveTypesComboBox; 038import org.openstreetmap.josm.spi.preferences.Config; 039import org.openstreetmap.josm.tools.Logging; 040import org.openstreetmap.josm.tools.Utils; 041 042/** 043 * Dialog prompt to user to let him choose OSM primitives by specifying their type and IDs. 044 * @since 6448, split from DownloadObjectDialog 045 */ 046public class OsmIdSelectionDialog extends ExtendedDialog implements WindowListener { 047 048 protected final JPanel panel = new JPanel(); 049 protected final OsmPrimitiveTypesComboBox cbType = new OsmPrimitiveTypesComboBox(); 050 protected final OsmIdTextField tfId = new OsmIdTextField(); 051 protected final HistoryComboBox cbId = new HistoryComboBox(); 052 protected final transient GroupLayout layout = new GroupLayout(panel); 053 054 /** 055 * Creates a new OsmIdSelectionDialog 056 * @param parent The parent element that will be used for position and maximum size 057 * @param title The text that will be shown in the window titlebar 058 * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one. 059 */ 060 public OsmIdSelectionDialog(Component parent, String title, String... buttonTexts) { 061 super(parent, title, buttonTexts); 062 } 063 064 /** 065 * Creates a new OsmIdSelectionDialog 066 * @param parent The parent element that will be used for position and maximum size 067 * @param title The text that will be shown in the window titlebar 068 * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one. 069 * @param modal Set it to {@code true} if you want the dialog to be modal 070 */ 071 public OsmIdSelectionDialog(Component parent, String title, String[] buttonTexts, boolean modal) { 072 super(parent, title, buttonTexts, modal); 073 } 074 075 /** 076 * Creates a new OsmIdSelectionDialog 077 * @param parent The parent element that will be used for position and maximum size 078 * @param title The text that will be shown in the window titlebar 079 * @param buttonTexts String Array of the text that will appear on the buttons. The first button is the default one. 080 * @param modal Set it to {@code true} if you want the dialog to be modal 081 * @param disposeOnClose whether to call {@link #dispose} when closing the dialog 082 */ 083 public OsmIdSelectionDialog(Component parent, String title, String[] buttonTexts, boolean modal, boolean disposeOnClose) { 084 super(parent, title, buttonTexts, modal, disposeOnClose); 085 } 086 087 protected void init() { 088 panel.setLayout(layout); 089 layout.setAutoCreateGaps(true); 090 layout.setAutoCreateContainerGaps(true); 091 092 JLabel lbl1 = new JLabel(tr("Object type:")); 093 lbl1.setLabelFor(cbType); 094 095 cbType.addItem(trc("osm object types", "mixed")); 096 cbType.setToolTipText(tr("Choose the OSM object type")); 097 JLabel lbl2 = new JLabel(tr("Object ID:")); 098 lbl2.setLabelFor(cbId); 099 100 cbId.setEditor(new BasicComboBoxEditor() { 101 @Override 102 protected JosmTextField createEditorComponent() { 103 return tfId; 104 } 105 }); 106 cbId.setToolTipText(tr("Enter the ID of the object that should be downloaded")); 107 restorePrimitivesHistory(cbId); 108 109 // forward the enter key stroke to the download button 110 tfId.getKeymap().removeKeyStrokeBinding(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false)); 111 tfId.setPreferredSize(new Dimension(400, tfId.getPreferredSize().height)); 112 113 final String help1 = /* I18n: {0} and contains example strings not meant for translation. */ 114 tr("Object IDs can be separated by comma or space, for instance: {0}", 115 "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("1 2 5", "1,2,5")) + "</b>"); 116 final String help2 = /* I18n: {0} and contains example strings not meant for translation. {1}=n, {2}=w, {3}=r. */ 117 tr("In mixed mode, specify objects like this: {0}<br/>" 118 + "({1} stands for <i>node</i>, {2} for <i>way</i>, and {3} for <i>relation</i>)", 119 "<b>w123, n110, w12, r15</b>", "<b>n</b>", "<b>w</b>", "<b>r</b>"); 120 final String help3 = /* I18n: {0} and contains example strings not meant for translation. */ 121 tr("Ranges of object IDs are specified with a hyphen, for instance: {0}", 122 "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("w1-5", "n30-37", "r501-5")) + "</b>"); 123 HtmlPanel help = new HtmlPanel(help1 + "<br/>" + help2 + "<br/><br/>" + help3); 124 help.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); 125 126 cbType.addItemListener(e -> { 127 tfId.setType(cbType.getType()); 128 tfId.performValidation(); 129 }); 130 131 final GroupLayout.SequentialGroup sequentialGroup = layout.createSequentialGroup() 132 .addGroup(layout.createParallelGroup() 133 .addComponent(lbl1) 134 .addComponent(cbType, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)) 135 .addGroup(layout.createParallelGroup() 136 .addComponent(lbl2) 137 .addComponent(cbId, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)); 138 139 final GroupLayout.ParallelGroup parallelGroup = layout.createParallelGroup() 140 .addGroup(layout.createSequentialGroup() 141 .addGroup(layout.createParallelGroup() 142 .addComponent(lbl1) 143 .addComponent(lbl2) 144 ) 145 .addGroup(layout.createParallelGroup() 146 .addComponent(cbType) 147 .addComponent(cbId)) 148 ); 149 150 for (Component i : getComponentsBeforeHelp()) { 151 sequentialGroup.addComponent(i); 152 parallelGroup.addComponent(i); 153 } 154 155 layout.setVerticalGroup(sequentialGroup.addComponent(help)); 156 layout.setHorizontalGroup(parallelGroup.addComponent(help)); 157 } 158 159 /** 160 * Let subclasses add custom components between the id input field and the help text 161 * @return the collections to add 162 */ 163 protected Collection<Component> getComponentsBeforeHelp() { 164 return Collections.emptySet(); 165 } 166 167 /** 168 * Allows subclasses to specify a different continue button index. If this button is pressed, the history is updated. 169 * @return the button index 170 */ 171 public int getContinueButtonIndex() { 172 return 1; 173 } 174 175 /** 176 * Restore the current history from the preferences 177 * 178 * @param cbHistory the {@link HistoryComboBox} to which the history is restored to 179 */ 180 protected void restorePrimitivesHistory(HistoryComboBox cbHistory) { 181 cbHistory.setPossibleItemsTopDown( 182 Config.getPref().getList(getClass().getName() + ".primitivesHistory", Collections.emptyList())); 183 } 184 185 /** 186 * Remind the current history in the preferences 187 * 188 * @param cbHistory the {@link HistoryComboBox} of which to restore the history 189 */ 190 protected void remindPrimitivesHistory(HistoryComboBox cbHistory) { 191 cbHistory.addCurrentItemToHistory(); 192 Config.getPref().putList(getClass().getName() + ".primitivesHistory", cbHistory.getHistory()); 193 } 194 195 /** 196 * Gets the requested OSM object IDs. 197 * 198 * @return The list of requested OSM object IDs 199 */ 200 public final List<PrimitiveId> getOsmIds() { 201 return tfId.getIds(); 202 } 203 204 @Override 205 public void setupDialog() { 206 setContent(panel, false); 207 try { 208 cbType.setSelectedIndex(Config.getPref().getInt("downloadprimitive.lasttype", 0)); 209 } catch (IllegalArgumentException e) { 210 cbType.setSelectedIndex(0); 211 Logging.warn(e); 212 } 213 tfId.setType(cbType.getType()); 214 if (Config.getPref().getBoolean("downloadprimitive.autopaste", true)) { 215 tryToPasteFromClipboard(tfId, cbType); 216 } 217 setDefaultButton(getContinueButtonIndex()); 218 addWindowListener(this); 219 super.setupDialog(); 220 } 221 222 protected void tryToPasteFromClipboard(OsmIdTextField tfId, OsmPrimitiveTypesComboBox cbType) { 223 String buf = ClipboardUtils.getClipboardStringContent(); 224 if (buf == null || buf.isEmpty()) return; 225 if (buf.length() > Config.getPref().getInt("downloadprimitive.max-autopaste-length", 2000)) return; 226 final List<SimplePrimitiveId> ids = SimplePrimitiveId.fuzzyParse(buf); 227 if (!ids.isEmpty()) { 228 final String parsedText = ids.stream().map(x -> x.getType().getAPIName().charAt(0) + String.valueOf(x.getUniqueId())) 229 .collect(Collectors.joining(", ")); 230 tfId.tryToPasteFrom(parsedText); 231 final EnumSet<OsmPrimitiveType> types = ids.stream().map(SimplePrimitiveId::getType).collect( 232 Collectors.toCollection(() -> EnumSet.noneOf(OsmPrimitiveType.class))); 233 if (types.size() == 1) { 234 // select corresponding type 235 cbType.setSelectedItem(types.iterator().next()); 236 } else { 237 // select "mixed" 238 cbType.setSelectedIndex(3); 239 } 240 } else if (buf.matches("[\\d,v\\s]+")) { 241 //fallback solution for id1,id2,id3 format 242 tfId.tryToPasteFrom(buf); 243 } 244 } 245 246 @Override public void windowClosed(WindowEvent e) { 247 if (e != null && e.getComponent() == this && getValue() == getContinueButtonIndex()) { 248 Config.getPref().putInt("downloadprimitive.lasttype", cbType.getSelectedIndex()); 249 250 if (!tfId.readIds()) { 251 JOptionPane.showMessageDialog(getParent(), 252 tr("Invalid ID list specified\n" 253 + "Cannot continue."), 254 tr("Information"), 255 JOptionPane.INFORMATION_MESSAGE 256 ); 257 return; 258 } 259 260 remindPrimitivesHistory(cbId); 261 } 262 } 263 264 @Override public void windowOpened(WindowEvent e) { 265 // Do nothing 266 } 267 268 @Override public void windowClosing(WindowEvent e) { 269 // Do nothing 270 } 271 272 @Override public void windowIconified(WindowEvent e) { 273 // Do nothing 274 } 275 276 @Override public void windowDeiconified(WindowEvent e) { 277 // Do nothing 278 } 279 280 @Override public void windowActivated(WindowEvent e) { 281 // Do nothing 282 } 283 284 @Override public void windowDeactivated(WindowEvent e) { 285 // Do nothing 286 } 287}