Skip to content

Commit

Permalink
Updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
parubok committed Sep 20, 2020
1 parent 2e0f502 commit be4eb3d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/swingk/MultilineLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MultilineLabel extends JComponent implements Scrollable {
private String text = "";
private TextLayout textLayout;
private int prefWidthLimit = DEFAULT_WIDTH_LIMIT;
private boolean currentWidthIgnoredForPreferredSizeCalculation;
private boolean useCurrentWidthForPreferredSize = true;

public MultilineLabel() {
super();
Expand Down Expand Up @@ -109,18 +109,18 @@ public void setPreferredWidthLimit(int prefWidthLimit) {
this.prefWidthLimit = prefWidthLimit;
}

public boolean isCurrentWidthIgnoredForPreferredSizeCalculation() {
return currentWidthIgnoredForPreferredSizeCalculation;
public boolean isUseCurrentWidthForPreferredSize() {
return useCurrentWidthForPreferredSize;
}

/**
* In some cases using current width to calculate the preferred size may produce undesired results.
* This parameter allows to disable this behavior.
*
* @param currentWidthIgnoredForPreferredSizeCalculation
* @param useCurrentWidthForPreferredSize
*/
public void setCurrentWidthIgnoredForPreferredSizeCalculation(boolean currentWidthIgnoredForPreferredSizeCalculation) {
this.currentWidthIgnoredForPreferredSizeCalculation = currentWidthIgnoredForPreferredSizeCalculation;
public void setUseCurrentWidthForPreferredSize(boolean useCurrentWidthForPreferredSize) {
this.useCurrentWidthForPreferredSize = useCurrentWidthForPreferredSize;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/swingk/WidthTextLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void requestLayout() {

@Override
public void preSetBounds(int x, int y, int width, int height) {
if (!label.isCurrentWidthIgnoredForPreferredSizeCalculation()
if (label.isUseCurrentWidthForPreferredSize()
&& !textToRender.isEmpty()
&& width > 0 && height > 0
&& width != label.getWidth()
Expand All @@ -72,7 +72,7 @@ protected Dimension calcPreferredSize(int expectedLabelWidth) {
final int wLimit;
if (expectedLabelWidth > 0) {
wLimit = expectedLabelWidth;
} else if (!label.isCurrentWidthIgnoredForPreferredSizeCalculation() && label.getWidth() > 0) {
} else if (label.isUseCurrentWidthForPreferredSize() && label.getWidth() > 0) {
// https://stackoverflow.com/questions/39455573/how-to-set-fixed-width-but-dynamic-height-on-jtextpane/39466255#39466255
wLimit = label.getWidth();
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/swingk/MultilineLabelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ void getPreferredSize_5() throws Exception {
void setCurrentWidthIgnoredForPreferredSizeCalculation_1() throws Exception {
SwingUtilities.invokeAndWait(() -> {
MultilineLabel label = new MultilineLabel(Demo.LOREM_IPSUM);
Assertions.assertTrue(label.isUseCurrentWidthForPreferredSize());
Assertions.assertEquals(new Dimension(488, 48), label.getPreferredSize());

label.setBounds(0, 0, 200, 20);
Assertions.assertEquals(new Dimension(195, 112), label.getPreferredSize());

label.setCurrentWidthIgnoredForPreferredSizeCalculation(true);
label.setUseCurrentWidthForPreferredSize(false);
Assertions.assertFalse(label.isUseCurrentWidthForPreferredSize());
Assertions.assertEquals(new Dimension(488, 48), label.getPreferredSize());

label.setCurrentWidthIgnoredForPreferredSizeCalculation(false);
label.setUseCurrentWidthForPreferredSize(true);
Assertions.assertTrue(label.isUseCurrentWidthForPreferredSize());
Assertions.assertEquals(new Dimension(195, 112), label.getPreferredSize());
});
}
Expand Down

0 comments on commit be4eb3d

Please sign in to comment.