dmolony-DiskBrowser/src/com/bytezone/diskbrowser/applefile/Alignment.java

83 lines
2.8 KiB
Java
Raw Permalink Normal View History

2021-03-03 13:19:13 +00:00
package com.bytezone.diskbrowser.applefile;
// ---------------------------------------------------------------------------------//
class Alignment implements ApplesoftConstants
// ---------------------------------------------------------------------------------//
{
int equalsPosition;
int targetLength;
SubLine firstSubLine;
SubLine lastSubLine;
// ---------------------------------------------------------------------------------//
void reset ()
// ---------------------------------------------------------------------------------//
{
equalsPosition = 0;
targetLength = 0;
firstSubLine = null;
lastSubLine = null;
}
// ---------------------------------------------------------------------------------//
void setFirst (SubLine subline)
// ---------------------------------------------------------------------------------//
{
reset ();
firstSubLine = subline;
check (subline);
}
// ---------------------------------------------------------------------------------//
void check (SubLine subline)
// ---------------------------------------------------------------------------------//
{
if (equalsPosition < subline.equalsPosition)
equalsPosition = subline.equalsPosition;
int temp = subline.endPosition - subline.equalsPosition;
if (targetLength < temp)
targetLength = temp;
lastSubLine = subline;
}
// ---------------------------------------------------------------------------------//
public String getAlignedText (SubLine subline)
// ---------------------------------------------------------------------------------//
{
2021-05-05 22:26:47 +00:00
StringBuilder line = subline.toStringBuilder (); // get line
2021-03-03 13:19:13 +00:00
if (equalsPosition == 0 || subline.is (TOKEN_REM))
return line.toString ();
int alignEqualsPos = equalsPosition;
int targetLength = subline.endPosition - equalsPosition;
// insert spaces before '=' until it lines up with the other assignment lines
2021-05-05 22:12:46 +00:00
while (alignEqualsPos-- > subline.equalsPosition)
line.insert (subline.equalsPosition, ' ');
2021-03-03 13:19:13 +00:00
if (line.charAt (line.length () - 1) == ':')
2021-05-05 22:12:46 +00:00
while (targetLength++ <= this.targetLength)
2021-03-03 13:19:13 +00:00
line.append (" ");
return line.toString ();
}
2021-05-05 22:12:46 +00:00
// ---------------------------------------------------------------------------------//
@Override
public String toString ()
// ---------------------------------------------------------------------------------//
{
StringBuilder text = new StringBuilder ();
text.append (String.format ("Equals position ..... %d%n", equalsPosition));
text.append (String.format ("Target length ....... %d%n", targetLength));
2021-05-05 22:26:47 +00:00
text.append (String.format ("First subline ....... %s%n", firstSubLine));
text.append (String.format ("Last subline ........ %s", lastSubLine));
2021-05-05 22:12:46 +00:00
return text.toString ();
}
2021-03-03 13:19:13 +00:00
}