Home | History | Annotate | Download | only in swing
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 /*
     28  * To change this template, choose Tools | Templates
     29  * and open the template in the editor.
     30  */
     31 
     32 package org.opensolaris.os.vp.panels.mysql.client.swing;
     33 
     34 import java.awt.GridLayout;
     35 import java.util.*;
     36 import javax.swing.*;
     37 import javax.swing.table.AbstractTableModel;
     38 import org.opensolaris.os.vp.util.misc.Finder;
     39 
     40 /**
     41  *
     42  * @author irfan
     43  */
     44 public class PropertiesTable extends JTable {
     45 
     46     public PropertiesTable(Map<String, String> data) throws Exception {
     47 	PropertiesTableModel tableModel = new PropertiesTableModel(data);
     48 	setModel(tableModel);
     49 	init();
     50     }
     51 
     52     public PropertiesTable() {
     53         HashMap<String, String> data = new HashMap<String, String>();
     54         data.put("", "");
     55 	PropertiesTableModel tableModel = new PropertiesTableModel(data);
     56 	setModel(tableModel);
     57 	init();
     58     }
     59 
     60     public void init() {
     61 	// set
     62 	setFillsViewportHeight(false);
     63     }
     64 
     65     public void updateTableModel(Map<String, String> data)
     66 	throws Exception {
     67 
     68         PropertiesTableModel tableModel = new PropertiesTableModel(data);
     69         setModel(tableModel);
     70     }
     71 
     72     class PropertiesTableModel extends AbstractTableModel {
     73 
     74 	private Object[][] tableData;
     75 	private Vector<String> colNames;
     76 
     77 	public PropertiesTableModel(Map<String, String> data) {
     78 	    initData(data);
     79 	    initColNames();
     80 	}
     81 
     82 	protected void initColNames() {
     83 	    colNames = new Vector<String>(2);
     84 	    colNames.add(Finder.getString(
     85 		"mysql.panel.propertiesTable.colName.label"));
     86 	    colNames.add(Finder.getString(
     87 		"mysql.panel.propertiesTable.colValue.label"));
     88 	}
     89 
     90 	protected void initData(Map<String, String> data) {
     91 	    tableData = new Object[data.size()][2];
     92 	    TreeSet<String> keys = new TreeSet<String>(data.keySet());
     93 	    int row = 0;
     94 	    for (String key : keys) {
     95 		Object value = data.get(key);
     96 		tableData[row][0] = key;
     97 		tableData[row][1] = value;
     98 		row++;
     99 	    }
    100 	}
    101 
    102 	public int getColumnCount() {
    103 	    return colNames.size();
    104 	}
    105 
    106 	public int getRowCount() {
    107 	    return tableData.length;
    108 	}
    109 
    110 	@Override
    111 	public String getColumnName(int col) {
    112 	    return colNames.get(col);
    113 	}
    114 
    115 	@Override
    116 	public Object getValueAt(int row, int col) {
    117 	    return tableData[row][col];
    118 	}
    119 
    120 	@Override
    121 	public Class getColumnClass(int col) {
    122 	    return getValueAt(0, col).getClass();
    123 	}
    124 
    125 	public String formatKey(String key) {
    126 	    // key is in format uptime_since_flush
    127 	    // This will be changed to Uptime Since Flush
    128 	    String formattedKey = "";
    129 	    try {
    130 		StringTokenizer tokens = new StringTokenizer(key, "_");
    131 		while (tokens.hasMoreElements()) {
    132 		    String token = tokens.nextToken();
    133 		    String firstChar = String.valueOf(token.charAt(0));
    134 		    formattedKey += firstChar.toUpperCase() +
    135 			token.substring(1) + " ";
    136 		}
    137 	    } catch (Exception ex) {
    138 		formattedKey = key;
    139 	    }
    140 
    141 	    return formattedKey;
    142 	}
    143     }
    144 
    145     //
    146     public static void createAndShowGUI() throws Exception {
    147 	// Create and set up the window.
    148 	JFrame frame = new JFrame("TableDemo");
    149         frame.setSize(500, 400);
    150 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    151 	Map<String, String> map = new HashMap<String, String>();
    152 	map.put("irfan", "ahmed");
    153 	map.put("Long long name ......sdjajksdhf", "value");
    154 	map.put("test", "kdsajlflkajsdbfajlsbdfljkahlskdjflkasjgd");
    155 
    156 	// Create and set up the content pane.
    157 	JPanel testPanel = new JPanel(new GridLayout(2, 1));
    158 
    159 	PropertiesTable table = new PropertiesTable(map);
    160 	// table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    161 	// table.setFillsViewportHeight(false);
    162 
    163 	// Create the scroll pane and add the table to it.
    164 	JScrollPane scrollPane = new JScrollPane(table);
    165 
    166 	// Add the scroll pane to this panel.
    167 	testPanel.add(scrollPane);
    168 	frame.setContentPane(testPanel);
    169 	// Display the window.
    170 	// frame.pack();
    171 	frame.setVisible(true);
    172     }
    173 
    174     public static void main(String[] args) {
    175 	// Schedule a job for the event-dispatching thread:
    176 	// creating and showing this application's GUI.
    177 	javax.swing.SwingUtilities.invokeLater(new Runnable() {
    178 
    179 	    public void run() {
    180 		try {
    181 		    createAndShowGUI();
    182 		} catch (Exception ex) {
    183 		    ex.printStackTrace();
    184 		}
    185 	    }
    186 	});
    187     }
    188 }
    189