Skip to content

Commit

Permalink
Remove get/set icon APIs from command button
Browse files Browse the repository at this point in the history
For #447
  • Loading branch information
kirill-grouchnikov committed May 31, 2023
1 parent d6bd896 commit 775c49d
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ public class JCommandButton extends JComponent implements RichTooltipManager.Wit
@SuppressWarnings("rawtypes")
private BaseCommandButtonPresentationModel commandPresentation;

/**
* Associated icon.
*
* @see #setIcon(RadianceIcon)
* @see #getIcon()
*/
private RadianceIcon icon;

/**
* The button text.
*
Expand Down Expand Up @@ -491,9 +483,6 @@ public JCommandButton(BaseCommandButtonProjection<? extends BaseCommand<?>,
this.setText(command.getText());
this.setExtraText(command.getExtraText());

this.setIcon((command.getIconFactory() != null)
? command.getIconFactory().createNewIcon()
: null);
RadianceThemingCortex.ComponentScope.setIconFilterStrategies(this,
commandPresentation.getActiveIconFilterStrategy(),
commandPresentation.getEnabledIconFilterStrategy(),
Expand Down Expand Up @@ -624,6 +613,15 @@ public CommandButtonUI getUI() {
return this.projection;
}

public BaseCommand<? extends BaseCommandMenuContentModel> getContentModel() {
return this.projection.getContentModel();
}

public BaseCommandButtonPresentationModel<? extends BaseCommandPopupMenuPresentationModel,
? extends BaseCommandButtonPresentationModel<?, ?>> getPresentationModel() {
return this.projection.getPresentationModel();
}

/**
* Sets new presentation state for <code>this</code> button. Fires a
* <code>presentationState</code> property change event.
Expand All @@ -638,38 +636,6 @@ public void setPresentationState(CommandButtonPresentationState state) {
this.firePropertyChange("presentationState", old, this.presentationState);
}

/**
* Returns the associated icon.
*
* @return The associated icon.
* @see #setIcon(RadianceIcon)
*/
public RadianceIcon getIcon() {
return icon;
}

/**
* Sets new icon for this button. Fires an <code>icon</code> property change
* event.
*
* @param defaultIcon New default icon for this button.
* @see #getIcon()
*/
public void setIcon(RadianceIcon defaultIcon) {
RadianceIcon oldValue = this.icon;
this.icon = defaultIcon;

firePropertyChange("icon", oldValue, defaultIcon);
if (defaultIcon != oldValue) {
if (defaultIcon == null || oldValue == null
|| defaultIcon.getIconWidth() != oldValue.getIconWidth()
|| defaultIcon.getIconHeight() != oldValue.getIconHeight()) {
revalidate();
}
repaint();
}
}

/**
* Return the current presentation state of <code>this</code> button.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,16 @@ public void fireRolloverActionPerformed(ActionEvent e) {
* Tracks possible usage of glowing icon.
*/
private void trackGlowingIcon() {
RadianceIcon currIcon = this.commandButton.getIcon();
if (currIcon instanceof GlowingRadianceIcon)
if (this.icon instanceof GlowingRadianceIcon)
return;
if (currIcon == null)
if (this.icon == null)
return;
boolean isPopupOnly = (this.commandButton.getCommandButtonKind() ==
JCommandButton.CommandButtonKind.POPUP_ONLY);
StateTransitionTracker tracker = isPopupOnly
? this.radianceVisualStateTracker.getPopupStateTransitionTracker()
: this.radianceVisualStateTracker.getActionStateTransitionTracker();
this.glowingIcon = new GlowingRadianceIcon(currIcon, tracker.getIconGlowTracker());
this.glowingIcon = new GlowingRadianceIcon(this.icon, tracker.getIconGlowTracker());
}

