Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87ed0eb3f8eaba51033561bc2a6a4e5c4cfdb113 (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
/*******************************************************************************
 * Copyright (c) 2013 Dirk Fauth and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Dirk Fauth <dirk.fauth@gmail.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.nebula.widgets.nattable.edit.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.coordinate.PositionCoordinate;
import org.eclipse.nebula.widgets.nattable.data.convert.DefaultBooleanDisplayConverter;
import org.eclipse.nebula.widgets.nattable.edit.ActiveCellEditorRegistry;
import org.eclipse.nebula.widgets.nattable.edit.EditConfigAttributes;
import org.eclipse.nebula.widgets.nattable.edit.editor.CheckBoxCellEditor;
import org.eclipse.nebula.widgets.nattable.edit.editor.ICellEditor;
import org.eclipse.nebula.widgets.nattable.edit.editor.TextCellEditor;
import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.layer.stack.DummyGridLayerStack;
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.test.fixture.NatTableFixture;
import org.junit.Before;
import org.junit.Test;

public class EditUtilsTest {

	private static final String TEST_LABEL = "testLabel";

	private NatTableFixture natTable;
	private DummyGridLayerStack gridLayerStack;
	private SelectionLayer selectionLayer;

	@Before
	public void setup() {
		gridLayerStack = new DummyGridLayerStack(5, 5);
		selectionLayer = gridLayerStack.getBodyLayer().getSelectionLayer();
		natTable = new NatTableFixture(gridLayerStack);

		// Ensure no active editor (static) is present
		assertNull(ActiveCellEditorRegistry.getActiveCellEditor());
	}

	@Test
	public void testGetLastSelectedCellWithoutSelection() {
		assertNull(EditUtils.getLastSelectedCell(selectionLayer));
	}

	@Test
	public void testGetLastSelectedCellWithSingleSelection() {
		selectionLayer.selectCell(1, 1, false, false);
		ILayerCell cell = EditUtils.getLastSelectedCell(selectionLayer);
		assertNotNull(cell);
		assertEquals(1, cell.getColumnIndex());
		assertEquals(1, cell.getRowIndex());
	}

	@Test
	public void testGetLastSelectedCellWithMultiSelection() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);
		ILayerCell cell = EditUtils.getLastSelectedCell(selectionLayer);
		assertNotNull(cell);
		assertEquals(3, cell.getColumnIndex());
		assertEquals(3, cell.getRowIndex());
	}

	@Test
	public void testGetLastSelectedCellEditorWithoutSelection() {
		assertNull(EditUtils.getLastSelectedCellEditor(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testGetLastSelectedCellEditorWithSingleSelection() {
		selectionLayer.selectCell(1, 1, false, false);
		ICellEditor editor = EditUtils.getLastSelectedCellEditor(selectionLayer, natTable.getConfigRegistry());
		assertNotNull(editor);
		assertTrue(editor instanceof TextCellEditor);
	}

	@Test
	public void testGetLastSelectedCellEditorWithMultiSelection() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);
		ICellEditor editor = EditUtils.getLastSelectedCellEditor(selectionLayer, natTable.getConfigRegistry());
		assertNotNull(editor);
		assertTrue(editor instanceof TextCellEditor);
	}

	@Test
	public void testAllCellsEditableWithoutSelection() {
		assertTrue(EditUtils.allCellsEditable(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testAllCellsEditableWithoutSelectionEnableEditing() {
		natTable.enableEditingOnAllCells();
		assertTrue(EditUtils.allCellsEditable(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testAllCellsEditableWithSingleSelection() {
		selectionLayer.selectCell(1, 1, false, false);
		assertFalse(EditUtils.allCellsEditable(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testAllCellsEditableWithSingleSelectionEnableEditing() {
		natTable.enableEditingOnAllCells();
		selectionLayer.selectCell(1, 1, false, false);
		assertTrue(EditUtils.allCellsEditable(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testAllCellsEditableWithMultiSelection() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);
		assertFalse(EditUtils.allCellsEditable(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testAllCellsEditableWithMultiSelectionEnableEditing() {
		natTable.enableEditingOnAllCells();
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);
		assertTrue(EditUtils.allCellsEditable(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsCellEditableWithoutSelection() {
		PositionCoordinate coord = new PositionCoordinate(selectionLayer, 0, 0);
		assertFalse(EditUtils.isCellEditable(selectionLayer, natTable.getConfigRegistry(), coord));
	}

	@Test
	public void testIsCellEditableWithoutSelectionEnableEditing() {
		natTable.enableEditingOnAllCells();
		PositionCoordinate coord = new PositionCoordinate(selectionLayer, 0, 0);
		assertTrue(EditUtils.isCellEditable(selectionLayer, natTable.getConfigRegistry(), coord));
	}

	@Test
	public void testIsCellEditableWithSingleSelection() {
		selectionLayer.selectCell(1, 1, false, false);
		PositionCoordinate coord = new PositionCoordinate(selectionLayer, 1, 1);
		assertFalse(EditUtils.isCellEditable(selectionLayer, natTable.getConfigRegistry(), coord));
	}

	@Test
	public void testIsCellEditableWithSingleSelectionEnableEditing() {
		natTable.enableEditingOnAllCells();
		selectionLayer.selectCell(1, 1, false, false);
		PositionCoordinate coord = new PositionCoordinate(selectionLayer, 1, 1);
		assertTrue(EditUtils.isCellEditable(selectionLayer, natTable.getConfigRegistry(), coord));
	}

	@Test
	public void testIsEditorSameWithoutSelection() {
		assertTrue(EditUtils.isEditorSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsEditorSameWithSingleSelection() {
		selectionLayer.selectCell(1, 1, false, false);
		assertTrue(EditUtils.isEditorSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsEditorSameWithMultiSelection() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);
		assertTrue(EditUtils.isEditorSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsEditorSameWithMultiSelectionOneChangedEditor() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);

		DataLayer bodyDataLayer = (DataLayer) gridLayerStack.getBodyDataLayer();
		natTable.registerLabelOnColumn(bodyDataLayer, 1, TEST_LABEL);
		natTable.getConfigRegistry().registerConfigAttribute(
				EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.EDIT, TEST_LABEL);
		
		assertFalse(EditUtils.isEditorSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsConverterSameWithoutSelection() {
		assertTrue(EditUtils.isConverterSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsConverterSameWithSingleSelection() {
		selectionLayer.selectCell(1, 1, false, false);
		assertTrue(EditUtils.isConverterSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsConverterSameWithMultiSelection() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);
		assertTrue(EditUtils.isConverterSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsConverterSameWithMultiSelectionOneChangedConverter() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);

		DataLayer bodyDataLayer = (DataLayer) gridLayerStack.getBodyDataLayer();
		natTable.registerLabelOnColumn(bodyDataLayer, 1, TEST_LABEL);
		natTable.getConfigRegistry().registerConfigAttribute(
				CellConfigAttributes.DISPLAY_CONVERTER, 
				new DefaultBooleanDisplayConverter(), 
				DisplayMode.EDIT, TEST_LABEL);
		
		assertFalse(EditUtils.isConverterSame(selectionLayer, natTable.getConfigRegistry()));
	}

	@Test
	public void testIsValueSameWithoutSelection() {
		assertTrue(EditUtils.isValueSame(selectionLayer));
	}

	@Test
	public void testIsValueSameWithSingleSelection() {
		selectionLayer.selectCell(1, 1, false, false);
		assertTrue(EditUtils.isValueSame(selectionLayer));
	}

	@Test
	public void testIsValueSameWithMultiSelection() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);
		assertFalse(EditUtils.isValueSame(selectionLayer));
	}

	@Test
	public void testIsValueSameWithMultiSelectionEnsureSameValues() {
		selectionLayer.selectCell(1, 1, false, true);
		selectionLayer.selectCell(2, 2, false, true);
		selectionLayer.selectCell(3, 3, false, true);

		selectionLayer.doCommand(new UpdateDataCommand(selectionLayer, 1, 1, "Test"));
		selectionLayer.doCommand(new UpdateDataCommand(selectionLayer, 2, 2, "Test"));
		selectionLayer.doCommand(new UpdateDataCommand(selectionLayer, 3, 3, "Test"));

		assertTrue(EditUtils.isValueSame(selectionLayer));
	}

}

Back to the top