001    /*
002     * BasicStatusBarUI.java
003     *
004     * Created on March 9, 2006, 11:30 PM
005     *
006     * To change this template, choose Tools | Template Manager
007     * and open the template in the editor.
008     */
009    
010    package org.jdesktop.swingx.plaf.basic;
011    
012    import java.awt.Graphics;
013    import java.awt.Graphics2D;
014    import java.awt.image.BufferedImage;
015    import javax.imageio.ImageIO;
016    import javax.swing.BorderFactory;
017    import javax.swing.JComponent;
018    import javax.swing.plaf.ComponentUI;
019    import org.jdesktop.swingx.JXStatusBar;
020    import org.jdesktop.swingx.plaf.StatusBarUI;
021    
022    /**
023     *
024     * @author Richard
025     */
026    public class BasicStatusBarUI extends StatusBarUI {
027        /**
028         * The one and only JXStatusBar for this UI delegate
029         */
030        private JXStatusBar statusBar;
031        private BufferedImage leftImage;
032        private BufferedImage middleImage;
033        private BufferedImage rightImage;
034        
035        /** Creates a new instance of BasicStatusBarUI */
036        public BasicStatusBarUI() {
037            try {
038                leftImage = ImageIO.read(getClass().getResource("resources/statusbar-left.png"));
039                middleImage = ImageIO.read(getClass().getResource("resources/statusbar-middle.png"));
040                rightImage = ImageIO.read(getClass().getResource("resources/statusbar-right.png"));
041            } catch (Exception e) {
042                //hmmmm... should log this I guess
043            }
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 BasicStatusBarUI();
058        }   
059        
060        /**
061         * Configures the specified component appropriate for the look and feel.
062         * This method is invoked when the <code>ComponentUI</code> instance is being installed
063         * as the UI delegate on the specified component.  This method should
064         * completely configure the component for the look and feel,
065         * including the following:
066         * <ol>
067         * <li>Install any default property values for color, fonts, borders,
068         *     icons, opacity, etc. on the component.  Whenever possible, 
069         *     property values initialized by the client program should <i>not</i> 
070         *     be overridden.
071         * <li>Install a <code>LayoutManager</code> on the component if necessary.
072         * <li>Create/add any required sub-components to the component.
073         * <li>Create/install event listeners on the component.
074         * <li>Create/install a <code>PropertyChangeListener</code> on the component in order
075         *     to detect and respond to component property changes appropriately.
076         * <li>Install keyboard UI (mnemonics, traversal, etc.) on the component.
077         * <li>Initialize any appropriate instance data.
078         * </ol>
079         * @param c the component where this UI delegate is being installed
080         *
081         * @see #uninstallUI
082         * @see javax.swing.JComponent#setUI
083         * @see javax.swing.JComponent#updateUI
084         */
085        @Override
086        public void installUI(JComponent c) {
087            assert c instanceof JXStatusBar;
088            statusBar = (JXStatusBar)c;
089            
090            statusBar.setBorder(BorderFactory.createEmptyBorder(4, 5, 4, 22));
091        }
092    
093        /**
094         * Reverses configuration which was done on the specified component during
095         * <code>installUI</code>.  This method is invoked when this 
096         * <code>UIComponent</code> instance is being removed as the UI delegate 
097         * for the specified component.  This method should undo the
098         * configuration performed in <code>installUI</code>, being careful to 
099         * leave the <code>JComponent</code> instance in a clean state (no 
100         * extraneous listeners, look-and-feel-specific property objects, etc.).
101         * This should include the following:
102         * <ol>
103         * <li>Remove any UI-set borders from the component.
104         * <li>Remove any UI-set layout managers on the component.
105         * <li>Remove any UI-added sub-components from the component.
106         * <li>Remove any UI-added event/property listeners from the component.
107         * <li>Remove any UI-installed keyboard UI from the component.
108         * <li>Nullify any allocated instance data objects to allow for GC.
109         * </ol>
110         * @param c the component from which this UI delegate is being removed;
111         *          this argument is often ignored,
112         *          but might be used if the UI object is stateless
113         *          and shared by multiple components
114         *
115         * @see #installUI
116         * @see javax.swing.JComponent#updateUI
117         */
118        @Override
119        public void uninstallUI(JComponent c) {
120            assert c instanceof JXStatusBar;
121        }
122    
123        @Override
124        public void paint(Graphics g, JComponent c) {
125            super.paint(g, c);
126            
127            //paint the background if opaque
128            if (statusBar.isOpaque()) {
129                //if bidi, reverse the image painting order
130                //TODO need to handle vertical stretching better
131                Graphics2D g2 = (Graphics2D)g;
132                g2.drawImage(leftImage, 0, 0, leftImage.getWidth(), statusBar.getHeight(), null);
133                g2.drawImage(middleImage, leftImage.getWidth(), 0, statusBar.getWidth() - leftImage.getWidth() - rightImage.getWidth(), statusBar.getHeight(), null);
134                g2.drawImage(rightImage, statusBar.getWidth() - rightImage.getWidth(), 0, rightImage.getWidth(), statusBar.getHeight(), null);
135            }
136        }
137    }