Package net.xoetrope.optional.laf.synth

Source Code of net.xoetrope.optional.laf.synth.BackgroundPainter

package net.xoetrope.optional.laf.synth;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.util.HashMap;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.synth.SynthConstants;
import javax.swing.plaf.synth.SynthContext;
import javax.swing.plaf.synth.SynthPainter;

import net.xoetrope.swing.painter.XLogoBackground;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.helper.XuiUtilities;
import net.xoetrope.xui.style.XStyle;
import net.xoetrope.xui.style.XStyleManager;

/**
* A Synth support class for painting panel backgrounds
* <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License: see license.txt
* $Revision: 1.14 $
*/
public class BackgroundPainter extends SynthPainter
{
  private static XLogoBackground painter = new XLogoBackground();
  private XProject currentProject = XProjectManager.getCurrentProject();
  private static HashMap painters = new HashMap();

  /**
   * Paint the background. Draws a gradient fill plus a logo
   * @param context the synth context
   * @param g the graphics device context
   * @param x the x coordinate of the panel
   * @param y the y coordinate of the panel
   * @param w the w dimension of the panel
   * @param h the h dimension of the panel
   */
  public void paintDesktopPaneBackground( SynthContext context, Graphics g, int x, int y, int w, int h )
  {
    JComponent c = (JComponent)context.getComponent();
    String startStyle = ( String ) UIManager.get( "Desktop.desktopStyle" );
    XStyleManager sm = currentProject.getStyleManager();
    XStyle panelStyle = sm.getStyle( startStyle );
    Color start = panelStyle.getStyleAsColor( XStyle.COLOR_FORE );
    Color end = panelStyle.getStyleAsColor( XStyle.COLOR_BACK );

//    c.setForeground( start );
//    c.setBackground( end );
//    g.setColor( java.awt.Color.white );
    painter.paint( (Graphics2D)g, c, w, h );
  }

  /**
   * Paint the background. Draws a gradient fill plus a logo
   * @param context the synth context
   * @param g the graphics device context
   * @param x the x coordinate of the panel
   * @param y the y coordinate of the panel
   * @param w the w dimension of the panel
   * @param h the h dimension of the panel
   */
  public void paintPanelBackground( SynthContext context, Graphics g, int x, int y, int w, int h )
  {
//    JPanel panel = (JPanel)context.getComponent();
//    if ( panel.isOpaque() ) {
//      String startStyle = ( String ) UIManager.get( "Panel.panelStyle" );
//      XStyleManager sm = currentProject.getStyleManager();
//      XStyle panelStyle = sm.getStyle( startStyle );
//      Color start = panelStyle.getStyleAsColor( XStyle.COLOR_FORE );
//      Color end = panelStyle.getStyleAsColor( XStyle.COLOR_BACK );
//
//      Color oldBkgr = panel.getBackground();
//      Color oldFrgd = panel.getForeground();
//
//      panel.setForeground( start );
//      panel.setBackground( end );
//      g.setColor( java.awt.Color.white );
//      painter.paint( g, panel, x, y, w, h );
//      panel.setForeground( oldFrgd );
//      panel.setBackground( oldBkgr );
//    }
//    super.paintPanelBackground( context, g, x, y, w, h );
   
    paintPanelBorder( context, g, x, y, w, h );
  }

  /**
   * Paint the border
   * @param context the synth context
   * @param g the graphics device context
   * @param x the x coordinate of the panel
   * @param y the y coordinate of the panel
   * @param w the w dimension of the panel
   * @param h the h dimension of the panel
   */
  public void paintPanelBorder( SynthContext context, Graphics g, int x, int y, int w, int h )
  {
    JPanel panel = ( JPanel ) context.getComponent();
    Border border = panel.getBorder();
    if ( border instanceof TitledBorder )
      paintTitledBorder( (TitledBorder)border, context, g, x, y, w, h );
    else if ( border instanceof CompoundBorder )
      paintCompoundBorder( (CompoundBorder)border, context, g, x, y, w, h );   
  }

