1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets.items;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.MouseAdapter;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Optional;
|
---|
11 |
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
17 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
18 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetLabel;
|
---|
19 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
20 | import org.openstreetmap.josm.tools.GBC;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Adds a link to an other preset.
|
---|
24 | * @since 8863
|
---|
25 | */
|
---|
26 | public class PresetLink extends TextItem {
|
---|
27 |
|
---|
28 | static final class TaggingPresetMouseAdapter extends MouseAdapter {
|
---|
29 | private final TaggingPreset t;
|
---|
30 |
|
---|
31 | TaggingPresetMouseAdapter(TaggingPreset t) {
|
---|
32 | this.t = t;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void mouseClicked(MouseEvent e) {
|
---|
37 | t.actionPerformed(null);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /** The exact name of the preset to link to. Required. */
|
---|
42 | public String preset_name = ""; // NOSONAR
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Creates a label to be inserted aboive this link
|
---|
46 | * @return a label
|
---|
47 | */
|
---|
48 | public JLabel createLabel() {
|
---|
49 | initializeLocaleText(tr("Edit also …"));
|
---|
50 | return new JLabel(locale_text);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
55 | final String presetName = preset_name;
|
---|
56 | Optional<TaggingPreset> found = TaggingPresets.getTaggingPresets().stream().filter(preset -> presetName.equals(preset.name)).findFirst();
|
---|
57 | if (found.isPresent()) {
|
---|
58 | TaggingPreset t = found.get();
|
---|
59 | JLabel lbl = new TaggingPresetLabel(t);
|
---|
60 | lbl.addMouseListener(new TaggingPresetMouseAdapter(t));
|
---|
61 | p.add(lbl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
62 | }
|
---|
63 | return false;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void addCommands(List<Tag> changedTags) {
|
---|
68 | // Do nothing
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public String toString() {
|
---|
73 | return "PresetLink [preset_name=" + preset_name + ']';
|
---|
74 | }
|
---|
75 | }
|
---|