Skip to content

Commit

Permalink
Remove more APIs from command button
Browse files Browse the repository at this point in the history
* Content padding
* Horizontal and vertical gap scale factor
* Fire action on rollover

For #447
  • Loading branch information
kirill-grouchnikov committed Jun 1, 2023
1 parent ecab4b5 commit 4b17e30
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,6 @@ public class JCommandButton extends JComponent implements RichTooltipManager.Wit
*/
private Dimension iconDimension;

private Insets contentPadding;

/**
* Scale factor for horizontal gaps.
*
* @see #setHGapScaleFactor(double)
* @see #getHGapScaleFactor()
*/
private double hgapScaleFactor;

/**
* Scale factor for vertical gaps.
*
* @see #setVGapScaleFactor(double)
* @see #getVGapScaleFactor()
*/
private double vgapScaleFactor;

/**
* Location order kind for buttons placed in command button strips or for
* buttons that need the visuals of segmented strips.
Expand Down Expand Up @@ -233,16 +215,6 @@ public enum CommandButtonLocationOrderKind {
*/
private int autoRepeatSubsequentInterval;

/**
* Indicates that rollover should result in firing the action. Used in
* conjunction with the {@link #isAutoRepeatAction} can model quick pan
* buttons such as breadcrumb bar scrollers.
*
* @see #setFireActionOnRollover(boolean)
* @see #isFireActionOnRollover()
*/
private boolean isFireActionOnRollover;

/**
* Popup model of this button.
*
Expand Down Expand Up @@ -464,9 +436,6 @@ public JCommandButton(BaseCommandButtonProjection<? extends BaseCommand<?>,
}

this.setPresentationState(commandPresentation.getPresentationState());
this.setContentPadding(commandPresentation.getContentPadding());
this.setHGapScaleFactor(commandPresentation.getHorizontalGapScaleFactor());
this.setVGapScaleFactor(commandPresentation.getVerticalGapScaleFactor());
this.setFocusable(commandPresentation.isFocusable());
if (commandPresentation.getIconDimension() != null) {
this.setIconDimension(commandPresentation.getIconDimension());
Expand Down Expand Up @@ -517,8 +486,6 @@ public JCommandButton(BaseCommandButtonProjection<? extends BaseCommand<?>,
}
}

this.setFireActionOnRollover(commandPresentation.getActionFireTrigger() ==
CommandButtonPresentationModel.ActionFireTrigger.ON_ROLLOVER);
this.getActionModel().setFireActionOnPress(commandPresentation.getActionFireTrigger() ==
CommandButtonPresentationModel.ActionFireTrigger.ON_PRESSED);

Expand Down Expand Up @@ -789,114 +756,6 @@ protected void fireActionPerformed(ActionEvent event) {
}
}

/**
* Sets new content padding for this button. Fires an <code>contentPadding</code> property
* change event.
*
* @param contentPadding New content padding
* @see #getContentPadding()
*/
public void setContentPadding(Insets contentPadding) {
if (contentPadding == this.contentPadding)
return;
Insets oldValue = this.contentPadding;
this.contentPadding = contentPadding;
firePropertyChange("contentPadding", oldValue, this.contentPadding);
if (this.contentPadding != oldValue) {
revalidate();
repaint();
}
}

/**
* Returns the content padding for this button.
*
* @return The content padding for this button.
* @see #setContentPadding(Insets)
*/
public Insets getContentPadding() {
return this.contentPadding;
}

/**
* Sets new horizontal gap scale factor for the content of this button.
* Fires an <code>hgapScaleFactor</code> property change event.
*
* @param hgapScaleFactor New horizontal gap scale factor for the content of this
* button.
* @see #getHGapScaleFactor()
* @see #setVGapScaleFactor(double)
* @see #setGapScaleFactor(double)
*/
public void setHGapScaleFactor(double hgapScaleFactor) {
if (hgapScaleFactor == this.hgapScaleFactor)
return;
double oldValue = this.hgapScaleFactor;
this.hgapScaleFactor = hgapScaleFactor;
firePropertyChange("hgapScaleFactor", oldValue, this.hgapScaleFactor);
if (this.hgapScaleFactor != oldValue) {
revalidate();
repaint();
}
}

/**
* Sets new vertical gap scale factor for the content of this button. Fires
* a <code>vgapScaleFactor</code> property change event.
*
* @param vgapScaleFactor New vertical gap scale factor for the content of this button.
* @see #getVGapScaleFactor()
* @see #setHGapScaleFactor(double)
* @see #setGapScaleFactor(double)
*/
public void setVGapScaleFactor(double vgapScaleFactor) {
if (vgapScaleFactor == this.vgapScaleFactor)
return;
double oldValue = this.vgapScaleFactor;
this.vgapScaleFactor = vgapScaleFactor;
firePropertyChange("vgapScaleFactor", oldValue, this.vgapScaleFactor);
if (this.vgapScaleFactor != oldValue) {
revalidate();
repaint();
}
}

