1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer.importers;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.datatransfer.DataFlavor;
|
---|
9 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.TransferHandler.TransferSupport;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
20 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
21 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
22 | import org.openstreetmap.josm.gui.widgets.UrlLabel;
|
---|
23 | import org.openstreetmap.josm.io.XmlWriter;
|
---|
24 | import org.openstreetmap.josm.tools.GBC;
|
---|
25 | import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
|
---|
26 | import org.openstreetmap.josm.tools.Logging;
|
---|
27 | import org.openstreetmap.josm.tools.TextTagParser;
|
---|
28 | import org.openstreetmap.josm.tools.TextTagParser.TagWarningCallback;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * This transfer support allows us to import tags from the text that was copied to the clipboard.
|
---|
32 | * @author Michael Zangl
|
---|
33 | * @since 10604
|
---|
34 | */
|
---|
35 | public final class TextTagPaster extends AbstractTagPaster {
|
---|
36 | private static final String HELP = ht("/Action/PasteTags");
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Create a new {@link TextTagPaster}
|
---|
40 | */
|
---|
41 | public TextTagPaster() {
|
---|
42 | super(DataFlavor.stringFlavor);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public boolean supports(TransferSupport support) {
|
---|
47 | try {
|
---|
48 | return super.supports(support) && containsValidTags(support);
|
---|
49 | } catch (UnsupportedFlavorException | IOException e) {
|
---|
50 | Logging.warn(e);
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | private boolean containsValidTags(TransferSupport support) throws UnsupportedFlavorException, IOException {
|
---|
56 | return !getTagsImpl(support).isEmpty();
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | protected Map<String, String> getTags(TransferSupport support) throws UnsupportedFlavorException, IOException {
|
---|
61 | Map<String, String> tags = getTagsImpl(support);
|
---|
62 | if (tags.isEmpty()) {
|
---|
63 | showBadBufferMessage(HELP);
|
---|
64 | throw new IOException("Invalid tags to paste.");
|
---|
65 | }
|
---|
66 | if (!TextTagParser.validateTags(tags, TextTagPaster::warning)) {
|
---|
67 | throw new IOException("Tags to paste are not valid.");
|
---|
68 | }
|
---|
69 | return tags;
|
---|
70 | }
|
---|
71 |
|
---|
72 | private Map<String, String> getTagsImpl(TransferSupport support) throws UnsupportedFlavorException, IOException {
|
---|
73 | return TextTagParser.readTagsFromText((String) support.getTransferable().getTransferData(df));
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Default {@link TagWarningCallback} implementation.
|
---|
78 | * Displays a warning about a problematic tag and ask user what to do about it.
|
---|
79 | * @param text Message to display
|
---|
80 | * @param data Tag key and/or value
|
---|
81 | * @param code to use with {@code ExtendedDialog#toggleEnable(String)}
|
---|
82 | * @return 1 to validate and display next warnings if any, 2 to cancel operation, 3 to clear buffer, 4 to paste tags
|
---|
83 | * @since 12683
|
---|
84 | */
|
---|
85 | public static int warning(String text, String data, String code) {
|
---|
86 | ExtendedDialog ed = new ExtendedDialog(
|
---|
87 | Main.parent,
|
---|
88 | tr("Do you want to paste these tags?"),
|
---|
89 | tr("Ok"), tr("Cancel"), tr("Clear buffer"), tr("Ignore warnings"));
|
---|
90 | ed.setButtonIcons("ok", "cancel", "dialogs/delete", "pastetags");
|
---|
91 | ed.setContent("<html><b>"+text + "</b><br/><br/><div width=\"300px\">"+XmlWriter.encode(data, true)+"</html>");
|
---|
92 | ed.setDefaultButton(2);
|
---|
93 | ed.setCancelButton(2);
|
---|
94 | ed.setIcon(JOptionPane.WARNING_MESSAGE);
|
---|
95 | ed.toggleEnable(code);
|
---|
96 | ed.showDialog();
|
---|
97 | int r = ed.getValue();
|
---|
98 | if (r == 0) r = 2;
|
---|
99 | // clean clipboard if user asked
|
---|
100 | if (r == 3) ClipboardUtils.copyString("");
|
---|
101 | return r;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Shows message that the buffer can not be pasted, allowing user to clean the buffer
|
---|
106 | * @param helpTopic the help topic of the parent action
|
---|
107 | * TODO: Replace by proper HelpAwareOptionPane instead of self-made help link
|
---|
108 | */
|
---|
109 | public static void showBadBufferMessage(String helpTopic) {
|
---|
110 | String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object"
|
---|
111 | + " or suitable text. </p></html>");
|
---|
112 | JPanel p = new JPanel(new GridBagLayout());
|
---|
113 | p.add(new JLabel(msg), GBC.eop());
|
---|
114 | String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(helpTopic, LocaleType.DEFAULT));
|
---|
115 | if (helpUrl != null) {
|
---|
116 | p.add(new UrlLabel(helpUrl), GBC.eop());
|
---|
117 | }
|
---|
118 |
|
---|
119 | ExtendedDialog ed = new ExtendedDialog(
|
---|
120 | Main.parent,
|
---|
121 | tr("Warning"),
|
---|
122 | tr("Ok"), tr("Clear buffer"))
|
---|
123 | .setButtonIcons("ok", "dialogs/delete")
|
---|
124 | .setContent(p)
|
---|
125 | .setDefaultButton(1)
|
---|
126 | .setCancelButton(1)
|
---|
127 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
128 | .toggleEnable("tags.paste.cleanbadbuffer");
|
---|
129 |
|
---|
130 | ed.showDialog();
|
---|
131 |
|
---|
132 | // clean clipboard if user asked
|
---|
133 | if (ed.getValue() == 2) ClipboardUtils.copyString("");
|
---|
134 | }
|
---|
135 | }
|
---|