Skip to content

Commit 39b113e

Browse files
committed
Impl #61 - Provide example on real row reorder
Signed-off-by: Dirk Fauth <[email protected]>
1 parent 3f05edd commit 39b113e

File tree

3 files changed

+741
-0
lines changed

3 files changed

+741
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Dirk Fauth and others.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Dirk Fauth <[email protected]> - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.nebula.widgets.nattable.ui.action;
14+
15+
import org.eclipse.nebula.widgets.nattable.NatTable;
16+
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
17+
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
18+
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
19+
import org.eclipse.nebula.widgets.nattable.painter.IOverlayPainter;
20+
import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
21+
import org.eclipse.swt.events.MouseEvent;
22+
import org.eclipse.swt.graphics.GC;
23+
import org.eclipse.swt.graphics.Image;
24+
import org.eclipse.swt.graphics.ImageData;
25+
import org.eclipse.swt.graphics.Rectangle;
26+
27+
/**
28+
* {@link IDragMode} to visualize a column as overlay.
29+
*
30+
* @since 2.3
31+
*/
32+
public class ColumnDragMode implements IDragMode {
33+
34+
private MouseEvent initialEvent;
35+
private MouseEvent currentEvent;
36+
37+
private int xOffset;
38+
private Image columnImage;
39+
protected ColumnImageOverlayPainter columnImageOverlayPainter = new ColumnImageOverlayPainter();
40+
41+
@Override
42+
public void mouseDown(NatTable natTable, MouseEvent event) {
43+
this.initialEvent = event;
44+
this.currentEvent = this.initialEvent;
45+
46+
setColumnImage(natTable);
47+
48+
natTable.forceFocus();
49+
50+
natTable.addOverlayPainter(this.columnImageOverlayPainter);
51+
}
52+
53+
@Override
54+
public void mouseMove(NatTable natTable, MouseEvent event) {
55+
this.currentEvent = event;
56+
57+
natTable.redraw(0, 0, natTable.getWidth(), natTable.getHeight(), false);
58+
}
59+
60+
@Override
61+
public void mouseUp(NatTable natTable, MouseEvent event) {
62+
natTable.removeOverlayPainter(this.columnImageOverlayPainter);
63+
64+
if (this.columnImage != null) {
65+
this.columnImage.dispose();
66+
}
67+
68+
natTable.redraw(0, 0, natTable.getWidth(), natTable.getHeight(), false);
69+
}
70+
71+
protected MouseEvent getInitialEvent() {
72+
return this.initialEvent;
73+
}
74+
75+
protected MouseEvent getCurrentEvent() {
76+
return this.currentEvent;
77+
}
78+
79+
private void setColumnImage(NatTable natTable) {
80+
int columnPosition = natTable.getColumnPositionByX(this.currentEvent.x);
81+
82+
IConfigRegistry configRegistry = natTable.getConfigRegistry();
83+
int y = 0;
84+
ILayerCell cell = null;
85+
86+
Image image = null;
87+
GC gc = null;
88+
89+
for (int rowPosition = 0; rowPosition < natTable.getRowCount(); rowPosition++) {
90+
cell = natTable.getCellByPosition(columnPosition, rowPosition);
91+
92+
if (cell != null) {
93+
Rectangle cellBounds = cell.getBounds();
94+
this.xOffset = this.currentEvent.x - cellBounds.x;
95+
96+
if (image == null && gc == null) {
97+
image = new Image(natTable.getDisplay(), cellBounds.width, natTable.getHeight());
98+
gc = new GC(image);
99+
}
100+
101+
ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
102+
if (cellPainter != null) {
103+
cellPainter.paintCell(cell, gc, new Rectangle(0, y, cellBounds.width, cellBounds.height), configRegistry);
104+
y += cellBounds.height;
105+
}
106+
}
107+
}
108+
109+
if (gc != null) {
110+
gc.dispose();
111+
}
112+
113+
if (image != null) {
114+
115+
ImageData imageData = image.getImageData();
116+
image.dispose();
117+
imageData.alpha = 150;
118+
119+
this.columnImage = new Image(natTable.getDisplay(), imageData);
120+
}
121+
}
122+
123+
private class ColumnImageOverlayPainter implements IOverlayPainter {
124+
125+
@Override
126+
public void paintOverlay(GC gc, ILayer layer) {
127+
if (ColumnDragMode.this.columnImage != null && !ColumnDragMode.this.columnImage.isDisposed()) {
128+
gc.drawImage(
129+
ColumnDragMode.this.columnImage,
130+
ColumnDragMode.this.currentEvent.x - ColumnDragMode.this.xOffset,
131+
0);
132+
}
133+
}
134+
}
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Dirk Fauth and others.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Dirk Fauth <[email protected]> - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.nebula.widgets.nattable.ui.action;
14+
15+
import org.eclipse.nebula.widgets.nattable.NatTable;
16+
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
17+
import org.eclipse.nebula.widgets.nattable.layer.ILayer;
18+
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
19+
import org.eclipse.nebula.widgets.nattable.painter.IOverlayPainter;
20+
import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
21+
import org.eclipse.swt.events.MouseEvent;
22+
import org.eclipse.swt.graphics.GC;
23+
import org.eclipse.swt.graphics.Image;
24+
import org.eclipse.swt.graphics.ImageData;
25+
import org.eclipse.swt.graphics.Rectangle;
26+
27+
/**
28+
* {@link IDragMode} to visualize a row as overlay.
29+
*
30+
* @since 2.3
31+
*/
32+
public class RowDragMode implements IDragMode {
33+
34+
private MouseEvent initialEvent;
35+
private MouseEvent currentEvent;
36+
37+
private int yOffset;
38+
private Image rowImage;
39+
protected RowImageOverlayPainter rowImageOverlayPainter = new RowImageOverlayPainter();
40+
41+
@Override
42+
public void mouseDown(NatTable natTable, MouseEvent event) {
43+
this.initialEvent = event;
44+
this.currentEvent = this.initialEvent;
45+
46+
setRowImage(natTable);
47+
48+
natTable.forceFocus();
49+
50+
natTable.addOverlayPainter(this.rowImageOverlayPainter);
51+
}
52+
53+
@Override
54+
public void mouseMove(NatTable natTable, MouseEvent event) {
55+
this.currentEvent = event;
56+
57+
natTable.redraw(0, 0, natTable.getWidth(), natTable.getHeight(), false);
58+
}
59+
60+
@Override
61+
public void mouseUp(NatTable natTable, MouseEvent event) {
62+
natTable.removeOverlayPainter(this.rowImageOverlayPainter);
63+
64+
if (this.rowImage != null) {
65+
this.rowImage.dispose();
66+
}
67+
68+
natTable.redraw(0, 0, natTable.getWidth(), natTable.getHeight(), false);
69+
}
70+
71+
protected MouseEvent getInitialEvent() {
72+
return this.initialEvent;
73+
}
74+
75+
protected MouseEvent getCurrentEvent() {
76+
return this.currentEvent;
77+
}
78+
79+
private void setRowImage(NatTable natTable) {
80+
int rowPosition = natTable.getRowPositionByY(this.currentEvent.y);
81+
82+
IConfigRegistry configRegistry = natTable.getConfigRegistry();
83+
int x = 0;
84+
ILayerCell cell = null;
85+
86+
Image image = null;
87+
GC gc = null;
88+
89+
for (int columnPosition = 0; columnPosition < natTable.getColumnCount(); columnPosition++) {
90+
cell = natTable.getCellByPosition(columnPosition, rowPosition);
91+
92+
if (cell != null) {
93+
Rectangle cellBounds = cell.getBounds();
94+
this.yOffset = this.currentEvent.y - cellBounds.y;
95+
96+
if (image == null && gc == null) {
97+
image = new Image(natTable.getDisplay(), natTable.getWidth(), cellBounds.height);
98+
gc = new GC(image);
99+
}
100+
101+
ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
102+
if (cellPainter != null) {
103+
cellPainter.paintCell(cell, gc, new Rectangle(x, 0, cellBounds.width, cellBounds.height), configRegistry);
104+
x += cellBounds.width;
105+
}
106+
}
107+
}
108+
109+
if (gc != null) {
110+
gc.dispose();
111+
}
112+
113+
if (image != null) {
114+
ImageData imageData = image.getImageData();
115+
image.dispose();
116+
imageData.alpha = 150;
117+
118+
this.rowImage = new Image(natTable.getDisplay(), imageData);
119+
}
120+
}
121+
122+
private class RowImageOverlayPainter implements IOverlayPainter {
123+
124+
@Override
125+
public void paintOverlay(GC gc, ILayer layer) {
126+
if (RowDragMode.this.rowImage != null && !RowDragMode.this.rowImage.isDisposed()) {
127+
gc.drawImage(
128+
RowDragMode.this.rowImage,
129+
0,
130+
RowDragMode.this.currentEvent.y - RowDragMode.this.yOffset);
131+
}
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)