Strange behavior with RowResizeCommand [message #1032241] |
Tue, 02 April 2013 19:08 |
Joachim Fuchs Messages: 34 Registered: February 2013 |
Member |
|
|
Hi,
I just tried out the row resize feature and it went pretty well. The code draws a nattable and two buttons. The lower button is responsible for resizing of the selected row. This is done in a thread because I wanted to check out some 'animation'. Using 2 as a step width (see the variable) works fine. The row grows or shrinks and cell selection still works. Using 3 for the step width breaks it: the button is now useless nad it is not possible to select a cell, the selected row grows instead.
Where is the problem? Is it my code or my approach wit hthe animation? Is it a bug somewhere? I'm running Ubuntu here and the latest nattable build from the p2 repo.
Thanks for your help.
J
...
public class NatTable01 {
private NatTable natTable;
private DataLayer bodyDataLayer;
private Button btnRowHeight;
private ILayerCell selectedCell;
private int step = 1;
// FINDME setting rowHeightNormal to 0 results in grid only painted table experience
private int rowHeightNormal = 20;
private int rowHeightBig = 77;
private int timerdelay = 5;
public static void main(String[] args) {
Display d = Display.getDefault();
Shell s = new Shell(d, SWT.CLOSE | SWT.RESIZE | SWT.DOUBLE_BUFFERED);
NatTable01 nt = new NatTable01();
nt.createControl(s);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch()) {
d.sleep();
}
}
d.dispose();
}
public NatTable01() {
}
public void createControl(Composite parent) {
parent.setLayout(new GridLayout(1, false));
final DummyBodyDataProvider bodyDataProvider = new DummyBodyDataProvider(5, 5);
bodyDataLayer = new DataLayer(bodyDataProvider);
final DefaultBodyLayerStack bodyLayer = new DefaultBodyLayerStack(bodyDataLayer);
final SelectionLayer selectionLayer = bodyLayer.getSelectionLayer();
selectionLayer.addLayerListener(new ILayerListener() {
@Override
public void handleLayerEvent(ILayerEvent event) {
System.out.println(event);
if (event instanceof CellSelectionEvent) {
CellSelectionEvent cse = (CellSelectionEvent) event;
ILayerCell selectedCell = natTable.getCellByPosition(cse.getColumnPosition(), cse.getRowPosition());
if (selectedCell != null) {
setSelectedCell(selectedCell);
}
} else if (event instanceof ColumnSelectionEvent) {
setSelectedCell(null);
} else if (event instanceof RowSelectionEvent) {
setSelectedCell(null);
}
}
});
final FreezeLayer freezeLayer = new FreezeLayer(selectionLayer);
final CompositeFreezeLayer compositeFreezeLayer = new CompositeFreezeLayer(freezeLayer,
bodyLayer.getViewportLayer(), selectionLayer);
// Column header
final IDataProvider columnHeaderDataProvider = new DummyColumnHeaderDataProvider(bodyDataProvider);
final ILayer columnHeaderLayer = new ColumnHeaderLayer(new DefaultColumnHeaderDataLayer(
columnHeaderDataProvider), compositeFreezeLayer, selectionLayer);
// Row header
final IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
final ILayer rowHeaderLayer = new RowHeaderLayer(new DefaultRowHeaderDataLayer(rowHeaderDataProvider),
compositeFreezeLayer, selectionLayer);
// Corner
final DefaultCornerDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider,
rowHeaderDataProvider);
final CornerLayer cornerLayer = new CornerLayer(new DataLayer(cornerDataProvider), rowHeaderLayer,
columnHeaderLayer);
// Grid
final GridLayer gridLayer = new GridLayer(compositeFreezeLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer);
natTable = new NatTable(parent, gridLayer, false);
ColumnLabelAccumulator columnLabelAccumulator = new ColumnLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition) {
super.accumulateConfigLabels(configLabels, columnPosition, rowPosition);
configLabels.addLabelOnTop("LABEL");
}
};
bodyDataLayer.setConfigLabelAccumulator(columnLabelAccumulator);
// Configuration
natTable.addConfiguration(new DefaultNatTableStyleConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
super.configureRegistry(configRegistry);
configRegistry.registerConfigAttribute(CellStyleAttributes.VERTICAL_ALIGNMENT,
VerticalAlignmentEnum.TOP, DisplayMode.NORMAL, "LABEL");
}
});
natTable.addConfiguration(new HeaderMenuConfiguration(natTable));
natTable.addConfiguration(new DefaultFreezeGridBindings());
// ADD BODY CONTEXT MENU CONFIG
natTable.addConfiguration(new BodyMenuConfiguration(natTable, selectionLayer));
natTable.configure();
natTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnResize = new Button(parent, SWT.PUSH);
btnResize.setText("Resize");
btnResize.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i < bodyDataLayer.getRowCount(); i++) {
bodyDataLayer.doCommand(new RowResizeCommand(bodyDataLayer, i, (int) (bodyDataLayer
.getRowHeightByPosition(i) * 1.1)));
}
}
});
btnRowHeight = new Button(parent, SWT.PUSH);
btnRowHeight.setText("Change Row Height");
btnRowHeight.setEnabled(false);
btnRowHeight.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int currentRowHeight = bodyDataLayer.getRowHeightByPosition(selectedCell.getRowPosition());
BiggerSmallerRunner bsr = new BiggerSmallerRunner();
bsr.cell = selectedCell;
if (currentRowHeight == rowHeightBig) {
bsr.targetSize = rowHeightNormal;
} else {
bsr.targetSize = rowHeightBig;
}
timer.scheduleAtFixedRate(bsr, 0, timerdelay);
selectionLayer.setSelectedCell(selectedCell.getColumnPosition(), selectedCell.getRowPosition());
}
});
}
Timer timer = new Timer();
class BiggerSmallerRunner extends TimerTask {
ILayerCell cell;
int targetSize;
@Override
public void run() {
natTable.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
int currentRowHeight = bodyDataLayer.getRowHeightByPosition(cell.getRowPosition());
if (targetSize + step < currentRowHeight) {
bodyDataLayer.doCommand(new RowResizeCommand(bodyDataLayer, cell.getRowPosition(),
currentRowHeight - step));
} else if (targetSize - step > currentRowHeight) {
bodyDataLayer.doCommand(new RowResizeCommand(bodyDataLayer, cell.getRowPosition(),
currentRowHeight + step));
} else if (currentRowHeight + step >= targetSize) {
bodyDataLayer.doCommand(new RowResizeCommand(bodyDataLayer, cell.getRowPosition(), targetSize));
cancel();
}
}
});
}
}
protected void setSelectedCell(ILayerCell selectedCell) {
this.selectedCell = selectedCell;
btnRowHeight.setEnabled(this.selectedCell != null);
}
}
[Updated on: Thu, 04 April 2013 08:39] Report message to a moderator
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03048 seconds