Skip to content

Commit

Permalink
Painter and margin support for WebSliderUI and WebSlider
Browse files Browse the repository at this point in the history
  • Loading branch information
mgarin committed Oct 15, 2013
1 parent ae65675 commit 2ebe70d
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 42 deletions.
7 changes: 3 additions & 4 deletions src/com/alee/laf/label/WebLabelUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,18 @@ public void setTransparency ( final Float transparency )
public void paint ( final Graphics g, final JComponent c )
{
final Graphics2D g2d = ( Graphics2D ) g;
final Composite oc = LafUtils.setupAlphaComposite ( g2d, transparency, transparency != null );

// Use background painter instead of default UI graphics
// Force painter to draw background
if ( painter != null )
{
painter.paint ( ( Graphics2D ) g, SwingUtils.size ( c ), c );
painter.paint ( g2d, SwingUtils.size ( c ), c );
}

final Composite oc = LafUtils.setupAlphaComposite ( g2d, transparency, transparency != null );
final Map textHints = drawShade ? StyleConstants.defaultTextRenderingHints : StyleConstants.textRenderingHints;
final Map oldHints = SwingUtils.setupTextAntialias ( g2d, textHints );
super.paint ( g, c );
SwingUtils.restoreTextAntialias ( g2d, oldHints );

LafUtils.restoreComposite ( g2d, oc, transparency != null );
}

Expand Down
33 changes: 33 additions & 0 deletions src/com/alee/laf/slider/WebSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.alee.laf.slider;

import com.alee.extended.painter.Painter;
import com.alee.laf.WebLookAndFeel;
import com.alee.managers.settings.DefaultValue;
import com.alee.managers.settings.SettingsManager;
Expand Down Expand Up @@ -267,6 +268,38 @@ public void setThumbAngleLength ( int thumbAngleLength )
getWebUI ().setThumbAngleLength ( thumbAngleLength );
}

public Insets getMargin ()
{
return getWebUI ().getMargin ();
}

public void setMargin ( Insets margin )
{
getWebUI ().setMargin ( margin );
}

public WebSlider setMargin ( int top, int left, int bottom, int right )
{
setMargin ( new Insets ( top, left, bottom, right ) );
return this;
}

public WebSlider setMargin ( int spacing )
{
return setMargin ( spacing, spacing, spacing, spacing );
}

public Painter getPainter ()
{
return getWebUI ().getPainter ();
}

public WebSlider setPainter ( Painter painter )
{
getWebUI ().setPainter ( painter );
return this;
}

