Source for org.jfree.ui.about.SystemPropertiesTableModel

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it
  10:  * under the terms of the GNU Lesser General Public License as published by
  11:  * the Free Software Foundation; either version 2.1 of the License, or
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  22:  * USA.
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------------------
  28:  * SystemPropertiesTableModel.java
  29:  * -------------------------------
  30:  * (C) Copyright 2000-2008, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: SystemPropertiesTableModel.java,v 1.6 2008/12/18 09:57:32 mungady Exp $
  36:  *
  37:  * Changes (from 26-Oct-2001)
  38:  * --------------------------
  39:  * 26-Oct-2001 : Changed package to com.jrefinery.ui (DG);
  40:  * 28-Feb-2001 : Changed package to com.jrefinery.ui.about (DG);
  41:  * 15-Mar-2002 : Modified to use a ResourceBundle for elements that require
  42:  *               localisation (DG);
  43:  * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  44:  * 18-Dec-2008 : Use ResourceBundleWrapper - see JFreeChart patch 1607918 by
  45:  *               Jess Thrysoee (DG);
  46:  *
  47:  */
  48: 
  49: package org.jfree.ui.about;
  50: 
  51: import java.util.Collections;
  52: import java.util.Comparator;
  53: import java.util.Iterator;
  54: import java.util.List;
  55: import java.util.Properties;
  56: import java.util.ResourceBundle;
  57: 
  58: import org.jfree.ui.SortableTableModel;
  59: import org.jfree.util.ResourceBundleWrapper;
  60: 
  61: /**
  62:  * A sortable table model containing the system properties.
  63:  *
  64:  * @author David Gilbert
  65:  */
  66: public class SystemPropertiesTableModel extends SortableTableModel {
  67: 
  68:     /**
  69:      * Useful class for holding the name and value of a system property.
  70:      *
  71:      */
  72:     protected static class SystemProperty {
  73: 
  74:         /** The property name. */
  75:         private String name;
  76: 
  77:         /** The property value. */
  78:         private String value;
  79: 
  80:         /**
  81:          * Standard constructor - builds a new SystemProperty.
  82:          *
  83:          * @param name  the property name.
  84:          * @param value  the property value.
  85:          */
  86:         public SystemProperty(final String name, final String value) {
  87:             this.name = name;
  88:             this.value = value;
  89:         }
  90: 
  91:         /**
  92:          * Returns the property name.
  93:          *
  94:          * @return the property name.
  95:          */
  96:         public String getName() {
  97:             return this.name;
  98:         }
  99: 
 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: }