Skip to content

Commit

Permalink
adding stongly typed methods for common usages
Browse files Browse the repository at this point in the history
  • Loading branch information
tbee committed Apr 30, 2023
1 parent c37e39c commit 341e16b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/net/miginfocom/layout/AC.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* Note that there are two way to build this constraint. Through String (e.g. <code>"[100]3[200,fill]"</code> or through API (E.g.
* <code>new AC().size("100").gap("3").size("200").fill()</code>.
*/
public final class AC implements Externalizable
public class AC implements Externalizable
{
private final ArrayList<DimConstraint> cList = new ArrayList<DimConstraint>(1);

Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/net/miginfocom/layout/AlignX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.miginfocom.layout;

public enum AlignX {
LEADING, LEFT, CENTER, RIGHT, TRAILING;
}
5 changes: 5 additions & 0 deletions core/src/main/java/net/miginfocom/layout/AlignY.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.miginfocom.layout;

public enum AlignY {
TOP, CENTER, BOTTOM, BASELINE;
}
27 changes: 26 additions & 1 deletion core/src/main/java/net/miginfocom/layout/CC.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

/** A simple value holder for one component's constraint.
*/
public final class CC implements Externalizable
public class CC implements Externalizable
{
private static final BoundSize DEF_GAP = BoundSize.NULL_SIZE; // Only used to denote default wrap/newline gap.

Expand Down Expand Up @@ -229,6 +229,18 @@ public final CC alignX(String align)
return this;
}

/**
* Strongly typed API for most common usages
*
* @see #alignX(String)
*
* @param align
* @return <code>this</code> so it is possible to chain calls.
*/
public final CC alignX(AlignX align) {
return alignX(align == null ? null : align.toString().toLowerCase());

This comment has been minimized.

Copy link
@vlsi

vlsi May 8, 2023

Contributor

Use toLowerCase(Locale.ROOT) to avoid surprising behaviour in tr_TR locale

This comment has been minimized.

Copy link
@tbee

tbee May 9, 2023

Author Collaborator

align is an enum with 5 values, all using only ASCII characters. What in the world would go wrong in tr_TR?

This comment has been minimized.

This comment has been minimized.

Copy link
@tbee

tbee May 10, 2023

Author Collaborator

To summarize: WTF?!
Will do.

This comment has been minimized.

Copy link
@tbee

tbee May 10, 2023

Author Collaborator

Or maybe better: explicitly include the code

This comment has been minimized.

Copy link
@vlsi

vlsi May 10, 2023

Contributor

It might work if you add code field to the enum itself.

However, I would suggest using toLowerCase(Locale.ROOT) instead of plain toLowerCase().

This comment has been minimized.

Copy link
@tbee

tbee May 10, 2023

Author Collaborator

It will work; the toLowercase is a trick for not having to type the actual code-string. Replacing that with the code is better.

This comment has been minimized.

Copy link
@tbee

tbee May 14, 2023

Author Collaborator

I believe I've fixed it now (also for other uses of toLowerCase)

}

/** The grow priority compared to other components in the same cell.
* <p>
* For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
Expand Down Expand Up @@ -501,6 +513,19 @@ public final CC alignY(String align)
return this;
}

/**
* Strongly typed API for most common usages
*
* @see #alignY(String)
*
* @param align
* @return <code>this</code> so it is possible to chain calls.
*/
public final CC alignY(AlignY align)
{
return alignY(align == null ? null : align.toString().toLowerCase());
}

/** The grow priority compared to other components in the same cell.
* <p>
* For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/java/net/miginfocom/layout/HideMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.miginfocom.layout;

/**
* NORMAL: Bounds will be calculated as if the component was visible.<br>
* SIZE_0_RETAIN_GAPS: If hidden the size will be 0, 0 but the gaps remain.<br>
* SIZE_0_GAPS_0: If hidden the size will be 0, 0 and gaps set to zero.<br>
* DISREGARD: If hidden the component will be disregarded completely and not take up a cell in the grid..
*/
public enum HideMode {
NORMAL(0), SIZE_0_RETAIN_GAPS(1), SIZE_0_GAPS_0(2), DISREGARD(3);

private final int code;

private HideMode(int code) {
this.code = code;
}

public int getCode() {
return code;
}

static public HideMode of(int code) {
for (HideMode hideMode : values()) {
if (hideMode.code == code) {
return hideMode;
}
}
throw new IllegalArgumentException("Code does not exist " + code);
}
}
42 changes: 40 additions & 2 deletions core/src/main/java/net/miginfocom/layout/LC.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

/** Contains the constraints for an instance of the {@link LC} layout manager.
*/
public final class LC implements Externalizable
public class LC implements Externalizable
{
// See the corresponding set/get method for documentation of the property!

Expand Down Expand Up @@ -259,7 +259,7 @@ public final int getHideMode()
* 0 == Normal. Bounds will be calculated as if the component was visible.<br>
* 1 == If hidden the size will be 0, 0 but the gaps remain.<br>
* 2 == If hidden the size will be 0, 0 and gaps set to zero.<br>
* 3 == If hidden the component will be disregarded completely and not take up a cell in the grid..
* 3 == If hidden the component will be disregarded completely and not take up a cell in the grid.
*/
public final void setHideMode(int mode)
{
Expand All @@ -269,6 +269,19 @@ public final void setHideMode(int mode)
this.hideMode = mode;
}

/**
* Strongly typed API for most common usages
*
* @see #setHideMode(int)
*
* @param mode
* @return <code>this</code> so it is possible to chain calls.
*/
public final LC hideMode(HideMode mode) {
setHideMode(mode.getCode());
return this;
}

/** The insets for the layed out panel. The insets will be an empty space around the components in the panel. <code>null</code> values
* means that the default panel insets for the platform is used. See {@link PlatformDefaults#setDialogInsets(net.miginfocom.layout.UnitValue, net.miginfocom.layout.UnitValue, net.miginfocom.layout.UnitValue, net.miginfocom.layout.UnitValue)}.
* @return The insets. Of length 4 (top, left, bottom, right) or <code>null</code>. The elements (1 to 4) may be <code>null</code>. The array is a copy and can be used freely.
Expand Down Expand Up @@ -821,6 +834,18 @@ public final LC alignX(String align)
return this;
}

/**
* Strongly typed API for most common usages
*
* @see #alignX(String)
*
* @param align
* @return <code>this</code> so it is possible to chain calls.
*/
public final LC alignX(AlignX align) {
return alignX(align == null ? null : align.toString().toLowerCase());
}

/** Same functionality as <code>setAlignY(ConstraintParser.parseUnitValueOrAlign(align, false))</code> only this method returns <code>this</code> for chaining multiple calls.
* <p>
* For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
Expand All @@ -834,6 +859,19 @@ public final LC alignY(String align)
return this;
}

/**
* Strongly typed API for most common usages
*
* @see #alignY(String)
*
* @param align
* @return <code>this</code> so it is possible to chain calls.
*/
public final LC alignY(AlignY align)
{
return alignY(align == null ? null : align.toString().toLowerCase());
}

/** Sets both the alignX and alignY as the same time.
* <p>
* For a more thorough explanation of what this constraint does see the white paper or cheat Sheet at www.migcomponents.com.
Expand Down

0 comments on commit 341e16b

Please sign in to comment.