Skip to content

Commit

Permalink
TextLineStyle: Changed to be PropObject and renamed prop keys and cop…
Browse files Browse the repository at this point in the history
…y methods
  • Loading branch information
reportmill committed Nov 22, 2024
1 parent 0bae07a commit 27004a8
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/snap/text/TextAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public HPos getLineAlign()
*/
public void setLineAlign(HPos anAlign)
{
setSelLineStyleValue(TextLineStyle.ALIGN_KEY, anAlign);
setSelLineStyleValue(TextLineStyle.Align_Prop, anAlign);
}

/**
Expand All @@ -629,7 +629,7 @@ public boolean isLineJustify()
*/
public void setLineJustify(boolean aValue)
{
setSelLineStyleValue(TextLineStyle.JUSTIFY_KEY, aValue);
setSelLineStyleValue(TextLineStyle.Justify_Prop, aValue);
}

/**
Expand Down Expand Up @@ -1628,7 +1628,7 @@ private void handleViewAlignChanged()
Pos viewAlign = _view.getAlign();

// Push align to TextBlock via DefaultLineStyle.Align (X) and TextBlock align Y
TextLineStyle lineStyle = getDefaultLineStyle().copyFor(TextLineStyle.ALIGN_KEY, viewAlign.getHPos());
TextLineStyle lineStyle = getDefaultLineStyle().copyForPropKeyValue(TextLineStyle.Align_Prop, viewAlign.getHPos());
setDefaultLineStyle(lineStyle);

// Forward to text block
Expand Down
6 changes: 3 additions & 3 deletions src/snap/text/TextBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ public void setLineStyleValue(String aKey, Object aValue, int aStart, int anEnd)
// Handle plain
else {
TextLineStyle oldStyle = getLine(0).getLineStyle();
TextLineStyle newStyle = oldStyle.copyFor(aKey, aValue);
TextLineStyle newStyle = oldStyle.copyForPropKeyValue(aKey, aValue);
setLineStyle(newStyle, 0, length());
}

Expand Down Expand Up @@ -714,7 +714,7 @@ private void setLineStyleValueRich(String aKey, Object aValue, int aStart, int a
for (int i = startLineIndex; i <= endLineIndex; i++) {
TextLine line = getLine(i);
TextLineStyle oldStyle = line.getLineStyle();
TextLineStyle newStyle = oldStyle.copyFor(aKey, aValue);
TextLineStyle newStyle = oldStyle.copyForPropKeyValue(aKey, aValue);
if (!newStyle.equals(oldStyle)) {
line.setLineStyle(newStyle);
if (isPropChangeEnabled())
Expand Down Expand Up @@ -997,7 +997,7 @@ public HPos getAlignX()
*/
public void setAlignX(HPos anAlignX)
{
setLineStyleValue(TextLineStyle.ALIGN_KEY, anAlignX, 0, length());
setLineStyleValue(TextLineStyle.Align_Prop, anAlignX, 0, length());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/snap/text/TextLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ public TextLine getPrevious()
*/
public void setAlignX(HPos anAlign)
{
TextLineStyle lineStyle = getLineStyle().copyFor(anAlign);
TextLineStyle lineStyle = getLineStyle().copyForAlign(anAlign);
setLineStyle(lineStyle);
}

Expand Down
191 changes: 123 additions & 68 deletions src/snap/text/TextLineStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,51 @@
*/
package snap.text;
import java.util.Arrays;

import snap.geom.HPos;
import snap.props.PropObject;
import snap.props.PropSet;
import snap.util.*;

/**
* A class to represent a line of text (for each newline) in RichText.
*/
public class TextLineStyle implements Cloneable, XMLArchiver.Archivable {
public class TextLineStyle extends PropObject implements Cloneable, XMLArchiver.Archivable {

// Horizontal text alignment
private HPos _align = HPos.LEFT;
private HPos _align;

// Whether text in line should be justified
private boolean _justify;
private boolean _justify;

// Indentation for first line of paragraph
private double _firstIndent = 0;
private double _firstIndent;

// Indention for whole paragraph
private double _leftIndent = 0;
private double _leftIndent;

// Indentation for right margin
private double _rightIndent = 0;
private double _rightIndent;

// Space between lines expressed as a constant in points
private double _spacing = 0;
private double _spacing;

// Space between lines expressed as a factor of the current line height
private double _spacingFactor = 1;
private double _spacingFactor;

// Spacing after a newline character
private double _newlineSpacing = 0;
private double _newlineSpacing;

// Min line height
private double _minHeight = 0;
private double _minHeight;

// Max line height
private double _maxHeight = Float.MAX_VALUE;
private double _maxHeight;

// Tab stops
private double[] _tabs = _defaultTabs;
private double[] _tabs;

// Tab stop types
private char[] _tabTypes = _defaultTypes;

// Default tab positions
private static double[] _defaultTabs = { 36f, 72f, 108f, 144f, 180f, 216f, 252f, 288f, 324f, 360f, 396f, 432f };

// Default tab types
private static char[] _defaultTypes = { 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L' };
private char[] _tabTypes;

// Constants for tab types
public static final char TAB_LEFT = 'L';
Expand All @@ -61,27 +56,39 @@ public class TextLineStyle implements Cloneable, XMLArchiver.Archivable {
public static final char TAB_DECIMAL = 'D';

// Constants for LineStyle keys
public static final String ALIGN_KEY = "Align";
public static final String JUSTIFY_KEY = "Justify";
public static final String FIRST_INDENT_KEY = "FirstIndent";
public static final String LEFT_INDENT_KEY = "LeftIndent";
public static final String RIGHT_INDENT_KEY = "RightIndent";
public static final String SPACING_KEY = "Spacing";
public static final String SPACING_FACTOR_KEY = "SpacingFactor";
public static final String NEWLINE_SPACING_KEY = "NewlineSpacing";
public static final String MIN_HEIGHT_KEY = "MinHeight";
public static final String MAX_HEIGHT_KEY = "MaxHeight";
public static final String Align_Prop = "Align";
public static final String Justify_Prop = "Justify";
public static final String FirstIndent_Prop = "FirstIndent";
public static final String LeftIndent_Prop = "LeftIndent";
public static final String RightIndent_Prop = "RightIndent";
public static final String Spacing_Prop = "Spacing";
public static final String SpacingFactor_Prop = "SpacingFactor";
public static final String NewlineSpacing_Prop = "NewlineSpacing";
public static final String MinHeight_Prop = "MinHeight";
public static final String MaxHeight_Prop = "MaxHeight";

// The System default line style
public static final TextLineStyle DEFAULT = new TextLineStyle();
public static final TextLineStyle DEFAULT_CENTERED = DEFAULT.copyFor(HPos.CENTER);
public static final TextLineStyle DEFAULT_CENTERED = DEFAULT.copyForAlign(HPos.CENTER);

// Constants for defaults
private static final HPos DEFAULT_ALIGN = HPos.LEFT;
private static final double DEFAULT_SPACING_FACTOR = 1;
private static final double DEFAULT_MAX_HEIGHT = Float.MAX_VALUE;
private static double[] DEFAULT_TABS = { 36f, 72f, 108f, 144f, 180f, 216f, 252f, 288f, 324f, 360f, 396f, 432f };
private static char[] DEFAULT_TAB_TYPES = { 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L' };

/**
* Constructor.
*/
public TextLineStyle()
{
super();
_align = DEFAULT_ALIGN;
_spacingFactor = DEFAULT_SPACING_FACTOR;
_maxHeight = DEFAULT_MAX_HEIGHT;
_tabs = DEFAULT_TABS;
_tabTypes = DEFAULT_TAB_TYPES;
}

/**
Expand Down Expand Up @@ -188,7 +195,7 @@ public int getTabIndexForX(double aX)
public String getTabsString()
{
// Iterate over tabs and add string rep to StringBuffer
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int i = 0, iMax = _tabs.length; i < iMax; i++) {
if (_tabs[i] == (int) _tabs[i]) // If tab is really int, append value as int
sb.append((int) _tabs[i]); // Otherwise append value as double
Expand All @@ -210,7 +217,7 @@ protected void setTabsString(String aString)
{
// Get individual tab strings
String[] tabs = aString.split("\\s*\\,\\s*");
if (tabs.length == 1 && tabs[0].length() == 0)
if (tabs.length == 1 && tabs[0].isEmpty())
tabs = new String[0];

// Create tabs and types arrays
Expand All @@ -226,22 +233,17 @@ protected void setTabsString(String aString)
}

/**
* Returns a clone with the new given value.
* Returns a copy with given align value.
*/
public TextLineStyle copyFor(Object anObj)
{
if (anObj instanceof HPos)
return copyFor(ALIGN_KEY, anObj);
return this;
}
public TextLineStyle copyForAlign(HPos anObj) { return copyForPropKeyValue(Align_Prop, anObj); }

/**
* Returns a clone with the new given value.
* Returns a copy with given value for given property name.
*/
public TextLineStyle copyFor(String aKey, Object aValue)
public TextLineStyle copyForPropKeyValue(String aKey, Object aValue)
{
TextLineStyle clone = clone();
clone.setValue(aKey, aValue);
clone.setPropValue(aKey, aValue);
return clone;
}

Expand All @@ -250,33 +252,12 @@ public TextLineStyle copyFor(String aKey, Object aValue)
*/
public TextLineStyle copyForIndents(double firstIndent, double leftIndent, double rightIndent)
{
TextLineStyle ls = copyFor(TextLineStyle.FIRST_INDENT_KEY, firstIndent);
ls = ls.copyFor(TextLineStyle.LEFT_INDENT_KEY, leftIndent);
ls = ls.copyFor(TextLineStyle.RIGHT_INDENT_KEY, rightIndent);
TextLineStyle ls = copyForPropKeyValue(TextLineStyle.FirstIndent_Prop, firstIndent);
ls = ls.copyForPropKeyValue(TextLineStyle.LeftIndent_Prop, leftIndent);
ls = ls.copyForPropKeyValue(TextLineStyle.RightIndent_Prop, rightIndent);
return ls;
}

/**
* Sets a value for given key.
*/
protected void setValue(String aKey, Object aValue)
{
if (aKey.equals(ALIGN_KEY)) {
_align = (HPos) aValue;
_justify = false;
}
else if (aKey.equals(JUSTIFY_KEY)) _justify = Convert.boolValue(aValue);
else if (aKey.equals(SPACING_KEY)) _spacing = Convert.doubleValue(aValue);
else if (aKey.equals(SPACING_FACTOR_KEY)) _spacingFactor = Convert.doubleValue(aValue);
else if (aKey.equals(NEWLINE_SPACING_KEY)) _newlineSpacing = Convert.doubleValue(aValue);
else if (aKey.equals(MIN_HEIGHT_KEY)) _minHeight = Convert.doubleValue(aValue);
else if (aKey.equals(MAX_HEIGHT_KEY)) _maxHeight = Convert.doubleValue(aValue);
else if (aKey.equals(FIRST_INDENT_KEY)) _firstIndent = Convert.doubleValue(aValue);
else if (aKey.equals(LEFT_INDENT_KEY)) _leftIndent = Convert.doubleValue(aValue);
else if (aKey.equals(RIGHT_INDENT_KEY)) _rightIndent = Convert.doubleValue(aValue);
else System.err.println("TextLineStyle.setValue: Unsupported key: " + aKey);
}

/**
* Standard clone implementation.
*/
Expand Down Expand Up @@ -311,6 +292,80 @@ public boolean equals(Object anObj)
return true;
}

/**
* Initialize Props. Override to provide custom defaults.
*/
@Override
protected void initProps(PropSet aPropSet)
{
// Do normal version
super.initProps(aPropSet);

// Align, Justify, FirstIndent, RightIndent, Spacing, SpacingFactor, NewlineSpacing, MinHeight, MaxHeight
aPropSet.addPropNamed(Align_Prop, HPos.class, DEFAULT_ALIGN);
aPropSet.addPropNamed(Justify_Prop, boolean.class, false);
aPropSet.addPropNamed(FirstIndent_Prop, double.class, 0d);
aPropSet.addPropNamed(LeftIndent_Prop, double.class, 0d);
aPropSet.addPropNamed(RightIndent_Prop, double.class, 0d);
aPropSet.addPropNamed(Spacing_Prop, double.class, 0d);
aPropSet.addPropNamed(SpacingFactor_Prop, double.class, DEFAULT_SPACING_FACTOR);
aPropSet.addPropNamed(NewlineSpacing_Prop, double.class, 0d);
aPropSet.addPropNamed(MinHeight_Prop, double.class, 0d);
aPropSet.addPropNamed(MaxHeight_Prop, double.class, DEFAULT_MAX_HEIGHT);
}

/**
* Returns the value for given prop name.
*/
@Override
public Object getPropValue(String aPropName)
{
// Handle properties
switch (aPropName) {

// Align, Justify, FirstIndent, LeftIndent, RightIndent, Spacing, SpacingFactor, NewlineSpacing, MinHeight, MaxHeight
case Align_Prop: return getAlign();
case Justify_Prop: return isJustify();
case FirstIndent_Prop: return getFirstIndent();
case LeftIndent_Prop: return getLeftIndent();
case RightIndent_Prop: return getRightIndent();
case Spacing_Prop: return getSpacing();
case SpacingFactor_Prop: return getSpacingFactor();
case NewlineSpacing_Prop: return getNewlineSpacing();
case MinHeight_Prop: return getMinHeight();
case MaxHeight_Prop: return getMaxHeight();

// Do normal version
default: return super.getPropValue(aPropName);
}
}

/**
* Sets the value for given prop name.
*/
@Override
public void setPropValue(String aPropName, Object aValue)
{
// Handle properties
switch (aPropName) {

// Align, Justify, FirstIndent, LeftIndent, RightIndent, Spacing, SpacingFactor, NewlineSpacing, MinHeight, MaxHeight
case Align_Prop: _align = HPos.of(aValue); _justify = false; break;
case Justify_Prop: _justify = Convert.boolValue(aValue); break;
case FirstIndent_Prop: _firstIndent = Convert.doubleValue(aValue); break;
case LeftIndent_Prop: _leftIndent = Convert.doubleValue(aValue); break;
case RightIndent_Prop: _rightIndent = Convert.doubleValue(aValue); break;
case Spacing_Prop: _spacing = Convert.doubleValue(aValue); break;
case SpacingFactor_Prop: _spacingFactor = Convert.doubleValue(aValue); break;
case NewlineSpacing_Prop: _newlineSpacing = Convert.doubleValue(aValue); break;
case MinHeight_Prop: _minHeight = Convert.doubleValue(aValue); break;
case MaxHeight_Prop: _maxHeight = Convert.doubleValue(aValue); break;

// Do normal version
default: super.setPropValue(aPropName, aValue);
}
}

/**
* Standard toString implementation.
*/
Expand Down Expand Up @@ -351,7 +406,7 @@ public XMLElement toXML(XMLArchiver anArchiver)
if (_newlineSpacing != 0) e.add("pgraph-space", _newlineSpacing);

// Archive Tabs
if (!Arrays.equals(_tabs, _defaultTabs) || !Arrays.equals(_tabTypes, _defaultTypes))
if (!Arrays.equals(_tabs, DEFAULT_TABS) || !Arrays.equals(_tabTypes, DEFAULT_TAB_TYPES))
e.add("tabs", getTabsString());

// Return element
Expand Down
2 changes: 1 addition & 1 deletion src/snap/viewx/DevPaneConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public DevConsoleView()

// Change line spacing
TextLineStyle lineStyle = textBlock.getDefaultLineStyle();
TextLineStyle lineStyle2 = lineStyle.copyFor(TextLineStyle.SPACING_KEY, 5);
TextLineStyle lineStyle2 = lineStyle.copyForPropKeyValue(TextLineStyle.Spacing_Prop, 5);
textBlock.setDefaultLineStyle(lineStyle2);

// Set Prompt
Expand Down

0 comments on commit 27004a8

Please sign in to comment.