001/*
002 * $Id: BasicLoginPaneUI.java 4082 2011-11-15 18:39:43Z kschaefe $
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 */
021package org.jdesktop.swingx.plaf.basic;
022import java.awt.Font;
023import java.awt.GradientPaint;
024import java.awt.Graphics2D;
025import java.awt.Image;
026import java.awt.RenderingHints;
027import java.awt.geom.GeneralPath;
028import java.awt.image.BufferedImage;
029import java.beans.PropertyChangeEvent;
030import java.beans.PropertyChangeListener;
031
032import javax.swing.JComponent;
033import javax.swing.UIManager;
034import javax.swing.plaf.ComponentUI;
035
036import org.jdesktop.swingx.JXLoginPane;
037import org.jdesktop.swingx.plaf.LoginPaneUI;
038import org.jdesktop.swingx.plaf.UIManagerExt;
039import org.jdesktop.swingx.util.GraphicsUtilities;
040/**
041 * Base implementation of the <code>JXLoginPane</code> UI.
042 *
043 * @author rbair
044 */
045public class BasicLoginPaneUI extends LoginPaneUI {
046    private class LocaleHandler implements PropertyChangeListener {
047        /**
048         * {@inheritDoc}
049         */
050        @Override
051        public void propertyChange(PropertyChangeEvent evt) {
052            Object src = evt.getSource();
053            
054            if (src instanceof JComponent) {
055                ((JComponent) src).updateUI();
056            }
057        }
058    }
059    
060    private JXLoginPane dlg;
061    
062    /** Creates a new instance of BasicLoginDialogUI */
063    public BasicLoginPaneUI(JXLoginPane dlg) {
064        this.dlg = dlg;
065//        dlg.addPropertyChangeListener("locale", new LocaleHandler());
066    }
067    
068  public static ComponentUI createUI(JComponent c) {
069    return new BasicLoginPaneUI((JXLoginPane)c);
070  }
071
072    @Override
073    public void installUI(JComponent c) {
074        installDefaults();
075    }
076    
077    protected void installDefaults() {
078        String s = dlg.getBannerText();
079        if (s == null || s.equals("")) {
080            dlg.setBannerText(UIManagerExt.getString("JXLoginPane.bannerString", dlg.getLocale()));
081        }
082        
083        s = dlg.getErrorMessage();
084        if (s == null || s.equals("")) {
085            dlg.setErrorMessage(UIManagerExt.getString("JXLoginPane.errorMessage", dlg.getLocale()));
086        }
087    }
088    
089    /**
090     * Creates default 400x60 banner for the login panel.
091     * @see org.jdesktop.swingx.plaf.LoginPaneUI#getBanner()
092     */
093    @Override
094    public Image getBanner() {
095        int w = 400;
096        int h = 60;
097        float loginStringX = w * .05f;
098        float loginStringY = h * .75f;
099
100        BufferedImage img = GraphicsUtilities.createCompatibleImage(w, h);
101        Graphics2D g2 = img.createGraphics();
102        try {
103            Font font = UIManager.getFont("JXLoginPane.bannerFont");
104            g2.setFont(font);
105            Graphics2D originalGraphics = g2;
106
107            try {
108                if (!dlg.getComponentOrientation().isLeftToRight()) {
109                    originalGraphics = (Graphics2D) g2.create();
110                    g2.scale(-1, 1);
111                    g2.translate(-w, 0);
112                    loginStringX = w
113                            - (((float) font.getStringBounds(
114                                    dlg.getBannerText(),
115                                    originalGraphics.getFontRenderContext())
116                                    .getWidth()) + w * .05f);
117                }
118
119                g2.setRenderingHint(RenderingHints.KEY_RENDERING,
120                        RenderingHints.VALUE_RENDER_QUALITY);
121                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
122                        RenderingHints.VALUE_ANTIALIAS_ON);
123                g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
124                        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
125                g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
126                        RenderingHints.VALUE_FRACTIONALMETRICS_ON);
127
128                // draw a big square
129                g2.setColor(UIManager
130                        .getColor("JXLoginPane.bannerDarkBackground"));
131                g2.fillRect(0, 0, w, h);
132
133                // create the curve shape
134                GeneralPath curveShape = new GeneralPath(
135                        GeneralPath.WIND_NON_ZERO);
136                curveShape.moveTo(0, h * .6f);
137                curveShape.curveTo(w * .167f, h * 1.2f, w * .667f, h * -.5f, w,
138                        h * .75f);
139                curveShape.lineTo(w, h);
140                curveShape.lineTo(0, h);
141                curveShape.lineTo(0, h * .8f);
142                curveShape.closePath();
143
144                // draw into the buffer a gradient (bottom to top), and the text
145                // "Login"
146                GradientPaint gp = new GradientPaint(0, h, UIManager
147                        .getColor("JXLoginPane.bannerDarkBackground"), 0, 0,
148                        UIManager.getColor("JXLoginPane.bannerLightBackground"));
149                g2.setPaint(gp);
150                g2.fill(curveShape);
151
152                originalGraphics.setColor(UIManager
153                        .getColor("JXLoginPane.bannerForeground"));
154                originalGraphics.drawString(dlg.getBannerText(), loginStringX,
155                        loginStringY);
156            } finally {
157                originalGraphics.dispose();
158            }
159        } finally {
160            g2.dispose();
161        }
162        return img;
163    }
164}