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