001/*
002 * $Id: MetalStatusBarUI.java 3472 2009-08-27 13:12:42Z kleopatra $
003 *
004 * Copyright 2004 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.metal;
023
024import java.awt.Color;
025import java.awt.GradientPaint;
026import java.awt.Graphics2D;
027import java.util.List;
028
029import javax.swing.JComponent;
030import javax.swing.UIManager;
031import javax.swing.plaf.ComponentUI;
032
033import org.jdesktop.swingx.JXStatusBar;
034import org.jdesktop.swingx.plaf.basic.BasicStatusBarUI;
035
036/**
037 *
038 * @author rbair
039 */
040public class MetalStatusBarUI extends BasicStatusBarUI {
041    
042    /** Creates a new instance of BasicStatusBarUI */
043    public MetalStatusBarUI() {
044    }
045    
046    /**
047     * Returns an instance of the UI delegate for the specified component.
048     * Each subclass must provide its own static <code>createUI</code>
049     * method that returns an instance of that UI delegate subclass.
050     * If the UI delegate subclass is stateless, it may return an instance
051     * that is shared by multiple components.  If the UI delegate is
052     * stateful, then it should return a new instance per component.
053     * The default implementation of this method throws an error, as it
054     * should never be invoked.
055     */
056    public static ComponentUI createUI(JComponent c) {
057        return new MetalStatusBarUI();
058    }
059    
060    @Override
061    protected void paintBackground(Graphics2D g, JXStatusBar bar) {
062        int w = bar.getWidth(); //TODO deal with insets
063        int h = bar.getHeight(); //TODO deal with insets
064        
065        //This list is comprised of floats and Colors, which together
066        //constitute the gradient.
067        List<?> gradient = (List<?>) UIManager.get("MenuBar.gradient");
068
069        if (gradient != null && w > 0 && 0 < h) {
070            float ratio1 = ((Number)gradient.get(0)).floatValue();
071            float ratio2 = ((Number)gradient.get(1)).floatValue();
072            Color c1 = (Color)gradient.get(2);
073            Color c2 = (Color)gradient.get(3);
074            Color c3 = (Color)gradient.get(4);
075            int mid = (int)(ratio1 * h);
076            int mid2 = (int)(ratio2 * h);
077            if (mid > 0) {
078                g.setPaint(new GradientPaint((float)0, (float)0, c1, (float)0,
079                                       (float)mid, c2));
080                g.fillRect(0, 0, w, mid);
081            }
082            if (mid2 > 0) {
083                g.setColor(c2);
084                g.fillRect(0, mid, w, mid2);
085            }
086            if (mid > 0) {
087                g.setPaint(new GradientPaint((float)0, (float)mid + mid2, c2,
088                                       (float)0, (float)mid * 2 + mid2, c1));
089                g.fillRect(0, mid + mid2, w, mid);
090            }
091            if (h - mid * 2 - mid2 > 0) {
092                g.setPaint(new GradientPaint((float)0, (float)mid * 2 + mid2, c1,
093                                       (float)0, (float)h, c3));
094                g.fillRect(0, mid * 2 + mid2, w, h - mid * 2 - mid2);
095            }
096        }
097    }
098}