• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 AndroidPlot.com
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 
17 package com.androidplot.ui;
18 
19 import android.graphics.RectF;
20 
21 import java.util.Iterator;
22 
23 public class FixedTableModel extends TableModel {
24     private float cellWidth;
25     private float cellHeight;
FixedTableModel(float cellWidth, float cellHeight, TableOrder order)26     protected FixedTableModel(float cellWidth, float cellHeight, TableOrder order) {
27         super(order);
28         setCellWidth(cellWidth);
29         setCellHeight(cellHeight);
30     }
31 
32     @Override
getIterator(RectF tableRect, int totalElements)33     public Iterator<RectF> getIterator(RectF tableRect, int totalElements) {
34         return new FixedTableModelIterator(this, tableRect, totalElements);
35     }
36 
getCellWidth()37     public float getCellWidth() {
38         return cellWidth;
39     }
40 
setCellWidth(float cellWidth)41     public void setCellWidth(float cellWidth) {
42         this.cellWidth = cellWidth;
43     }
44 
getCellHeight()45     public float getCellHeight() {
46         return cellHeight;
47     }
48 
setCellHeight(float cellHeight)49     public void setCellHeight(float cellHeight) {
50         this.cellHeight = cellHeight;
51     }
52 
53     private class FixedTableModelIterator implements Iterator<RectF> {
54 
55         private FixedTableModel model;
56         private RectF tableRect;
57         private RectF lastRect;
58         private int numElements;
59         private int lastElement;
FixedTableModelIterator(FixedTableModel model, RectF tableRect, int numElements)60         protected FixedTableModelIterator(FixedTableModel model, RectF tableRect, int numElements) {
61             this.model = model;
62             this.tableRect = tableRect;
63             this.numElements = numElements;
64             lastRect = new RectF(
65                     tableRect.left,
66                     tableRect.top,
67                     tableRect.left + model.getCellWidth(),
68                     tableRect.top + model.getCellHeight());
69         }
70 
71         @Override
hasNext()72         public boolean hasNext() {
73             // was this the last element or is there no room in either axis for another cell?
74             return !(lastElement >= numElements || (isColumnFinished() && isRowFinished()));
75         }
76 
isColumnFinished()77         private boolean isColumnFinished() {
78             return lastRect.bottom + model.getCellHeight() > tableRect.height();
79             }
80 
isRowFinished()81         private boolean isRowFinished() {
82             return lastRect.right + model.getCellWidth() > tableRect.width();
83             }
84 
85         @Override
next()86         public RectF next() {
87             try {
88                 if (lastElement == 0) {
89                     return lastRect;
90                 }
91 
92                 if (lastElement >= numElements) {
93                     throw new IndexOutOfBoundsException();
94                 }
95                 switch (model.getOrder()) {
96                     case ROW_MAJOR:
97                         if (isColumnFinished()) {
98                             moveOverAndUp();
99                         } else {
100                             moveDown();
101                         }
102                         break;
103                     case COLUMN_MAJOR:
104                         if (isRowFinished()) {
105                             moveDownAndBack();
106                         } else {
107                             moveOver();
108                         }
109                         break;
110                     default:
111                         throw new UnsupportedOperationException();
112                 }
113                 return lastRect;
114             } finally {
115                 lastElement++;
116             }
117         }
118 
moveDownAndBack()119         private void moveDownAndBack() {
120             //RectF rect = new RectF(lastRect);
121             lastRect.offsetTo(tableRect.left, lastRect.bottom);
122             //return rect;
123         }
124 
moveOverAndUp()125         private void moveOverAndUp() {
126             //RectF rect = new RectF(lastRect);
127             lastRect.offsetTo(lastRect.right, tableRect.top);
128             //return rect;
129         }
130 
moveOver()131         private void moveOver() {
132             //RectF rect = new RectF(lastRect);
133             lastRect.offsetTo(lastRect.right, lastRect.top);
134             //return rect;
135         }
136 
moveDown()137         private void moveDown() {
138             //RectF rect = new RectF(lastRect);
139             lastRect.offsetTo(lastRect.left, lastRect.bottom);
140             //return rect;
141         }
142 
143         @Override
remove()144         public void remove() {
145             throw new UnsupportedOperationException();
146         }
147     }
148 }
149