Changeset 8903 in josm for trunk/src/org


Ignore:
Timestamp:
2015-10-18T23:57:31+02:00 (4 years ago)
Author:
Don-vip
Message:

fix #11968 - Show node icons from style in selection list and relation editor (patch by Augustus Kling)

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/OsmPrimitivRenderer.java

    r8510 r8903  
    33
    44import java.awt.Component;
     5import java.awt.Rectangle;
    56
    67import javax.swing.DefaultListCellRenderer;
     
    6768     * provided that it's a kind of JLabel.
    6869     * @param def the rendering component
    69      * @param value the OsmPrimtive to render
     70     * @param value the OsmPrimitive to render
    7071     * @return the modified rendering component
    7172     */
     
    7374        if (value != null && def instanceof JLabel) {
    7475            ((JLabel) def).setText(getComponentText(value));
    75             ImageIcon icon = ImageProvider.get(value.getDisplayType());
     76            ImageIcon icon = ImageProvider.getPadded(value,
     77                // Height of component no yet known, assume the default 16px.
     78                new Rectangle(16, 16));
    7679            if (icon != null) {
    7780                ((JLabel) def).setIcon(icon);
     
    8891     * provided that it's a kind of JLabel.
    8992     * @param def the rendering component
    90      * @param value the HistoryOsmPrimtive to render
     93     * @param value the HistoryOsmPrimitive to render
    9194     * @return the modified rendering component
    9295     */
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableMemberCellRenderer.java

    r6476 r8903  
    33
    44import java.awt.Component;
     5import java.awt.Rectangle;
    56
    67import javax.swing.JTable;
     
    1011import org.openstreetmap.josm.tools.ImageProvider;
    1112
     13/**
     14 * "Refers to" column in relation editor's member list.
     15 */
    1216public class MemberTableMemberCellRenderer extends MemberTableCellRenderer {
    1317
     18        /**
     19         * Constructs a new {@code MemberTableMemberCellRenderer}.
     20         */
    1421    public MemberTableMemberCellRenderer() {
    1522        super();
    1623    }
    1724
    18     protected void renderPrimitive(OsmPrimitive primitive) {
    19         setIcon(ImageProvider.get(primitive.getDisplayType()));
     25    protected void renderPrimitive(OsmPrimitive primitive, Rectangle cellSize) {
     26        // Make icon the full height of the table cell. Icon background is square.
     27        setIcon(ImageProvider.getPadded(primitive, cellSize));
    2028        setText(primitive.getDisplayName(DefaultNameFormatter.getInstance()));
    2129        setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(primitive));
     
    3038            return this;
    3139
     40        Rectangle cellSize = table.getCellRect(row, column, false);
     41
    3242        renderForeground(isSelected);
    3343        OsmPrimitive primitive = (OsmPrimitive) value;
    3444        renderBackground(getModel(table), primitive, isSelected);
    35         renderPrimitive(primitive);
     45        renderPrimitive(primitive, cellSize);
    3646        return this;
    3747    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/SelectionTableCellRenderer.java

    r8855 r8903  
    44import java.awt.Color;
    55import java.awt.Component;
     6import java.awt.Rectangle;
    67
    78import javax.swing.JLabel;
     
    5859    }
    5960
    60     protected void renderPrimitive(OsmPrimitive primitive) {
    61         setIcon(ImageProvider.get(primitive.getDisplayType()));
     61    protected void renderPrimitive(OsmPrimitive primitive, Rectangle cellSize) {
     62        setIcon(ImageProvider.getPadded(primitive, cellSize));
    6263        setText(primitive.getDisplayName(DefaultNameFormatter.getInstance()));
    6364        setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(primitive));
     
    7273            return this;
    7374
     75        Rectangle cellSize = table.getCellRect(row, column, false);
     76
    7477        renderBackground((OsmPrimitive) value);
    75         renderPrimitive((OsmPrimitive) value);
     78        renderPrimitive((OsmPrimitive) value, cellSize);
    7679        return this;
    7780    }
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r8846 r8903  
    1212import java.awt.Image;
    1313import java.awt.Point;
     14import java.awt.Rectangle;
    1415import java.awt.RenderingHints;
    1516import java.awt.Toolkit;
     
    5657
    5758import org.openstreetmap.josm.Main;
     59import org.openstreetmap.josm.data.osm.OsmPrimitive;
    5860import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     61import org.openstreetmap.josm.gui.mappaint.ElemStyle;
     62import org.openstreetmap.josm.gui.mappaint.MapImage;
     63import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
     64import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
     65import org.openstreetmap.josm.gui.mappaint.Range;
     66import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList;
    5967import org.openstreetmap.josm.io.CachedFile;
    6068import org.openstreetmap.josm.plugins.PluginHandler;
     
    12661274        CheckParameterUtil.ensureParameterNotNull(type, "type");
    12671275        return get("data", type.getAPIName());
     1276    }
     1277
     1278    /**
     1279     * @param primitive Object for which an icon shall be fetched. The icon is chosen based on tags.
     1280     * @param iconSize Target size of icon. Icon is padded if required.
     1281     * @return Icon for {@code primitive} that fits in cell.
     1282     * @since 8903
     1283     */
     1284    public static ImageIcon getPadded(OsmPrimitive primitive, Rectangle iconSize) {
     1285        // Check if the current styles have special icon for tagged nodes.
     1286        if (primitive instanceof org.openstreetmap.josm.data.osm.Node) {
     1287            Pair<StyleList, Range> nodeStyles = MapPaintStyles.getStyles().generateStyles(primitive, 100, false);
     1288            for (ElemStyle style : nodeStyles.a) {
     1289                if (style instanceof NodeElemStyle) {
     1290                    NodeElemStyle nodeStyle = (NodeElemStyle) style;
     1291                    MapImage icon = nodeStyle.mapImage;
     1292                    if (icon != null) {
     1293                        int backgroundWidth = iconSize.height;
     1294                        int backgroundHeight = iconSize.height;
     1295                        int iconWidth = icon.getWidth();
     1296                        int iconHeight = icon.getHeight();
     1297                        BufferedImage image = new BufferedImage(backgroundWidth, backgroundHeight,
     1298                                BufferedImage.TYPE_INT_ARGB);
     1299                        double scaleFactor = Math.min(backgroundWidth / (double) iconWidth, backgroundHeight
     1300                                / (double) iconHeight);
     1301                        BufferedImage iconImage = icon.getImage(false);
     1302                        Image scaledIcon;
     1303                        final int scaledWidth;
     1304                        final int scaledHeight;
     1305                        if (scaleFactor < 1) {
     1306                            // Scale icon such that it fits on background.
     1307                            scaledWidth = (int) (iconWidth * scaleFactor);
     1308                            scaledHeight = (int) (iconHeight * scaleFactor);
     1309                            scaledIcon = iconImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
     1310                        } else {
     1311                            // Use original size, don't upscale.
     1312                            scaledWidth = iconWidth;
     1313                            scaledHeight = iconHeight;
     1314                            scaledIcon = iconImage;
     1315                        }
     1316                        image.getGraphics().drawImage(scaledIcon, (backgroundWidth - scaledWidth) / 2,
     1317                                (backgroundHeight - scaledHeight) / 2, null);
     1318
     1319                        return new ImageIcon(image);
     1320                    }
     1321                }
     1322            }
     1323        }
     1324
     1325        // Use generic default icon.
     1326        return ImageProvider.get(primitive.getDisplayType());
    12681327    }
    12691328
Note: See TracChangeset for help on using the changeset viewer.