Skip to content

Commit d053fab

Browse files
committed
Expose column and row min setting in GridView
- Add method setMinSize for setting minimum width for columns and minimum height for rows. - Relates #805
1 parent 3ac48bf commit d053fab

File tree

2 files changed

+218
-21
lines changed
  • spring-shell-core/src

2 files changed

+218
-21
lines changed

spring-shell-core/src/main/java/org/springframework/shell/component/view/control/GridView.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.shell.component.view.event.MouseHandler.MouseHandlerResult;
3030
import org.springframework.shell.component.view.geom.Rectangle;
3131
import org.springframework.shell.component.view.screen.Screen;
32+
import org.springframework.util.Assert;
3233

3334
/**
3435
* {@code GridView} is a layout container with no initial {@link View views}.
@@ -54,55 +55,74 @@ public class GridView extends BoxView {
5455
private boolean showBorders;
5556

5657
/**
57-
* Defines how the columns of the grid are distributed. Each value
58-
* defines the size of one column, starting with the leftmost column. Values
59-
* greater 0 represent absolute column widths (gaps not included). Values less
60-
* or equal 0 represent proportional column widths or fractions of the remaining
61-
* free space, where 0 is treated the same as -1. That is, a column with a value
58+
* Defines how the columns of the grid are distributed. Each value defines the
59+
* size of one column, starting with the leftmost column. Values greater 0
60+
* represent absolute column widths (gaps not included). Values less or equal 0
61+
* represent proportional column widths or fractions of the remaining free
62+
* space, where 0 is treated the same as -1. That is, a column with a value
6263
* of -3 will have three times the width of a column with a value of -1 (or 0).
63-
* The minimum width set with SetMinSize() is always observed.
64+
* The minimum width set with {@link #setMinSize(int, int)} is always observed.
6465
*
65-
* Views may extend beyond the columns defined explicitly with this
66-
* function. A value of 0 is assumed for any undefined column. In fact, if you
67-
* never call this function, all columns occupied by Views will have the
68-
* same width. On the other hand, unoccupied columns defined with this function
69-
* will always take their place.
66+
* <p>Views may extend beyond the columns defined explicitly with this function. A
67+
* value of 0 is assumed for any undefined column. In fact, if you never call
68+
* this function, all columns occupied by Views will have the same width. On the
69+
* other hand, unoccupied columns defined with this function will always take
70+
* their place.
7071
*
71-
* Assuming a total width of the grid of 100 cells and a minimum width of 0, the
72+
* <p>Assuming a total width of the grid of 100 cells and a minimum width of 0, the
7273
* following call will result in columns with widths of 30, 10, 15, 15, and 30
7374
* cells:
75+
* <p>
7476
*
75-
* grid.SetColumns(30, 10, -1, -1, -2)
77+
* grid.setColumnSize(30, 10, -1, -1, -2)
7678
*
77-
* If a primitive were then placed in the 6th and 7th column, the resulting
79+
* <p>If a {@link View} were then placed in the 6th and 7th column, the resulting
7880
* widths would be: 30, 10, 10, 10, 20, 10, and 10 cells.
7981
*
80-
* If you then called SetMinSize() as follows:
82+
* If you then called setMinSize() as follows:
83+
* <p>
8184
*
82-
* grid.SetMinSize(15, 20)
85+
* grid.setMinSize(15, 20)
8386
*
84-
* The resulting widths would be: 30, 15, 15, 15, 20, 15, and 15 cells, a total
87+
* <p>The resulting widths would be: 30, 15, 15, 15, 20, 15, and 15 cells, a total
8588
* of 125 cells, 25 cells wider than the available grid width.
8689
*
87-
* @param columns
88-
* @return
90+
* @param columns the column sizes
91+
* @return a grid view for chaining
92+
* @see #setRowSize(int...)
8993
*/
9094
public GridView setColumnSize(int... columns) {
9195
this.columnSize = columns;
9296
return this;
9397
}
9498

9599
/**
100+
* For documentation see {@link #setColumnSize(int...)} as it's equivalent for rows.
96101
*
97-
* @param rows
98-
* @return
102+
* @param rows the row sizes
103+
* @return a grid view for chaining
99104
* @see #setColumnSize(int...)
100105
*/
101106
public GridView setRowSize(int... rows) {
102107
this.rowSize = rows;
103108
return this;
104109
}
105110

