Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a5548b1afd28cfa5f142cbd0441660efb97cb507 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*******************************************************************************
 * Copyright (c) 2012, 2013 Original authors 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:
 *     Original authors and others - initial API and implementation
 ******************************************************************************/

package org.eclipse.nebula.widgets.nattable.layer;

import java.util.Collection;
import java.util.Properties;

import org.eclipse.nebula.widgets.nattable.NatTable;
import org.eclipse.nebula.widgets.nattable.command.ILayerCommand;
import org.eclipse.nebula.widgets.nattable.command.StructuralRefreshCommandHandler;
import org.eclipse.nebula.widgets.nattable.command.VisualRefreshCommandHandler;
import org.eclipse.nebula.widgets.nattable.coordinate.Range;
import org.eclipse.nebula.widgets.nattable.data.IDataProvider;
import org.eclipse.nebula.widgets.nattable.edit.command.UpdateDataCommandHandler;
import org.eclipse.nebula.widgets.nattable.grid.command.ClientAreaResizeCommand;
import org.eclipse.nebula.widgets.nattable.layer.event.ResizeStructuralRefreshEvent;
import org.eclipse.nebula.widgets.nattable.layer.event.StructuralRefreshEvent;
import org.eclipse.nebula.widgets.nattable.persistence.IPersistable;
import org.eclipse.nebula.widgets.nattable.resize.command.ColumnResizeCommandHandler;
import org.eclipse.nebula.widgets.nattable.resize.command.MultiColumnResizeCommandHandler;
import org.eclipse.nebula.widgets.nattable.resize.command.MultiRowResizeCommandHandler;
import org.eclipse.nebula.widgets.nattable.resize.command.RowResizeCommandHandler;
import org.eclipse.nebula.widgets.nattable.resize.event.ColumnResizeEvent;
import org.eclipse.nebula.widgets.nattable.resize.event.RowResizeEvent;


/**
 * Wraps the {@link IDataProvider}, and serves as the data source for all
 * other layers. Also, tracks the size of the columns and the rows using
 * {@link SizeConfig} objects. Since this layer sits directly on top of the
 * data source, at this layer index == position.
 */
public class DataLayer extends AbstractLayer implements IUniqueIndexLayer {

	public static final String PERSISTENCE_KEY_ROW_HEIGHT = ".rowHeight"; //$NON-NLS-1$
	public static final String PERSISTENCE_KEY_COLUMN_WIDTH = ".columnWidth"; //$NON-NLS-1$

	public static final int DEFAULT_COLUMN_WIDTH = 100;
	public static final int DEFAULT_ROW_HEIGHT = 20;

	protected IDataProvider dataProvider;

	private final SizeConfig columnWidthConfig;
	private final SizeConfig rowHeightConfig;
	
	public DataLayer(IDataProvider dataProvider) {
		this(dataProvider, DEFAULT_COLUMN_WIDTH, DEFAULT_ROW_HEIGHT);
	}

	public DataLayer(IDataProvider dataProvider, int defaultColumnWidth, int defaultRowHeight) {
		this(defaultColumnWidth, defaultRowHeight);
		
		setDataProvider(dataProvider);
	}

	protected DataLayer() {
		this(DEFAULT_COLUMN_WIDTH, DEFAULT_ROW_HEIGHT);
	}

	protected DataLayer(int defaultColumnWidth, int defaultRowHeight) {
		columnWidthConfig = new SizeConfig(defaultColumnWidth);
		rowHeightConfig = new SizeConfig(defaultRowHeight);

		registerCommandHandlers();
	}

	// Persistence

	@Override
	public void saveState(String prefix, Properties properties) {
		super.saveState(prefix, properties);
		columnWidthConfig.saveState(prefix + PERSISTENCE_KEY_COLUMN_WIDTH, properties);
		rowHeightConfig.saveState(prefix + PERSISTENCE_KEY_ROW_HEIGHT, properties);
	}

	@Override
	public void loadState(String prefix, Properties properties) {
		super.loadState(prefix, properties);
		columnWidthConfig.loadState(prefix + PERSISTENCE_KEY_COLUMN_WIDTH, properties);
		rowHeightConfig.loadState(prefix + PERSISTENCE_KEY_ROW_HEIGHT, properties);
		
		if (!properties.containsKey(NatTable.INITIAL_PAINT_COMPLETE_FLAG))
			fireLayerEvent(new StructuralRefreshEvent(this));
	}

	// Configuration

