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
112: Comparator {
113:
114: /** Indicates the sort order. */
115: private boolean ascending;
116:
117: /**
118: * Standard constructor.
119: *
120: * @param ascending a flag that controls the sort order (ascending or descending).
121: */
122: public SystemPropertyComparator(final boolean ascending) {
123: this .ascending = ascending;
124: }
125:
126: /**
127: * Compares two objects.
128: *
129: * @param o1 the first object.
130: * @param o2 the second object.
131: *
132: * @return an integer that indicates the relative order of the objects.
133: */
134: public int compare(final Object o1, final Object o2) {
135:
136: if ((o1 instanceof SystemProperty)
137: && (o2 instanceof SystemProperty)) {
138: final SystemProperty sp1 = (SystemProperty) o1;
139: final SystemProperty sp2 = (SystemProperty) o2;
140: if (this .ascending) {
141: return sp1.getName().compareTo(sp2.getName());
142: } else {
143: return sp2.getName().compareTo(sp1.getName());
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,
208: value);
209: this .properties.add(sp);
210: }
211: } catch (SecurityException se) {
212: // ignore SecurityExceptions
213: }
214:
215: Collections.sort(this .properties, new SystemPropertyComparator(
216: true));
217:
218: final String baseName = "org.jfree.ui.about.resources.AboutResources";
219: final ResourceBundle resources = ResourceBundle
220: .getBundle(baseName);
221:
222: this .nameColumnLabel = resources
223: .getString("system-properties-table.column.name");
224: this .valueColumnLabel = resources
225: .getString("system-properties-table.column.value");
226:
227: }
228:
229: /**
230: * Returns true for the first column, and false otherwise - sorting is only allowed on the
231: * first column.
232: *
233: * @param column the column index.
234: *
235: * @return true for column 0, and false for all other columns.
236: */
237: public boolean isSortable(final int column) {
238:
239: if (column == 0) {
240: return true;
241: } else {
242: return false;
243: }
244:
245: }
246:
247: /**
248: * Returns the number of rows in the table model (that is, the number of system properties).
249: *
250: * @return the row count.
251: */
252: public int getRowCount() {
253: return this .properties.size();
254: }
255:
256: /**
257: * Returns the number of columns in the table model. In this case, there are two columns: one
258: * for the property name, and one for the property value.
259: *
260: * @return the column count (always 2 in this case).
261: */
262: public int getColumnCount() {
263: return 2;
264: }
265:
266: /**
267: * Returns the name of the specified column.
268: *
269: * @param column the column index.
270: *
271: * @return the column name.
272: */
273: public String getColumnName(final int column) {
274:
275: if (column == 0) {
276: return this .nameColumnLabel;
277: } else {
278: return this .valueColumnLabel;
279: }
280:
281: }
282:
283: /**
284: * Returns the value at the specified row and column. This method supports the TableModel
285: * interface.
286: *
287: * @param row the row index.
288: * @param column the column index.
289: *
290: * @return the value.
291: */
292: public Object getValueAt(final int row, final int column) {
293:
294: final SystemProperty sp = (SystemProperty) this .properties
295: .get(row);
296: if (column == 0) {
297: return sp.getName();
298: } else {
299: if (column == 1) {
300: return sp.getValue();
301: } else {
302: return null;
303: }
304: }
305:
306: }
307:
308: /**
309: * Sorts on the specified column.
310: *
311: * @param column the column index.
312: * @param ascending a flag that controls the sort order.
313: *
314: */
315: public void sortByColumn(final int column, final boolean ascending) {
316:
317: if (isSortable(column)) {
318: super .sortByColumn(column, ascending);
319: Collections.sort(this .properties,
320: new SystemPropertyComparator(ascending));
321: }
322:
323: }
324:
325: }
|