111+
/**
112+
* Sets an absolute minimum width for rows and an absolute minimum height for
113+
* columns. Negative values cannot be used.
114+
*
115+
* @param minWidth the rows minimum width
116+
* @param minHeight the columns minimum height
117+
* @return a grid view for chaining
118+
*/
119+
public GridView setMinSize(int minWidth, int minHeight) {
120+
Assert.state(minWidth > -1 || minHeight > -1, "Minimum sizes for rows or colums cannot be negative");
121+
this.minWidth = minWidth;
122+
this.minHeight = minHeight;
123+
return this;
124+
}
125+
106126
public GridView addItem(View view, int row, int column, int rowSpan, int colSpan, int minGridHeight,
107127
int minGridWidth) {
108128
GridItem gridItem = new GridItem(view, row, column, colSpan, rowSpan, minGridHeight,

spring-shell-core/src/test/java/org/springframework/shell/component/view/control/GridViewTests.java

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.shell.component.view.screen.DefaultScreen;
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.mockito.Mockito.never;
2425
import static org.mockito.Mockito.spy;
2526
import static org.mockito.Mockito.verify;
2627

@@ -88,6 +89,182 @@ void positionsWith1x2() {
8889
verify(sbox2).setRect(0, 12, 80, 12);
8990
}
9091

92+
@Test
93+
void positionsWith3x3() {
94+
BoxView box1 = new BoxView();
95+
BoxView box2 = new BoxView();
96+
BoxView box3 = new BoxView();
97+
BoxView box4 = new BoxView();
98+
BoxView box5 = new BoxView();
99+
BoxView box6 = new BoxView();
100+
BoxView box7 = new BoxView();
101+
BoxView box8 = new BoxView();
102+
BoxView box9 = new BoxView();
103+
BoxView sbox1 = spy(box1);
104+
BoxView sbox2 = spy(box2);
105+
BoxView sbox3 = spy(box3);
106+
BoxView sbox4 = spy(box4);
107+
BoxView sbox5 = spy(box5);
108+
BoxView sbox6 = spy(box6);
109+
BoxView sbox7 = spy(box7);
110+
BoxView sbox8 = spy(box8);
111+
BoxView sbox9 = spy(box9);
112+
GridView grid = new GridView();
113+
114+
grid.setShowBorders(false);
115+
grid.setRowSize(0, 0, 0);
116+
grid.setColumnSize(0, 0, 0);
117+
grid.setShowBorder(false);
118+
119+
grid.addItem(sbox1, 0, 0, 1, 1, 0, 0);
120+
grid.addItem(sbox2, 0, 1, 1, 1, 0, 0);
121+
grid.addItem(sbox3, 0, 2, 1, 1, 0, 0);
122+
123+
grid.addItem(sbox4, 1, 0, 1, 1, 0, 0);
124+
grid.addItem(sbox5, 1, 1, 1, 1, 0, 0);
125+
grid.addItem(sbox6, 1, 2, 1, 1, 0, 0);
126+
127+
grid.addItem(sbox7, 2, 0, 1, 1, 0, 0);
128+
grid.addItem(sbox8, 2, 1, 1, 1, 0, 0);
129+
grid.addItem(sbox9, 2, 2, 1, 1, 0, 0);
130+
131+
grid.setRect(0, 0, 80, 24);
132+
grid.draw(screen24x80);
133+
134+
verify(sbox1).setRect(0, 0, 26, 8);
135+
verify(sbox2).setRect(26, 0, 27, 8);
136+
verify(sbox3).setRect(53, 0, 27, 8);
137+
verify(sbox4).setRect(0, 8, 26, 8);
138+
verify(sbox5).setRect(26, 8, 27, 8);
139+
verify(sbox6).setRect(53, 8, 27, 8);
140+
verify(sbox7).setRect(0, 16, 26, 8);
141+
verify(sbox8).setRect(26, 16, 27, 8);
142+
verify(sbox9).setRect(53, 16, 27, 8);
143+
}
144+
145+
@Test
146+
void minimumWidthWith3x3_1() {
147+
BoxView box1 = new BoxView();
148+
BoxView box2 = new BoxView();
149+
BoxView box3 = new BoxView();
150+
BoxView box4 = new BoxView();
151+
BoxView box5 = new BoxView();
152+
BoxView sbox1 = spy(box1);
153+
BoxView sbox2 = spy(box2);
154+
BoxView sbox3 = spy(box3);
155+
BoxView sbox4 = spy(box4);
156+
BoxView sbox5 = spy(box5);
157+
GridView grid = new GridView();
158+
159+
grid.setShowBorders(false);
160+
grid.setRowSize(0);
161+
grid.setColumnSize(30, 10, -1, -1, -2);
162+
grid.setShowBorder(false);
163+
164+
grid.addItem(sbox1, 0, 0, 1, 1, 0, 0);
165+
grid.addItem(sbox2, 0, 1, 1, 1, 0, 0);
166+
grid.addItem(sbox3, 0, 2, 1, 1, 0, 0);
167+
grid.addItem(sbox4, 0, 3, 1, 1, 0, 0);
168+
grid.addItem(sbox5, 0, 4, 1, 1, 0, 0);
169+
170+
grid.setRect(0, 0, 100, 100);
171+
grid.draw(screen24x80);
172+
173+
verify(sbox1).setRect(0, 0, 30, 100);
174+
verify(sbox2).setRect(30, 0, 10, 100);
175+
verify(sbox3).setRect(40, 0, 15, 100);
176+
verify(sbox4).setRect(55, 0, 15, 100);
177+
verify(sbox5).setRect(70, 0, 30, 100);
178+
}
179+
180+
@Test
181+
void minimumWidthWith3x3_2() {
182+
BoxView box1 = new BoxView();
183+
BoxView box2 = new BoxView();
184+
BoxView box3 = new BoxView();
185+
BoxView box4 = new BoxView();
186+
BoxView box5 = new BoxView();
187+
BoxView box6 = new BoxView();
188+
BoxView box7 = new BoxView();
189+
BoxView sbox1 = spy(box1);
190+
BoxView sbox2 = spy(box2);
191+
BoxView sbox3 = spy(box3);
192+
BoxView sbox4 = spy(box4);
193+
BoxView sbox5 = spy(box5);
194+
BoxView sbox6 = spy(box6);
195+
BoxView sbox7 = spy(box7);
196+
GridView grid = new GridView();
197+
198+
grid.setShowBorders(false);
199+
grid.setRowSize(0);
200+
grid.setColumnSize(30, 10, -1, -1, -2);
201+
grid.setShowBorder(false);
202+
203+
grid.addItem(sbox1, 0, 0, 1, 1, 0, 0);
204+
grid.addItem(sbox2, 0, 1, 1, 1, 0, 0);
205+
grid.addItem(sbox3, 0, 2, 1, 1, 0, 0);
206+
grid.addItem(sbox4, 0, 3, 1, 1, 0, 0);
207+
grid.addItem(sbox5, 0, 4, 1, 1, 0, 0);
208+
grid.addItem(sbox6, 0, 5, 1, 1, 0, 0);
209+
grid.addItem(sbox7, 0, 6, 1, 1, 0, 0);
210+
211+
grid.setRect(0, 0, 100, 100);
212+
grid.draw(screen24x80);
213+
214+
verify(sbox1).setRect(0, 0, 30, 100);
215+
verify(sbox2).setRect(30, 0, 10, 100);
216+
verify(sbox3).setRect(40, 0, 10, 100);
217+
verify(sbox4).setRect(50, 0, 10, 100);
218+
verify(sbox5).setRect(60, 0, 20, 100);
219+
verify(sbox6).setRect(80, 0, 10, 100);
220+
verify(sbox7).setRect(90, 0, 10, 100);
221+
}
222+
223+
@Test
224+
void minimumWidthWith3x3_3() {
225+
BoxView box1 = new BoxView();
226+
BoxView box2 = new BoxView();
227+
BoxView box3 = new BoxView();
228+
BoxView box4 = new BoxView();
229+
BoxView box5 = new BoxView();
230+
BoxView box6 = new BoxView();
231+
BoxView box7 = new BoxView();
232+
BoxView sbox1 = spy(box1);
233+
BoxView sbox2 = spy(box2);
234+
BoxView sbox3 = spy(box3);
235+
BoxView sbox4 = spy(box4);
236+
BoxView sbox5 = spy(box5);
237+
BoxView sbox6 = spy(box6);
238+
BoxView sbox7 = spy(box7);
239+
GridView grid = new GridView();
240+
241+
grid.setShowBorders(false);
242+
grid.setRowSize(0);
243+
grid.setColumnSize(30, 10, -1, -1, -2);
244+
grid.setShowBorder(false);
245+
246+
grid.addItem(sbox1, 0, 0, 1, 1, 0, 0);
247+
grid.addItem(sbox2, 0, 1, 1, 1, 0, 0);
248+
grid.addItem(sbox3, 0, 2, 1, 1, 0, 0);
249+
grid.addItem(sbox4, 0, 3, 1, 1, 0, 0);
250+
grid.addItem(sbox5, 0, 4, 1, 1, 0, 0);
251+
grid.addItem(sbox6, 0, 5, 1, 1, 0, 0);
252+
grid.addItem(sbox7, 0, 6, 1, 1, 0, 0);
253+
254+
grid.setRect(0, 0, 100, 100);
255+
grid.setMinSize(15, 20);
256+
grid.draw(screen24x80);
257+
258+
verify(sbox1).setRect(0, 0, 30, 100);
259+
verify(sbox2).setRect(30, 0, 15, 100);
260+
verify(sbox3).setRect(45, 0, 15, 100);
261+
verify(sbox4).setRect(60, 0, 15, 100);
262+
verify(sbox5).setRect(75, 0, 18, 100);
263+
verify(sbox6).setRect(93, 0, 7, 100);
264+
// last one outside should not get called
265+
verify(sbox7, never()).setRect(0, 0, 0, 0);
266+
}
267+
91268
}
92269

93270
@Nested

0 commit comments

Comments
 (0)