	@Override
	protected void registerCommandHandlers() {
		registerCommandHandler(new ColumnResizeCommandHandler(this));
		registerCommandHandler(new MultiColumnResizeCommandHandler(this));
		registerCommandHandler(new RowResizeCommandHandler(this));
		registerCommandHandler(new MultiRowResizeCommandHandler(this));
		registerCommandHandler(new UpdateDataCommandHandler(this));
		registerCommandHandler(new StructuralRefreshCommandHandler());
		registerCommandHandler(new VisualRefreshCommandHandler());
	}

	public IDataProvider getDataProvider() {
		return dataProvider;
	}

	protected void setDataProvider(IDataProvider dataProvider) {
		if (this.dataProvider instanceof IPersistable) {
			unregisterPersistable((IPersistable) this.dataProvider);
		}
		
		this.dataProvider = dataProvider;
		
		if (dataProvider instanceof IPersistable) {
			registerPersistable((IPersistable) dataProvider);
		}
	}

	/**
	 * Gets the value at the given column and row index.
	 *
	 * @param columnIndex
	 * @param rowIndex
	 * @return the data value associated with the specified cell
	 */
	public Object getDataValue(int columnIndex, int rowIndex) {
		return this.dataProvider.getDataValue(columnIndex, rowIndex);
	}

	/**
	 * Sets the value at the given column and row index. Optional operation. Should throw UnsupportedOperationException
	 * if this operation is not supported.
	 *
	 * @param columnIndex
	 * @param rowIndex
	 * @param newValue
	 */
	public void setDataValue(int columnIndex, int rowIndex, Object newValue) {
		this.dataProvider.setDataValue(columnIndex, rowIndex, newValue);
	}
	
	// Horizontal features

	// Columns

	@Override
	public int getColumnCount() {
		return dataProvider.getColumnCount();
	}

	@Override
	public int getPreferredColumnCount() {
		return getColumnCount();
	}

    /**
	 * This is the root coordinate system, so the column index is always equal to the column position.
	 */
	@Override
	public int getColumnIndexByPosition(int columnPosition) {
		if (columnPosition >=0 && columnPosition < getColumnCount()) {
			return columnPosition;
		} else {
			return -1;
		}
	}

	/**
	 * This is the root coordinate system, so the column position is always equal to the column index.
	 */
	@Override
	public int getColumnPositionByIndex(int columnIndex) {
		if (columnIndex >=0 && columnIndex < getColumnCount()) {
			return columnIndex;
		} else {
			return -1;
		}
	}

	@Override
	public int localToUnderlyingColumnPosition(int localColumnPosition) {
		return localColumnPosition;
	}

	@Override
	public int underlyingToLocalColumnPosition(ILayer sourceUnderlyingLayer, int underlyingColumnPosition) {
		return underlyingColumnPosition;
	}
	
	@Override
	public Collection<Range> underlyingToLocalColumnPositions(ILayer sourceUnderlyingLayer, Collection<Range> underlyingColumnPositionRanges) {
		return underlyingColumnPositionRanges;
	}

	// Width

    @Override
	public int getWidth() {
		return columnWidthConfig.getAggregateSize(getColumnCount());
	}

	@Override
	public int getPreferredWidth() {
		return getWidth();
	}

	@Override
	public int getColumnWidthByPosition(int columnPosition) {
		return columnWidthConfig.getSize(columnPosition);
	}

	public void setColumnWidthByPosition(int columnPosition, int width) {
		setColumnWidthByPosition(columnPosition, width, true);
	}

	public void setColumnWidthByPosition(int columnPosition, int width, boolean fireEvent) {
		columnWidthConfig.setSize(columnPosition, width);
		if (fireEvent) fireLayerEvent(new ColumnResizeEvent(this, columnPosition));
	}

	public void setColumnWidthPercentageByPosition(int columnPosition, int width) {
		columnWidthConfig.setPercentage(columnPosition, width);
		fireLayerEvent(new ColumnResizeEvent(this, columnPosition));
	}

	public void setDefaultColumnWidth(int width) {
		columnWidthConfig.setDefaultSize(width);
	}

	public void setDefaultColumnWidthByPosition(int columnPosition, int width) {
		columnWidthConfig.setDefaultSize(columnPosition, width);
	}

	// Column resize

	@Override
	public boolean isColumnPositionResizable(int columnPosition) {
		return columnWidthConfig.isPositionResizable(columnPosition);
	}

	public void setColumnPositionResizable(int columnPosition, boolean resizable) {
		columnWidthConfig.setPositionResizable(columnPosition, resizable);
	}

