Skip to content

Commit

Permalink
[Modernize Code] Internal Clean-ups in Figure updating (#283)
Browse files Browse the repository at this point in the history
Upgraded code in Figure, DeferredUpdateMnager, LightweightSystem and
UpdateManager.
  • Loading branch information
azoitl authored Nov 20, 2023
1 parent 4594f54 commit 75456f6
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand All @@ -36,7 +35,7 @@ public class DeferredUpdateManager extends UpdateManager {
protected class UpdateRequest implements Runnable {

public UpdateRequest() {
super();
// this constructor is here for the visibility of the constructor of this class
}

/**
Expand All @@ -52,7 +51,7 @@ public void run() {
private Map<IFigure, Rectangle> dirtyRegions = new HashMap<>();

private GraphicsSource graphicsSource;
private List invalidFigures = new ArrayList();
private final List<IFigure> invalidFigures = new ArrayList<>();
private IFigure root;
private boolean updateQueued;

Expand All @@ -70,8 +69,9 @@ private static class RunnableChain {
}

void run() {
if (next != null)
if (next != null) {
next.run();
}
run.run();
}
}
Expand Down Expand Up @@ -104,15 +104,17 @@ public DeferredUpdateManager(GraphicsSource gs) {
*/
@Override
public synchronized void addDirtyRegion(IFigure figure, int x, int y, int w, int h) {
if (w == 0 || h == 0 || !figure.isShowing())
if (w == 0 || h == 0 || !figure.isShowing()) {
return;
}

Rectangle rect = dirtyRegions.get(figure);
if (rect == null) {
rect = new Rectangle(x, y, w, h);
dirtyRegions.put(figure, rect);
} else
} else {
rect.union(x, y, w, h);
}

queueWork();
}
Expand All @@ -125,8 +127,9 @@ public synchronized void addDirtyRegion(IFigure figure, int x, int y, int w, int
*/
@Override
public synchronized void addInvalidFigure(IFigure f) {
if (invalidFigures.contains(f))
if (invalidFigures.contains(f)) {
return;
}
queueWork();
invalidFigures.add(f);
}
Expand All @@ -138,8 +141,9 @@ public synchronized void addInvalidFigure(IFigure f) {
* @return the Graphics object
*/
protected Graphics getGraphics(Rectangle region) {
if (graphicsSource == null)
if (graphicsSource == null) {
return null;
}
return graphicsSource.getGraphics(region);
}

Expand All @@ -156,7 +160,7 @@ protected void paint(GC gc) {
* is being painted. Otherwise, notification already occurs in repairDamage().
*/
Rectangle rect = graphics.getClip(new Rectangle());
HashMap map = new HashMap();
HashMap<IFigure, Rectangle> map = new HashMap<>();
map.put(root, rect);
firePainting(rect, map);
}
Expand All @@ -182,8 +186,9 @@ protected void paint(GC gc) {
*/
@Override
public synchronized void performUpdate() {
if (isDisposed() || updating)
if (isDisposed() || updating) {
return;
}
updating = true;
try {
performValidation();
Expand All @@ -193,8 +198,9 @@ public synchronized void performUpdate() {
RunnableChain chain = afterUpdate;
afterUpdate = null;
chain.run(); // chain may queue additional Runnable.
if (afterUpdate != null)
if (afterUpdate != null) {
queueWork();
}
}
} finally {
updating = false;
Expand All @@ -206,14 +212,15 @@ public synchronized void performUpdate() {
*/
@Override
public synchronized void performValidation() {
if (invalidFigures.isEmpty() || validating)
if (invalidFigures.isEmpty() || validating) {
return;
}
try {
IFigure fig;
validating = true;
fireValidating();
for (int i = 0; i < invalidFigures.size(); i++) {
fig = (IFigure) invalidFigures.get(i);
fig = invalidFigures.get(i);
invalidFigures.set(i, null);
fig.validate();
}
Expand Down Expand Up @@ -284,10 +291,11 @@ protected void repairDamage() {
contribution.intersect(walker.getBounds());
walker = walker.getParent();
}
if (damage == null)
if (damage == null) {
damage = new Rectangle(contribution);
else
} else {
damage.union(contribution);
}
});

if (!dirtyRegions.isEmpty()) {
Expand All @@ -297,7 +305,6 @@ protected void repairDamage() {
}

if (damage != null && !damage.isEmpty()) {
// ystem.out.println(damage);
Graphics graphics = getGraphics(damage);
if (graphics != null) {
root.paint(graphics);
Expand All @@ -316,8 +323,9 @@ protected void repairDamage() {
@Override
public synchronized void runWithUpdate(Runnable runnable) {
afterUpdate = new RunnableChain(runnable, afterUpdate);
if (!updating)
if (!updating) {
queueWork();
}
}

/**
Expand Down
Loading

0 comments on commit 75456f6

Please sign in to comment.