001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trc;
006import static org.openstreetmap.josm.tools.I18n.trn;
007
008import java.awt.Component;
009import java.awt.Dimension;
010import java.awt.GridBagLayout;
011import java.awt.Insets;
012import java.awt.event.ActionEvent;
013import java.io.File;
014import java.util.ArrayList;
015import java.util.Collection;
016import java.util.Collections;
017import java.util.EnumSet;
018import java.util.HashSet;
019import java.util.LinkedList;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023import java.util.concurrent.CompletableFuture;
024import java.util.function.Predicate;
025import java.util.stream.Collectors;
026
027import javax.swing.AbstractAction;
028import javax.swing.Action;
029import javax.swing.ImageIcon;
030import javax.swing.JLabel;
031import javax.swing.JOptionPane;
032import javax.swing.JPanel;
033import javax.swing.JToggleButton;
034import javax.swing.SwingUtilities;
035
036import org.openstreetmap.josm.actions.AdaptableAction;
037import org.openstreetmap.josm.command.ChangePropertyCommand;
038import org.openstreetmap.josm.command.Command;
039import org.openstreetmap.josm.command.SequenceCommand;
040import org.openstreetmap.josm.data.UndoRedoHandler;
041import org.openstreetmap.josm.data.osm.DataSet;
042import org.openstreetmap.josm.data.osm.IPrimitive;
043import org.openstreetmap.josm.data.osm.OsmData;
044import org.openstreetmap.josm.data.osm.OsmDataManager;
045import org.openstreetmap.josm.data.osm.OsmPrimitive;
046import org.openstreetmap.josm.data.osm.Relation;
047import org.openstreetmap.josm.data.osm.RelationMember;
048import org.openstreetmap.josm.data.osm.Tag;
049import org.openstreetmap.josm.data.osm.search.SearchCompiler;
050import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
051import org.openstreetmap.josm.data.osm.search.SearchParseError;
052import org.openstreetmap.josm.gui.ExtendedDialog;
053import org.openstreetmap.josm.gui.MainApplication;
054import org.openstreetmap.josm.gui.Notification;
055import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
056import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
057import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
058import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
059import org.openstreetmap.josm.gui.tagging.presets.items.Key;
060import org.openstreetmap.josm.gui.tagging.presets.items.Link;
061import org.openstreetmap.josm.gui.tagging.presets.items.Optional;
062import org.openstreetmap.josm.gui.tagging.presets.items.PresetLink;
063import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
064import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role;
065import org.openstreetmap.josm.gui.tagging.presets.items.Space;
066import org.openstreetmap.josm.gui.util.GuiHelper;
067import org.openstreetmap.josm.spi.preferences.Config;
068import org.openstreetmap.josm.tools.GBC;
069import org.openstreetmap.josm.tools.ImageProvider;
070import org.openstreetmap.josm.tools.Logging;
071import org.openstreetmap.josm.tools.Utils;
072import org.openstreetmap.josm.tools.template_engine.ParseError;
073import org.openstreetmap.josm.tools.template_engine.TemplateEntry;
074import org.openstreetmap.josm.tools.template_engine.TemplateParser;
075import org.xml.sax.SAXException;
076
077/**
078 * This class read encapsulate one tagging preset. A class method can
079 * read in all predefined presets, either shipped with JOSM or that are
080 * in the config directory.
081 *
082 * It is also able to construct dialogs out of preset definitions.
083 * @since 294
084 */
085public class TaggingPreset extends AbstractAction implements ActiveLayerChangeListener, AdaptableAction, Predicate<IPrimitive> {
086
087    public static final int DIALOG_ANSWER_APPLY = 1;
088    public static final int DIALOG_ANSWER_NEW_RELATION = 2;
089    public static final int DIALOG_ANSWER_CANCEL = 3;
090
091    public static final String OPTIONAL_TOOLTIP_TEXT = "Optional tooltip text";
092
093    /** Prefix of preset icon loading failure error message */
094    public static final String PRESET_ICON_ERROR_MSG_PREFIX = "Could not get presets icon ";
095
096    /**
097     * The preset group this preset belongs to.
098     */
099    public TaggingPresetMenu group;
100
101    /**
102     * The name of the tagging preset.
103     * @see #getRawName()
104     */
105    public String name;
106    /**
107     * The icon name assigned to this preset.
108     */
109    public String iconName;
110    public String name_context;
111    /**
112     * A cache for the local name. Should never be accessed directly.
113     * @see #getLocaleName()
114     */
115    public String locale_name;
116    public boolean preset_name_label;
117
118    /**
119     * The types as preparsed collection.
120     */
121    public transient Set<TaggingPresetType> types;
122    public final transient List<TaggingPresetItem> data = new LinkedList<>();
123    public transient Roles roles;
124    public transient TemplateEntry nameTemplate;
125    public transient Match nameTemplateFilter;
126
127    /**
128     * True whenever the original selection given into createSelection was empty
129     */
130    private boolean originalSelectionEmpty;
131
132    /** The completable future task of asynchronous icon loading */
133    private CompletableFuture<Void> iconFuture;
134
135    /**
136     * Create an empty tagging preset. This will not have any items and
137     * will be an empty string as text. createPanel will return null.
138     * Use this as default item for "do not select anything".
139     */
140    public TaggingPreset() {
141        MainApplication.getLayerManager().addActiveLayerChangeListener(this);
142        updateEnabledState();
143    }
144
145    /**
146     * Change the display name without changing the toolbar value.
147     */
148    public void setDisplayName() {
149        putValue(Action.NAME, getName());
150        putValue("toolbar", "tagging_" + getRawName());
151        putValue(OPTIONAL_TOOLTIP_TEXT, group != null ?
152                tr("Use preset ''{0}'' of group ''{1}''", getLocaleName(), group.getName()) :
153                    tr("Use preset ''{0}''", getLocaleName()));
154    }
155
156    /**
157     * Gets the localized version of the name
158     * @return The name that should be displayed to the user.
159     */
160    public String getLocaleName() {
161        if (locale_name == null) {
162            if (name_context != null) {
163                locale_name = trc(name_context, TaggingPresetItem.fixPresetString(name));
164            } else {
165                locale_name = tr(TaggingPresetItem.fixPresetString(name));
166            }
167        }
168        return locale_name;
169    }
170
171    /**
172     * Returns the translated name of this preset, prefixed with the group names it belongs to.
173     * @return the translated name of this preset, prefixed with the group names it belongs to
174     */
175    public String getName() {
176        return group != null ? group.getName() + '/' + getLocaleName() : getLocaleName();
177    }
178
179    /**
180     * Returns the non translated name of this preset, prefixed with the (non translated) group names it belongs to.
181     * @return the non translated name of this preset, prefixed with the (non translated) group names it belongs to
182     */
183    public String getRawName() {
184        return group != null ? group.getRawName() + '/' + name : name;
185    }
186
187    /**
188     * Returns the preset icon (16px).
189     * @return The preset icon, or {@code null} if none defined
190     * @since 6403
191     */
192    public final ImageIcon getIcon() {
193        return getIcon(Action.SMALL_ICON);
194    }
195
196    /**
197     * Returns the preset icon (16 or 24px).
198     * @param key Key determining icon size: {@code Action.SMALL_ICON} for 16x, {@code Action.LARGE_ICON_KEY} for 24px
199     * @return The preset icon, or {@code null} if none defined
200     * @since 10849
201     */
202    public final ImageIcon getIcon(String key) {
203        Object icon = getValue(key);
204        if (icon instanceof ImageIcon) {
205            return (ImageIcon) icon;
206        }
207        return null;
208    }
209
210    /**
211     * Called from the XML parser to set the icon.
212     * The loading task is performed in the background in order to speedup startup.
213     * @param iconName icon name
214     */
215    public void setIcon(final String iconName) {
216        this.iconName = iconName;
217        if (iconName == null || !TaggingPresetReader.isLoadIcons()) {
218            return;
219        }
220        File arch = TaggingPresetReader.getZipIcons();
221        final Collection<String> s = Config.getPref().getList("taggingpreset.icon.sources", null);
222        this.iconFuture = new ImageProvider(iconName)
223            .setDirs(s)
224            .setId("presets")
225            .setArchive(arch)
226            .setOptional(true)
227            .getResourceAsync(result -> {
228                if (result != null) {
229                    GuiHelper.runInEDT(() -> {
230                        try {
231                            result.attachImageIcon(this);
232                        } catch (IllegalArgumentException e) {
233                            Logging.warn(toString() + ": " + PRESET_ICON_ERROR_MSG_PREFIX + iconName);
234                            Logging.warn(e);
235                        }
236                    });
237                } else {
238                    Logging.warn(toString() + ": " + PRESET_ICON_ERROR_MSG_PREFIX + iconName);
239                }
240            });
241    }
242
243    /**
244     * Called from the XML parser to set the types this preset affects.
245     * @param types comma-separated primitive types ("node", "way", "relation" or "closedway")
246     * @throws SAXException if any SAX error occurs
247     * @see TaggingPresetType#fromString
248     */
249    public void setType(String types) throws SAXException {
250        this.types = TaggingPresetItem.getType(types);
251    }
252
253    public void setName_template(String pattern) throws SAXException {
254        try {
255            this.nameTemplate = new TemplateParser(pattern).parse();
256        } catch (ParseError e) {
257            Logging.error("Error while parsing " + pattern + ": " + e.getMessage());
258            throw new SAXException(e);
259        }
260    }
261
262    public void setName_template_filter(String filter) throws SAXException {
263        try {
264            this.nameTemplateFilter = SearchCompiler.compile(filter);
265        } catch (SearchParseError e) {
266            Logging.error("Error while parsing" + filter + ": " + e.getMessage());
267            throw new SAXException(e);
268        }
269    }
270
271    private static class PresetPanel extends JPanel {
272        private boolean hasElements;
273
274        PresetPanel() {
275            super(new GridBagLayout());
276        }
277    }
278
279    /**
280     * Returns the tags being directly applied (without UI element) by {@link Key} items
281     *
282     * @return a list of tags
283     */
284    private List<Tag> getDirectlyAppliedTags() {
285        List<Tag> tags = new ArrayList<>();
286        for (TaggingPresetItem item : data) {
287            if (item instanceof Key) {
288                tags.add(((Key) item).asTag());
289            }
290        }
291        return tags;
292    }
293
294    /**
295     * Creates a panel for this preset. This includes general information such as name and supported {@link TaggingPresetType types}.
296     * This includes the elements from the individual {@link TaggingPresetItem items}.
297     *
298     * @param selected the selected primitives
299     * @return the newly created panel
300     */
301    public PresetPanel createPanel(Collection<OsmPrimitive> selected) {
302        PresetPanel p = new PresetPanel();
303        List<Link> l = new LinkedList<>();
304        List<PresetLink> presetLink = new LinkedList<>();
305
306        final JPanel pp = new JPanel();
307        if (types != null) {
308            for (TaggingPresetType t : types) {
309                JLabel la = new JLabel(ImageProvider.get(t.getIconName()));
310                la.setToolTipText(tr("Elements of type {0} are supported.", tr(t.getName())));
311                pp.add(la);
312            }
313        }
314        final List<Tag> directlyAppliedTags = getDirectlyAppliedTags();
315        if (!directlyAppliedTags.isEmpty()) {
316            final JLabel label = new JLabel(ImageProvider.get("pastetags"));
317            label.setToolTipText("<html>" + tr("This preset also sets: {0}", Utils.joinAsHtmlUnorderedList(directlyAppliedTags)));
318            pp.add(label);
319        }
320        final int count = pp.getComponentCount();
321        if (preset_name_label) {
322            p.add(new JLabel(getIcon(Action.LARGE_ICON_KEY)), GBC.std(0, 0).span(1, count > 0 ? 2 : 1).insets(0, 0, 5, 0));
323        }
324        if (count > 0) {
325            p.add(pp, GBC.std(1, 0).span(GBC.REMAINDER));
326        }
327        if (preset_name_label) {
328            p.add(new JLabel(getName()), GBC.std(1, count > 0 ? 1 : 0).insets(5, 0, 0, 0).span(GBC.REMAINDER).fill(GBC.HORIZONTAL));
329        }
330
331        boolean presetInitiallyMatches = !selected.isEmpty() && selected.stream().allMatch(this);
332        JPanel items = new JPanel(new GridBagLayout());
333        for (TaggingPresetItem i : data) {
334            if (i instanceof Link) {
335                l.add((Link) i);
336                p.hasElements = true;
337            } else if (i instanceof PresetLink) {
338                presetLink.add((PresetLink) i);
339            } else {
340                if (i.addToPanel(items, selected, presetInitiallyMatches)) {
341                    p.hasElements = true;
342                }
343            }
344        }
345        p.add(items, GBC.eol().fill());
346        if (selected.isEmpty() && !supportsRelation()) {
347            GuiHelper.setEnabledRec(items, false);
348        }
349
350        // add PresetLink
351        if (!presetLink.isEmpty()) {
352            p.add(new JLabel(tr("Edit also …")), GBC.eol().insets(0, 8, 0, 0));
353            for (PresetLink link : presetLink) {
354                link.addToPanel(p, selected, presetInitiallyMatches);
355            }
356        }
357
358        // add Link
359        for (Link link : l) {
360            link.addToPanel(p, selected, presetInitiallyMatches);
361        }
362
363        // "Add toolbar button"
364        JToggleButton tb = new JToggleButton(new ToolbarButtonAction());
365        tb.setFocusable(false);
366        p.add(tb, GBC.std(1, 0).anchor(GBC.LINE_END));
367        return p;
368    }
369
370    /**
371     * Determines whether a dialog can be shown for this preset, i.e., at least one tag can/must be set by the user.
372     *
373     * @return {@code true} if a dialog can be shown for this preset
374     */
375    public boolean isShowable() {
376        for (TaggingPresetItem i : data) {
377            if (!(i instanceof Optional || i instanceof Space || i instanceof Key))
378                return true;
379        }
380        return false;
381    }
382
383    public String suggestRoleForOsmPrimitive(OsmPrimitive osm) {
384        if (roles != null && osm != null) {
385            for (Role i : roles.roles) {
386                if (i.memberExpression != null && i.memberExpression.match(osm)
387                        && (i.types == null || i.types.isEmpty() || i.types.contains(TaggingPresetType.forPrimitive(osm)))) {
388                    return i.key;
389                }
390            }
391        }
392        return null;
393    }
394
395    @Override
396    public void actionPerformed(ActionEvent e) {
397        DataSet ds = OsmDataManager.getInstance().getEditDataSet();
398        Collection<OsmPrimitive> participants = Collections.emptyList();
399        if (ds != null) {
400            participants = ds.getSelected();
401        }
402
403        // Display dialog even if no data layer (used by preset-tagging-tester plugin)
404        Collection<OsmPrimitive> sel = createSelection(participants);
405        int answer = showDialog(sel, supportsRelation());
406
407        if (ds == null) {
408            return;
409        }
410
411        if (!sel.isEmpty() && answer == DIALOG_ANSWER_APPLY) {
412            Command cmd = createCommand(sel, getChangedTags());
413            if (cmd != null) {
414                UndoRedoHandler.getInstance().add(cmd);
415            }
416        } else if (answer == DIALOG_ANSWER_NEW_RELATION) {
417            final Relation r = new Relation();
418            final Collection<RelationMember> members = new HashSet<>();
419            for (Tag t : getChangedTags()) {
420                r.put(t.getKey(), t.getValue());
421            }
422            for (OsmPrimitive osm : ds.getSelected()) {
423                String role = suggestRoleForOsmPrimitive(osm);
424                RelationMember rm = new RelationMember(role == null ? "" : role, osm);
425                r.addMember(rm);
426                members.add(rm);
427            }
428            SwingUtilities.invokeLater(() -> RelationEditor.getEditor(
429                    MainApplication.getLayerManager().getEditLayer(), r, members).setVisible(true));
430        }
431        ds.setSelected(ds.getSelected()); // force update
432    }
433
434    private static class PresetDialog extends ExtendedDialog {
435
436        /**
437         * Constructs a new {@code PresetDialog}.
438         * @param content the content that will be displayed in this dialog
439         * @param title the text that will be shown in the window titlebar
440         * @param icon the image to be displayed as the icon for this window
441         * @param disableApply whether to disable "Apply" button
442         * @param showNewRelation whether to display "New relation" button
443         */
444        PresetDialog(Component content, String title, ImageIcon icon, boolean disableApply, boolean showNewRelation) {
445            super(MainApplication.getMainFrame(), title,
446                    showNewRelation ?
447                            (new String[] {tr("Apply Preset"), tr("New relation"), tr("Cancel")}) :
448                            (new String[] {tr("Apply Preset"), tr("Cancel")}),
449                    true);
450            if (icon != null)
451                setIconImage(icon.getImage());
452            contentInsets = new Insets(10, 5, 0, 5);
453            if (showNewRelation) {
454                setButtonIcons("ok", "dialogs/addrelation", "cancel");
455            } else {
456                setButtonIcons("ok", "cancel");
457            }
458            setContent(content);
459            setDefaultButton(1);
460            setupDialog();
461            buttons.get(0).setEnabled(!disableApply);
462            buttons.get(0).setToolTipText(title);
463            // Prevent dialogs of being too narrow (fix #6261)
464            Dimension d = getSize();
465            if (d.width < 350) {
466                d.width = 350;
467                setSize(d);
468            }
469            super.showDialog();
470        }
471    }
472
473    /**
474     * Shows the preset dialog.
475     * @param sel selection
476     * @param showNewRelation whether to display "New relation" button
477     * @return the user choice after the dialog has been closed
478     */
479    public int showDialog(Collection<OsmPrimitive> sel, boolean showNewRelation) {
480        PresetPanel p = createPanel(sel);
481
482        int answer = 1;
483        boolean canCreateRelation = types == null || types.contains(TaggingPresetType.RELATION);
484        if (originalSelectionEmpty && !canCreateRelation) {
485            new Notification(
486                    tr("The preset <i>{0}</i> cannot be applied since nothing has been selected!", getLocaleName()))
487                    .setIcon(JOptionPane.WARNING_MESSAGE)
488                    .show();
489            return DIALOG_ANSWER_CANCEL;
490        } else if (sel.isEmpty() && !canCreateRelation) {
491            new Notification(
492                    tr("The preset <i>{0}</i> cannot be applied since the selection is unsuitable!", getLocaleName()))
493                    .setIcon(JOptionPane.WARNING_MESSAGE)
494                    .show();
495            return DIALOG_ANSWER_CANCEL;
496        } else if (p.getComponentCount() != 0 && (sel.isEmpty() || p.hasElements)) {
497            int size = sel.size();
498            String title = trn("Change {0} object", "Change {0} objects", size, size);
499            if (!showNewRelation && size == 0) {
500                if (originalSelectionEmpty) {
501                    title = tr("Nothing selected!");
502                } else {
503                    title = tr("Selection unsuitable!");
504                }
505            }
506
507            boolean disableApply = size == 0;
508            if (!disableApply) {
509                OsmData<?, ?, ?, ?> ds = sel.iterator().next().getDataSet();
510                disableApply = ds != null && ds.isLocked();
511            }
512            answer = new PresetDialog(p, title, preset_name_label ? null : (ImageIcon) getValue(Action.SMALL_ICON),
513                    disableApply, showNewRelation).getValue();
514        }
515        if (!showNewRelation && answer == 2)
516            return DIALOG_ANSWER_CANCEL;
517        else
518            return answer;
519    }
520
521    /**
522     * Removes all unsuitable OsmPrimitives from the given list
523     * @param participants List of possible OsmPrimitives to tag
524     * @return Cleaned list with suitable OsmPrimitives only
525     */
526    public Collection<OsmPrimitive> createSelection(Collection<OsmPrimitive> participants) {
527        originalSelectionEmpty = participants.isEmpty();
528        return participants.stream().filter(this::typeMatches).collect(Collectors.toList());
529    }
530
531    /**
532     * Gets a list of tags that are set by this preset.
533     * @return The list of tags.
534     */
535    public List<Tag> getChangedTags() {
536        List<Tag> result = new ArrayList<>();
537        data.forEach(i -> i.addCommands(result));
538        return result;
539    }
540
541    /**
542     * Create a command to change the given list of tags.
543     * @param sel The primitives to change the tags for
544     * @param changedTags The tags to change
545     * @return A command that changes the tags.
546     */
547    public static Command createCommand(Collection<OsmPrimitive> sel, List<Tag> changedTags) {
548        List<Command> cmds = new ArrayList<>();
549        for (Tag tag: changedTags) {
550            ChangePropertyCommand cmd = new ChangePropertyCommand(sel, tag.getKey(), tag.getValue());
551            if (cmd.getObjectsNumber() > 0) {
552                cmds.add(cmd);
553            }
554        }
555
556        if (cmds.isEmpty())
557            return null;
558        else if (cmds.size() == 1)
559            return cmds.get(0);
560        else
561            return new SequenceCommand(tr("Change Tags"), cmds);
562    }
563
564    private boolean supportsRelation() {
565        return types == null || types.contains(TaggingPresetType.RELATION);
566    }
567
568    protected final void updateEnabledState() {
569        setEnabled(OsmDataManager.getInstance().getEditDataSet() != null);
570    }
571
572    @Override
573    public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
574        updateEnabledState();
575    }
576
577    @Override
578    public String toString() {
579        return (types == null ? "" : types.toString()) + ' ' + name;
580    }
581
582    /**
583     * Determines whether this preset matches the OSM primitive type.
584     * @param primitive The OSM primitive for which type must match
585     * @return <code>true</code> if type matches.
586     * @since 15640
587     */
588    public final boolean typeMatches(IPrimitive primitive) {
589        return typeMatches(EnumSet.of(TaggingPresetType.forPrimitive(primitive)));
590    }
591
592    /**
593     * Determines whether this preset matches the types.
594     * @param t The types that must match
595     * @return <code>true</code> if all types match.
596     */
597    public boolean typeMatches(Collection<TaggingPresetType> t) {
598        return t == null || types == null || types.containsAll(t);
599    }
600
601    /**
602     * Determines whether this preset matches the given primitive, i.e.,
603     * whether the {@link #typeMatches(Collection) type matches} and the {@link TaggingPresetItem#matches(Map) tags match}.
604     *
605     * @param p the primitive
606     * @return {@code true} if this preset matches the primitive
607     * @since 13623 (signature)
608     */
609    @Override
610    public boolean test(IPrimitive p) {
611        return matches(EnumSet.of(TaggingPresetType.forPrimitive(p)), p.getKeys(), false);
612    }
613
614    /**
615     * Determines whether this preset matches the parameters.
616     *
617     * @param t the preset types to include, see {@link #typeMatches(Collection)}
618     * @param tags the tags to perform matching on, see {@link TaggingPresetItem#matches(Map)}
619     * @param onlyShowable whether the preset must be {@link #isShowable() showable}
620     * @return {@code true} if this preset matches the parameters.
621     */
622    public boolean matches(Collection<TaggingPresetType> t, Map<String, String> tags, boolean onlyShowable) {
623        if ((onlyShowable && !isShowable()) || !typeMatches(t)) {
624            return false;
625        } else {
626            return TaggingPresetItem.matches(data, tags);
627        }
628    }
629
630    /**
631     * Action that adds or removes the button on main toolbar
632     */
633    public class ToolbarButtonAction extends AbstractAction {
634        private final int toolbarIndex;
635
636        /**
637         * Constructs a new {@code ToolbarButtonAction}.
638         */
639        public ToolbarButtonAction() {
640            super("");
641            new ImageProvider("dialogs", "pin").getResource().attachImageIcon(this, true);
642            putValue(SHORT_DESCRIPTION, tr("Add or remove toolbar button"));
643            List<String> t = new LinkedList<>(ToolbarPreferences.getToolString());
644            toolbarIndex = t.indexOf(getToolbarString());
645            putValue(SELECTED_KEY, toolbarIndex >= 0);
646        }
647
648        @Override
649        public void actionPerformed(ActionEvent ae) {
650            String res = getToolbarString();
651            MainApplication.getToolbar().addCustomButton(res, toolbarIndex, true);
652        }
653    }
654
655    /**
656     * Gets a string describing this preset that can be used for the toolbar
657     * @return A String that can be passed on to the toolbar
658     * @see ToolbarPreferences#addCustomButton(String, int, boolean)
659     */
660    public String getToolbarString() {
661        ToolbarPreferences.ActionParser actionParser = new ToolbarPreferences.ActionParser(null);
662        return actionParser.saveAction(new ToolbarPreferences.ActionDefinition(this));
663    }
664
665    /**
666     * Returns the completable future task that performs icon loading, if any.
667     * @return the completable future task that performs icon loading, or null
668     * @since 14449
669     */
670    public CompletableFuture<Void> getIconLoadingTask() {
671        return iconFuture;
672    }
673}