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 | private final Collection<OsmPrimitive> sel;
|
---|
31 |
|
---|
32 | TaggingPresetMouseAdapter(TaggingPreset t, Collection<OsmPrimitive> sel) {
|
---|
33 | this.t = t;
|
---|
34 | this.sel = sel;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void mouseClicked(MouseEvent e) {
|
---|
39 | t.showAndApply(sel);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | /** The exact name of the preset to link to. Required. */
|
---|
44 | public String preset_name = ""; // NOSONAR
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Creates a label to be inserted aboive this link
|
---|
48 | * @return a label
|
---|
49 | */
|
---|
50 | public JLabel createLabel() {
|
---|
51 | initializeLocaleText(tr("Edit also …"));
|
---|
52 | return new JLabel(locale_text);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
57 | final String presetName = preset_name;
|
---|
58 | Optional<TaggingPreset> found = TaggingPresets.getTaggingPresets().stream().filter(preset -> presetName.equals(preset.name)).findFirst();
|
---|
59 | if (found.isPresent()) {
|
---|
60 | TaggingPreset t = found.get();
|
---|
61 | JLabel lbl = new TaggingPresetLabel(t);
|
---|
62 | lbl.addMouseListener(new TaggingPresetMouseAdapter(t, sel));
|
---|
63 | p.add(lbl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
64 | }
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void addCommands(List<Tag> changedTags) {
|
---|
70 | // Do nothing
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public String toString() {
|
---|
75 | return "PresetLink [preset_name=" + preset_name + ']';
|
---|
76 | }
|
---|
77 | }
|
---|