  /**
   * Paint the border of a CompoundBorder
   * @param cb the coumpound border
   * @param context the synth context
   * @param g the graphics device context
   * @param x the x coordinate of the panel
   * @param y the y coordinate of the panel
   * @param w the w dimension of the panel
   * @param h the h dimension of the panel
   */
  private void paintCompoundBorder( CompoundBorder cb, SynthContext context, Graphics g, int x, int y, int w, int h )
  {
    Border outsideBorder = cb.getOutsideBorder();
    if ( outsideBorder instanceof CompoundBorder )
      paintCompoundBorder( (CompoundBorder)outsideBorder, context, g, x, y, w, h );
    else if ( outsideBorder instanceof TitledBorder )
      paintTitledBorder( (TitledBorder)outsideBorder, context, g, x, y, w, h );

    Border insideBorder = cb.getInsideBorder();
    if ( insideBorder == null )
      return;

    Insets outsideInsets = outsideBorder.getBorderInsets( context.getComponent() );
    y += outsideInsets.top;
    x += outsideInsets.left;
    h -= outsideInsets.top + outsideInsets.bottom;
    w -= outsideInsets.left + outsideInsets.right;
    if ( insideBorder instanceof CompoundBorder )
      paintCompoundBorder( (CompoundBorder)insideBorder, context, g, x, y, w, h );
    else if ( insideBorder instanceof TitledBorder )
      paintTitledBorder( (TitledBorder)insideBorder, context, g, x, y, w, h );
  }

  /**
   * Paint the border of a TitledBorder
   * @param titledBorder the titled border
   * @param context the synth context
   * @param g the graphics device context
   * @param x the x coordinate of the panel
   * @param y the y coordinate of the panel
   * @param w the w dimension of the panel
   * @param h the h dimension of the panel
   */
  private void paintTitledBorder( TitledBorder titledBorder, SynthContext context, Graphics g, int x, int y, int w, int h )
  {
    int xcorr = 2;
    x += xcorr;
    y += 2;
    w -= xcorr * 2;
    h -= 4;

    JPanel panel = ( JPanel ) context.getComponent();
    FontMetrics fm = panel.getFontMetrics( panel.getFont() );
    int textHeight = fm.getHeight();
    int textWidth = fm.stringWidth( titledBorder.getTitle() );
    int dy = 0;
    if ( titledBorder.getTitlePosition() == 2 ) {
      dy = ( fm.getAscent() - 1 ) / 2;
      y += dy;
    }
    else if ( titledBorder.getTitlePosition() == 1 ) {
      dy = textHeight + 2;
      y += dy;
    }
    else if ( titledBorder.getTitlePosition() == 3 ) {
      dy = 2;
      y += dy;
    }
    else if ( titledBorder.getTitlePosition() == 5 )
      dy = ( textHeight - 1 ) / 2;
    else if ( titledBorder.getTitlePosition() == 4 )
      dy = 2;
    else if ( titledBorder.getTitlePosition() == 6 )
      dy = textHeight;

    h -= dy;
    String imagePath = ( String ) UIManager.get( "Panel.border" );
    Insets sourceInsets = new Insets( 3, 3, 3, 3 );
    Insets destInsets = new Insets( 3, 3, 3, 3 );
   
    String key = imagePath + "_" + w + "_" + h;
    ImagePainter imagePainter = (ImagePainter)painters.get( key );
    if ( imagePainter == null ) {
      imagePainter = new ImagePainter( context, g, x, y, w, h, imagePath, sourceInsets, destInsets, true, true );
      painters.put( key, imagePainter );
    }
    imagePainter.paint( g );

    if ( titledBorder.getTitlePosition() == 2 )
      y -= dy;
    else if ( titledBorder.getTitlePosition() == 5 )
      y = ( y + h ) - dy;
    else
      return;

    y += 4;
    textHeight = 1;

    Insets borderInsets = titledBorder.getBorderInsets( panel );
    g.setColor( panel.getBackground() );
    if ( titledBorder.getTitleJustification() == 2 )
      g.fillRect( ( x + ( w - textWidth ) / 2 ) - 1, y, textWidth + 2, textHeight );
    else if ( titledBorder.getTitleJustification() == 3 )
      g.fillRect( ( ( x + w ) - borderInsets.right * 2 - textWidth ) + xcorr, y, textWidth + 2, textHeight );
    else
      g.fillRect( ( x + borderInsets.left * 2 ) - 1 - 1 - xcorr, y, textWidth + 2, textHeight );
  }
 
  /**
   * Paints the border of an internal frame title pane border.
   *
   * @param context SynthContext identifying the <code>JComponent</code> and
   *        <code>Region</code> to paint to
   * @param g <code>Graphics</code> to paint to
   * @param x X coordinate of the area to paint to
   * @param y Y coordinate of the area to paint to
   * @param w Width of the area to paint to
   * @param h Height of the area to paint to
   */
  public void paintInternalFrameTitlePaneBorder( SynthContext context, Graphics g, int x, int y, int w, int h )
  {
    String imagePath = ( String ) UIManager.get( "InternalFrameBorderBackgroundSelected.background" );
    JInternalFrame frame = ( JInternalFrame ) context.getComponent();
    if ( frame.isSelected() )
      imagePath = ( String ) UIManager.get( "InternalFrameBorderBackgroundSelected.background" );
   
    Insets sourceInsets = new Insets( 1, 1, 1, 1 );
    Insets destInsets = new Insets( 1, 1, 1, 1 );
    String key = imagePath + "_" + w + "_" + h;
    ImagePainter imagePainter = (ImagePainter)painters.get( key );
    if ( imagePainter == null ) {
      imagePainter = new ImagePainter( context, g, x, y, w, h, imagePath, sourceInsets, destInsets, true, false );
      painters.put( key, imagePainter );
    }
    imagePainter.paint( g );
  }

