View Javadoc

1   package com.sharkysoft.printf;
2   
3   import com.sharkysoft.printf.engine.AlignmentMode;
4   import com.sharkysoft.printf.engine.StringFormatter;
5   import com.sharkysoft.util.UnreachableCodeException;
6   
7   abstract class Formatter_Field extends Formatter
8   {
9   
10    protected final StringFormatter mpFormatter;
11    
12    protected final boolean mzVariableWidth;
13    
14    Formatter_Field(final FormatSpecifier ipPfs, final StringFormatter ipSf)
15    {
16      super();
17      mpFormatter = ipSf;
18      switch (ipPfs.mnWidth)
19      {
20      case FormatSpecifier.WIDTH_ARGUMENT:
21        mzVariableWidth = true;
22        break;
23      case FormatSpecifier.WIDTH_UNSPECIFIED:
24        mzVariableWidth = false;
25        break;
26      default:
27      	mzVariableWidth = false;
28      	mpFormatter.setFieldWidth(ipPfs.mnWidth);    
29      }
30  //    if (ipPfs.mnWidth == PrintfFormatSpecifier.WIDTH_ARGUMENT)
31  //      mzVariableWidth = true;
32  //    else
33  //    {
34  //      mzVariableWidth = false;
35  //      mpFormatter.setFieldWidth(ipPfs.mnWidth);
36  //    }
37      switch (ipPfs.mcJustification)
38      {
39      case FormatSpecifier.JUSTIFICATION_RIGHT :
40        mpFormatter.setAlignment(AlignmentMode.gpRight);
41        break;
42      case FormatSpecifier.JUSTIFICATION_LEFT :
43        mpFormatter.setAlignment(AlignmentMode.gpLeft);
44        break;
45      case FormatSpecifier.JUSTIFICATION_CENTER :
46        mpFormatter.setAlignment(AlignmentMode.gpCenter);
47        break;
48      case FormatSpecifier.JUSTIFICATION_FULL:
49        mpFormatter.setAlignment(AlignmentMode.gpFull);
50  			if (ipPfs.mcPadChar == FormatSpecifier.PADCHAR_ZERO)
51  				mpFormatter.setPadChar('0');
52        break;
53      default :
54        throw new UnreachableCodeException();
55      }
56    }
57  
58    protected final void adjustWidth(final PrintfState ipPs)
59    {
60      if (mzVariableWidth)
61        mpFormatter.setFieldWidth(((Number) ipPs.mapArgs[ipPs.mnArgIndex++]).intValue());
62    }
63    
64  }
65