/* * Created on Aug 26, 2004 @ Erasmus University Rotterdam Copyright (c) Delft * University of Technology */ package nl.tudelft.simulation.dsol.swing.introspection.mapping; import java.awt.Component; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; /** * Renders Map values in a human-readable manner. *

* Copyright (c) 2018-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See * for project information https://simulation.tudelft.nl. The DSOL * project is distributed under a three-clause BSD-style license, which can be found at * * https://simulation.tudelft.nl/dsol/3.0/license.html. *

* @author Alexander Verbraeck. */ public class MapRenderer extends DefaultTableCellRenderer { /** */ private static final long serialVersionUID = 20140831L; /** {@inheritDoc} */ @Override public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) { Map map = new LinkedHashMap((Map) value); String content = "Map of <"; if (map.size() > 0) { Object key = map.keySet().iterator().next(); content += getShortName(key.getClass()) + "," + getShortName(map.get(key).getClass()); } else { content += "?,?"; } content += ">: "; Iterator> entries = map.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = entries.next(); content += "{" + entry.getKey().toString() + "," + entry.getValue().toString() + "}; "; } return new JLabel(content); } /** * Returns the short name of a class. * @param clasz Class<?>; the class * @return the short name */ private static String getShortName(final Class clasz) { String name = clasz.getName(); return name.substring(name.lastIndexOf('.') + 1); } }