Skip to content

Commit

Permalink
Decoration
Browse files Browse the repository at this point in the history
- BasicHTML.java - Replacement for Swing `javax.swing.plaf.basic.BasicHTML` added to decrease amount of redundant component repaints and allow customizing basic HTML foreground color [ #420 ]
- AbstractTextContent.java - Now contains basic HTML `View` retrieval and check based on newly added `BasicHTML` class
- Removed redundant `javax.swing.plaf.basic.BasicHTML` utility class usage from all text content implementations
- AbstractTextContent.java, AbstractStyledTextContent.java - Fixed text Y coordinate calculation algorithms, all centered texts should be positioned correctly
- AbstractStyledTextContent.java - Fixed possibility of additional trimmed parts appearance in text containing different styles
- AbstractTextContent.java - Moved paint color setup to the main painting method
- AbstractTextContent.java - Added proper checks for horizontal and vertical alignments
- AbstractContent.java - Restored proper content clipping

ToolTip
- WebToolTipUI.java - Removed redundant HTML content updates, removed basic UI usage, added proper basic settings initialization
- WToolTipUI.java, WebToolTip.java - New appropriate intermediate UI for `WebToolTip` component
- tooltip.xml - Enhanced dark skin tooltip colors to emphasize it amongst other UI elements

StyledLabel
- StyleRanges.java - Fixed NPE for the `null` text within any component using styled text content

Skins
- button.xml, togglebutton.xml, splitbutton.xml, checkbox.xml, radiobutton.xml, tristatecheckbox.xml - Removed redundant copy-pasted icon definition

Utilities
- SwingUtils.java - Updated `isUIResource` method to accept any objects as there are multiple `UIResource` types available
  • Loading branch information
mgarin committed Nov 11, 2016
1 parent 03ae6ac commit 462b375
Show file tree
Hide file tree
Showing 34 changed files with 1,160 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.alee.extended.label;

import com.alee.painter.decoration.DecorationException;
import com.alee.painter.decoration.IDecoration;
import com.alee.painter.decoration.content.AbstractTextContent;
import com.alee.utils.*;
Expand Down Expand Up @@ -45,6 +46,7 @@ public abstract class AbstractStyledTextContent<E extends JComponent, D extends
/**
* todo 1. Implement minimum rows count
* todo 2. Implement custom colors for custom style elements
* todo 3. Implement different fonts for text parts
*/

/**
Expand Down Expand Up @@ -223,9 +225,12 @@ protected void paintText ( final Graphics2D g2d, final Rectangle bounds, final E
{
if ( textRanges != null )
{
// Text rows gap
final int rg = Math.max ( 0, getRowGap ( c, d ) );

// Calculating text bounds coordinates
final int x = bounds.x;
int y = bounds.y;
final int rg = Math.max ( 0, getRowGap ( c, d ) );

// Layout the text
final List<StyledTextRow> rows = layout ( c, d, bounds );
Expand All @@ -243,17 +248,24 @@ protected void paintText ( final Graphics2D g2d, final Rectangle bounds, final E
th += row.height + rg;
}

// Adjusting vertical position according to alignment
if ( th < bounds.height )
{
switch ( va )
{
case TOP:
break;

case CENTER:
y += ( bounds.height - th ) / 2;
y += Math.ceil ( ( bounds.height - th ) / 2.0 );
break;

case BOTTOM:
y += bounds.height - th;
break;

default:
throw new DecorationException ( "Incorrect vertical alignment provided: " + va );
}
}
}
Expand Down Expand Up @@ -498,38 +510,37 @@ else if ( row.isEmpty () )
protected void paintRow ( final E c, final D d, final Graphics2D g2d, final Rectangle bounds, final int textX, final int textY,
final StyledTextRow row, final boolean isLast )
{
int horizontalAlignment = getHorizontalAlignment ( c, d );
final boolean ltr = c.getComponentOrientation ().isLeftToRight ();
if ( ( horizontalAlignment == SwingConstants.TRAILING && !ltr ) || ( horizontalAlignment == SwingConstants.LEADING && ltr ) )
{
horizontalAlignment = SwingConstants.LEFT;
}
else if ( ( horizontalAlignment == SwingConstants.LEADING && !ltr ) || ( horizontalAlignment == SwingConstants.TRAILING && ltr ) )
{
horizontalAlignment = SwingConstants.RIGHT;
}
// Painting settings
final Font font = c.getFont ();
final int defaultFontSize = font.getSize ();
final FontMetrics fm = c.getFontMetrics ( font );
final TextWrap wt = getWrapType ( c, d );
final int ha = getAdjustedHorizontalAlignment ( c, d );

// Calculating text X coordinate
int x = textX;

if ( bounds.width > row.width )
{
switch ( horizontalAlignment )
switch ( ha )
{
case LEFT:
break;

case CENTER:
x += ( bounds.width - row.width ) / 2;
x += Math.floor ( ( bounds.width - row.width ) / 2.0 );
break;

case RIGHT:
x += bounds.width - row.width;
break;

default:
throw new DecorationException ( "Incorrect horizontal alignment provided: " + ha );
}
}

final Font font = c.getFont ();
final int defaultFontSize = font.getSize ();
final FontMetrics fm = c.getFontMetrics ( font );
final TextWrap wt = getWrapType ( c, d );
final boolean truncated = isTruncated ( c, d );
// Painting styled text fragments
int charDisplayed = 0;

for ( final TextRange textRange : row.fragments )
{
final StyleRange style = textRange.getStyleRange ();
Expand All @@ -550,20 +561,28 @@ else if ( ( horizontalAlignment == SwingConstants.LEADING && !ltr ) || ( horizon
final int strWidth = cfm.stringWidth ( s );

// Checking mnemonic
int mneIndex = -1;
int mnemonicIndex = -1;
if ( row.mnemonic >= 0 && row.mnemonic < charDisplayed + s.length () )
{
mneIndex = row.mnemonic - charDisplayed;
mnemonicIndex = row.mnemonic - charDisplayed;
}

// Checking trim needs
final int availableWidth = bounds.width + bounds.x - x;
if ( truncated && availableWidth < strWidth &&
( wt == TextWrap.none || wt == TextWrap.word || isLast ) )
// Checking whether or not text should be truncated
final boolean truncated;
if ( isTruncate ( c, d ) )
{
// Clip string
s = SwingUtilities.layoutCompoundLabel ( cfm, s, null, 0, horizontalAlignment, 0, 0,
new Rectangle ( x, y, availableWidth, bounds.height ), new Rectangle (), new Rectangle (), 0 );
final int availableWidth = bounds.width + bounds.x - x;
truncated = availableWidth < strWidth && ( wt == TextWrap.none || wt == TextWrap.word || isLast );
if ( truncated )
{
// Clip string
s = SwingUtilities.layoutCompoundLabel ( cfm, s, null, 0, ha, 0, 0,
new Rectangle ( x, y, availableWidth, bounds.height ), new Rectangle (), new Rectangle (), 0 );
}
}
else
{
truncated = false;
}

// Starting of actual painting
Expand All @@ -590,7 +609,14 @@ else if ( style.isSubscript () )
final boolean useStyleForeground = style != null && !isIgnoreColorSettings ( c, d ) && style.getForeground () != null;
final Color textColor = useStyleForeground ? style.getForeground () : getColor ( c, d );
g2d.setPaint ( textColor );
paintStyledTextFragment ( c, d, g2d, s, x, y, mneIndex, cfm, style, strWidth );
paintStyledTextFragment ( c, d, g2d, s, x, y, mnemonicIndex, cfm, style, strWidth );

// Stop on truncated part
// Otherwise we might end up having two truncated parts
if ( truncated )
{
break;
}

x += strWidth;
charDisplayed += s.length ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.alee.extended.label;

import com.alee.painter.decoration.IDecoration;
import com.alee.painter.decoration.content.ContentPropertyListener;
import com.alee.utils.SwingUtils;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

Expand All @@ -37,7 +38,7 @@
* @see <a href="https://github.com/mgarin/weblaf/wiki/How-to-use-WebStyledLabel">How to use WebStyledLabel</a>
*/

@SuppressWarnings ( "UnusedParameters" )
@SuppressWarnings ("UnusedParameters")
public abstract class SimpleStyledTextContent<E extends JComponent, D extends IDecoration<E, D>, I extends SimpleStyledTextContent<E, D, I>>
extends AbstractStyledTextContent<E, D, I>
{
Expand All @@ -63,6 +64,11 @@ public abstract class SimpleStyledTextContent<E extends JComponent, D extends ID
*/
protected transient Integer mnemonicIndex;

/**
* Component property change listener.
*/
protected transient ContentPropertyListener<E, D> contentListener;

@Override
public void activate ( final E c, final D d )
{
Expand All @@ -71,11 +77,17 @@ public void activate ( final E c, final D d )

// Performing default actions
super.activate ( c, d );

// Installing content property listener
installContentPropertyListener ( c, d );
}

@Override
public void deactivate ( final E c, final D d )
{
// Uninstalling content property listener
uninstallContentPropertyListener ( c, d );

// Performing default actions
super.deactivate ( c, d );

Expand Down Expand Up @@ -114,6 +126,55 @@ protected void destroyContentCache ( final E c, final D d )
styleRanges = null;
}

/**
* Installs content property listener.
*
* @param c painted component
* @param d painted decoration state
*/
protected void installContentPropertyListener ( final E c, final D d )
{
// Adding text change listener
final String property = getStyledTextProperty ();
if ( property != null )
{
contentListener = new ContentPropertyListener<E, D> ( c, d )
{
@Override
public void propertyChange ( final E c, final D d, final String property, final Object oldValue, final Object newValue )
{
updateContentCache ( c, d );
}
};
c.addPropertyChangeListener ( property, contentListener );
}
}

/**
* Uninstalls content property listener.
*
* @param c painted component
* @param d painted decoration state
*/
protected void uninstallContentPropertyListener ( final E c, final D d )
{
// Removing text change listener
final String property = getStyledTextProperty ();
if ( property != null )
{
c.removePropertyChangeListener ( property, contentListener );
contentListener = null;
}
}

/**
* Returns name of the property that contains styled text.
* It is used for registering content property listener that updates style caches.
*
* @return name of the property that contains styled text
*/
protected abstract String getStyledTextProperty ();

/**
* Performs style caches update.
*
Expand Down
79 changes: 43 additions & 36 deletions modules/ui/src/com/alee/extended/label/StyleRanges.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,59 +86,66 @@ public List<StyleRange> getStyleRanges ()
protected StyleRanges parseStyledText ()
{
// Parse only if it is needed and it wasn't already completed
if ( !TextUtils.isEmpty ( styledText ) && plainText == null )
if ( styleRanges == null )
{
styleRanges = new ArrayList<StyleRange> ();
int begin = nextUnescaped ( styledText, "{", 0 );
if ( begin != -1 )
if ( !TextUtils.isEmpty ( styledText ) )
{
plainText = "";
String trimmedText = styledText;
while ( begin != -1 )
int begin = nextUnescaped ( styledText, "{", 0 );
if ( begin != -1 )
{
final int end = nextUnescaped ( trimmedText, "}", begin + 1 );
if ( end != -1 )
plainText = "";
String trimmedText = styledText;
while ( begin != -1 )
{
// Clipping statement
final String statement = trimmedText.substring ( begin + 1, end );
if ( statement.equals ( "br" ) )
final int end = nextUnescaped ( trimmedText, "}", begin + 1 );
if ( end != -1 )
{
// Adding linebreak and proceeding
plainText += trimmedText.substring ( 0, begin ) + "\n";
}
else
{
// Parsing possible style syntax
final TextRange range = parseStatement ( plainText.length () + begin, statement );
if ( range != null && range.getStyleRange () != null )
// Clipping statement
final String statement = trimmedText.substring ( begin + 1, end );
if ( statement.equals ( "br" ) )
{
// Adding text and style range
plainText += trimmedText.substring ( 0, begin ) + range.getText ();
styleRanges.add ( range.getStyleRange () );
// Adding linebreak and proceeding
plainText += trimmedText.substring ( 0, begin ) + "\n";
}
else
{
// Adding plain text
plainText += trimmedText.substring ( 0, begin ) + statement;
// Parsing possible style syntax
final TextRange range = parseStatement ( plainText.length () + begin, statement );
if ( range != null && range.getStyleRange () != null )
{
// Adding text and style range
plainText += trimmedText.substring ( 0, begin ) + range.getText ();
styleRanges.add ( range.getStyleRange () );
}
else
{
// Adding plain text
plainText += trimmedText.substring ( 0, begin ) + statement;
}
}
}

// Continue to next
trimmedText = trimmedText.substring ( end + 1 );
begin = nextUnescaped ( trimmedText, "{", 0 );
}
else
{
// Something wrong with the syntax
// Abort parsing and add the rest as plain text
break;
// Continue to next
trimmedText = trimmedText.substring ( end + 1 );
begin = nextUnescaped ( trimmedText, "{", 0 );
}
else
{
// Something wrong with the syntax
// Abort parsing and add the rest as plain text
break;
}
}
plainText += trimmedText;
}
else
{
plainText = styledText;
}
plainText += trimmedText;
}
else
{
plainText = styledText;
plainText = null;
}
}
return this;
Expand Down
Loading

0 comments on commit 462b375

Please sign in to comment.