Changeset 12683 in josm for trunk/src/org


Ignore:
Timestamp:
2017-08-28T14:44:23+02:00 (7 years ago)
Author:
Don-vip
Message:

see #15182 - transfer GUI dependencies from tools.TextTagParser to gui.datatransfer.importers.TextTagPaster

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/datatransfer/importers/TextTagPaster.java

    r12620 r12683  
    33
    44import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
     5import static org.openstreetmap.josm.tools.I18n.tr;
    56
     7import java.awt.GridBagLayout;
    68import java.awt.datatransfer.DataFlavor;
    79import java.awt.datatransfer.UnsupportedFlavorException;
     
    911import java.util.Map;
    1012
     13import javax.swing.JLabel;
     14import javax.swing.JOptionPane;
     15import javax.swing.JPanel;
    1116import javax.swing.TransferHandler.TransferSupport;
    1217
     18import org.openstreetmap.josm.Main;
     19import org.openstreetmap.josm.gui.ExtendedDialog;
     20import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
     21import org.openstreetmap.josm.gui.help.HelpUtil;
     22import org.openstreetmap.josm.gui.widgets.UrlLabel;
     23import org.openstreetmap.josm.io.XmlWriter;
     24import org.openstreetmap.josm.tools.GBC;
     25import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
    1326import org.openstreetmap.josm.tools.Logging;
    1427import org.openstreetmap.josm.tools.TextTagParser;
     28import org.openstreetmap.josm.tools.TextTagParser.TagWarningCallback;
    1529
    1630/**
     
    4761        Map<String, String> tags = getTagsImpl(support);
    4862        if (tags.isEmpty()) {
    49             TextTagParser.showBadBufferMessage(HELP);
     63            showBadBufferMessage(HELP);
    5064            throw new IOException("Invalid tags to paste.");
    5165        }
    52         if (!TextTagParser.validateTags(tags)) {
     66        if (!TextTagParser.validateTags(tags, TextTagPaster::warning)) {
    5367            throw new IOException("Tags to paste are not valid.");
    5468        }
     
    5973        return TextTagParser.readTagsFromText((String) support.getTransferable().getTransferData(df));
    6074    }
     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    }
    61135}
  • trunk/src/org/openstreetmap/josm/tools/TextTagParser.java

    r12382 r12683  
    55import static org.openstreetmap.josm.tools.I18n.trn;
    66
    7 import java.awt.GridBagLayout;
    87import java.util.Arrays;
    98import java.util.HashMap;
     
    1312import java.util.regex.Pattern;
    1413
    15 import javax.swing.JLabel;
    16 import javax.swing.JOptionPane;
    17 import javax.swing.JPanel;
    18 
    1914import org.openstreetmap.josm.Main;
    20 import org.openstreetmap.josm.gui.ExtendedDialog;
    21 import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
    22 import org.openstreetmap.josm.gui.help.HelpUtil;
    23 import org.openstreetmap.josm.gui.widgets.UrlLabel;
    24 import org.openstreetmap.josm.io.XmlWriter;
    25 import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
    2615
    2716/**
     
    211200     * Gets a list of tags that are in the given text
    212201     * @param buf The text to parse
     202     * @param callback warning callback
    213203     * @return The tags or <code>null</code> if the tags are not valid
    214      */
    215     public static Map<String, String> getValidatedTagsFromText(String buf) {
     204     * @since 12683
     205     */
     206    public static Map<String, String> getValidatedTagsFromText(String buf, TagWarningCallback callback) {
    216207        Map<String, String> tags = readTagsFromText(buf);
    217         return validateTags(tags) ? tags : null;
     208        return validateTags(tags, callback) ? tags : null;
    218209    }
    219210
     
    258249     * Check tags for correctness and display warnings if needed
    259250     * @param tags - map key-&gt;value to check
     251     * @param callback warning callback
    260252     * @return true if the tags should be pasted
    261      */
    262     public static boolean validateTags(Map<String, String> tags) {
     253     * @since 12683
     254     */
     255    public static boolean validateTags(Map<String, String> tags, TagWarningCallback callback) {
    263256        int r;
    264257        int s = tags.size();
    265258        if (s > MAX_KEY_COUNT) {
    266259            // Use trn() even if for english it makes no sense, as s > 30
    267             r = warning(trn("There was {0} tag found in the buffer, it is suspicious!",
     260            r = callback.warning(trn("There was {0} tag found in the buffer, it is suspicious!",
    268261            "There were {0} tags found in the buffer, it is suspicious!", s,
    269262            s), "", "tags.paste.toomanytags");
     
    274267            String value = entry.getValue();
    275268            if (key.length() > MAX_KEY_LENGTH) {
    276                 r = warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+'='+value, "tags.paste.keytoolong");
     269                r = callback.warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+'='+value, "tags.paste.keytoolong");
    277270                if (r == 2 || r == 3) return false; if (r == 4) return true;
    278271            }
    279272            if (!key.matches(KEY_PATTERN)) {
    280                 r = warning(tr("Suspicious characters in key:"), key, "tags.paste.keydoesnotmatch");
     273                r = callback.warning(tr("Suspicious characters in key:"), key, "tags.paste.keydoesnotmatch");
    281274                if (r == 2 || r == 3) return false; if (r == 4) return true;
    282275            }
    283276            if (value.length() > MAX_VALUE_LENGTH) {
    284                 r = warning(tr("Value is too long (max {0} characters):", MAX_VALUE_LENGTH), value, "tags.paste.valuetoolong");
     277                r = callback.warning(tr("Value is too long (max {0} characters):", MAX_VALUE_LENGTH), value, "tags.paste.valuetoolong");
    285278                if (r == 2 || r == 3) return false; if (r == 4) return true;
    286279            }
     
    289282    }
    290283
    291     private static int warning(String text, String data, String code) {
    292         ExtendedDialog ed = new ExtendedDialog(
    293                     Main.parent,
    294                     tr("Do you want to paste these tags?"),
    295                     tr("Ok"), tr("Cancel"), tr("Clear buffer"), tr("Ignore warnings"));
    296         ed.setButtonIcons("ok", "cancel", "dialogs/delete", "pastetags");
    297         ed.setContent("<html><b>"+text + "</b><br/><br/><div width=\"300px\">"+XmlWriter.encode(data, true)+"</html>");
    298         ed.setDefaultButton(2);
    299         ed.setCancelButton(2);
    300         ed.setIcon(JOptionPane.WARNING_MESSAGE);
    301         ed.toggleEnable(code);
    302         ed.showDialog();
    303         int r = ed.getValue();
    304         if (r == 0) r = 2;
    305         // clean clipboard if user asked
    306         if (r == 3) ClipboardUtils.copyString("");
    307         return r;
    308     }
    309 
    310     /**
    311      * Shows message that the buffer can not be pasted, allowing user to clean the buffer
    312      * @param helpTopic the help topic of the parent action
    313      * TODO: Replace by proper HelpAwareOptionPane instead of self-made help link
    314      */
    315     public static void showBadBufferMessage(String helpTopic) {
    316         String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object"
    317             + " or suitable text. </p></html>");
    318         JPanel p = new JPanel(new GridBagLayout());
    319         p.add(new JLabel(msg), GBC.eop());
    320         String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(helpTopic, LocaleType.DEFAULT));
    321         if (helpUrl != null) {
    322             p.add(new UrlLabel(helpUrl), GBC.eop());
    323         }
    324 
    325         ExtendedDialog ed = new ExtendedDialog(
    326                     Main.parent,
    327                     tr("Warning"),
    328                     tr("Ok"), tr("Clear buffer"))
    329             .setButtonIcons("ok", "dialogs/delete")
    330             .setContent(p)
    331             .setDefaultButton(1)
    332             .setCancelButton(1)
    333             .setIcon(JOptionPane.WARNING_MESSAGE)
    334             .toggleEnable("tags.paste.cleanbadbuffer");
    335 
    336         ed.showDialog();
    337 
    338         // clean clipboard if user asked
    339         if (ed.getValue() == 2) ClipboardUtils.copyString("");
     284    /**
     285     * Called when a problematic tag is encountered.
     286     * @since 12683
     287     */
     288    @FunctionalInterface
     289    public interface TagWarningCallback {
     290        /**
     291         * Displays a warning about a problematic tag and ask user what to do about it.
     292         * @param text Message to display
     293         * @param data Tag key and/or value
     294         * @param code to use with {@code ExtendedDialog#toggleEnable(String)}
     295         * @return 1 to validate and display next warnings if any, 2 to cancel operation, 3 to clear buffer, 4 to paste tags
     296         */
     297        int warning(String text, String data, String code);
    340298    }
    341299}
Note: See TracChangeset for help on using the changeset viewer.