Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3063a5a0de1d45cc2ae903f21c43ba3e4b222c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (c) 2014, 2015 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *    Cornel Izbasa <cizbasa@info.uvt.ro> - Bug 465236
 */
package org.eclipse.oomph.internal.util.table;

import org.eclipse.oomph.internal.util.table.Range.Alignment;

import java.io.PrintStream;

/**
 * @author Eike Stepper
 */
public class Dumper
{
  public static final Dumper ASCII = new Dumper();

  public static final Dumper UTF8 = new Dumper('\u2500', '\u2534', '\u252c', '\u253c', '\u2518', '\u2510', '\u2524', '\u2502', '\u251c', '\u2514', '\u250c',
      "\n", " ");

  public final char borderLeftRight;

  public final char borderLeftRightUp;

  public final char borderLeftRightDown;

  public final char borderLeftRightUpDown;

  public final char borderLeftUp;

  public final char borderLeftDown;

  public final char borderLeftUpDown;

  public final char borderUpDown;

  public final char borderUpDownRight;

  public final char borderUpRight;

  public final char borderDownRight;

  public final String newLine;

  public final String padding;

  public final String paddingBorder;

  public Dumper(char borderLeftRight, char borderLeftRightUp, char borderLeftRightDown, char borderLeftRightUpDown, char borderLeftUp, char borderLeftDown,
      char borderLeftUpDown, char borderUpDown, char borderUpDownRight, char borderUpRight, char borderDownRight, String newLine, String padding)
  {
    this.borderLeftRight = borderLeftRight;
    this.borderLeftRightUp = borderLeftRightUp;
    this.borderLeftRightDown = borderLeftRightDown;
    this.borderLeftRightUpDown = borderLeftRightUpDown;
    this.borderLeftUp = borderLeftUp;
    this.borderLeftDown = borderLeftDown;
    this.borderLeftUpDown = borderLeftUpDown;
    this.borderUpDown = borderUpDown;
    this.borderUpDownRight = borderUpDownRight;
    this.borderUpRight = borderUpRight;
    this.borderDownRight = borderDownRight;

    this.newLine = newLine == null ? "" : newLine;
    this.padding = padding == null ? "" : padding;

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < this.padding.length(); i++)
    {
      builder.append(borderLeftRight);
    }

    paddingBorder = builder.toString();
  }

  public Dumper()
  {
    this('-', '+', '+', '+', '+', '+', '+', '|', '+', '+', '+', "\n", " ");
  }

  public String dump(RectangularRange range, int... rowSeparators)
  {
    StringBuilder builder = new StringBuilder();
    dump(builder, range, rowSeparators);
    return builder.toString();
  }

  public void dump(PrintStream stream, RectangularRange range, int... rowSeparators)
  {
    String string = dump(range, rowSeparators);
    stream.print(string);
    stream.flush();
  }

  public void dump(StringBuilder builder, RectangularRange range, int... rowSeparators)
  {
    Table table = range.table();
    Coordinate topLeft = range.topLeft();
    int cols = range.cols();
    int rows = range.rows();

    String[][] strings = new String[cols][];
    Alignment[][] alignments = new Alignment[cols][];
    int[] widths = new int[cols];

    for (int col = 0; col < cols; col++)
    {
      strings[col] = new String[rows];
      alignments[col] = new Alignment[rows];

      for (int row = 0; row < rows; row++)
      {
        Cell cell = table.cell(topLeft.col + col, range.topLeft().row + row);
        Object value = cell.value();
        String string = cell.applyFormat(value);

        strings[col][row] = string;
        alignments[col][row] = cell.alignmentFor(value);

        widths[col] = Math.max(widths[col], string.length());
      }
    }

    dumpSeparator(builder, Position.BEGIN, widths);
    for (int row = 0; row < rows; row++)
    {
      // Position vertical = row == 0 ? Position.BEGIN : row == rows - 1 ? Position.END : Position.MIDDLE;

      builder.append(borderUpDown);
      builder.append(padding);

      for (int col = 0; col < cols; col++)
      {
        if (col != 0)
        {
          builder.append(padding);
          builder.append(borderUpDown);
          builder.append(padding);
        }

        String value = strings[col][row];
        Alignment alignment = alignments[col][row];
        int width = widths[col];

        builder.append(alignment.apply(value, width));
      }

      builder.append(padding);
      builder.append(borderUpDown);
      builder.append(newLine);

      if (row < rows - 1 && needsSeparator(row, rowSeparators))
      {
        dumpSeparator(builder, Position.MIDDLE, widths);
      }
    }

    dumpSeparator(builder, Position.END, widths);
  }

  private void dumpSeparator(StringBuilder builder, Position vertical, int[] widths)
  {
    builder.append(border(Position.BEGIN, vertical));
    builder.append(paddingBorder);

    for (int c = 0; c < widths.length; c++)
    {
      if (c > 0)
      {
        builder.append(paddingBorder);
        builder.append(border(Position.MIDDLE, vertical));
        builder.append(paddingBorder);
      }

      for (int i = 0; i < widths[c]; i++)
      {
        builder.append(borderLeftRight);
      }
    }

    builder.append(paddingBorder);
    builder.append(border(Position.END, vertical));
    builder.append(newLine);
  }

  private char border(Position horizontal, Position vertical)
  {
    switch (horizontal)
    {
      case BEGIN:
        switch (vertical)
        {
          case BEGIN:
            return borderDownRight;
          case MIDDLE:
            return borderUpDownRight;
          case END:
            return borderUpRight;
        }

        break;

      case MIDDLE:
        switch (vertical)
        {
          case BEGIN:
            return borderLeftRightDown;
          case MIDDLE:
            return borderLeftRightUpDown;
          case END:
            return borderLeftRightUp;
        }

        break;

      case END:
        switch (vertical)
        {
          case BEGIN:
            return borderLeftDown;
          case MIDDLE:
            return borderLeftUpDown;
          case END:
            return borderLeftUp;
        }

        break;
    }

    throw new IllegalArgumentException();
  }

  private static boolean needsSeparator(int row, int[] rowSeparators)
  {
    for (int i = 0; i < rowSeparators.length; i++)
    {
      if (rowSeparators[i] == row)
      {
        return true;
      }
    }

    return false;
  }

  /**
   * @author Eike Stepper
   */
  private enum Position
  {
    BEGIN, MIDDLE, END
  }
}

Back to the top