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

* Copyright (c) 2002-2018 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 Peter Jacobs. * @author Alexander Verbraeck. * @author Niels Lang. * @since 1.5 */ public class ArrayRenderer extends DefaultTableCellRenderer { /** */ private static final long serialVersionUID = 20140831L; /** * A LABEL is returned, preventing users from editing the array contents directly. * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, * java.lang.Object, boolean, boolean, int, int) */ @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Class clasz = value.getClass().getComponentType(); String text = "[] of " + getShortName(clasz) + ": "; try { for (int i = 0; i < Array.getLength(value); i++) text += Array.get(value, i) + "; "; } catch (Exception e) { // Unfortunately, we now have nothing more than: text += "?"; } return new JLabel(text); } /** * @param clasz a class * @return the short name of the class */ private static String getShortName(final Class clasz) { String name = clasz.getName(); return name.substring(name.lastIndexOf('.') + 1); } }