Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 65811ecadb9f935cfb2ef63b87d16fda5fb4b7b8 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (c) 2014 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
 */
package org.eclipse.oomph.internal.util.table;

import org.eclipse.oomph.internal.util.table.Formula.Percent;

import java.text.DecimalFormat;
import java.text.Format;

/**
 * @author Eike Stepper
 */
public class Table extends RectangularRange
{
  Cell[][] cells;

  public Table(int cols, int rows)
  {
    super(null, Coordinate.ZERO, new Coordinate(cols - 1, rows - 1));
    cells = new Cell[cols][];
    for (int col = 0; col < cols; col++)
    {
      cells[col] = new Cell[rows];
      for (int row = 0; row < rows; row++)
      {
        cells[col][row] = new Cell(this, col, row);
      }
    }
  }

  public Table(int cols)
  {
    this(cols, 1);
  }

  public Table()
  {
    this(1);
  }

  @Override
  public Table table()
  {
    return this;
  }

  public Cell cell(Coordinate coordinate)
  {
    return cell(coordinate.col, coordinate.row);
  }

  public Cell cell(int col, int row)
  {
    for (int c = cols(); c <= col; c++)
    {
      addColumn();
    }

    for (int r = rows(); r <= row; r++)
    {
      addRow();
    }

    return cells[col][row];
  }

  public RectangularRange range(Coordinate coordinate1, Coordinate coordinate2)
  {
    return new RectangularRange(this, coordinate1, coordinate2);
  }

  public RectangularRange range(int col1, int row1, int col2, int row2)
  {
    return range(new Coordinate(col1, row1), new Coordinate(col2, row2));
  }

  public RectangularRange column(int col)
  {
    return column(col, 0);
  }

  public RectangularRange column(int col, int skipTopRows)
  {
    return column(col, skipTopRows, 0);
  }

  public RectangularRange column(int col, int skipTopRows, int skipBottomRows)
  {
    return range(new Coordinate(col, skipTopRows), new Coordinate(col, bottomRight.row - skipBottomRows));
  }

  public Table addColumn(int index)
  {
    int cols = cells.length;
    addColumn();

    if (index < cols)
    {
      moveColumn(cols, index);
    }

    return this;
  }

  public Table addColumn()
  {
    int cols = cells.length;
    int rows = cells[0].length;

    Cell[][] newCells = new Cell[cols + 1][];
    System.arraycopy(cells, 0, newCells, 0, cols);

    newCells[cols] = new Cell[rows];
    for (int row = 0; row < rows; row++)
    {
      newCells[cols][row] = new Cell(this, cols, row);
    }

    cells = newCells;
    bottomRight = bottomRight.offset(1, 0);
    return this;
  }

  public Table moveColumn(int from, int to)
  {
    int rows = cells[0].length;

    if (from != to)
    {
      for (int row = 0; row < rows; row++)
      {
        Cell cell = cells[from][row];
        if (from < to)
        {
          for (int col = from; col < to; col++)
          {
            setCell(col, row, cells[col + 1][row]);
          }
        }
        else
        {
          for (int col = from; col > to; col--)
          {
            setCell(col, row, cells[col - 1][row]);
          }
        }

        setCell(to, row, cell);
      }
    }

    return this;
  }

  public RectangularRange row(int row)
  {
    return row(row, 0);
  }

  public RectangularRange row(int row, int skipLeftColumns)
  {
    return row(row, skipLeftColumns, 0);
  }

  public RectangularRange row(int row, int skipLeftColumns, int skipRightColumns)
  {
    return range(new Coordinate(skipLeftColumns, row), new Coordinate(bottomRight.col - skipRightColumns, row));
  }

  public Table addRow(int index)
  {
    int rows = cells[0].length;
    addRow();

    if (index < rows)
    {
      moveRow(rows, index);
    }

    return this;
  }

  public Table addRow()
  {
    int cols = cells.length;
    int rows = cells[0].length;

    for (int col = 0; col < cols; col++)
    {
      Cell[] newColumn = new Cell[rows + 1];
      System.arraycopy(cells[col], 0, newColumn, 0, rows);
      newColumn[rows] = new Cell(this, col, rows);
      cells[col] = newColumn;
    }

    bottomRight = bottomRight.offset(0, 1);
    return this;
  }

  public Table moveRow(int from, int to)
  {
    int cols = cells.length;

    if (from != to)
    {
      for (int col = 0; col < cols; col++)
      {
        Cell cell = cells[col][from];
        if (from < to)
        {
          for (int row = from; row < to; row++)
          {
            setCell(col, row, cells[col][row + 1]);
          }
        }
        else
        {
          for (int row = from; row > to; row--)
          {
            setCell(col, row, cells[col][row - 1]);
          }
        }

        setCell(col, to, cell);
      }
    }

    return this;
  }

  @Override
  public Table value(Object value)
  {
    return (Table)super.value(value);
  }

  @Override
  public Table format(Format format)
  {
    return (Table)super.format(format);
  }

  @Override
  public Table alignment(Alignment alignment)
  {
    return (Table)super.alignment(alignment);
  }

  private void setCell(int col, int row, Cell cell)
  {
    cells[col][row] = cell;
    cell.col = col;
    cell.row = row;
  }

  /**
   * Example
   */
  public static void main(String[] args) throws Exception
  {
    Table table1 = new Table();
    table1.range(0, 0, 5, 5).subtractRange(0, 0, 7, 0).value(new Generator.Incrementer());
    table1.row(0).format(new DecimalFormat("Series 0")).value(new Generator.Incrementer(1));
    Dumper.UTF8.dump(System.out, table1, 0);

    Table table2 = new Table(2, 11);
    table2.row(0).alignment(Alignment.RIGHT);
    table2.cell(0, 0).value("Probe");
    table2.cell(1, 0).value("Percent");

    Range probes = table2.column(0, 1).value(new Generator.Incrementer(1));
    for (Cell cell : probes.offset(1, 0).format(new DecimalFormat("0.00 %")))
    {
      cell.value(new Percent(probes, cell.offset(-1, 0)));
    }

    Dumper.UTF8.dump(System.out, table2, 0);
  }
}

Back to the top