source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPreset.java@ 16280

Last change on this file since 16280 was 16280, checked in by simon04, 4 years ago

fix #19022 - PresetLink: allow to customize label text and UI position

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