/*
 * @(#)UIDefaultsTableModel.java 1.3.2  2010-11-24
 *
 * Copyright (c) 2001-2006 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.debug;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

/**
 * Table model for UIDefaults.
 *
 * @author  Werner Randelshofer
 * @version 1.3.2 2010-11-24 UIDefaults key does not necessarily have to be a
 * String.
 * <br>1.3.1 2010-04-10 Catch throwables when getting values from UIManager.
 * <br>1.3 2006-07-20 Reworked for Java 1.5.
 * <br>1.2 2004-05-22 UIDefaults key does not necessarily have to be a
 * String.
 * <br>1.1 2003-03-16
 */
public class UIDefaultsTableModel extends AbstractTableModel {

    private Object[][] data;
    private final static String[] columnNames = {"Key", "Value"};

    public UIDefaultsTableModel(UIDefaults p) {
        setUIDefaults(p);
    }

    public void setUIDefaults(UIDefaults p) {
        HashMap combinedDefaults = new HashMap();
        // We need this very bogus code to retrieve all the defaults, because
        // the UIDefaults class returned by the UIManager is bogus. :(
        for (Enumeration iter = UIManager.getDefaults().keys(); iter.hasMoreElements();) {
            Object key = iter.nextElement();
            try {
                combinedDefaults.put(key, UIManager.get(key));
            } catch (Throwable t) {
                combinedDefaults.put(key, t.getMessage());
            }
        }
        for (Iterator iter = UIManager.getDefaults().keySet().iterator(); iter.hasNext();) {
            Object key = iter.next();
            try {
                combinedDefaults.put(key, UIManager.get(key));
            } catch (Throwable t) {
                combinedDefaults.put(key, t.getMessage());
            }
        }
        for (Enumeration iter = p.keys(); iter.hasMoreElements();) {
            Object key = iter.nextElement();
            try {
                combinedDefaults.put(key, p.get(key));
            } catch (Throwable t) {
                combinedDefaults.put(key, t.getMessage());
            }
        }
        data = new Object[combinedDefaults.size()][2];
        int i = 0;
        for (Iterator iter2 = combinedDefaults.entrySet().iterator(); iter2.hasNext();) {
            Map.Entry entry = (Map.Entry) iter2.next();
            data[i][0] = entry.getKey().toString();
            data[i][1] = entry.getValue();
            i++;
        }
        Arrays.sort(data, new Comparator() {

            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Object[]) o1)[0]).compareTo(((Object[]) o2)[0]);
            }
        });
        fireTableDataChanged();
    }

    public int getColumnCount() {
        return 2;
    }

    public int getRowCount() {
        return data.length;
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }
}
