alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * ColorTable.java - Color selection table
 * Copyright (C) 2001 Matt Benson
 * Portions copyright (C) 1999 Slava Pestov
 * Portions copyright (C) 1999 mike dillon
 * Portions copyright (C) 1999-2001 Romain Guy
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.jext.options;

import java.awt.Color;
import java.awt.Component;

import java.util.Map;
import java.util.Iterator;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.JColorChooser;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.border.EmptyBorder;

import org.jext.Jext;
import org.jext.GUIUtilities;
import org.jext.gui.DisabledCellRenderer;


/**
 * This has been extracted from StylesOptions for universal use, especially by
 * colorizing plugins.
 * @author Slava Pestov, Mike Dillon, Romain Guy, Matt Benson
 */
public class ColorTable
 extends JTable
{
	
/**
 * Construct a ColorTable with an empty model.
 */
	public ColorTable()
	{
		this(new ColorTableModel());
	}//end default constructor
	
	
/**
 * Construct a ColorTable with the specified model.
 * @param model   the ColorTableModel to use.
 */
	public ColorTable(ColorTableModel model)
	{
		super(model);
    getTableHeader().setReorderingAllowed(false);
    getSelectionModel().addListSelectionListener(new ListHandler());
    getColumnModel().getColumn(1).setCellRenderer(new ColorTableModel.ColorRenderer());
    getColumnModel().getColumn(0).setCellRenderer(new DisabledCellRenderer());
	}//end constructor(ColorTableModel)

  
  private class ListHandler
	 implements ListSelectionListener
  {

    public void valueChanged(ListSelectionEvent evt)
    {
      if (evt.getValueIsAdjusting())
        return;
			Color color = JColorChooser.showDialog(ColorTable.this,
							Jext.getProperty("colorChooser.title"),
							(Color) (dataModel.getValueAt(getSelectedRow(), 1)));
			if (color != null)
				dataModel.setValueAt(color, getSelectedRow(), 1);
    }
  }

	
/**
 * TableModel for the ColorTable.
 */
	public static class ColorTableModel
	 extends AbstractTableModel
	{
		private ArrayList colorChoices;
	
/**
 * Construct an empty ColorTableModel.
 */
		public ColorTableModel()
		{
			colorChoices = new ArrayList(24);
		}//end default constructor
		

/**
 * Construct a ColorTableModel and initialize it with the contents
 * of the specified Map.
 * @param choices   the Map containing the initial choices for this
 *                  ColorTableModel.  This should be a map of
 *                  String to String, so it would be
 *                  sensible to use a Properties object, but this
 *                  is not enforced beyond that the String forms of
 *                  the keys and values in the Map will be used.
 *                  Each map entry will be added to the
 *                  ColorTableModel as if by
 *                  addColorChoice(String, String).
 * @see addColorChoice(String, String)
 */
		public ColorTableModel(Map choices)
		{
			this();
			Iterator it = choices.entrySet().iterator();
			Map.Entry entry = null;
			while (it.hasNext())
			{
				entry = (Map.Entry)(it.next());
				addColorChoice(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
			}//end while more keys
		}//end constructor(Map)
		
/**
 * @see TableModel#getColumnCount()
 */	
		public int getColumnCount()
		{
			return 2;
		}//end getColumnCount
	
/**
 * @see TableModel#getRowCount()
 */
		public int getRowCount()
		{
			return colorChoices.size();
		}//end getRowCount
	
/**
 * @see TableModel#getValueAt(int, int)
 */
		public Object getValueAt(int row, int col)
		{
			ColorChoice ch = (ColorChoice) colorChoices.get(row);
			switch (col)
			{
				case 0:
					return ch.label;
				case 1:
					return ch.color;
				default:
					return null;
			}//end switch on column number
		}//end getValueAt
	
/**
 * @see TableModel#setValueAt(Object, int, int)
 */
		public void setValueAt(Object value, int row, int col)
		{
			ColorChoice ch = (ColorChoice) colorChoices.get(row);
			if (col == 1)
				ch.color = (Color) value;
			fireTableRowsUpdated(row, row);
		}//end setValueAt
	
/**
 * @see TableModel#getColumnName(int)
 */
		public String getColumnName(int index)
		{
			switch (index)
			{
				case 0:
					return Jext.getProperty("options.styles.object");
				case 1:
					return Jext.getProperty("options.styles.color");
				default:
					return null;
			}//end switch on index
		}//end getColumnName
	
	
/**
 * Save the contents of this ColorTableModel to Jext properties.
 */
		public void save()
		{
			for (int i = 0; i < colorChoices.size(); i++)
			{
				ColorChoice ch = (ColorChoice) colorChoices.get(i);
				Jext.setProperty(ch.property, GUIUtilities.getColorHexString(ch.color));
			}//end for through color choices
		}//end save
/**
 * Reload all the colors of this ColorTableModel from Jext properties.
 * Added for the Option Dialog caching.
 */    
    public void load()
    {
			for (int i = 0; i < colorChoices.size(); i++)
				( (ColorChoice) colorChoices.get(i) ).resetColor();
      fireTableRowsUpdated(0, colorChoices.size() - 1);
    }//end load
	
/**
 * Add the specified color choice to this ColorTableModel.
 * @param label      String property name for the description.
 * @param property   String property name for the color.
 */
		public void addColorChoice(String label, String property)
		{
			colorChoices.add(new ColorChoice(Jext.getProperty(label), property));
							
		}//end addColorChoice
	
		private static class ColorChoice
		{
			String label;
			String property;
			Color color;
	
			ColorChoice(String label, String property/*, Color color*/)
			{
				this.label = label;
				this.property = property;
				this.color = GUIUtilities.parseColor(Jext.getProperty(property));
			}//end constructor
      
      public void resetColor()
      {
				this.color = GUIUtilities.parseColor(Jext.getProperty(property));
      }
		}//end class ColorChoice
	
		private static class ColorRenderer
		 extends JLabel
		 implements TableCellRenderer
		{
			public ColorRenderer()
			{
				setOpaque(true);
				setBorder(StylesOptions.noFocusBorder);
			}//end constructor
	
			// TableCellRenderer implementation
			public Component getTableCellRendererComponent(JTable table,
							Object value, boolean isSelected, boolean cellHasFocus,
							int row, int col)
			{
				if (isSelected)
				{
					setBackground(table.getSelectionBackground());
					setForeground(table.getSelectionForeground());
				}//end if selected
				else
				{
					setBackground(table.getBackground());
					setForeground(table.getForeground());
				}//end else, not selected
	
				if (value != null)
					setBackground((Color) value);
	
				setBorder((cellHasFocus) ?
								UIManager.getBorder("Table.focusCellHighlightBorder") :
								StylesOptions.noFocusBorder);
				return this;
			}//end getTableCellRendererComponent
		}//end class ColorRenderer
	}//end class ColorTableModel
}//end class ColorTable
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.