ProgressBar: support square painting and larger height even if no string is painted

This commit is contained in:
Karl Tauber
2020-01-15 17:13:39 +01:00
parent fe15078bbd
commit f9accc2a7a
3 changed files with 51 additions and 2 deletions

View File

@@ -23,6 +23,9 @@ FlatLaf Change Log
and "Flat Darcula" themes. and "Flat Darcula" themes.
- TabbedPane: Support per component tab height (set client property - TabbedPane: Support per component tab height (set client property
`JTabbedPane.tabHeight` to an integer). `JTabbedPane.tabHeight` to an integer).
- ProgressBar: Support square painting (set client property
`JProgressBar.square` to `true`) and larger height even if no string is
painted (set client property `JProgressBar.largeHeight` to `true`).
## 0.24 ## 0.24

View File

@@ -93,6 +93,22 @@ public interface FlatClientProperties
*/ */
String MINIMUM_HEIGHT = "JComponent.minimumHeight"; String MINIMUM_HEIGHT = "JComponent.minimumHeight";
/**
* Specifies whether the progress bar has always the larger height even if no string is painted.
* <p>
* <strong>Component</strong> {@link javax.swing.JProgressBar}<br>
* <strong>Value type</strong> {@link java.lang.Boolean}
*/
String PROGRESS_BAR_LARGE_HEIGHT = "JProgressBar.largeHeight";
/**
* Specifies whether the progress bar is paint with square edges.
* <p>
* <strong>Component</strong> {@link javax.swing.JProgressBar}<br>
* <strong>Value type</strong> {@link java.lang.Boolean}
*/
String PROGRESS_BAR_SQUARE = "JProgressBar.square";
/** /**
* Specifies whether the decrease/increase arrow buttons of a scrollbar are shown. * Specifies whether the decrease/increase arrow buttons of a scrollbar are shown.
* <p> * <p>

View File

@@ -16,6 +16,7 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.FlatClientProperties.*;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.Graphics; import java.awt.Graphics;
@@ -23,6 +24,7 @@ import java.awt.Graphics2D;
import java.awt.Insets; import java.awt.Insets;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D; import java.awt.geom.RoundRectangle2D;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JProgressBar; import javax.swing.JProgressBar;
import javax.swing.LookAndFeel; import javax.swing.LookAndFeel;
@@ -60,6 +62,8 @@ public class FlatProgressBarUI
protected Dimension horizontalSize; protected Dimension horizontalSize;
protected Dimension verticalSize; protected Dimension verticalSize;
private PropertyChangeListener propertyChangeListener;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatProgressBarUI(); return new FlatProgressBarUI();
} }
@@ -75,11 +79,35 @@ public class FlatProgressBarUI
verticalSize = UIManager.getDimension( "ProgressBar.verticalSize" ); verticalSize = UIManager.getDimension( "ProgressBar.verticalSize" );
} }
@Override
protected void installListeners() {
super.installListeners();
propertyChangeListener = e -> {
switch( e.getPropertyName() ) {
case PROGRESS_BAR_LARGE_HEIGHT:
case PROGRESS_BAR_SQUARE:
progressBar.revalidate();
progressBar.repaint();
break;
}
};
progressBar.addPropertyChangeListener( propertyChangeListener );
}
@Override
protected void uninstallListeners() {
super.uninstallListeners();
progressBar.removePropertyChangeListener( propertyChangeListener );
propertyChangeListener = null;
}
@Override @Override
public Dimension getPreferredSize( JComponent c ) { public Dimension getPreferredSize( JComponent c ) {
Dimension size = super.getPreferredSize( c ); Dimension size = super.getPreferredSize( c );
if( progressBar.isStringPainted() ) { if( progressBar.isStringPainted() || clientPropertyBoolean( c, PROGRESS_BAR_LARGE_HEIGHT, false ) ) {
// recalculate progress height/width to make it smaller // recalculate progress height/width to make it smaller
Insets insets = progressBar.getInsets(); Insets insets = progressBar.getInsets();
FontMetrics fm = progressBar.getFontMetrics( progressBar.getFont() ); FontMetrics fm = progressBar.getFontMetrics( progressBar.getFont() );
@@ -122,7 +150,9 @@ public class FlatProgressBarUI
return; return;
boolean horizontal = (progressBar.getOrientation() == JProgressBar.HORIZONTAL); boolean horizontal = (progressBar.getOrientation() == JProgressBar.HORIZONTAL);
int arc = Math.min( UIScale.scale( this.arc ), horizontal ? height : width ); int arc = clientPropertyBoolean( c, PROGRESS_BAR_SQUARE, false )
? 0
: Math.min( UIScale.scale( this.arc ), horizontal ? height : width );
FlatUIUtils.setRenderingHints( (Graphics2D) g ); FlatUIUtils.setRenderingHints( (Graphics2D) g );