001/*
002 * $Id$
003 *
004 * Copyright 2009 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 *
021 */
022package org.jdesktop.swingx.plaf.synth;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import javax.swing.JComponent;
028import javax.swing.plaf.synth.Region;
029
030/**
031 * Extended Region to register custom component delegates.
032 * 
033 * @author Jeanette Winzenburg
034 */
035public class XRegion extends Region {
036
037    static Map<String, XRegion> uiToXRegionMap = new HashMap<String, XRegion>();
038    public static final Region XLIST = new XRegion("XList", null, false, "XListUI", LIST);
039    
040    /** the Region which identifies the base styles */
041    private Region parent;
042    
043    /**
044     * Creates a XRegion with the specified name.
045     * 
046     * @param name Name of the region
047     * @param subregion Whether or not this is a subregion.
048     * @param realUI String that will be returned from
049     *           <code>component.getUIClassID</code>. 
050     * @param parent the parent region which this is extending.          
051     */
052    public XRegion(String name, String dummyUI, boolean subregion, String realUI, Region parent) {
053        super(name, dummyUI, subregion);
054        this.parent = parent;
055        if (realUI != null) {
056            uiToXRegionMap.put(realUI, this);
057        }
058    }
059    
060    /**
061     * Returns a region appropriate for the component. 
062     * 
063     * @param component the component to get the region for
064     * @param useParent a boolean indicating whether or not to return a fallback
065     *    of the XRegion, if available
066     * @return a region for the component or null if not available.
067     */
068    public static Region getXRegion(JComponent component, boolean useParent) {
069        XRegion region = uiToXRegionMap.get(component.getUIClassID());
070        if (region != null)
071            return useParent && region.parent != null ? region.parent : region;
072        return region;
073    }
074}