001    /*
002     * Copyright 2009-2015 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2009-2015 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.util;
022    
023    
024    
025    /**
026     * This enumeration defines a set of values that may indicate how text should be
027     * horizontally aligned.
028     */
029    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
030    public enum HorizontalAlignment
031    {
032      /**
033       * Indicates that items should be aligned along their left edges.
034       */
035      LEFT(),
036    
037    
038    
039      /**
040       * Indicates that text should be aligned along their centers.
041       */
042      CENTER(),
043    
044    
045    
046      /**
047       * Indicates that text should be aligned along right edges.
048       */
049      RIGHT();
050    
051    
052    
053      /**
054       * Appends the provided string to the given buffer, aligned properly within
055       * the specified width.  Spaces will be inserted before and/or after the text
056       * as necessary to achieve the desired alignment.  This method will always
057       * append exactly {@code width} characters (including spaces added to achieve
058       * the desired alignment) to the provided buffer.  If the given text is longer
059       * than {@code width}, then only the first {@code width} characters of the
060       * provided text will be appended.
061       *
062       * @param  buffer  The buffer to which the formatted text should be appended.
063       *                 It must not be {@code null}.
064       * @param  text    The text to be added to the provided buffer, with
065       *                 additional spaces as necessary to achieve the desired
066       *                 width.  It must not be {@code null}.
067       * @param  width   The number of characters to append to the provided buffer.
068       *                 It must be greater than or equal to 1.
069       */
070      public void format(final StringBuilder buffer, final String text,
071                         final int width)
072      {
073        final int length = text.length();
074        if (length >= width)
075        {
076          buffer.append(text.substring(0, width));
077          return;
078        }
079    
080        final int spacesBefore;
081        final int spacesAfter;
082        switch (this)
083        {
084          case LEFT:
085            spacesBefore = 0;
086            spacesAfter  = width - length;
087            break;
088          case CENTER:
089            final int totalSpaces = width - length;
090            spacesBefore = totalSpaces / 2;
091            spacesAfter  = totalSpaces - spacesBefore;
092            break;
093          case RIGHT:
094          default:
095            spacesBefore = width - length;
096            spacesAfter  = 0;
097            break;
098        }
099    
100        for (int i=0; i < spacesBefore; i++)
101        {
102          buffer.append(' ');
103        }
104    
105        buffer.append(text);
106    
107        for (int i=0; i < spacesAfter; i++)
108        {
109          buffer.append(' ');
110        }
111      }
112    }