001/*
002 * $Id: MappedValue.java 3927 2011-02-22 16:34:11Z kleopatra $
003 *
004 * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.renderer;
022
023import javax.swing.Icon;
024
025/**
026 * Compound implementation of XXValue. Currently, XX stands for String, 
027 * Icon, Boolean. <p>
028 * 
029 * Quick hack around #590-swingx: LabelProvider should respect StringValue
030 * when formatting (instead of going clever with icons).
031 * 
032 * Note: this will change!
033 * 
034 * @see CheckBoxProvider
035 */
036public class MappedValue implements StringValue, IconValue, BooleanValue {
037
038    private StringValue stringDelegate;
039    private IconValue iconDelegate;
040    private BooleanValue booleanDelegate;
041
042    public MappedValue(StringValue stringDelegate, IconValue iconDelegate) {
043        this(stringDelegate, iconDelegate, null);
044    }
045    
046    public MappedValue(StringValue stringDelegate, IconValue iconDelegate, 
047            BooleanValue booleanDelegate) {
048        this.stringDelegate = stringDelegate;
049        this.iconDelegate = iconDelegate;
050        this.booleanDelegate = booleanDelegate;
051    }
052    
053    /**
054     * {@inheritDoc}<p>
055     * 
056     * This implementation delegates to the contained StringValue if available or
057     * returns an empty String, if not.
058     *  
059     */
060    @Override
061    public String getString(Object value) {
062        if (stringDelegate != null) {
063            return stringDelegate.getString(value);
064        }
065        return "";
066    }
067
068    /**
069     * {@inheritDoc}<p>
070     * 
071     * This implementation delegates to the contained IconValue if available or
072     * returns null, if not.
073     *  
074     */
075    @Override
076    public Icon getIcon(Object value) {
077        if (iconDelegate != null) {
078            return iconDelegate.getIcon(value);
079        }
080        return null;
081    }
082    
083    /**
084     * {@inheritDoc}<p>
085     * 
086     * This implementation delegates to the contained BooleanValue if available or
087     * returns false, if not.
088     *  
089     */
090    @Override
091    public boolean getBoolean(Object value) {
092        if (booleanDelegate != null) {
093            return booleanDelegate.getBoolean(value);
094        }
095        return false;
096    }
097    
098}