private void paintButtonBackground(Graphics graphics) {
Expand All @@ -303,15 +302,14 @@ private void paintButtonBackground(Graphics graphics) {
}

private void paintButtonIconRegular(Graphics g, Rectangle iconRect, Color textColor) {
Icon icon = this.commandButton.getIcon();
if ((iconRect == null) || (icon == null) || (iconRect.width == 0)
if ((iconRect == null) || (this.icon == null) || (iconRect.width == 0)
|| (iconRect.height == 0)) {
return;
}

Graphics2D g2d = (Graphics2D) g.create();

GhostPaintingUtils.paintGhostIcon(g2d, this.commandButton, icon, iconRect);
GhostPaintingUtils.paintGhostIcon(g2d, this.commandButton, this.icon, iconRect);
g2d.setComposite(WidgetUtilities.getAlphaComposite(this.commandButton, g));

StateTransitionTracker tracker = this.radianceVisualStateTracker
Expand All @@ -322,7 +320,7 @@ private void paintButtonIconRegular(Graphics g, Rectangle iconRect, Color textCo
model = this.commandButton.getPopupModel();
}
this.commandButtonBackgroundDelegate.paintCommandButtonIcon(g2d, iconRect,
this.commandButton, icon, this.glowingIcon, model, tracker, textColor);
this.commandButton, this.icon, this.glowingIcon, model, tracker, textColor);
g2d.dispose();
}

Expand Down Expand Up @@ -369,8 +367,7 @@ protected void paintButtonIcon(Graphics g, Rectangle iconRect, Color textColor)
}
this.paintButtonIconRegular(g, iconRect, textColor);
// does it actually have an icon?
Icon iconToPaint = this.commandButton.getIcon();
if (showSelectionAroundIcon && (iconToPaint == null)) {
if (showSelectionAroundIcon && (this.icon == null)) {
// draw a checkmark
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
Expand Down Expand Up @@ -456,7 +453,7 @@ protected boolean isPaintingSeparators() {
if (super.isPaintingSeparators()) {
return true;
}
boolean hasIcon = (this.commandButton.getIcon() != null);
boolean hasIcon = (this.icon != null);
return this.commandButton.hasFocus() || (hasIcon && (this.overallStateTransitionTracker
.getFacetStrength(ComponentStateFacet.ROLLOVER) > 0.0f));
}
Expand Down Expand Up @@ -668,10 +665,9 @@ private void paintPopupActionIcon(Graphics g, Rectangle popupActionRect) {

@Override
public Dimension getPreferredSize(JComponent c) {
JCommandButton button = (JCommandButton) c;
RadianceButtonShaper shaper = RadianceCoreUtilities.getButtonShaper(button);
RadianceButtonShaper shaper = RadianceCoreUtilities.getButtonShaper(this.commandButton);

Dimension superPref = super.getPreferredSize(button);
Dimension superPref = super.getPreferredSize(this.commandButton);
if (superPref == null)
return null;

Expand All @@ -683,13 +679,13 @@ public Dimension getPreferredSize(JComponent c) {
// not have min size enforced as well
// Additional fix - buttons in popup menus and breadcrumb bars should
// not have min size enforced
if ((button.getPresentationState() == CommandButtonPresentationState.MEDIUM)
&& (SwingUtilities.getAncestorOfClass(JRibbon.class, button) == null)
&& (SwingUtilities.getAncestorOfClass(JBreadcrumbBar.class, button) == null)
&& (SwingUtilities.getAncestorOfClass(AbstractPopupMenuPanel.class, button) == null)) {
JButton forSizing = new JButton(button.getText(), button.getIcon());
if ((this.commandButton.getPresentationState() == CommandButtonPresentationState.MEDIUM)
&& (SwingUtilities.getAncestorOfClass(JRibbon.class, this.commandButton) == null)
&& (SwingUtilities.getAncestorOfClass(JBreadcrumbBar.class, this.commandButton) == null)
&& (SwingUtilities.getAncestorOfClass(AbstractPopupMenuPanel.class, this.commandButton) == null)) {
JButton forSizing = new JButton(this.commandButton.getContentModel().getText(), this.icon);
Dimension result = shaper.getPreferredSize(forSizing, superPref);
if (ComponentUtilities.hasPopupAction(button)) {
if (ComponentUtilities.hasPopupAction(this.commandButton)) {
result.width = superPref.width;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,9 @@ private static Color getForegroundColor(JCommandButton button,

@Override
public Dimension getPreferredSize(JComponent c) {
JCommandButton button = (JCommandButton) c;

JButton forSizing = new JButton(button.getText(), button.getIcon());
JButton forSizing = new JButton(this.commandButton.getText(), this.icon);
Dimension result = forSizing.getUI().getPreferredSize(forSizing);
Insets borderInsets = button.getBorder().getBorderInsets(button);
Insets borderInsets = this.commandButton.getBorder().getBorderInsets(this.commandButton);
result.width += (borderInsets.left + borderInsets.right);
result.height += (borderInsets.top + borderInsets.bottom);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public abstract class BasicCommandButtonUI extends CommandButtonUI {

protected boolean isInnerFocusOnAction;

protected RadianceIcon icon;

/**
* Client property to mark the command button to not dispose the popups on activation.
*
Expand Down Expand Up @@ -164,6 +166,9 @@ protected void installDefaults() {
this.commandButton.setFont(RadianceThemingCortex.GlobalScope.getFontPolicy()
.getFontSet().getControlFont());
}

RadianceIcon.Factory iconFactory = this.commandButton.getContentModel().getIconFactory();
this.icon = (iconFactory != null) ? iconFactory.createNewIcon() : null;
this.syncIconDimension();

// Support for focus traversal inside command buttons that have action area
Expand Down Expand Up @@ -226,9 +231,8 @@ private void updateBorder() {
protected void installComponents() {
this.updatePopupActionIcon();

RadianceIcon buttonIcon = this.commandButton.getIcon();
if (buttonIcon instanceof AsynchronousLoading) {
((AsynchronousLoading) buttonIcon).addAsynchronousLoadListener((boolean success) -> {
if (this.icon instanceof AsynchronousLoading) {
((AsynchronousLoading) this.icon).addAsynchronousLoadListener((boolean success) -> {
if (success && (commandButton != null))
commandButton.repaint();
});
Expand Down Expand Up @@ -333,7 +337,8 @@ protected void installListeners() {
}
if ("iconFactory".equals(propertyChangeEvent.getPropertyName())) {
RadianceIcon.Factory factory = (RadianceIcon.Factory) propertyChangeEvent.getNewValue();
commandButton.setIcon((factory != null) ? factory.createNewIcon() : null);
icon = (factory != null) ? factory.createNewIcon() : null;
commandButton.repaint();
}
if ("isToggleSelected".equals(propertyChangeEvent.getPropertyName())) {
commandButton.getActionModel().setSelected((Boolean) propertyChangeEvent.getNewValue());
Expand Down Expand Up @@ -513,7 +518,7 @@ private void updateIconDimension() {
Dimension dimension = this.commandButton.getIconDimension();

if (dimension != null) {
this.commandButton.getIcon().setDimension(dimension);
this.icon.setDimension(dimension);
this.commandButton.setPresentationState(CommandButtonPresentationState.BIG_FIT_TO_ICON);

this.commandButton.invalidate();
Expand Down Expand Up @@ -677,8 +682,7 @@ public void processPopupAction() {
}

private void syncIconDimension() {
RadianceIcon icon = this.commandButton.getIcon();
if (icon == null) {
if (this.icon == null) {
return;
}

Expand All @@ -695,19 +699,19 @@ private void syncIconDimension() {

int iconWidth = (preferredIconSize != null) ? preferredIconSize.width : -1;
if (iconWidth < 0) {
iconWidth = this.commandButton.getIcon().getIconWidth();
iconWidth = this.icon.getIconWidth();
}
int iconHeight = (preferredIconSize != null) ? preferredIconSize.height : -1;
if (iconHeight < 0) {
iconHeight = this.commandButton.getIcon().getIconHeight();
iconHeight = this.icon.getIconHeight();
}

if ((icon.getIconWidth() == iconWidth) && (icon.getIconHeight() == iconHeight)) {
if ((this.icon.getIconWidth() == iconWidth) && (this.icon.getIconHeight() == iconHeight)) {
return;
}

Dimension newDim = new Dimension(iconWidth, iconHeight);
icon.setDimension(newDim);
this.icon.setDimension(newDim);
}

private void syncActionPreview(BaseCommand<?> command, Command.CommandActionPreview actionPreview) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public Dimension getPreferredIconSize(JCommandButton commandButton) {
}

protected int getCurrentIconWidth(JCommandButton commandButton) {
return (commandButton.getIcon() != null)
return (commandButton.getContentModel().getIconFactory() != null)
? this.getPreferredIconSize(commandButton).width
: 0;
}

protected int getCurrentIconHeight(JCommandButton commandButton) {
return (commandButton.getIcon() != null)
return (commandButton.getContentModel().getIconFactory() != null)
? this.getPreferredIconSize(commandButton).height
: 0;
}
Expand All @@ -91,7 +91,7 @@ public Dimension getPreferredSize(JCommandButton commandButton) {
int layoutHGap = ComponentUtilities.getHLayoutGap(commandButton);
int layoutVGap = ComponentUtilities.getVLayoutGap(commandButton);

boolean hasIcon = (commandButton.getIcon() != null);
boolean hasIcon = (commandButton.getContentModel().getIconFactory() != null);
boolean hasText = (this.titlePart1 != null);
boolean hasPopupIcon = ComponentUtilities.hasPopupAction(commandButton);

Expand Down Expand Up @@ -251,9 +251,7 @@ public CommandButtonLayoutInfo getLayoutInfo(JCommandButton commandButton) {

JCommandButton.CommandButtonKind buttonKind = commandButton.getCommandButtonKind();

RadianceIcon buttonIcon = commandButton.getIcon();

boolean hasIcon = (commandButton.getIcon() != null);
boolean hasIcon = (commandButton.getContentModel().getIconFactory() != null);
boolean hasText = (this.titlePart1 != null);
boolean hasPopupIcon = ComponentUtilities.hasPopupAction(commandButton);

Expand All @@ -272,8 +270,8 @@ public CommandButtonLayoutInfo getLayoutInfo(JCommandButton commandButton) {
if (hasIcon) {
y += layoutVGap;

int iconHeight = buttonIcon.getIconHeight();
int iconWidth = buttonIcon.getIconWidth();
int iconHeight = getCurrentIconHeight(commandButton);
int iconWidth = getCurrentIconWidth(commandButton);

result.iconRect.x = (width - iconWidth) / 2;
result.iconRect.y = y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected float getIconTextGapFactor() {
}

private boolean hasIcon(JCommandButton button) {
if (button.getIcon() != null) {
if (button.getContentModel().getIconFactory() != null) {
return true;
}
if (Boolean.TRUE.equals(button.getClientProperty(BasicCommandPopupMenuPanelUI.FORCE_ICON))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Dimension getPreferredSize(JCommandButton commandButton) {

int layoutHGap = ComponentUtilities.getHLayoutGap(commandButton);

boolean hasIcon = (commandButton.getIcon() != null);
boolean hasIcon = (commandButton.getContentModel().getIconFactory() != null);
boolean hasPopupIcon = ComponentUtilities.hasPopupAction(commandButton);

int prefIconWidth = hasIcon ? this.getPreferredIconSize(commandButton).width : 0;
Expand Down Expand Up @@ -177,10 +177,9 @@ public CommandButtonLayoutInfo getLayoutInfo(JCommandButton commandButton) {
}
}

RadianceIcon buttonIcon = commandButton.getIcon();
String buttonText = commandButton.getText();

boolean hasIcon = (buttonIcon != null);
boolean hasIcon = (commandButton.getContentModel().getIconFactory() != null);
boolean hasText = (buttonText != null);
boolean hasPopupIcon = ComponentUtilities.hasPopupAction(commandButton);

Expand All @@ -198,8 +197,9 @@ public CommandButtonLayoutInfo getLayoutInfo(JCommandButton commandButton) {
if (hasIcon) {
x += layoutHGap;

int iconHeight = buttonIcon.getIconHeight();
int iconWidth = buttonIcon.getIconWidth();
Dimension iconSize = getPreferredIconSize(commandButton);
int iconHeight = iconSize.height;
int iconWidth = iconSize.width;

result.iconRect.x = x;
result.iconRect.y = (height - iconHeight) / 2;
Expand Down Expand Up @@ -299,8 +299,9 @@ public CommandButtonLayoutInfo getLayoutInfo(JCommandButton commandButton) {
if (hasIcon) {
x -= layoutHGap;

int iconHeight = buttonIcon.getIconHeight();
int iconWidth = buttonIcon.getIconWidth();
Dimension iconSize = getPreferredIconSize(commandButton);
int iconHeight = iconSize.height;
int iconWidth = iconSize.width;

result.iconRect.x = x - iconWidth;
result.iconRect.y = (height - iconHeight) / 2;
Expand Down
Loading

0 comments on commit 775c49d

Please sign in to comment.