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-2005, 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-2004, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: SystemPropertiesTableModel.java,v 1.5 2005/10/18 13:19:13 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 localisation (DG);
  42:  * 08-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  43:  *
  44:  */
  45: 
  46: package org.jfree.ui.about;
  47: 
  48: import java.util.Collections;
  49: import java.util.Comparator;
  50: import java.util.Iterator;
  51: import java.util.List;
  52: import java.util.Properties;
  53: import java.util.ResourceBundle;
  54: 
  55: import org.jfree.ui.SortableTableModel;
  56: 
  57: /**
  58:  * A sortable table model containing the system properties.
  59:  *
  60:  * @author David Gilbert
  61:  */
  62: public class SystemPropertiesTableModel extends SortableTableModel {
  63: 
  64:     /**
  65:      * Useful class for holding the name and value of a system property.
  66:      *
  67:      */
  68:     protected static class SystemProperty {
  69: 
  70:         /** The property name. */
  71:         private String name;
  72: 
  73:         /** The property value. */
  74:         private String value;
  75: 
  76:         /**
  77:          * Standard constructor - builds a new SystemProperty.
  78:          *
  79:          * @param name  the property name.
  80:          * @param value  the property value.
  81:          */
  82:         public SystemProperty(final String name, final String value) {
  83:                 this.name = name;
  84:                 this.value = value;
  85:         }
  86: 
  87:         /**
  88:          * Returns the property name.
  89:          *
  90:          * @return the property name.
  91:          */
  92:         public String getName() {
  93:             return this.name;
  94:         }
  95: 
  96:         /**
  97:          * Returns the property value.
  98:          *
  99:          * @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: }