[3518] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs.properties;
|
---|
| 3 |
|
---|
| 4 | import java.awt.Cursor;
|
---|
| 5 | import java.awt.Dimension;
|
---|
| 6 | import java.awt.Font;
|
---|
| 7 | import java.awt.GridBagLayout;
|
---|
| 8 | import java.awt.event.MouseEvent;
|
---|
| 9 | import java.awt.event.MouseListener;
|
---|
[4407] | 10 | import java.awt.font.TextAttribute;
|
---|
[3518] | 11 | import java.util.Collection;
|
---|
[4407] | 12 | import java.util.Collections;
|
---|
[3518] | 13 | import java.util.List;
|
---|
| 14 | import java.util.Map;
|
---|
| 15 |
|
---|
[4407] | 16 | import javax.swing.Action;
|
---|
| 17 | import javax.swing.Icon;
|
---|
[3518] | 18 | import javax.swing.JLabel;
|
---|
| 19 | import javax.swing.JPanel;
|
---|
| 20 |
|
---|
| 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 22 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
| 23 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
[6068] | 24 | import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
|
---|
[3518] | 25 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 26 |
|
---|
| 27 | public class PresetListPanel extends JPanel {
|
---|
| 28 |
|
---|
| 29 | public PresetListPanel() {
|
---|
| 30 | super(new GridBagLayout());
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public interface PresetHandler {
|
---|
| 34 | Collection<OsmPrimitive> getSelection();
|
---|
| 35 | void updateTags(List<Tag> tags);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Small helper class that manages the highlighting of the label on hover as well as opening
|
---|
| 40 | * the corresponding preset when clicked
|
---|
| 41 | */
|
---|
| 42 | private static class PresetLabelML implements MouseListener {
|
---|
| 43 | final JLabel label;
|
---|
[4407] | 44 | final Font hover;
|
---|
[3518] | 45 | final Font normal;
|
---|
| 46 | final TaggingPreset tag;
|
---|
| 47 | final PresetHandler presetHandler;
|
---|
| 48 |
|
---|
| 49 | PresetLabelML(JLabel lbl, TaggingPreset t, PresetHandler presetHandler) {
|
---|
| 50 | super();
|
---|
| 51 | label = lbl;
|
---|
| 52 | lbl.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
---|
| 53 | normal = label.getFont();
|
---|
[4407] | 54 | hover = normal.deriveFont(Collections.singletonMap(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_DOTTED));
|
---|
[3518] | 55 | tag = t;
|
---|
| 56 | this.presetHandler = presetHandler;
|
---|
| 57 | }
|
---|
[6084] | 58 | @Override
|
---|
[3518] | 59 | public void mouseClicked(MouseEvent arg0) {
|
---|
[4148] | 60 | Collection<OsmPrimitive> selection = tag.createSelection(presetHandler.getSelection());
|
---|
[3518] | 61 | if (selection == null || selection.isEmpty())
|
---|
| 62 | return;
|
---|
| 63 | int answer = tag.showDialog(selection, false);
|
---|
| 64 |
|
---|
| 65 | if (answer == TaggingPreset.DIALOG_ANSWER_APPLY) {
|
---|
| 66 | presetHandler.updateTags(tag.getChangedTags());
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | }
|
---|
[6084] | 70 | @Override
|
---|
[3518] | 71 | public void mouseEntered(MouseEvent arg0) {
|
---|
[4407] | 72 | label.setFont(hover);
|
---|
[3518] | 73 | }
|
---|
[6084] | 74 | @Override
|
---|
[3518] | 75 | public void mouseExited(MouseEvent arg0) {
|
---|
| 76 | label.setFont(normal);
|
---|
| 77 | }
|
---|
[6084] | 78 | @Override
|
---|
[3518] | 79 | public void mousePressed(MouseEvent arg0) {}
|
---|
[6084] | 80 | @Override
|
---|
[3518] | 81 | public void mouseReleased(MouseEvent arg0) {}
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[6068] | 84 | public void updatePresets(final Collection<TaggingPresetType> types, final Map<String, String> tags, PresetHandler presetHandler) {
|
---|
[3518] | 85 |
|
---|
| 86 | removeAll();
|
---|
[5155] | 87 | if (types.isEmpty()) {
|
---|
[3518] | 88 | setVisible(false);
|
---|
| 89 | return;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[5614] | 92 | for (TaggingPreset t : TaggingPreset.getMatchingPresets(types, tags, true)) {
|
---|
[5155] | 93 | JLabel lbl = new JLabel(t.getName() + " …");
|
---|
| 94 | lbl.setIcon((Icon) t.getValue(Action.SMALL_ICON));
|
---|
| 95 | lbl.addMouseListener(new PresetLabelML(lbl, t, presetHandler));
|
---|
| 96 | add(lbl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[3518] | 97 | }
|
---|
| 98 |
|
---|
[5155] | 99 | if (getComponentCount() > 0) {
|
---|
[3518] | 100 | setVisible(true);
|
---|
| 101 | // This ensures the presets are exactly as high as needed.
|
---|
| 102 | int height = getComponentCount() * getComponent(0).getHeight();
|
---|
| 103 | Dimension size = new Dimension(getWidth(), height);
|
---|
| 104 | setMaximumSize(size);
|
---|
| 105 | setMinimumSize(size);
|
---|
| 106 | } else {
|
---|
| 107 | setVisible(false);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|