	public void setColumnsResizableByDefault(boolean resizableByDefault) {
		columnWidthConfig.setResizableByDefault(resizableByDefault);
	}

	// Underlying

	@Override
	public Collection<ILayer> getUnderlyingLayersByColumnPosition(int columnPosition) {
		return null;
	}

	// Vertical features

	// Rows

	@Override
	public int getRowCount() {
		return dataProvider.getRowCount();
	}

	@Override
	public int getPreferredRowCount() {
		return getRowCount();
	}

	/**
	 * This is the root coordinate system, so the row index is always equal to the row position.
	 */
	@Override
	public int getRowIndexByPosition(int rowPosition) {
		if (rowPosition >=0 && rowPosition < getRowCount()) {
			return rowPosition;
		} else {
			return -1;
		}
	}

	/**
	 * This is the root coordinate system, so the row position is always equal to the row index.
	 */
	@Override
	public int getRowPositionByIndex(int rowIndex) {
		if (rowIndex >= 0 && rowIndex < getRowCount()) {
			return rowIndex;
		} else {
			return -1;
		}
	}

	@Override
	public int localToUnderlyingRowPosition(int localRowPosition) {
		return localRowPosition;
	}

	@Override
	public int underlyingToLocalRowPosition(ILayer sourceUnderlyingLayer, int underlyingRowPosition) {
		return underlyingRowPosition;
	}
	
	@Override
	public Collection<Range> underlyingToLocalRowPositions(ILayer sourceUnderlyingLayer, Collection<Range> underlyingRowPositionRanges) {
		return underlyingRowPositionRanges;
	}

	// Height

    @Override
	public int getHeight() {
		return rowHeightConfig.getAggregateSize(getRowCount());
	}

	@Override
	public int getPreferredHeight() {
		return getHeight();
	}

	@Override
	public int getRowHeightByPosition(int rowPosition) {
		return rowHeightConfig.getSize(rowPosition);
	}

	public void setRowHeightByPosition(int rowPosition, int height) {
		setRowHeightByPosition(rowPosition, height, true);
	}

	public void setRowHeightByPosition(int rowPosition, int height, boolean fireEvent) {
		rowHeightConfig.setSize(rowPosition, height);
		if (fireEvent) fireLayerEvent(new RowResizeEvent(this, rowPosition));
	}

	public void setRowHeightPercentageByPosition(int rowPosition, int height) {
		rowHeightConfig.setPercentage(rowPosition, height);
		fireLayerEvent(new ColumnResizeEvent(this, rowPosition));
	}

	public void setDefaultRowHeight(int height) {
		rowHeightConfig.setDefaultSize(height);
	}

	public void setDefaultRowHeightByPosition(int rowPosition, int height) {
		rowHeightConfig.setDefaultSize(rowPosition, height);
	}

	// Row resize

	@Override
	public boolean isRowPositionResizable(int rowPosition) {
		return rowHeightConfig.isPositionResizable(rowPosition);
	}

	public void setRowPositionResizable(int rowPosition, boolean resizable) {
		rowHeightConfig.setPositionResizable(rowPosition, resizable);
	}

	public void setRowsResizableByDefault(boolean resizableByDefault) {
		rowHeightConfig.setResizableByDefault(resizableByDefault);
	}

	// Underlying

	@Override
	public Collection<ILayer> getUnderlyingLayersByRowPosition(int rowPosition) {
		return null;
	}

	// Cell features

	@Override
	public Object getDataValueByPosition(int columnPosition, int rowPosition) {
		int columnIndex = getColumnIndexByPosition(columnPosition);
		int rowIndex = getRowIndexByPosition(rowPosition);
		return getDataValue(columnIndex, rowIndex);
	}

	@Override
	public int getColumnPositionByX(int x) {
		return LayerUtil.getColumnPositionByX(this, x);
	}

	@Override
	public int getRowPositionByY(int y) {
		return LayerUtil.getRowPositionByY(this, y);
	}

	@Override
	public int getStartXOfColumnPosition(int columnPosition) {
		return columnWidthConfig.getAggregateSize(columnPosition);
	}

	@Override
	public int getStartYOfRowPosition(int rowPosition) {
		return rowHeightConfig.getAggregateSize(rowPosition);
	}

	@Override
	public ILayer getUnderlyingLayerByPosition(int columnPosition, int rowPosition) {
		return null;
	}
	
