Skip to content

Commit

Permalink
prmr#422 Change cache to be multilevel map; Add utility methods to Al…
Browse files Browse the repository at this point in the history
…ignment
  • Loading branch information
yannsartori authored and louib committed Aug 15, 2021
1 parent 667d7d3 commit 967fee2
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions src/ca/mcgill/cs/jetuml/views/StringViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import ca.mcgill.cs.jetuml.annotations.Flyweight;
import ca.mcgill.cs.jetuml.annotations.Immutable;
Expand Down Expand Up @@ -55,15 +54,46 @@ public final class StringViewer
private static final int DEFAULT_HORIZONTAL_TEXT_PADDING = 7;
private static final int DEFAULT_VERTICAL_TEXT_PADDING = 7;

private static final Map<Set<Object>, StringViewer> STORE = new HashMap<Set<Object>, StringViewer>();
private static final Map<Alignment, Map<EnumSet<TextDecoration>, StringViewer>> STORE = new HashMap<>();

/**
* How to align the text in this string.
*/
public enum Alignment
{ TOP_LEFT, TOP_CENTER, TOP_RIGHT,
CENTER_LEFT, CENTER_CENTER, CENTER_RIGHT,
BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT
BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT;

private boolean isTop()
{
return this == TOP_LEFT || this == TOP_CENTER || this == TOP_RIGHT;
}

private boolean isVerticallyCentered()
{
return this == CENTER_LEFT || this == CENTER_CENTER || this == CENTER_RIGHT;
}

private boolean isBottom()
{
return this == BOTTOM_LEFT || this == BOTTOM_CENTER || this == BOTTOM_RIGHT;
}

private boolean isLeft()
{
return this == TOP_LEFT || this == CENTER_LEFT || this == BOTTOM_LEFT;
}

private boolean isHorizontallyCentered()
{
return this == TOP_CENTER || this == CENTER_CENTER || this == BOTTOM_CENTER;
}

private boolean isRight()
{
return this == TOP_RIGHT || this == CENTER_RIGHT || this == BOTTOM_RIGHT;
}

}

/**
Expand Down Expand Up @@ -104,9 +134,8 @@ public static StringViewer get(Alignment pAlign, TextDecoration... pDecorations)
EnumSet<TextDecoration> decorationSet = EnumSet.noneOf(TextDecoration.class);
Collections.addAll(decorationSet, pDecorations);

Set<Object> keySet = Set.of(pAlign, decorationSet);

return STORE.computeIfAbsent(keySet, k -> new StringViewer(pAlign, decorationSet));
Map<EnumSet<TextDecoration>, StringViewer> innerMap = STORE.computeIfAbsent(pAlign, k -> new HashMap<>());
return innerMap.computeIfAbsent(decorationSet, k -> new StringViewer(pAlign, decorationSet));
}

private Font getFont()
Expand Down Expand Up @@ -151,11 +180,11 @@ public Dimension getDimension(String pString)

private TextAlignment getTextAlignment()
{
if ( aAlign == Alignment.TOP_LEFT || aAlign == Alignment.CENTER_LEFT || aAlign == Alignment.BOTTOM_LEFT )
if ( aAlign.isLeft() )
{
return TextAlignment.LEFT;
}
else if ( aAlign == Alignment.TOP_CENTER || aAlign == Alignment.CENTER_CENTER || aAlign == Alignment.BOTTOM_CENTER )
else if ( aAlign.isHorizontallyCentered() )
{
return TextAlignment.CENTER;
}
Expand All @@ -164,15 +193,15 @@ else if ( aAlign == Alignment.TOP_CENTER || aAlign == Alignment.CENTER_CENTER ||

private VPos getTextBaseline()
{
if ( aAlign == Alignment.TOP_LEFT || aAlign == Alignment.TOP_CENTER || aAlign == Alignment.TOP_RIGHT )
if ( aAlign.isBottom() )
{
return VPos.TOP;
return VPos.BASELINE;
}
else if ( aAlign == Alignment.CENTER_LEFT || aAlign == Alignment.CENTER_CENTER || aAlign == Alignment.CENTER_RIGHT )
else if ( aAlign.isTop() )
{
return VPos.CENTER;
return VPos.TOP;
}
return VPos.BASELINE;
return VPos.CENTER;
}

/**
Expand All @@ -186,20 +215,13 @@ public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle
final VPos oldVPos = pGraphics.getTextBaseline();
final TextAlignment oldAlign = pGraphics.getTextAlign();

boolean isHorizontallyCentered = aAlign == Alignment.TOP_CENTER || aAlign == Alignment.CENTER_CENTER ||
aAlign == Alignment.BOTTOM_CENTER;
boolean isRightJustified = aAlign == Alignment.TOP_RIGHT || aAlign == Alignment.CENTER_RIGHT ||
aAlign == Alignment.BOTTOM_RIGHT;
boolean isVerticallyCentered = aAlign == Alignment.CENTER_LEFT || aAlign == Alignment.CENTER_CENTER ||
aAlign == Alignment.CENTER_RIGHT;

pGraphics.setTextAlign(getTextAlignment());
pGraphics.setTextBaseline(getTextBaseline());


int textX = 0;
int textY = 0;
if( isHorizontallyCentered )
if( aAlign.isHorizontallyCentered() )
{
textX = pRectangle.getWidth()/2;
}
Expand All @@ -208,7 +230,7 @@ public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle
textX = aHorizontalPadding;
}

if ( isVerticallyCentered )
if ( aAlign.isVerticallyCentered() )
{
textY = pRectangle.getHeight()/2;
}
Expand All @@ -221,12 +243,12 @@ public void draw(String pString, GraphicsContext pGraphics, Rectangle pRectangle
int xOffset = 0;
int yOffset = 0;
Dimension dimension = getFontMetrics().getDimension(pString);
if( isHorizontallyCentered )
if( aAlign.isHorizontallyCentered() )
{
xOffset = dimension.width()/2;
yOffset = (int) (getFont().getSize()/2) + 1;
}
else if( isRightJustified )
else if( aAlign.isRight() )
{
xOffset = dimension.width();
}
Expand Down

0 comments on commit 967fee2

Please sign in to comment.