Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a293d2dd4506f4f8d9168296dbbd44be4aac427 (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
/*
 * 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.util.AbstractIterator;

import java.text.Format;
import java.util.Iterator;

/**
 * @author Eike Stepper
 */
public class RectangularRange extends AbstractRange
{
  final Table table;

  Coordinate topLeft;

  Coordinate bottomRight;

  RectangularRange(Table table, Coordinate coordinate1, Coordinate coordinate2)
  {
    this.table = table;

    int row1, row2;
    if (coordinate1.row <= coordinate2.row)
    {
      row1 = coordinate1.row;
      row2 = coordinate2.row;
    }
    else
    {
      row1 = coordinate2.row;
      row2 = coordinate1.row;
    }

    int col1, col2;
    if (coordinate1.col <= coordinate2.col)
    {
      col1 = coordinate1.col;
      col2 = coordinate2.col;
    }
    else
    {
      col1 = coordinate2.col;
      col2 = coordinate1.col;
    }

    topLeft = new Coordinate(col1, row1);
    bottomRight = new Coordinate(col2, row2);

    if (table != null)
    {
      // Make sure that the table is large enough
      table.cell(bottomRight);
    }
  }

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

  public Coordinate topLeft()
  {
    return topLeft;
  }

  public Coordinate bottomRight()
  {
    return bottomRight;
  }

  public int cols()
  {
    return bottomRight.col - topLeft.col + 1;
  }

  public int rows()
  {
    return bottomRight.row - topLeft.row + 1;
  }

  @Override
  public boolean contains(int col, int row)
  {
    return topLeft.col <= col && col <= bottomRight.col && topLeft.row <= row && row <= bottomRight.row;
  }

  @Override
  public boolean contains(Range range)
  {
    if (range instanceof RectangularRange)
    {
      RectangularRange rect = (RectangularRange)range;
      return topLeft.col <= rect.topLeft.col && rect.bottomRight.col <= bottomRight.col && topLeft.row <= rect.topLeft.row
          && rect.bottomRight.row <= bottomRight.row;
    }

    return super.contains(range);
  }

  @Override
  public Iterator<Cell> iterator()
  {
    return new CellIterator();
  }

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

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

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

  /**
   * @author Eike Stepper
   */
  private final class CellIterator extends AbstractIterator<Cell>
  {
    private int col = topLeft.col;

    private int row = topLeft.row;

    @Override
    protected Object computeNextElement()
    {
      try
      {
        if (col > bottomRight.col)
        {
          if (++row > bottomRight.row)
          {
            return END_OF_DATA;
          }

          col = topLeft.col;
        }

        return table().cell(col, row);
      }
      finally
      {
        ++col;
      }
    }
  }
}

Back to the top