1 /*
2 * Prometheus: Application Framework
3 * Copyright 2012-2026. Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17 package io.github.tonywasher.joceanus.prometheus.service.sheet;
18
19 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheet.PrometheusSheetRowCtl;
20 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheet.PrometheusSheetSheetCtl;
21 import io.github.tonywasher.joceanus.prometheus.service.sheet.PrometheusSheet.PrometheusSheetViewCtl;
22
23 import java.util.ListIterator;
24
25 /**
26 * Class representing a row within a sheet or a view.
27 */
28 public abstract class PrometheusSheetRow
29 implements PrometheusSheetRowCtl {
30 /**
31 * The underlying sheet.
32 */
33 private final PrometheusSheetSheetCtl theSheet;
34
35 /**
36 * The index of this row.
37 */
38 private final int theRowIndex;
39
40 /**
41 * Is the row readOnly?
42 */
43 private final boolean isReadOnly;
44
45 /**
46 * Constructor.
47 *
48 * @param pSheet the sheet for the row
49 * @param pRowIndex the Row index
50 * @param pReadOnly is the row readOnly?
51 */
52 protected PrometheusSheetRow(final PrometheusSheetSheetCtl pSheet,
53 final int pRowIndex,
54 final boolean pReadOnly) {
55 /* Store parameters */
56 theSheet = pSheet;
57 theRowIndex = pRowIndex;
58 isReadOnly = pReadOnly;
59 }
60
61 /**
62 * Constructor.
63 *
64 * @param pView the view for the row
65 * @param pRowIndex the Row index
66 */
67 protected PrometheusSheetRow(final PrometheusSheetViewCtl pView,
68 final int pRowIndex) {
69 /* Store parameters */
70 theSheet = pView.getSheet();
71 theRowIndex = pRowIndex;
72 isReadOnly = true;
73 }
74
75 /**
76 * Obtain the sheet.
77 *
78 * @return the sheet
79 */
80 public PrometheusSheetSheetCtl getSheet() {
81 return theSheet;
82 }
83
84 /**
85 * Obtain the row index.
86 *
87 * @return the row index
88 */
89 public int getRowIndex() {
90 return theRowIndex;
91 }
92
93 /**
94 * Is the row readOnly?
95 *
96 * @return true/false
97 */
98 public boolean isReadOnly() {
99 return isReadOnly;
100 }
101
102 /**
103 * Get the next row.
104 *
105 * @return the next row
106 */
107 public abstract PrometheusSheetRow getNextRow();
108
109 /**
110 * Get the previous row.
111 *
112 * @return the previous row
113 */
114 public abstract PrometheusSheetRow getPreviousRow();
115
116 /**
117 * Determine number of cells in this row.
118 *
119 * @return the number of cells.
120 */
121 public abstract int getCellCount();
122
123 /**
124 * Determine index of the max valued cell.
125 *
126 * @return the index.
127 */
128 public abstract int getMaxValuedCellIndex();
129
130 /**
131 * Set hidden status.
132 *
133 * @param isHidden is the column hidden?
134 */
135 public void setHidden(final boolean isHidden) {
136 if (!isReadOnly) {
137 setHiddenValue(isHidden);
138 }
139 }
140
141 /**
142 * Set hidden status.
143 *
144 * @param isHidden is the column hidden?
145 */
146 protected abstract void setHiddenValue(boolean isHidden);
147
148 /**
149 * Is the column hidden?
150 *
151 * @return true/false
152 */
153 public abstract boolean isHidden();
154
155 /**
156 * Get the cell at the required index.
157 *
158 * @param pIndex the column index.
159 * @return the cell
160 */
161 public abstract PrometheusSheetCell getReadOnlyCellByIndex(int pIndex);
162
163 /**
164 * Get the cell at the required index.
165 *
166 * @param pIndex the column index.
167 * @return the cell
168 */
169 public PrometheusSheetCell getMutableCellByIndex(final int pIndex) {
170 return !isReadOnly && pIndex >= 0
171 ? getWriteableCellByIndex(pIndex)
172 : null;
173 }
174
175 /**
176 * Get the cell at the required index.
177 *
178 * @param pIndex the column index.
179 * @return the cell
180 */
181 protected abstract PrometheusSheetCell getWriteableCellByIndex(int pIndex);
182
183 /**
184 * Obtain an iterator of non-null cells for the row in the view.
185 *
186 * @param pFirstIndex the first cell in the view
187 * @param pLastIndex the last cell in the view
188 * @return the iterator
189 */
190 protected abstract ListIterator<PrometheusSheetCell> iteratorForRange(int pFirstIndex,
191 int pLastIndex);
192 }