Skip to content

Commit

Permalink
locks
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Jan 14, 2025
1 parent 6ac9d9e commit 7ba4c86
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
1 change: 1 addition & 0 deletions meja-fx/src/main/java/com/dua3/meja/ui/fx/FxRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public void dispose() {
}

private void render() {
LOG.trace("render()");
PlatformHelper.checkApplicationThread();
try (var __ = sheetViewDelegate.automaticReadLock()) {
switch (getItem().rowIndex()) {
Expand Down
2 changes: 2 additions & 0 deletions meja-fx/src/main/java/com/dua3/meja/ui/fx/FxSegmentView.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public void refresh() {
* filtered to include only the rows belonging to the specified quadrant.
*/
public FxSegmentView(FxSheetViewDelegate svDelegate, SheetView.Quadrant quadrant, ObservableSheet observableSheet) {
LOG.trace("FxSegmentView()");

this.quadrant = quadrant;
this.svDelegate = svDelegate;
this.segmentDelegate = new SegmentViewDelegate(this, svDelegate, quadrant);
Expand Down
12 changes: 11 additions & 1 deletion meja-fx/src/main/java/com/dua3/meja/ui/fx/FxSheetView.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class FxSheetView extends StackPane implements SheetView {
private final ScrollBar hScrollbar;
private final ScrollBar vScrollbar;

private boolean updating = false;

/**
* Constructs a new instance of FxSheetView using the specified Sheet object.
* Initializes the layout and scrollbars, and sets up the view quadrants based on the provided sheet.
Expand Down Expand Up @@ -347,7 +349,13 @@ public void updateContent() {
LOG.debug("updateContent()");
PlatformHelper.checkApplicationThread();

try (var __ = delegate.automaticReadLock()) {
if (updating) {
return;
}

try (var __ = delegate.automaticWriteLock()) {
updating = true;

updateDelegate(getSheet());

topLeftQuadrant.updateLayout();
Expand All @@ -359,6 +367,8 @@ public void updateContent() {
topRightQuadrant.refresh();
bottomLeftQuadrant.refresh();
bottomRightQuadrant.refresh();
} finally {
updating = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public void scrollIntoView(Cell cell) {
return;
}

LOG.trace("scrollIntoView()");
try (var __ = svDelegate.automaticReadLock()) {
Rectangle2f r = svDelegate.getCellRect(cell);
AffineTransformation2f t = ssvDelegate.getTransformation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ private SwingSegmentView getViewContainingCell(Cell cell) {

@Override
public void updateContent() {
LOG.debug("updating content");
LOG.trace("updateContent()");

Sheet sheet = getSheet();
try (var __ = delegate.automaticWriteLock()){
try (var __ = delegate.automaticWriteLock()) {
int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
delegate.setDisplayScale(getDisplayScale());
delegate.setScale(new Scale2f(sheet.getZoom() * dpi / 72.0f));
Expand Down
85 changes: 52 additions & 33 deletions meja-ui/src/main/java/com/dua3/meja/ui/SheetViewDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,40 +355,32 @@ public void onSubscribe(Flow.Subscription subscription) {

@Override
public void onNext(SheetEvent item) {
LOG.trace("received event: {}", item);
LOG.trace("onNext() - {}", item);

switch (item.type()) {
case SheetEvent.ZOOM_CHANGED, SheetEvent.LAYOUT_CHANGED, SheetEvent.ROWS_ADDED -> {
try (var __ = automaticReadLock()) {
owner.updateContent();
}
owner.updateContent();
}
case SheetEvent.SPLIT_CHANGED -> {
try (var __ = automaticReadLock()) {
owner.updateContent();
owner.scrollToCurrentCell();
}
owner.updateContent();
owner.scrollToCurrentCell();
}
case SheetEvent.ACTIVE_CELL_CHANGED -> {
try (var __ = automaticReadLock()) {
SheetEvent.ActiveCellChanged evt = (SheetEvent.ActiveCellChanged) item;

Cell oldCell = evt.oldValue();
Cell newCell = evt.newValue();

if (oldCell != null) {
owner.repaintCell(oldCell);
}
owner.scrollToCurrentCell();
if (newCell != null) {
owner.repaintCell(newCell);
}
SheetEvent.ActiveCellChanged evt = (SheetEvent.ActiveCellChanged) item;

Cell oldCell = evt.oldValue();
Cell newCell = evt.newValue();

if (oldCell != null) {
owner.repaintCell(oldCell);
}
owner.scrollToCurrentCell();
if (newCell != null) {
owner.repaintCell(newCell);
}
}
case SheetEvent.CELL_VALUE_CHANGED, SheetEvent.CELL_STYLE_CHANGED -> {
try (var __ = automaticReadLock()) {
owner.repaintCell(((SheetEvent.CellChanged<?>) item).cell());
}
owner.repaintCell(((SheetEvent.CellChanged<?>) item).cell());
}
default -> {
}
Expand Down Expand Up @@ -451,6 +443,7 @@ public void updateLayout() {
return;
}

LOG.trace("updateLayout()");
try (var __ = automaticWriteLock()) {
this.pixelWidthInPoints = 1.0f / scale.sx();
this.pixelHeightInPoints = 1.0f / scale.sy();
Expand Down Expand Up @@ -530,6 +523,7 @@ public void setEditing(boolean editing) {
}

public void setColumnNames(IntFunction<String> columnNames) {
LOG.trace("setColumnNames()");
try (var __ = automaticWriteLock()) {
this.columnNames = columnNames;
markLayoutChanged();
Expand All @@ -545,18 +539,21 @@ public int getSplitRow() {
}

public boolean setCurrentCell(int i, int j) {
LOG.trace("setCurrentCell()");
try (var __ = automaticWriteLock()) {
return sheet.setCurrentCell(i, j);
}
}

public boolean setCurrentCell(Cell cell) {
LOG.trace("setCurrentCell()");
try (var __ = automaticWriteLock()) {
return sheet.setCurrentCell(cell);
}
}

public void setRowNames(IntFunction<String> rowNames) {
LOG.trace("setRowNames()");
try (var __ = automaticWriteLock()) {
this.rowNames = rowNames;
markLayoutChanged();
Expand All @@ -568,6 +565,7 @@ public Scale2f getScale() {
}

public void setScale(Scale2f scale) {
LOG.trace("setScale()");
try (var __ = automaticReadLock()) {
if (!scale.equals(this.scale)) {
this.scale = scale;
Expand All @@ -577,6 +575,7 @@ public void setScale(Scale2f scale) {
}

public void setDisplayScale(Scale2f displayScale) {
LOG.trace("setDisplayScale()");
try (var __ = automaticReadLock()) {
if (!displayScale.equals(this.displayScale)) {
this.displayScale = displayScale;
Expand All @@ -598,8 +597,30 @@ public void setBackground(Color background) {
}

public void move(Direction d) {
LOG.trace("move()");
try (var __ = automaticWriteLock()) {
getCurrentLogicalCell().ifPresent(cell -> {
switch (d) {
case NORTH -> setCurrentRowNum(cell.getRowNumber() - 1);
case SOUTH -> setCurrentRowNum(cell.getRowNumber() + cell.getVerticalSpan());
case WEST -> setCurrentColNum(cell.getColumnNumber() - 1);
case EAST -> setCurrentColNum(cell.getColumnNumber() + cell.getHorizontalSpan());
}
});
}
}

/**
* Move the selection rectangle to an adjacent cell.
*
* @param d direction
*/
public void movePage(Direction d) {
LOG.trace("movePage()");
try (var __ = automaticWriteLock()) {
getCurrentLogicalCell().ifPresent(cell -> {
Rectangle2f cellRect = getCellRect(cell.getLogicalCell());
//cellRect.yMin() - this.getVisibleAreaInSheet();
switch (d) {
case NORTH -> setCurrentRowNum(cell.getRowNumber() - 1);
case SOUTH -> setCurrentRowNum(cell.getRowNumber() + cell.getVerticalSpan());
Expand All @@ -615,13 +636,15 @@ public Optional<Cell> getCurrentLogicalCell() {
}

public void setCurrentColNum(int colNum) {
LOG.trace("setCurrentColNum()");
try (var __ = automaticWriteLock()) {
int rowNum = sheet.getCurrentCell().map(Cell::getRowNumber).orElse(0);
setCurrentCell(sheet.getCell(rowNum, Math.max(0, colNum)));
}
}

public void setCurrentRowNum(int rowNum) {
LOG.trace("setCurrentRowNum()");
try (var __ = automaticWriteLock()) {
int colNum = sheet.getCurrentCell().map(Cell::getColumnNumber).orElse(0);
setCurrentCell(Math.max(0, rowNum), colNum);
Expand All @@ -632,31 +655,26 @@ public void setCurrentRowNum(int rowNum) {
* Move the selection rectangle to the bottom right cell.
*/
public void moveEnd() {
LOG.trace("moveEnd()");
try (var __ = automaticWriteLock()) {
int row = sheet.getLastRowNum();
int col = sheet.getLastColNum();
setCurrentCell(row, col);
}
}

/**
* Move the selection rectangle to the top left cell.
*/
public void moveHome() {
LOG.trace("moveHome()");
try (var __ = automaticWriteLock()) {
int row = sheet.getFirstRowNum();
int col = sheet.getFirstColNum();
setCurrentCell(row, col);
}
}

/**
* Move the selection rectangle to an adjacent cell.
*
* @param d direction
*/
public void movePage(Direction d) {
move(d); // TODO
}

public void onMousePressed(Cell cell) {
LOG.debug("onMousePressed({})", cell::getCellRef);

Expand Down Expand Up @@ -716,6 +734,7 @@ public Font getLabelFont() {
}

public void setLabelFont(Font labelFont) {
LOG.trace("setLabelFont()");
try (var __ = automaticWriteLock()) {
this.labelFont = labelFont;
markLayoutChanged();
Expand Down
Binary file modified testdata/test.xlsx
Binary file not shown.

0 comments on commit 7ba4c86

Please sign in to comment.