View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.ouput.asciitable;
28  
29  /**
30   * A table's row contains the data, keeps a reference to its parent and computes
31   * its own width.
32   * 
33   * @author Alexander Onea, neue Couch
34   */
35  public final class Row extends AbstractTabularComponent {
36  
37      /**
38       * The data, converted to string.
39       */
40      private transient final String[] data;
41  
42      /**
43       * Constructor.
44       * 
45       * @param myParent
46       *            table to be printed to
47       * @param paramData
48       *            data to be plotted
49       */
50      public Row(final NiceTable myParent, final String[] paramData) {
51          super(myParent);
52          data = new String[paramData.length];
53          System.arraycopy(paramData, 0, data, 0, paramData.length);
54          for (int i = 0; i < data.length; i++) {
55              getTable().updateColumnWidth(i, data[i].length());
56              data[i] = data[i].trim();
57          }
58      }
59  
60      /**
61       * Returns the row's total width.
62       * 
63       * @return the width of the row.
64       */
65      public int getRowWidth() {
66  
67          int overallWidth = 0;
68          for (int i = 0; i < data.length; i++) {
69              overallWidth += getTable().getColumnWidth(i);
70          }
71          return overallWidth;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public String draw() {
79  
80          final String[] row = new String[data.length];
81          for (int i = 0; i < data.length; i++) {
82              row[i] = Util.pad(data[i], ' ', getTable().getColumnWidth(i), getTable().getOrientation(i));
83  
84          }
85          return Util.combine(new String(new char[] {
86              AbstractTabularComponent.BORDER
87          }), SPACE, Util.implode(Util.combine(SPACE, new String(new char[] {
88              AbstractTabularComponent.BORDER
89          }), SPACE), row), SPACE, new String(new char[] {
90              AbstractTabularComponent.BORDER
91          }), NEWLINE);
92  
93      }
94  
95  }