	@Override
	public boolean doCommand(ILayerCommand command) {
		if (command instanceof ClientAreaResizeCommand && command.convertToTargetLayer(this)) {
			ClientAreaResizeCommand clientAreaResizeCommand = (ClientAreaResizeCommand) command;
			
			boolean refresh = false;
			if (isColumnPercentageSizing()) {
				this.columnWidthConfig.calculatePercentages(clientAreaResizeCommand.getCalcArea().width, getColumnCount());
				refresh = true;
			}
			if (isRowPercentageSizing()) {
				this.rowHeightConfig.calculatePercentages(clientAreaResizeCommand.getCalcArea().height, getRowCount());
				refresh = true;
			}
			
			if (refresh) {
				fireLayerEvent(new ResizeStructuralRefreshEvent(this));
			}

			return refresh;
		}
		return super.doCommand(command);
	}
	
	/**
	 * @return <code>true</code> if the column sizing is done by percentage calculation,
	 * 			<code>false</code> if the column sizing is done by pixel (default)
	 */
	public boolean isColumnPercentageSizing() {
		return this.columnWidthConfig.isPercentageSizing();
	}
	
	/**
	 * Configures how the column sizing of this {@link DataLayer} is handled.
	 * Default is pixel sizing.
	 * If percentage sizing should be used you have to ensure that the size value for every 
	 * column is set explicitly and that the sum of the column sizes doesn't exceed 100.
	 * @param percentageSizing <code>true</code> if the column sizing should be done by percentage 
	 * 			calculation, <code>false</code> if the column sizing should be done by pixel (default)
	 */
	public void setColumnPercentageSizing(boolean percentageSizing) {
		this.columnWidthConfig.setPercentageSizing(percentageSizing);
	}
	
	/**
	 * @param position The position which is asked for the percentage sizing configuration.
	 * @return <code>true</code> if the column sizing for the given position is done by percentage 
	 * 			calculation, <code>false</code> if the column sizing is done by pixel (default)
	 */
	public boolean isColumnPercentageSizing(int position) {
		return this.columnWidthConfig.isPercentageSizing(position);
	}
	
	/**
	 * Configures how the column sizing of this {@link DataLayer} is handled.
	 * Default is pixel sizing.
	 * If percentage sizing should be used you have to ensure that the size value for every 
	 * column is set explicitly and that the sum of the column sizes doesn't exceed 100.
	 * @param position The position for which the sizing configuration should be set.
	 * @param percentageSizing <code>true</code> if the column sizing should be done by percentage 
	 * 			calculation, <code>false</code> if the column sizing should be done by pixel (default)
	 */
	public void setColumnPercentageSizing(int position, boolean percentageSizing) {
		this.columnWidthConfig.setPercentageSizing(position, percentageSizing);
	}
	
	/**
	 * @return <code>true</code> if the row sizing is done by percentage calculation, 
	 * 			<code>false</code> if the row sizing is done by pixel (default)
	 */
	public boolean isRowPercentageSizing() {
		return this.rowHeightConfig.isPercentageSizing();
	}
	
	/**
	 * Configures how the row sizing of this {@link DataLayer} is handled.
	 * Default is pixel sizing.
	 * If percentage sizing should be used you have to ensure that the size value for every 
	 * row is set explicitly and that the sum of the row sizes doesn't exceed 100.
	 * @param percentageSizing <code>true</code> if the row sizing should be done by percentage 
	 * 			calculation, <code>false</code> if the row sizing should be done by pixel (default)
	 */
	public void setRowPercentageSizing(boolean percentageSizing) {
		this.rowHeightConfig.setPercentageSizing(percentageSizing);
	}
	
	/**
	 * @param position The position which is asked for the percentage sizing configuration.
	 * @return <code>true</code> if the row sizing for the given position is done by percentage 
	 * 			calculation, <code>false</code> if the row sizing is done by pixel (default)
	 */
	public boolean isRowPercentageSizing(int position) {
		return this.rowHeightConfig.isPercentageSizing(position);
	}
	
	/**
	 * Configures how the row sizing of this {@link DataLayer} is handled.
	 * Default is pixel sizing.
	 * If percentage sizing should be used you have to ensure that the size value for every 
	 * row is set explicitly and that the sum of the row sizes doesn't exceed 100.
	 * @param position The row position for which the sizing configuration should be set.
	 * @param percentageSizing <code>true</code> if the row sizing should be done by percentage 
	 * 			calculation, <code>false</code> if the row sizing should be done by pixel (default)
	 */
	public void setRowPercentageSizing(int position, boolean percentageSizing) {
		this.rowHeightConfig.setPercentageSizing(position, percentageSizing);
	}
}

Back to the top