001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020package org.crsh.text.ui;
021
022import org.crsh.text.LineRenderer;
023
024import java.util.List;
025
026class TableRowLineRenderer {
027
028  /** . */
029  final TableLineRenderer table;
030
031  /** . */
032  final RowLineRenderer row;
033
034  /** . */
035  final boolean header;
036
037  /** . */
038  private TableRowLineRenderer previous;
039
040  /** . */
041  private TableRowLineRenderer next;
042
043  /** . */
044  private int index;
045
046  TableRowLineRenderer(TableLineRenderer table, RowElement row) {
047    this.table = table;
048    this.row = new RowLineRenderer(row, table.separator, table.leftCellPadding, table.rightCellPadding);
049    this.header = row.header;
050    this.index = 0;
051  }
052
053  TableRowLineRenderer add(TableRowLineRenderer next) {
054    next.previous = this;
055    next.index = index + 1;
056    this.next = next;
057    return next;
058  }
059
060  boolean hasTop() {
061    return header && previous != null;
062  }
063
064  boolean hasBottom() {
065    return header && next != null && !next.header;
066  }
067
068  int getIndex() {
069    return index;
070  }
071
072  int getSize() {
073    return index + 1;
074  }
075
076  TableRowLineRenderer previous() {
077    return previous;
078  }
079
080  TableRowLineRenderer next() {
081    return next;
082  }
083
084  boolean isHeader() {
085    return header;
086  }
087
088  int getColsSize() {
089    return row.getSize();
090  }
091
092  List<LineRenderer> getCols() {
093    return row.getCols();
094  }
095
096  int getActualWidth() {
097    return row.getActualWidth();
098  }
099
100  int getMinWidth() {
101    return row.getMinWidth();
102  }
103
104  int getActualHeight(int width) {
105    int actualHeight;
106    switch (table.overflow) {
107      case HIDDEN:
108        actualHeight = 1;
109        break;
110      case WRAP:
111        actualHeight = row.getActualHeight(width);
112        break;
113      default:
114        throw new AssertionError();
115    }
116    if (hasTop()) {
117      actualHeight++;
118    }
119    if (hasBottom()) {
120      actualHeight++;
121    }
122    return actualHeight;
123  }
124
125  int getActualHeight(int[] widths) {
126    int actualHeight;
127    switch (table.overflow) {
128      case HIDDEN:
129        actualHeight = 1;
130        break;
131      case WRAP:
132        actualHeight = 0;
133        for (int i = 0;i < widths.length;i++) {
134          LineRenderer col = row.getCols().get(i);
135          actualHeight = Math.max(actualHeight, col.getActualHeight(widths[i]));
136        }
137        break;
138      default:
139        throw new AssertionError();
140    }
141    if (hasTop()) {
142      actualHeight++;
143    }
144    if (hasBottom()) {
145      actualHeight++;
146    }
147    return actualHeight;
148  }
149
150  int getMinHeight(int[] widths) {
151    int minHeight;
152    switch (table.overflow) {
153      case HIDDEN:
154        minHeight = 1;
155        break;
156      case WRAP:
157        minHeight = 0;
158        for (int i = 0;i < widths.length;i++) {
159          LineRenderer col = row.getCols().get(i);
160          minHeight = Math.max(minHeight, col.getMinHeight(widths[i]));
161        }
162        break;
163      default:
164        throw new AssertionError();
165    }
166    if (hasTop()) {
167      minHeight++;
168    }
169    if (hasBottom()) {
170      minHeight++;
171    }
172    return minHeight;
173  }
174
175  TableRowReader renderer(int[] widths, int height) {
176    return new TableRowReader(this, row, widths, height);
177  }
178}