001    /////////////////////////////////////////////////
002    //This file is part of Sears project.
003    //Subtitle Editor And Re-Synch
004    //A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    //at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    /////////////////////////////////////////////////
017    
018    // some suggestions about this class: floriaen@gmail.com
019    
020    package sears.gui.glassPane;
021    
022    import java.awt.AlphaComposite;
023    import java.awt.Color;
024    import java.awt.Composite;
025    import java.awt.Graphics;
026    import java.awt.Graphics2D;
027    
028    import javax.swing.JViewport;
029    
030    /**
031     * This class, although it's a non Abstract class and could be use like it is,
032     * is a <i>prototype</i> class and should be overwritted.
033     */
034    public class DefaultViewportGlassPane extends ViewportGlassPane {
035    
036            private static final long serialVersionUID = -7782241093516309531L;
037    
038            /** the default color of the virtual glass pane */
039            protected static final Color DEFAULT_COLOR = Color.BLACK;
040            /** the default factor of the virtual glass pane alpha transparency*/
041            protected static final float DEFAULT_ALPHA = .22f;
042    
043            private Color virtualBoundsColor;
044            private float alpha;
045    
046            /**
047             * Creates a new class instance with default color and alpha factor transparency
048             * @param viewport      the viewport
049             * @throws                      NullPointerException if viewport is null
050             * @see ViewportGlassPane#ViewportGlassPane(JViewport)
051             */
052            public DefaultViewportGlassPane(JViewport viewport) {
053                    super(viewport);
054                    init(DEFAULT_COLOR, DEFAULT_ALPHA);
055            }
056    
057            /**
058             * Creates a new class instance with non default color and alpha factor transparency
059             * 
060             * @param viewport                              the viewport
061             * @param virtualBoundsColor    the color of the drawing virtual glass pane
062             * @param alpha                                 a float that define the opacity of the virtual glass pane
063             * @throws                      NullPointerException if viewport is null
064             * @see ViewportGlassPane#ViewportGlassPane(JViewport)
065             */
066            public DefaultViewportGlassPane(JViewport viewport, Color virtualBoundsColor, float alpha) {
067                    super(viewport);
068                    init(virtualBoundsColor, alpha);
069            }
070    
071            /**
072             * Initializes color and alpha factor with values given on parameters
073             * <br>If null values is given, default values is set.
074             * @param c     the color 
075             * @param a     the alpha factor
076             */
077            private void init(Color c, float a) {
078                    if( c == null ) {
079                            c = DEFAULT_COLOR;
080                    }
081                    if( a < 0 ) {
082                            a = DEFAULT_ALPHA;
083                    }
084    
085                    virtualBoundsColor = c;
086                    alpha = a;
087            }
088    
089            /*
090             * (non-Javadoc)
091             * @see sears.gui.glassPane.ViewportGlassPane#paintWithGraphics(java.awt.Graphics2D)
092             */
093            protected void paintComponentWithGraphics(Graphics2D gr) {
094                    Color color = gr.getColor();
095                    Composite comp = gr.getComposite();     
096                    gr.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha ));   
097                    gr.setColor(virtualBoundsColor);        
098                    gr.fill(this.getVirtualBounds());
099                    gr.setColor(color);
100                    gr.setComposite(comp);
101            }
102    
103            /*
104             * (non-Javadoc)
105             * @see javax.swing.JComponent#paintChildren(java.awt.Graphics)
106             */
107            public void paintChildren(Graphics g) {
108                    super.paintChildren(g);
109                    Graphics2D gr = (Graphics2D) g;
110                    paintChildrenWithGraphics(gr);
111            }
112    
113            // This method is an HOOK method.
114            // It's mean that it would have to be reimplemented
115            //
116            /**
117             * This method is called in <code>paintChildren(g)</code> method.
118             * Allow a way to use the graphics context of this component
119             * 
120             * @param gr the graphics context of this component
121             * @see ViewportGlassPane#paintComponent(Graphics)
122             */
123            protected void paintChildrenWithGraphics(Graphics2D gr) {
124                    // NOTHING IS DONE HERE
125            }
126    }