public WebSliderUI getWebUI ()
{
return ( WebSliderUI ) getUI ();
Expand Down
171 changes: 133 additions & 38 deletions src/com/alee/laf/slider/WebSliderUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

package com.alee.laf.slider;

import com.alee.extended.painter.Painter;
import com.alee.extended.painter.PainterSupport;
import com.alee.laf.StyleConstants;
import com.alee.laf.WebLookAndFeel;
import com.alee.laf.label.WebLabelStyle;
import com.alee.utils.ColorUtils;
import com.alee.utils.LafUtils;
import com.alee.utils.SwingUtils;
import com.alee.utils.swing.BorderMethods;
import com.alee.utils.swing.WebTimer;

import javax.swing.*;
Expand All @@ -33,47 +38,59 @@
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
* User: mgarin Date: 26.05.11 Time: 18:03
*/

public class WebSliderUI extends BasicSliderUI
public class WebSliderUI extends BasicSliderUI implements BorderMethods
{
public static final int MAX_DARKNESS = 5;

private Color trackBgTop = WebSliderStyle.trackBgTop;
private Color trackBgBottom = WebSliderStyle.trackBgBottom;
private int trackHeight = WebSliderStyle.trackHeight;
private int trackRound = WebSliderStyle.trackRound;
private int trackShadeWidth = WebSliderStyle.trackShadeWidth;

private boolean drawProgress = WebSliderStyle.drawProgress;
private int progressRound = WebSliderStyle.progressRound;
private int progressShadeWidth = WebSliderStyle.progressShadeWidth;

private boolean drawThumb = WebSliderStyle.drawThumb;
private Color thumbBgTop = WebSliderStyle.thumbBgTop;
private Color thumbBgBottom = WebSliderStyle.thumbBgBottom;
private int thumbWidth = WebSliderStyle.thumbWidth;
private int thumbHeight = WebSliderStyle.thumbHeight;
private int thumbRound = WebSliderStyle.thumbRound;
private int thumbShadeWidth = WebSliderStyle.thumbShadeWidth;
private boolean angledThumb = WebSliderStyle.angledThumb;
private boolean sharpThumbAngle = WebSliderStyle.sharpThumbAngle;
private int thumbAngleLength = WebSliderStyle.thumbAngleLength;

private boolean animated = WebSliderStyle.animated;

private boolean rolloverDarkBorderOnly = WebSliderStyle.rolloverDarkBorderOnly;

private MouseWheelListener mouseWheelListener;
private ChangeListener changeListener;
private MouseAdapter mouseAdapter;

private boolean rollover = false;
private int rolloverDarkness = 0;
private WebTimer rolloverTimer;
protected Color trackBgTop = WebSliderStyle.trackBgTop;
protected Color trackBgBottom = WebSliderStyle.trackBgBottom;
protected int trackHeight = WebSliderStyle.trackHeight;
protected int trackRound = WebSliderStyle.trackRound;
protected int trackShadeWidth = WebSliderStyle.trackShadeWidth;

protected boolean drawProgress = WebSliderStyle.drawProgress;
protected int progressRound = WebSliderStyle.progressRound;
protected int progressShadeWidth = WebSliderStyle.progressShadeWidth;

protected boolean drawThumb = WebSliderStyle.drawThumb;
protected Color thumbBgTop = WebSliderStyle.thumbBgTop;
protected Color thumbBgBottom = WebSliderStyle.thumbBgBottom;
protected int thumbWidth = WebSliderStyle.thumbWidth;
protected int thumbHeight = WebSliderStyle.thumbHeight;
protected int thumbRound = WebSliderStyle.thumbRound;
protected int thumbShadeWidth = WebSliderStyle.thumbShadeWidth;
protected boolean angledThumb = WebSliderStyle.angledThumb;
protected boolean sharpThumbAngle = WebSliderStyle.sharpThumbAngle;
protected int thumbAngleLength = WebSliderStyle.thumbAngleLength;

protected boolean animated = WebSliderStyle.animated;

protected boolean rolloverDarkBorderOnly = WebSliderStyle.rolloverDarkBorderOnly;

protected Insets margin = WebLabelStyle.margin;
protected Painter painter = WebLabelStyle.painter;

/**
* Slider listeners.
*/
protected PropertyChangeListener propertyChangeListener;
protected MouseWheelListener mouseWheelListener;
protected ChangeListener changeListener;
protected MouseAdapter mouseAdapter;

/**
* Runtime variables.
*/
protected boolean rollover = false;
protected int rolloverDarkness = 0;
protected WebTimer rolloverTimer;

public WebSliderUI ( JSlider b )
{
Expand All @@ -93,6 +110,20 @@ public void installUI ( final JComponent c )
// Default settings
SwingUtils.setOrientation ( slider );
slider.setOpaque ( false );
slider.setForeground ( StyleConstants.textColor );
PainterSupport.installPainter ( slider, this.painter );
updateBorder ();

// Orientation change listener
propertyChangeListener = new PropertyChangeListener ()
{
@Override
public void propertyChange ( PropertyChangeEvent evt )
{
updateBorder ();
}
};
slider.addPropertyChangeListener ( WebLookAndFeel.COMPONENT_ORIENTATION_PROPERTY, propertyChangeListener );

// Rollover mouse wheel scroll
mouseWheelListener = new MouseWheelListener ()
Expand Down Expand Up @@ -198,6 +229,9 @@ public void mouseExited ( MouseEvent e )
@Override
public void uninstallUI ( JComponent c )
{
PainterSupport.uninstallPainter ( slider, this.painter );

slider.removePropertyChangeListener ( WebLookAndFeel.COMPONENT_ORIENTATION_PROPERTY, propertyChangeListener );
slider.removeMouseWheelListener ( mouseWheelListener );
slider.removeChangeListener ( changeListener );
slider.removeMouseListener ( mouseAdapter );
Expand All @@ -206,6 +240,31 @@ public void uninstallUI ( JComponent c )
super.uninstallUI ( c );
}

@Override
public void updateBorder ()
{
if ( slider != null )
{
// Actual margin
final boolean ltr = slider.getComponentOrientation ().isLeftToRight ();
final Insets m = new Insets ( margin.top, ltr ? margin.left : margin.right, margin.bottom, ltr ? margin.right : margin.left );

// Calculating additional borders
if ( painter != null )
{
// Painter borders
final Insets pi = painter.getMargin ( slider );
m.top += pi.top;
m.bottom += pi.bottom;
m.left += ltr ? pi.left : pi.right;
m.right += ltr ? pi.right : pi.left;
}

// Installing border
slider.setBorder ( LafUtils.createWebBorder ( m ) );
}
}

public boolean isAnimated ()
{
return animated;
Expand Down Expand Up @@ -406,6 +465,30 @@ public void setThumbAngleLength ( int thumbAngleLength )
this.thumbAngleLength = thumbAngleLength;
}

public Insets getMargin ()
{
return margin;
}

public void setMargin ( Insets margin )
{
this.margin = margin;
updateBorder ();
}

public Painter getPainter ()
{
return painter;
}

public void setPainter ( Painter painter )
{
PainterSupport.uninstallPainter ( slider, this.painter );
this.painter = painter;
PainterSupport.installPainter ( slider, this.painter );
updateBorder ();
}

@Override
protected Dimension getThumbSize ()
{
Expand All @@ -419,6 +502,18 @@ protected Dimension getThumbSize ()
}
}

@Override
public void paint ( Graphics g, JComponent c )
{
// Force painter to draw background
if ( painter != null )
{
painter.paint ( ( Graphics2D ) g, SwingUtils.size ( c ), c );
}

super.paint ( g, c );
}

@Override
public void paintFocus ( Graphics g )
{
Expand Down Expand Up @@ -465,17 +560,17 @@ public void paintThumb ( Graphics g )
}


private Color getBorderColor ()
protected Color getBorderColor ()
{
return ColorUtils.getIntermediateColor ( StyleConstants.borderColor, StyleConstants.darkBorderColor, getProgress () );
}

private float getProgress ()
protected float getProgress ()
{
return ( float ) rolloverDarkness / MAX_DARKNESS;
}

private Shape getThumbShape ()
protected Shape getThumbShape ()
{
if ( angledThumb && ( slider.getPaintLabels () || slider.getPaintTicks () ) )
{
Expand Down Expand Up @@ -631,7 +726,7 @@ public void paintTrack ( Graphics g )
LafUtils.restoreAntialias ( g2d, aa );
}

private Shape getTrackShape ()
protected Shape getTrackShape ()
{
if ( trackRound > 0 )
{
Expand Down Expand Up @@ -661,7 +756,7 @@ private Shape getTrackShape ()
}
}

private Shape getProgressShape ()
protected Shape getProgressShape ()
{
if ( trackRound > 0 )
{
Expand Down

0 comments on commit 2ebe70d

Please sign in to comment.