/**
* Sets new gap scale factor for the content of this button.
*
* @param gapScaleFactor New gap scale factor for the content of this button.
* @see #getHGapScaleFactor()
* @see #getVGapScaleFactor()
*/
public void setGapScaleFactor(double gapScaleFactor) {
setHGapScaleFactor(gapScaleFactor);
setVGapScaleFactor(gapScaleFactor);
}

/**
* Returns the horizontal gap scale factor for the content of this button.
*
* @return The horizontal gap scale factor for the content of this button.
* @see #setHGapScaleFactor(double)
* @see #setGapScaleFactor(double)
* @see #getVGapScaleFactor()
*/
public double getHGapScaleFactor() {
return this.hgapScaleFactor;
}

/**
* Returns the vertical gap scale factor for the content of this button.
*
* @return The vertical gap scale factor for the content of this button.
* @see #setVGapScaleFactor(double)
* @see #setGapScaleFactor(double)
* @see #getHGapScaleFactor()
*/
public double getVGapScaleFactor() {
return this.vgapScaleFactor;
}

/**
* Programmatically perform an action "click". This does the same thing as
* if the user had pressed and released the action area of the button.
Expand Down Expand Up @@ -1155,32 +1014,6 @@ public int getAutoRepeatSubsequentInterval() {
return autoRepeatSubsequentInterval;
}

/**
* Sets action-on-rollover mode. When this mode is on, button will fire
* action events when it gets rollover (instead of press). Combine with
* {@link #setAutoRepeatAction(boolean)} passing <code>true</code> to get
* auto-repeat action fired on rollover (useful for quicker manipulation of
* scroller buttons, for example).
*
* @param isFireActionOnRollover If <code>true</code>, the button is moved into the
* action-on-rollover mode.
* @see #isFireActionOnRollover()
*/
public void setFireActionOnRollover(boolean isFireActionOnRollover) {
this.isFireActionOnRollover = isFireActionOnRollover;
}

/**
* Returns indication whether this button is in action-on-rollover mode.
*
* @return <code>true</code> if this button is in action-on-rollover mode,
* <code>false</code> otherwise.
* @see #setFireActionOnRollover(boolean)
*/
public boolean isFireActionOnRollover() {
return this.isFireActionOnRollover;
}

