Skip to content

Commit

Permalink
Exo label tweaks
Browse files Browse the repository at this point in the history
Remove multiline presentation configuration and fix support for horizontal alignment and display prototype. Also add a small demo.

Fixes #450
  • Loading branch information
kirill-grouchnikov committed May 26, 2023
1 parent 3defd7f commit 077417d
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class LabelPresentationModel implements ImmutablePresentationModel {
private RadianceThemingSlices.IconFilterStrategy enabledIconFilterStrategy;
private RadianceThemingSlices.IconFilterStrategy disabledIconFilterStrategy;
private Font font;
private int textMaxLines;
private HorizontalAlignment horizontalAlignment;
private int iconTextGap;
private float horizontalGapScaleFactor;
Expand Down Expand Up @@ -73,10 +72,6 @@ public Font getFont() {
return this.font;
}

public int getTextMaxLines() {
return this.textMaxLines;
}

public HorizontalAlignment getHorizontalAlignment() {
return this.horizontalAlignment;
}
Expand Down Expand Up @@ -106,8 +101,7 @@ public static class Builder {
private RadianceThemingSlices.IconFilterStrategy disabledIconFilterStrategy =
RadianceThemingSlices.IconFilterStrategy.THEMED_FOLLOW_COLOR_SCHEME;
private Font font = null;
private int textMaxLines = Integer.MAX_VALUE;
private HorizontalAlignment horizontalAlignment = HorizontalAlignment.CENTER;
private HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEADING;
private int iconTextGap = DEFAULT_ICON_TEXT_GAP;
private float horizontalGapScaleFactor = 1.0f;
private String singleLineDisplayPrototype = null;
Expand Down Expand Up @@ -138,11 +132,6 @@ public Builder setFont(Font font) {
return this;
}

public Builder setTextMaxLines(int textMaxLines) {
this.textMaxLines = textMaxLines;
return this;
}

public Builder setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
if (horizontalAlignment == null) {
throw new IllegalArgumentException("Cannot pass null horizontal alignment");
Expand Down Expand Up @@ -173,7 +162,6 @@ public LabelPresentationModel build() {
presentationModel.enabledIconFilterStrategy = this.enabledIconFilterStrategy;
presentationModel.disabledIconFilterStrategy = this.disabledIconFilterStrategy;
presentationModel.font = this.font;
presentationModel.textMaxLines = this.textMaxLines;
presentationModel.horizontalAlignment = this.horizontalAlignment;
presentationModel.iconTextGap = this.iconTextGap;
presentationModel.horizontalGapScaleFactor = this.horizontalGapScaleFactor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@
import org.pushingpixels.radiance.component.api.common.JExoLabel;
import org.pushingpixels.radiance.component.api.common.model.LabelContentModel;
import org.pushingpixels.radiance.component.api.common.model.LabelPresentationModel;
import org.pushingpixels.radiance.component.api.common.popup.JPopupPanel;
import org.pushingpixels.radiance.theming.api.ComponentState;
import org.pushingpixels.radiance.theming.api.RadianceThemingCortex;
import org.pushingpixels.radiance.theming.internal.utils.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ComponentUI;
import java.awt.*;
import java.beans.PropertyChangeListener;
Expand All @@ -54,7 +52,6 @@
public class RadianceExoLabelUI extends ComponentUI {
private RadianceIcon icon;
private JLabel singleLineLabel;
private JTextArea multiLineLabel;
private PropertyChangeListener propertyChangeListener;

public static ComponentUI createUI(JComponent comp) {
Expand Down Expand Up @@ -83,47 +80,18 @@ public void installUI(JComponent c) {
presentationModel.getEnabledIconFilterStrategy(),
presentationModel.getDisabledIconFilterStrategy());

if (presentationModel.getTextMaxLines() == 1) {
this.singleLineLabel = new JLabel(contentModel.getText());
this.singleLineLabel.setBorder(new EmptyBorder(0, 0, 0, 0));
this.singleLineLabel.setFocusable(false);
if (presentationModel.getFont() != null) {
this.singleLineLabel.setFont(presentationModel.getFont());
}
switch (presentationModel.getHorizontalAlignment()) {
case LEADING:
case FILL:
this.singleLineLabel.setHorizontalAlignment(SwingUtilities.LEADING);
break;
case CENTER:
this.singleLineLabel.setHorizontalAlignment(SwingUtilities.CENTER);
break;
case TRAILING:
this.singleLineLabel.setHorizontalAlignment(SwingUtilities.TRAILING);
break;
}
label.add(this.singleLineLabel);
} else {
this.multiLineLabel = new JTextArea(contentModel.getText());
this.multiLineLabel.setRows(presentationModel.getTextMaxLines());
this.multiLineLabel.setLineWrap(true);
this.multiLineLabel.setWrapStyleWord(true);
this.multiLineLabel.setEditable(false);
this.multiLineLabel.setFocusable(false);
if (presentationModel.getFont() != null) {
this.multiLineLabel.setFont(presentationModel.getFont());
}
label.add(this.multiLineLabel);
this.singleLineLabel = new JLabel(contentModel.getText());
this.singleLineLabel.setBorder(new EmptyBorder(0, 0, 0, 0));
this.singleLineLabel.setFocusable(false);
if (presentationModel.getFont() != null) {
this.singleLineLabel.setFont(presentationModel.getFont());
}
label.add(this.singleLineLabel);

this.propertyChangeListener = evt -> {
if ("text".equals(evt.getPropertyName())) {
String newText = (String) evt.getNewValue();
if (presentationModel.getTextMaxLines() == 1) {
singleLineLabel.setText(newText);
} else {
multiLineLabel.setText(newText);
}
singleLineLabel.setText(newText);
}
};
contentModel.addPropertyChangeListener(this.propertyChangeListener);
Expand All @@ -140,16 +108,7 @@ public void removeLayoutComponent(Component comp) {
@Override
public Dimension preferredLayoutSize(Container parent) {
JExoLabel label = (JExoLabel) c;

LabelContentModel contentModel = label.getProjection().getContentModel();
LabelPresentationModel presentationModel = label.getProjection().getPresentationModel();

if (presentationModel.getTextMaxLines() == 1) {
return getSingleLinePreferredDimension(label);
}

// TODO - support multiline mode
return new Dimension(0, 0);
return getSingleLinePreferredDimension(label);
}

@Override
Expand All @@ -166,57 +125,55 @@ public void layoutContainer(Container parent) {

int width = label.getWidth();

if (presentationModel.getTextMaxLines() == 1) {
int shiftX = 0;
int preferredWidth = getSingleLinePreferredDimension(label).width;
if (preferredWidth < label.getWidth()) {
// We have more horizontal space than needed to display the content.
// Consult the horizontal alignment attribute of the command button to see
// how we should shift the content horizontally.
switch (presentationModel.getHorizontalAlignment()) {
case LEADING:
case FILL:
break;
case CENTER:
// shift everything to be centered horizontally
shiftX = (width - preferredWidth) / 2;
break;
case TRAILING:
// shift everything to the end of the button bounds
shiftX = width - preferredWidth;
}
int shiftX = 0;
int preferredWidth = getSingleLineNoPrototypePreferredDimension(label).width;
if (preferredWidth < label.getWidth()) {
// We have more horizontal space than needed to display the content.
// Consult the horizontal alignment attribute of the command button to see
// how we should shift the content horizontally.
switch (presentationModel.getHorizontalAlignment()) {
case LEADING:
case FILL:
break;
case CENTER:
// shift everything to be centered horizontally
shiftX = (width - preferredWidth) / 2;
break;
case TRAILING:
// shift everything to the end of the button bounds
shiftX = width - preferredWidth;
}
}

int textLeft = 0;
int textTop = presentationModel.getContentPadding().top;
int textRight = 0;
int textBottom = label.getHeight() - presentationModel.getContentPadding().bottom;

if (label.getComponentOrientation().isLeftToRight()) {
if (contentModel.getIconFactory() == null) {
textLeft = presentationModel.getContentPadding().left;
} else {
textLeft = presentationModel.getContentPadding().left +
presentationModel.getIconDimension().width +
(int) (presentationModel.getIconTextGap() *
presentationModel.getHorizontalGapScaleFactor());
}
int textLeft = 0;
int textTop = presentationModel.getContentPadding().top;
int textRight = 0;
int textBottom = label.getHeight() - presentationModel.getContentPadding().bottom;

if (label.getComponentOrientation().isLeftToRight()) {
if (contentModel.getIconFactory() == null) {
textLeft = presentationModel.getContentPadding().left;
} else {
textLeft = presentationModel.getContentPadding().left +
presentationModel.getIconDimension().width +
(int) (presentationModel.getIconTextGap() *
presentationModel.getHorizontalGapScaleFactor());
}
textRight = width - presentationModel.getContentPadding().right;
} else {
textLeft = presentationModel.getContentPadding().left;
if (contentModel.getIconFactory() == null) {
textRight = width - presentationModel.getContentPadding().right;
} else {
textLeft = presentationModel.getContentPadding().left;
if (contentModel.getIconFactory() == null) {
textRight = width - presentationModel.getContentPadding().right;
} else {
textRight = width - presentationModel.getContentPadding().right -
presentationModel.getIconDimension().width -
(int) (presentationModel.getIconTextGap() *
presentationModel.getHorizontalGapScaleFactor());
}
textRight = width - presentationModel.getContentPadding().right -
presentationModel.getIconDimension().width -
(int) (presentationModel.getIconTextGap() *
presentationModel.getHorizontalGapScaleFactor());
}

label.getComponent(0).setBounds(shiftX + textLeft, textTop,
shiftX + textRight - textLeft, textBottom - textTop);
}

label.getComponent(0).setBounds(shiftX + textLeft, textTop,
shiftX + textRight - textLeft, textBottom - textTop);
}
});
}
Expand Down Expand Up @@ -277,6 +234,43 @@ private static Dimension getSingleLinePreferredDimension(JExoLabel label) {
return new Dimension(width, height);
}

private static Dimension getSingleLineNoPrototypePreferredDimension(JExoLabel label) {
LabelContentModel contentModel = label.getProjection().getContentModel();
LabelPresentationModel presentationModel = label.getProjection().getPresentationModel();

int width = presentationModel.getContentPadding().left +
presentationModel.getContentPadding().right;
int height = presentationModel.getContentPadding().top +
presentationModel.getContentPadding().bottom;

int iconHeight = (contentModel.getIconFactory() != null)
? presentationModel.getIconDimension().height
: 0;
int iconWidth = (contentModel.getIconFactory() != null)
? presentationModel.getIconDimension().width
: 0;

if (iconWidth > 0) {
width = width + iconWidth +
(int) (presentationModel.getIconTextGap() *
presentationModel.getHorizontalGapScaleFactor());
}

Font font = (presentationModel.getFont() != null)
? presentationModel.getFont()
: label.getFont();
Dimension textDimension =
RadianceMetricsUtilities.getLabelPreferredSingleLineDimension(
label, contentModel.getText(), font);
int textWidth = textDimension.width;
int textHeight = textDimension.height;

width = width + textWidth;
height += Math.max(iconHeight, textHeight);

return new Dimension(width, height);
}

@Override
public void paint(Graphics g, JComponent c) {
JExoLabel label = (JExoLabel) c;
Expand Down
Loading

0 comments on commit 077417d

Please sign in to comment.