  /**
   * Paints the border of an internal frame.
   *
   * @param context SynthContext identifying the <code>JComponent</code> and
   *        <code>Region</code> to paint to
   * @param g <code>Graphics</code> to paint to
   * @param x X coordinate of the area to paint to
   * @param y Y coordinate of the area to paint to
   * @param w Width of the area to paint to
   * @param h Height of the area to paint to
   */
  public void paintInternalFrameBorder( SynthContext context, Graphics g, int x, int y, int w, int h )
  {
    String imagePath = ( String ) UIManager.get( "InternalFrameBorderBackground.background" );
    JInternalFrame frame = ( JInternalFrame ) context.getComponent();
    frame.setOpaque( false );
    if ( frame.isSelected() )
      imagePath = ( String ) UIManager.get( "InternalFrameBorderBackgroundSelected.background" );
   
    Insets sourceInsets = new Insets( 3, 3, 3, 3 );
    Insets destInsets = new Insets( 3, 3, 3, 3 );
    String key = imagePath + "_" + w + "_" + h;
    ImagePainter imagePainter = (ImagePainter)painters.get( key );
    if ( imagePainter == null ) {
      imagePainter = new ImagePainter( context, g, x, y, w, h, imagePath, sourceInsets, destInsets, true, false );
      painters.put( key, imagePainter );
    }
    imagePainter.paint( g );
  }

  /**
   * Paints the internal frame title background.
   *
   * @param context SynthContext identifying the <code>JComponent</code> and
   *        <code>Region</code> to paint to
   * @param g <code>Graphics</code> to paint to
   * @param x X coordinate of the area to paint to
   * @param y Y coordinate of the area to paint to
   * @param w Width of the area to paint to
   * @param h Height of the area to paint to
   */
  public void paintInternalFrameTitlePaneBackground( SynthContext context, Graphics g, int x, int y, int w, int h )
  {
    BasicInternalFrameTitlePane pane = (BasicInternalFrameTitlePane)context.getComponent();

    int cc = pane.getComponentCount();
    for ( int i = 0; i< cc; i++ ) {
      JComponent c = (JComponent)pane.getComponent( i );
      c.setBorder( null);
      //c.setPreferredSize( new java.awt.Dimension( 16, 16 ));
    }
    pane.getLayout().minimumLayoutSize( pane );
   
    java.awt.Component parent = pane.getParent();
    String title = "";
    if ( parent instanceof JInternalFrame )
      title = ( ( JInternalFrame ) parent ).getTitle();
    else {
      JInternalFrame.JDesktopIcon desktopIcon = ( JInternalFrame.JDesktopIcon ) parent;
      title = desktopIcon.getInternalFrame().getTitle();
    }

    String imagePath = ( String ) UIManager.get( "InternalFrameTitlePane.background" );
    if ( ( context.getComponentState() & SynthConstants.SELECTED ) > 0 )
      imagePath = ( String ) UIManager.get( "InternalFrameTitlePaneSelected.background" );
   
    Insets sourceInsets = new Insets( 0, 0, 0, 0 );
    Insets destInsets = new Insets( 0, 1, 0, 0 );
    String key = imagePath + "_" + w + "_" + h;
    ImagePainter imagePainter = (ImagePainter)painters.get( key );
    if ( imagePainter == null ) {
      imagePainter = new ImagePainter( context, g, x, y, w, h, imagePath, sourceInsets, destInsets, true, true );
      painters.put( key, imagePainter );
    }
    imagePainter.paint( g );
   
    /* JDK 6 b104 now takes care of this painting, and uses the property TextForeground */
    if ( XuiUtilities.getMinorVersion() < 6 ) {
      Color titleTextColor = UIManager.getColor( "InternalFrameTitlePaneText.forground" );
      g.setColor( titleTextColor );
      g.drawString( title, 25, 14 );
    }
  }
}
TOP

Related Classes of net.xoetrope.optional.laf.synth.BackgroundPainter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.