/**
* Returns the popup model of this button.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public void setPressed(boolean b) {
stateMask &= ~PRESSED;
}

BaseCommand<?> command = this.commandButton.getProjection().getContentModel();
BaseCommand<?> command = this.commandButton.getContentModel();
BaseCommandButtonPresentationModel<?, ?> presentationModel =
this.commandButton.getPresentationModel();
if (command.isToggle() && isArmed()) {
// change selection prior to firing the action event on a toggle button
if (!this.isFireActionOnPress()) {
Expand All @@ -139,7 +141,7 @@ public void setPressed(boolean b) {
boolean toFireFirstAction = isArmed();
// if the button is in auto-repeat action mode, the action
// starts firing on press-down and not on press-up
if (commandButton.isAutoRepeatAction() || isFireActionOnPress())
if (presentationModel.isAutoRepeatAction() || isFireActionOnPress())
toFireFirstAction = isPressed() && toFireFirstAction;
else
toFireFirstAction = !isPressed() && toFireFirstAction;
Expand All @@ -148,7 +150,8 @@ public void setPressed(boolean b) {
if (commandButton.getCommandButtonKind() == JCommandButton.CommandButtonKind.POPUP_ONLY)
toFireFirstAction = false;

if (this.commandButton.isFireActionOnRollover()) {
if (presentationModel.getActionFireTrigger() ==
CommandButtonPresentationModel.ActionFireTrigger.ON_ROLLOVER) {
// the action is invoked on rollover
toFireFirstAction = false;
}
Expand All @@ -175,7 +178,8 @@ public void setPressed(boolean b) {

// we need to stop timer when the non-action-on-rollover button
// gets pressed=false and it is in auto-repeat mode
if (!this.commandButton.isFireActionOnRollover()) {
if (presentationModel.getActionFireTrigger() !=
CommandButtonPresentationModel.ActionFireTrigger.ON_ROLLOVER) {
if (this.commandButton.isAutoRepeatAction() && !b) {
this.stopActionTimer();
}
Expand All @@ -196,7 +200,10 @@ public void setRollover(boolean b) {
stateMask &= ~ROLLOVER;
}

if (this.commandButton.isFireActionOnRollover()) {
BaseCommandButtonPresentationModel<?, ?> presentationModel =
this.commandButton.getPresentationModel();
if (presentationModel.getActionFireTrigger() ==
CommandButtonPresentationModel.ActionFireTrigger.ON_ROLLOVER) {
if (b && !this.isActionTimerRunning()
&& (this.commandButton.getCommandButtonKind() != JCommandButton.CommandButtonKind
.POPUP_ONLY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.pushingpixels.radiance.component.api.common.*;
import org.pushingpixels.radiance.component.api.common.CommandButtonLayoutManager.CommandButtonLayoutInfo;
import org.pushingpixels.radiance.component.api.common.model.BaseCommand;
import org.pushingpixels.radiance.component.api.common.model.BaseCommandButtonPresentationModel;
import org.pushingpixels.radiance.component.api.common.model.Command;
import org.pushingpixels.radiance.component.api.common.model.PopupButtonModel;
import org.pushingpixels.radiance.component.api.common.popup.JCommandPopupMenuPanel;
Expand Down Expand Up @@ -221,9 +222,11 @@ public void actionPerformed(ActionEvent e) {
private void updateBorder() {
Border currBorder = this.commandButton.getBorder();
if ((currBorder == null) || (currBorder instanceof UIResource)) {
Insets contentPadding = this.commandButton.getContentPadding();
double hgapScaleFactor = this.commandButton.getHGapScaleFactor();
double vgapScaleFactor = this.commandButton.getVGapScaleFactor();
BaseCommandButtonPresentationModel presentationModel =
this.commandButton.getPresentationModel();
Insets contentPadding = presentationModel.getContentPadding();
double hgapScaleFactor = presentationModel.getHorizontalGapScaleFactor();
double vgapScaleFactor = presentationModel.getVerticalGapScaleFactor();

int top = (int) (vgapScaleFactor * contentPadding.top);
int bottom = (int) (vgapScaleFactor * contentPadding.bottom);
Expand Down Expand Up @@ -382,9 +385,6 @@ protected void installListeners() {
if ("isAutoRepeatAction".equals(propertyChangeEvent.getPropertyName())) {
commandButton.setAutoRepeatAction((Boolean) propertyChangeEvent.getNewValue());
}
if ("isFireActionOnRollover".equals(propertyChangeEvent.getPropertyName())) {
commandButton.setFireActionOnRollover((Boolean) propertyChangeEvent.getNewValue());
}
if ("isFireActionOnPress".equals(propertyChangeEvent.getPropertyName())) {
commandButton.getActionModel().setFireActionOnPress((Boolean) propertyChangeEvent.getNewValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ protected void installListeners() {

this.propertyChangeListener = propertyChangeEvent -> {
if ("scrollOnRollover".equals(propertyChangeEvent.getPropertyName())) {
boolean isScrollOnRollover = (Boolean) propertyChangeEvent.getNewValue();
leadingScroller.setFireActionOnRollover(isScrollOnRollover);
trailingScroller.setFireActionOnRollover(isScrollOnRollover);
scrollablePanel.remove(leadingScroller);
scrollablePanel.remove(trailingScroller);
recomputeScrollers();
}
};
this.scrollablePanel.addPropertyChangeListener(this.propertyChangeListener);
Expand Down Expand Up @@ -202,6 +202,13 @@ public void layoutContainer(Container parent) {
scheme);
}, new Dimension(arrowIconHeight, arrowIconWidth)))
.build();
this.recomputeScrollers();
}

private void recomputeScrollers() {
final int fontSize = RadianceSizeUtils.getComponentFontSize(null);
int arrowIconWidth = (int) RadianceSizeUtils.getSmallArrowIconWidth(fontSize);
int arrowIconHeight = (int) RadianceSizeUtils.getSmallDoubleArrowIconHeight(fontSize);

// Common scroller command presentation
CommandButtonPresentationModel scrollerActionsPresentation = CommandButtonPresentationModel.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public class ComponentUtilities {

public static int getHLayoutGap(JCommandButton commandButton) {
Font font = commandButton.getFont();
return (int) Math.ceil(commandButton.getHGapScaleFactor() * (font.getSize() - 4) / 4);
return (int) Math.ceil(commandButton.getPresentationModel().getHorizontalGapScaleFactor()
* (font.getSize() - 4) / 4);
}

public static int getVLayoutGap(JCommandButton commandButton) {
Font font = commandButton.getFont();
return (int) Math.ceil(commandButton.getVGapScaleFactor() * (font.getSize() - 4) / 4);
return (int) Math.ceil(commandButton.getPresentationModel().getVerticalGapScaleFactor()
* (font.getSize() - 4) / 4);
}

public static boolean hasPopupAction(JCommandButton commandButton) {
Expand Down

0 comments on commit 4b17e30

Please sign in to comment.