All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.org.retep.swing.model.EnumModel Maven / Gradle / Ivy

The newest version!
/*
 * 

Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.

* *

Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met:

* *
    *
  • Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
  • * *
  • Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
  • * *
  • Neither the name of the retep.org.uk nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission.
  • * *
* *

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/ package uk.org.retep.swing.model; import java.awt.Color; import java.awt.Component; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; /** * A ComboBoxModel that displays values of an enum * * @param Enum implementing EnumModel.Renderable used to view * @author peter */ public class EnumModel extends AbstractModel { private T values[]; /** Creates a new instance of ColorModel * @param values The enum values to include */ public EnumModel( final T values[] ) { this( values, values[0] ); } /** Creates a new instance of ColorModel * @param values The enum values to include * @param defaultValue The default value */ public EnumModel( final T values[], final T defaultValue ) { super( defaultValue ); this.values = values; } @SuppressWarnings( "unchecked" ) @Override public ListCellRenderer getRenderer() { return new EnumRenderer( getDefaultValue() ); } /** * Returns the length of the list. * @return the length of the list */ @Override public int getSize() { return values.length; } /** * Returns the value at the specified index. * @param index the requested index * @return the value at index */ @Override public Object getElementAt( int index ) { return values[index]; } public Object select( String name ) { Object selected = null; if( name != null ) { for( T v : values ) { if( name.equals( v.toString() ) ) { selected = v; break; } } } return selected; } private class EnumRenderer extends JLabel implements ListCellRenderer { static final long serialVersionUID = -7200159879726204139L; private Map cache = new HashMap(); /** Creates a new instance of JoinEndRenderer */ public EnumRenderer( EnumModel.Renderable defaultValue ) { setOpaque( false ); for( T value : values ) { BufferedImage img = new BufferedImage( 20, 20, BufferedImage.TYPE_3BYTE_BGR ); Graphics2D g = img.createGraphics(); try { // They tend to look better antialiased g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); g.setColor( Color.white ); g.fillRect( 0, 0, 20, 20 ); g.setBackground( Color.white ); g.setColor( Color.black ); value.render( g, 20, 20 ); } finally { g.dispose(); } cache.put( value, new ImageIcon( img ) ); } } @SuppressWarnings( "unchecked" ) private T getValue( Object value ) { return (T) value; } @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { if( value == null ) { setText( "" ); setIcon( null ); } else { T end = getValue( value ); setText( end.getDescription() ); setIcon( cache.get( end ) ); } return this; } } public static interface Renderable { String getDescription(); void render( Graphics2D g, int w, int h ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy