    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * -------------------------------
    028     * SystemPropertiesTableModel.java
    029     * -------------------------------
    030     * (C) Copyright 2000-2004, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: SystemPropertiesTableModel.java,v 1.5 2005/10/18 13:19:13 mungady Exp $
    036     *
    037     * Changes (from 26-Oct-2001)
    038     * --------------------------
    039     * 26-Oct-2001 : Changed package to com.jrefinery.ui (DG);
    040     * 28-Feb-2001 : Changed package to com.jrefinery.ui.about (DG);
    041     * 15-Mar-2002 : Modified to use a ResourceBundle for elements that require localisation (DG);
    042     * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    043     *
    044     */
    045
    046    package org.jfree.ui.about;
    047
    048    import java.util.Collections;
    049    import java.util.Comparator;
    050    import java.util.Iterator;
    051    import java.util.List;
    052    import java.util.Properties;
    053    import java.util.ResourceBundle;
    054
    055    import org.jfree.ui.SortableTableModel;
    056
    057    /**
    058     * A sortable table model containing the system properties.
    059     *
    060     * @author David Gilbert
    061     */
    062    public class SystemPropertiesTableModel extends SortableTableModel {
    063
    064        /**
    065         * Useful class for holding the name and value of a system property.
    066         *
    067         */
    068        protected static class SystemProperty {
    069
    070            /** The property name. */
    071            private String name;
    072
    073            /** The property value. */
    074            private String value;
    075
    076            /**
    077             * Standard constructor - builds a new SystemProperty.
    078             *
    079             * @param name  the property name.
    080             * @param value  the property value.
    081             */
    082            public SystemProperty(final String name, final String value) {
    083                    this.name = name;
    084                    this.value = value;
    085            }
    086
    087            /**
    088             * Returns the property name.
    089             *
    090             * @return the property name.
    091             */
    092            public String getName() {
    093                return this.name;
    094            }
    095
    096            /**
    097             * Returns the property value.
    098             *
    099             * @return the property value.
    100             */
    101            public String getValue() {
    102                return this.value;
    103            }
    104
    105        }
    106
    107        /**
    108         * A class for comparing SystemProperty objects.
    109         *
    110         */
    111        protected static class SystemPropertyComparator implements Comparator {
    112
    113            /** Indicates the sort order. */
    114            private boolean ascending;
    115
    116            /**
    117             * Standard constructor.
    118             *
    119             * @param ascending  a flag that controls the sort order (ascending or descending).
    120             */
    121            public SystemPropertyComparator(final boolean ascending) {
    122                this.ascending = ascending;
    123            }
    124
    125            /**
    126             * Compares two objects.
    127             *
    128             * @param o1  the first object.
    129             * @param o2  the second object.
    130             *
    131             * @return an integer that indicates the relative order of the objects.
    132             */
    133            public int compare(final Object o1, final Object o2) {
    134
    135                if ((o1 instanceof SystemProperty) && (o2 instanceof SystemProperty)) {
    136                    final SystemProperty sp1 = (SystemProperty) o1;
    137                    final SystemProperty sp2 = (SystemProperty) o2;
    138                    if (this.ascending) {
    139                        return sp1.getName().compareTo(sp2.getName());
    140                    }
    141                    else {
    142                        return sp2.getName().compareTo(sp1.getName());
    143                    }
    144                }
    145                else {
    146                    return 0;
    147                }
    148
    149            }
    150
    151            /**
    152             * Returns <code>true</code> if this object is equal to the specified object, and
    153             * <code>false</code> otherwise.
    154             *
    155             * @param o  the other object.
    156             *
    157             * @return A boolean.
    158             */
    159            public boolean equals(final Object o) {
    160                if (this == o) {
    161                    return true;
    162                }
    163                if (!(o instanceof SystemPropertyComparator)) {
    164                    return false;
    165                }
    166
    167                final SystemPropertyComparator systemPropertyComparator = (SystemPropertyComparator) o;
    168
    169                if (this.ascending != systemPropertyComparator.ascending) {
    170                    return false;
    171                }
    172
    173                return true;
    174            }
    175
    176            /**
    177             * Returns a hash code value for the object.
    178             *
    179             * @return the hashcode
    180             */
    181            public int hashCode() {
    182                return (this.ascending ? 1 : 0);
    183            }
    184        }
    185
    186        /** Storage for the properties. */
    187        private List properties;
    188
    189        /** Localised name column label. */
    190        private String nameColumnLabel;
    191
    192        /** Localised property column label. */
    193        private String valueColumnLabel;
    194
    195        /**
    196         * Creates a new table model using the properties of the current Java Virtual Machine.
    197         */
    198        public SystemPropertiesTableModel() {
    199
    200            this.properties = new java.util.ArrayList();
    201            try {
    202                final Properties p = System.getProperties();
    203                final Iterator iterator = p.keySet().iterator();
    204                while (iterator.hasNext()) {
    205                    final String name = (String) iterator.next();
    206                        final String value = System.getProperty(name);
    207                        final SystemProperty sp = new SystemProperty(name, value);
    208                        this.properties.add(sp);
    209                }
    210            }
    211            catch (SecurityException se) {
    212                // ignore SecurityExceptions
    213            }
    214
    215            Collections.sort(this.properties, new SystemPropertyComparator(true));
    216
    217            final String baseName = "org.jfree.ui.about.resources.AboutResources";
    218            final ResourceBundle resources = ResourceBundle.getBundle(baseName);
    219
    220            this.nameColumnLabel = resources.getString("system-properties-table.column.name");
    221            this.valueColumnLabel = resources.getString("system-properties-table.column.value");
    222
    223        }
    224
    225        /**
    226         * Returns true for the first column, and false otherwise - sorting is only allowed on the
    227         * first column.
    228         *
    229         * @param column  the column index.
    230         *
    231         * @return true for column 0, and false for all other columns.
    232         */
    233        public boolean isSortable(final int column) {
    234
    235            if (column == 0) {
    236                return true;
    237            }
    238            else {
    239                return false;
    240            }
    241
    242        }
    243
    244        /**
    245         * Returns the number of rows in the table model (that is, the number of system properties).
    246         *
    247         * @return the row count.
    248         */
    249        public int getRowCount() {
    250            return this.properties.size();
    251        }
    252
    253        /**
    254         * Returns the number of columns in the table model.  In this case, there are two columns: one
    255         * for the property name, and one for the property value.
    256         *
    257         * @return the column count (always 2 in this case).
    258         */
    259        public int getColumnCount() {
    260            return 2;
    261        }
    262
    263        /**
    264         * Returns the name of the specified column.
    265         *
    266         * @param column  the column index.
    267         *
    268         * @return the column name.
    269         */
    270        public String getColumnName(final int column) {
    271
    272            if (column == 0) {
    273                return this.nameColumnLabel;
    274            }
    275            else {
    276                return this.valueColumnLabel;
    277            }
    278
    279        }
    280
    281        /**
    282         * Returns the value at the specified row and column.  This method supports the TableModel
    283         * interface.
    284         *
    285         * @param row  the row index.
    286         * @param column  the column index.
    287         *
    288         * @return the value.
    289         */
    290        public Object getValueAt(final int row, final int column) {
    291
    292            final SystemProperty sp = (SystemProperty) this.properties.get(row);
    293            if (column == 0) {
    294                return sp.getName();
    295            }
    296            else {
    297                if (column == 1) {
    298                    return sp.getValue();
    299                }
    300                else {
    301                    return null;
    302                }
    303            }
    304
    305        }
    306
    307        /**
    308         * Sorts on the specified column.
    309         *
    310         * @param column  the column index.
    311         * @param ascending  a flag that controls the sort order.
    312         *
    313         */
    314        public void sortByColumn(final int column, final boolean ascending) {
    315
    316            if (isSortable(column)) {
    317                super.sortByColumn(column, ascending);
    318                Collections.sort(this.properties, new SystemPropertyComparator(ascending));
    319            }
    320
    321        }
    322
    323
    324    }