001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2008, 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-2008, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: SystemPropertiesTableModel.java,v 1.6 2008/12/18 09:57:32 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
042     *               localisation (DG);
043     * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
044     * 18-Dec-2008 : Use ResourceBundleWrapper - see JFreeChart patch 1607918 by
045     *               Jess Thrysoee (DG);
046     *
047     */
048    
049    package org.jfree.ui.about;
050    
051    import java.util.Collections;
052    import java.util.Comparator;
053    import java.util.Iterator;
054    import java.util.List;
055    import java.util.Properties;
056    import java.util.ResourceBundle;
057    
058    import org.jfree.ui.SortableTableModel;
059    import org.jfree.util.ResourceBundleWrapper;
060    
061    /**
062     * A sortable table model containing the system properties.
063     *
064     * @author David Gilbert
065     */
066    public class SystemPropertiesTableModel extends SortableTableModel {
067    
068        /**
069         * Useful class for holding the name and value of a system property.
070         *
071         */
072        protected static class SystemProperty {
073    
074            /** The property name. */
075            private String name;
076    
077            /** The property value. */
078            private String value;
079    
080            /**
081             * Standard constructor - builds a new SystemProperty.
082             *
083             * @param name  the property name.
084             * @param value  the property value.
085             */
086            public SystemProperty(final String name, final String value) {
087                this.name = name;
088                this.value = value;
089            }
090    
091            /**
092             * Returns the property name.
093             *
094             * @return the property name.
095             */
096            public String getName() {
097                return this.name;
098            }
099    
100            /**
101             * Returns the property value.
102             *
103             * @return the property value.
104             */
105            public String getValue() {
106                return this.value;
107            }
108    
109        }
110    
111        /**
112         * A class for comparing SystemProperty objects.
113         *
114         */
115        protected static class SystemPropertyComparator implements Comparator {
116    
117            /** Indicates the sort order. */
118            private boolean ascending;
119    
120            /**
121             * Standard constructor.
122             *
123             * @param ascending  a flag that controls the sort order (ascending or
124             *                   descending).
125             */
126            public SystemPropertyComparator(final boolean ascending) {
127                this.ascending = ascending;
128            }
129    
130            /**
131             * Compares two objects.
132             *
133             * @param o1  the first object.
134             * @param o2  the second object.
135             *
136             * @return an integer that indicates the relative order of the objects.
137             */
138            public int compare(final Object o1, final Object o2) {
139    
140                if ((o1 instanceof SystemProperty)
141                        && (o2 instanceof SystemProperty)) {
142                    final SystemProperty sp1 = (SystemProperty) o1;
143                    final SystemProperty sp2 = (SystemProperty) o2;
144                    if (this.ascending) {
145                        return sp1.getName().compareTo(sp2.getName());
146                    }
147                    else {
148                        return sp2.getName().compareTo(sp1.getName());
149                    }
150                }
151                else {
152                    return 0;
153                }
154    
155            }
156    
157            /**
158             * Returns <code>true</code> if this object is equal to the specified
159             * object, and <code>false</code> otherwise.
160             *
161             * @param o  the other object.
162             *
163             * @return A boolean.
164             */
165            public boolean equals(final Object o) {
166                if (this == o) {
167                    return true;
168                }
169                if (!(o instanceof SystemPropertyComparator)) {
170                    return false;
171                }
172    
173                final SystemPropertyComparator systemPropertyComparator
174                        = (SystemPropertyComparator) o;
175    
176                if (this.ascending != systemPropertyComparator.ascending) {
177                    return false;
178                }
179    
180                return true;
181            }
182    
183            /**
184             * Returns a hash code value for the object.
185             *
186             * @return the hashcode
187             */
188            public int hashCode() {
189                return (this.ascending ? 1 : 0);
190            }
191        }
192    
193        /** Storage for the properties. */
194        private List properties;
195    
196        /** Localised name column label. */
197        private String nameColumnLabel;
198    
199        /** Localised property column label. */
200        private String valueColumnLabel;
201    
202        /**
203         * Creates a new table model using the properties of the current Java
204         * Virtual Machine.
205         */
206        public SystemPropertiesTableModel() {
207    
208            this.properties = new java.util.ArrayList();
209            try {
210                final Properties p = System.getProperties();
211                final Iterator iterator = p.keySet().iterator();
212                while (iterator.hasNext()) {
213                    final String name = (String) iterator.next();
214                        final String value = System.getProperty(name);
215                        final SystemProperty sp = new SystemProperty(name, value);
216                        this.properties.add(sp);
217                }
218            }
219            catch (SecurityException se) {
220                // ignore SecurityExceptions
221            }
222    
223            Collections.sort(this.properties, new SystemPropertyComparator(true));
224    
225            final String baseName = "org.jfree.ui.about.resources.AboutResources";
226            final ResourceBundle resources = ResourceBundleWrapper.getBundle(
227                    baseName);
228    
229            this.nameColumnLabel = resources.getString(
230                    "system-properties-table.column.name");
231            this.valueColumnLabel = resources.getString(
232                    "system-properties-table.column.value");
233    
234        }
235    
236        /**
237         * Returns true for the first column, and false otherwise - sorting is only
238         * allowed on the first column.
239         *
240         * @param column  the column index.
241         *
242         * @return true for column 0, and false for all other columns.
243         */
244        public boolean isSortable(final int column) {
245    
246            if (column == 0) {
247                return true;
248            }
249            else {
250                return false;
251            }
252    
253        }
254    
255        /**
256         * Returns the number of rows in the table model (that is, the number of
257         * system properties).
258         *
259         * @return the row count.
260         */
261        public int getRowCount() {
262            return this.properties.size();
263        }
264    
265        /**
266         * Returns the number of columns in the table model.  In this case, there
267         * are two columns: one for the property name, and one for the property
268         * value.
269         *
270         * @return the column count (always 2 in this case).
271         */
272        public int getColumnCount() {
273            return 2;
274        }
275    
276        /**
277         * Returns the name of the specified column.
278         *
279         * @param column  the column index.
280         *
281         * @return the column name.
282         */
283        public String getColumnName(final int column) {
284    
285            if (column == 0) {
286                return this.nameColumnLabel;
287            }
288            else {
289                return this.valueColumnLabel;
290            }
291    
292        }
293    
294        /**
295         * Returns the value at the specified row and column.  This method supports
296         * the TableModel interface.
297         *
298         * @param row  the row index.
299         * @param column  the column index.
300         *
301         * @return the value.
302         */
303        public Object getValueAt(final int row, final int column) {
304    
305            final SystemProperty sp = (SystemProperty) this.properties.get(row);
306            if (column == 0) {
307                return sp.getName();
308            }
309            else {
310                if (column == 1) {
311                    return sp.getValue();
312                }
313                else {
314                    return null;
315                }
316            }
317    
318        }
319    
320        /**
321         * Sorts on the specified column.
322         *
323         * @param column  the column index.
324         * @param ascending  a flag that controls the sort order.
325         *
326         */
327        public void sortByColumn(final int column, final boolean ascending) {
328    
329            if (isSortable(column)) {
330                super.sortByColumn(column, ascending);
331                Collections.sort(this.properties,
332                        new SystemPropertyComparator(ascending));
333            }
334    
335        }
336    
337    
338    }