Ticket #7671: 7671v3.patch

File 7671v3.patch, 10.5 KB (added by Don-vip, 13 years ago)
  • core/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

     
    66
    77import java.awt.BorderLayout;
    88import java.awt.Component;
     9import java.awt.Cursor;
     10import java.awt.Dialog.ModalityType;
    911import java.awt.Dimension;
    1012import java.awt.Font;
     13import java.awt.GridBagConstraints;
    1114import java.awt.GridBagLayout;
    1215import java.awt.Point;
    1316import java.awt.Toolkit;
    14 import java.awt.Dialog.ModalityType;
    1517import java.awt.datatransfer.Clipboard;
    1618import java.awt.datatransfer.Transferable;
    1719import java.awt.event.ActionEvent;
    1820import java.awt.event.ActionListener;
    1921import java.awt.event.FocusAdapter;
    2022import java.awt.event.FocusEvent;
    21 import java.awt.event.InputEvent;
    2223import java.awt.event.KeyEvent;
    2324import java.awt.event.MouseAdapter;
    2425import java.awt.event.MouseEvent;
     26import java.awt.image.BufferedImage;
    2527import java.net.HttpURLConnection;
    2628import java.net.URI;
    2729import java.net.URLEncoder;
     
    3436import java.util.HashMap;
    3537import java.util.HashSet;
    3638import java.util.Iterator;
     39import java.util.LinkedHashMap;
    3740import java.util.LinkedList;
    3841import java.util.List;
    3942import java.util.Map;
     43import java.util.Map.Entry;
    4044import java.util.Set;
    4145import java.util.TreeMap;
    4246import java.util.TreeSet;
    4347import java.util.Vector;
    44 import java.util.Map.Entry;
    4548
    4649import javax.swing.AbstractAction;
    4750import javax.swing.Action;
    4851import javax.swing.Box;
    4952import javax.swing.DefaultListCellRenderer;
    50 import javax.swing.InputMap;
     53import javax.swing.ImageIcon;
    5154import javax.swing.JComboBox;
    5255import javax.swing.JComponent;
    5356import javax.swing.JDialog;
     
    6164import javax.swing.JTable;
    6265import javax.swing.KeyStroke;
    6366import javax.swing.ListSelectionModel;
    64 import javax.swing.SwingUtilities;
    6567import javax.swing.event.ListSelectionEvent;
    6668import javax.swing.event.ListSelectionListener;
    6769import javax.swing.event.PopupMenuListener;
     
    9294import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
    9395import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
    9496import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
    95 import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    9697import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
     98import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    9799import org.openstreetmap.josm.gui.DefaultNameFormatter;
    98100import org.openstreetmap.josm.gui.ExtendedDialog;
    99101import org.openstreetmap.josm.gui.MapFrame;
     
    104106import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
    105107import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
    106108import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     109import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    107110import org.openstreetmap.josm.gui.tagging.TaggingPreset;
    108111import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
    109112import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
     
    457460
    458461    private static String lastAddKey = null;
    459462    private static String lastAddValue = null;
     463    // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
     464    private static final Map<Tag, Void> recentTags = new LinkedHashMap<Tag, Void>(5, 1.1f, true);
     465   
    460466    /**
    461467     * Open the add selection dialog and add a new key/value to the table (and
    462468     * to the dataset, of course).
     
    472478        }
    473479        if (sel.isEmpty()) return;
    474480
    475         JPanel p = new JPanel(new BorderLayout());
     481        JPanel p = new JPanel(new GridBagLayout());
    476482        p.add(new JLabel("<html>"+trn("This will change up to {0} object.",
    477483                "This will change up to {0} objects.", sel.size(),sel.size())
    478                 +"<br><br>"+tr("Please select a key")), BorderLayout.NORTH);
     484                +"<br><br>"+tr("Please select a key")), GBC.eol());
    479485        final AutoCompletingComboBox keys = new AutoCompletingComboBox();
    480486        AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
    481487        List<AutoCompletionListItem> keyList = autocomplete.getKeys();
     
    503509        keys.setPossibleACItems(keyList);
    504510        keys.setEditable(true);
    505511
    506         p.add(keys, BorderLayout.CENTER);
     512        p.add(keys, GBC.eop().fill());
    507513
    508         JPanel p2 = new JPanel(new BorderLayout());
    509         p.add(p2, BorderLayout.SOUTH);
    510         p2.add(new JLabel(tr("Please select a value")), BorderLayout.NORTH);
     514        p.add(new JLabel(tr("Please select a value")), GBC.eol());
    511515        final AutoCompletingComboBox values = new AutoCompletingComboBox();
    512516        values.setEditable(true);
    513         p2.add(values, BorderLayout.CENTER);
     517        p.add(values, GBC.eop().fill());
    514518        if (itemToSelect != null) {
    515519            keys.setSelectedItem(itemToSelect);
    516520            /* don't add single chars, as they are no properly selected */
     
    518522                values.setSelectedItem(lastAddValue);
    519523            }
    520524        }
     525       
     526        if (!recentTags.isEmpty()) {
     527            p.add(new JLabel(tr("Recently added tags")), GBC.eol());
     528        }
     529       
     530        List<JosmAction> recentTagsActions = new ArrayList<JosmAction>();
     531
     532        int count = 1;
     533        for (final Tag t : recentTags.keySet()) {
     534            // Find and display icon
     535            ImageIcon icon = MapPaintStyles.getNodeIcon(t);
     536            if (icon == null) {
     537                icon = new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB));
     538            }
     539            GridBagConstraints gbc = new GridBagConstraints();
     540            gbc.ipadx = 5;
     541            p.add(new JLabel(icon), gbc);
     542            // Create action for reusing the tag, with keyboard shortcut Ctrl+(1-5)
     543            String actionShortcutKey = "properties:recent:"+count;
     544            Shortcut sc = Shortcut.registerShortcut(actionShortcutKey, null, KeyEvent.VK_0+count, Shortcut.CTRL);
     545            final JosmAction action = new JosmAction(actionShortcutKey, null, tr("Use this tag again"), sc, false) {
     546                @Override
     547                public void actionPerformed(ActionEvent e) {
     548                    keys.getEditor().setItem(t.getKey());
     549                    values.getEditor().setItem(t.getValue());
     550                }
     551            };
     552            p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), actionShortcutKey);
     553            p.getActionMap().put(actionShortcutKey, action);
     554            recentTagsActions.add(action);
     555            // Display clickable tag
     556            final JLabel tagLabel = new JLabel("<html>"
     557                + "<style>td{border:1px solid gray; font-weight:normal;}</style>"
     558                + "<table><tr><td>" + t.toString() + "</td></tr></table></html>");
     559            tagLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
     560            tagLabel.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
     561            tagLabel.addMouseListener(new MouseAdapter() {
     562                @Override
     563                public void mouseClicked(MouseEvent e) {
     564                    action.actionPerformed(null);
     565                }
     566            });
     567            p.add(tagLabel, GBC.eol());
     568            count++;
     569        }
    521570
    522571        FocusAdapter focus = addFocusAdapter(-1, keys, values, autocomplete, defaultACItemComparator);
    523572        // fire focus event in advance or otherwise the popup list will be too small at first
     
    541590        JDialog dialog = pane.createDialog(Main.parent, tr("Add value?"));
    542591        dialog.setModalityType(ModalityType.DOCUMENT_MODAL);
    543592        dialog.setVisible(true);
     593       
     594        for (JosmAction action : recentTagsActions) {
     595            action.destroy();
     596        }
    544597
    545598        if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    546599            return;
     
    550603            return;
    551604        lastAddKey = key;
    552605        lastAddValue = value;
     606        recentTags.put(new Tag(key, value), null);
    553607        Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
    554608        btnAdd.requestFocusInWindow();
    555609    }
    556 
     610   
    557611    /**
    558612     * @param allData
    559613     * @param keys
  • core/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

     
    1010import java.util.ArrayList;
    1111import java.util.Arrays;
    1212import java.util.Collection;
     13import java.util.Iterator;
    1314import java.util.LinkedList;
    1415import java.util.List;
    1516import java.util.concurrent.CopyOnWriteArrayList;
     
    1819import javax.swing.SwingUtilities;
    1920
    2021import org.openstreetmap.josm.Main;
     22import org.openstreetmap.josm.data.osm.Node;
     23import org.openstreetmap.josm.data.osm.Tag;
    2124import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     25import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
    2226import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
    2327import org.openstreetmap.josm.gui.mappaint.xml.XmlStyleSource;
    2428import org.openstreetmap.josm.gui.preferences.SourceEntry;
     
    118122                .setArchive(source.zipIcons)
    119123                .setOptional(true).get();
    120124    }
     125   
     126    public static ImageIcon getNodeIcon(Tag tag) {
     127        if (tag != null) {
     128            Node virtualNode = new Node();
     129            virtualNode.put(tag.getKey(), tag.getValue());
     130            StyleList styleList = getStyles().generateStyles(virtualNode, 0, null, false).a;
     131            if (styleList != null) {
     132                for (Iterator<ElemStyle> it = styleList.iterator(); it.hasNext(); ) {
     133                    ElemStyle style = styleList.iterator().next();
     134                    if (style instanceof NodeElemStyle) {
     135                        MapImage mapImage = ((NodeElemStyle) style).mapImage;
     136                        if (mapImage != null) {
     137                            return new ImageIcon(mapImage.getImage());
     138                        }
     139                    }
     140                }
     141            }
     142        }
     143        return null;
     144    }
    121145
    122146    public static List<String> getIconSourceDirs(StyleSource source) {
    123147        List<String> dirs = new LinkedList<String>();