diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/DeferredUpdateManager.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/DeferredUpdateManager.java index d21268e10..3575ef0ae 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/DeferredUpdateManager.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/DeferredUpdateManager.java @@ -14,7 +14,6 @@ import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -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 } /** @@ -52,7 +51,7 @@ public void run() { private Map dirtyRegions = new HashMap<>(); private GraphicsSource graphicsSource; - private List invalidFigures = new ArrayList(); + private final List invalidFigures = new ArrayList<>(); private IFigure root; private boolean updateQueued; @@ -70,8 +69,9 @@ private static class RunnableChain { } void run() { - if (next != null) + if (next != null) { next.run(); + } run.run(); } } @@ -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(); } @@ -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); } @@ -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); } @@ -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 map = new HashMap<>(); map.put(root, rect); firePainting(rect, map); } @@ -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(); @@ -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; @@ -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(); } @@ -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()) { @@ -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); @@ -316,8 +323,9 @@ protected void repairDamage() { @Override public synchronized void runWithUpdate(Runnable runnable) { afterUpdate = new RunnableChain(runnable, afterUpdate); - if (!updating) + if (!updating) { queueWork(); + } } /** diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java index d23060f63..9537e681a 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/Figure.java @@ -79,7 +79,7 @@ public class Figure implements IFigure { private Cursor cursor; private PropertyChangeSupport propertyListeners; - private EventListenerList eventListeners = new EventListenerList(); + private final EventListenerList eventListeners = new EventListenerList(); private List children = Collections.emptyList(); @@ -101,26 +101,31 @@ public class Figure implements IFigure { /** * @deprecated access using {@link #getLocalFont()} */ + @Deprecated protected Font font; /** * @deprecated access using {@link #getLocalBackgroundColor()}. */ + @Deprecated protected Color bgColor; /** * @deprecated access using {@link #getLocalForegroundColor()}. */ + @Deprecated protected Color fgColor; /** * @deprecated access using {@link #getBorder()} */ + @Deprecated protected Border border; /** * @deprecated access using {@link #getToolTip()} */ + @Deprecated protected IFigure toolTip; private AncestorHelper ancestorHelper; @@ -140,33 +145,41 @@ public final void add(IFigure figure, Object constraint) { */ @Override public void add(IFigure figure, Object constraint, int index) { - if (children.equals(Collections.emptyList())) + if (children.equals(Collections.emptyList())) { children = new ArrayList<>(2); - if (index < -1 || index > children.size()) + } + if (index < -1 || index > children.size()) { throw new IndexOutOfBoundsException("Index does not exist"); //$NON-NLS-1$ + } // Check for Cycle in hierarchy - for (IFigure f = this; f != null; f = f.getParent()) - if (figure == f) + for (IFigure f = this; f != null; f = f.getParent()) { + if (figure == f) { throw new IllegalArgumentException("Figure being added introduces cycle"); //$NON-NLS-1$ + } + } // Detach the child from previous parent - if (figure.getParent() != null) + if (figure.getParent() != null) { figure.getParent().remove(figure); + } - if (index == -1) + if (index == -1) { children.add(figure); - else + } else { children.add(index, figure); + } figure.setParent(this); - if (layoutManager != null) + if (layoutManager != null) { layoutManager.setConstraint(figure, constraint); + } revalidate(); - if (getFlag(FLAG_REALIZED)) + if (getFlag(FLAG_REALIZED)) { figure.addNotify(); + } figure.repaint(); } @@ -197,8 +210,9 @@ public final void add(IFigure figure, int index) { */ @Override public void addAncestorListener(AncestorListener ancestorListener) { - if (ancestorHelper == null) + if (ancestorHelper == null) { ancestorHelper = new AncestorHelper(this); + } ancestorHelper.addAncestorListener(ancestorListener); } @@ -244,8 +258,9 @@ public void addKeyListener(KeyListener listener) { public void addLayoutListener(LayoutListener listener) { if (layoutManager instanceof LayoutNotifier notifier) { notifier.listeners.add(listener); - } else + } else { layoutManager = new LayoutNotifier(layoutManager, listener); + } } /** @@ -283,8 +298,9 @@ public void addMouseMotionListener(MouseMotionListener listener) { */ @Override public void addNotify() { - if (getFlag(FLAG_REALIZED)) + if (getFlag(FLAG_REALIZED)) { throw new RuntimeException("addNotify() should not be called multiple times"); //$NON-NLS-1$ + } setFlag(FLAG_REALIZED, true); children.forEach(IFigure::addNotify); } @@ -294,8 +310,9 @@ public void addNotify() { */ @Override public void addPropertyChangeListener(String property, PropertyChangeListener listener) { - if (propertyListeners == null) + if (propertyListeners == null) { propertyListeners = new PropertyChangeSupport(this); + } propertyListeners.addPropertyChangeListener(property, listener); } @@ -304,8 +321,9 @@ public void addPropertyChangeListener(String property, PropertyChangeListener li */ @Override public void addPropertyChangeListener(PropertyChangeListener listener) { - if (propertyListeners == null) + if (propertyListeners == null) { propertyListeners = new PropertyChangeSupport(this); + } propertyListeners.addPropertyChangeListener(listener); } @@ -333,8 +351,9 @@ public boolean containsPoint(int x, int y) { */ @Override public void erase() { - if (getParent() == null || !isVisible()) + if (getParent() == null || !isVisible()) { return; + } Rectangle r = new Rectangle(getBounds()); getParent().translateToParent(r); @@ -354,8 +373,9 @@ public void erase() { protected IFigure findDescendantAtExcluding(int x, int y, TreeSearch search) { PRIVATE_POINT.setLocation(x, y); translateFromParent(PRIVATE_POINT); - if (!getClientArea(Rectangle.SINGLETON).contains(PRIVATE_POINT)) + if (!getClientArea(Rectangle.SINGLETON).contains(PRIVATE_POINT)) { return null; + } x = PRIVATE_POINT.x; y = PRIVATE_POINT.y; @@ -363,8 +383,9 @@ protected IFigure findDescendantAtExcluding(int x, int y, TreeSearch search) { for (IFigure fig : getChildrenRevIterable()) { if (fig.isVisible()) { fig = fig.findFigureAt(x, y, search); - if (fig != null) + if (fig != null) { return fig; + } } } // No descendants were found @@ -392,15 +413,19 @@ public final IFigure findFigureAt(int x, int y) { */ @Override public IFigure findFigureAt(int x, int y, TreeSearch search) { - if (!containsPoint(x, y)) + if (!containsPoint(x, y)) { return null; - if (search.prune(this)) + } + if (search.prune(this)) { return null; + } IFigure child = findDescendantAtExcluding(x, y, search); - if (child != null) + if (child != null) { return child; - if (search.accept(this)) + } + if (search.accept(this)) { return this; + } return null; } @@ -427,13 +452,16 @@ public final IFigure findFigureAtExcluding(int x, int y, Collection c) */ @Override public IFigure findMouseEventTargetAt(int x, int y) { - if (!containsPoint(x, y)) + if (!containsPoint(x, y)) { return null; + } IFigure f = findMouseEventTargetInDescendantsAt(x, y); - if (f != null) + if (f != null) { return f; - if (isMouseEventTarget()) + } + if (isMouseEventTarget()) { return this; + } return null; } @@ -451,8 +479,9 @@ protected IFigure findMouseEventTargetInDescendantsAt(int x, int y) { PRIVATE_POINT.setLocation(x, y); translateFromParent(PRIVATE_POINT); - if (!getClientArea(Rectangle.SINGLETON).contains(PRIVATE_POINT)) + if (!getClientArea(Rectangle.SINGLETON).contains(PRIVATE_POINT)) { return null; + } for (IFigure fig : getChildrenRevIterable()) { if (fig.isVisible() && fig.isEnabled() && fig.containsPoint(PRIVATE_POINT.x, PRIVATE_POINT.y)) { @@ -473,8 +502,9 @@ protected IFigure findMouseEventTargetInDescendantsAt(int x, int y) { * @since 3.1 */ protected void fireCoordinateSystemChanged() { - if (!eventListeners.containsListener(CoordinateListener.class)) + if (!eventListeners.containsListener(CoordinateListener.class)) { return; + } eventListeners.getListenersIterable(CoordinateListener.class).forEach(lst -> lst.coordinateSystemChanged(this)); } @@ -485,8 +515,9 @@ protected void fireCoordinateSystemChanged() { * @since 3.1 */ protected void fireFigureMoved() { - if (!eventListeners.containsListener(FigureListener.class)) + if (!eventListeners.containsListener(FigureListener.class)) { return; + } eventListeners.getListenersIterable(FigureListener.class).forEach(lst -> lst.figureMoved(this)); } @@ -501,6 +532,7 @@ protected void fireFigureMoved() { * @deprecated call fireFigureMoved() or fireCoordinateSystemChanged() as * appropriate */ + @Deprecated protected void fireMoved() { fireFigureMoved(); fireCoordinateSystemChanged(); @@ -516,8 +548,9 @@ protected void fireMoved() { * @since 2.0 */ protected void firePropertyChange(String property, boolean old, boolean current) { - if (propertyListeners == null) + if (propertyListeners == null) { return; + } propertyListeners.firePropertyChange(property, old, current); } @@ -531,8 +564,9 @@ protected void firePropertyChange(String property, boolean old, boolean current) * @since 2.0 */ protected void firePropertyChange(String property, Object old, Object current) { - if (propertyListeners == null) + if (propertyListeners == null) { return; + } propertyListeners.firePropertyChange(property, old, current); } @@ -547,8 +581,9 @@ protected void firePropertyChange(String property, Object old, Object current) { * @since 2.0 */ protected void firePropertyChange(String property, int old, int current) { - if (propertyListeners == null) + if (propertyListeners == null) { return; + } propertyListeners.firePropertyChange(property, old, current); } @@ -561,8 +596,9 @@ protected void firePropertyChange(String property, int old, int current) { */ @Override public Color getBackgroundColor() { - if (bgColor == null && getParent() != null) + if (bgColor == null && getParent() != null) { return getParent().getBackgroundColor(); + } return bgColor; } @@ -611,8 +647,9 @@ public Iterable getChildrenRevIterable() { public Rectangle getClientArea(Rectangle rect) { rect.setBounds(getBounds()); rect.crop(getInsets()); - if (useLocalCoordinates()) + if (useLocalCoordinates()) { rect.setLocation(0, 0); + } return rect; } @@ -640,8 +677,9 @@ public IClippingStrategy getClippingStrategy() { */ @Override public Cursor getCursor() { - if (cursor == null && getParent() != null) + if (cursor == null && getParent() != null) { return getParent().getCursor(); + } return cursor; } @@ -660,10 +698,12 @@ protected boolean getFlag(int flag) { */ @Override public Font getFont() { - if (font != null) + if (font != null) { return font; - if (getParent() != null) + } + if (getParent() != null) { return getParent().getFont(); + } return null; } @@ -672,8 +712,9 @@ public Font getFont() { */ @Override public Color getForegroundColor() { - if (fgColor == null && getParent() != null) + if (fgColor == null && getParent() != null) { return getParent().getForegroundColor(); + } return fgColor; } @@ -686,8 +727,9 @@ public Color getForegroundColor() { */ @Override public Insets getInsets() { - if (getBorder() != null) + if (getBorder() != null) { return getBorder().getInsets(this); + } return NO_INSETS; } @@ -696,8 +738,9 @@ public Insets getInsets() { */ @Override public LayoutManager getLayoutManager() { - if (layoutManager instanceof LayoutNotifier) + if (layoutManager instanceof LayoutNotifier) { return ((LayoutNotifier) layoutManager).realLayout; + } return layoutManager; } @@ -711,8 +754,9 @@ public LayoutManager getLayoutManager() { * @since 2.0 */ protected Iterator getListeners(Class clazz) { - if (eventListeners == null) + if (eventListeners == null) { return Collections.emptyIterator(); + } return eventListeners.getListeners(clazz); } @@ -726,8 +770,9 @@ protected Iterator getListeners(Class clazz) { * @since 3.13 */ protected Iterable getListenersIterable(final Class listenerType) { - if (eventListeners == null) + if (eventListeners == null) { return Collections.emptyList(); + } return eventListeners.getListenersIterable(listenerType); } @@ -780,8 +825,9 @@ public final Point getLocation() { */ @Override public Dimension getMaximumSize() { - if (maxSize != null) + if (maxSize != null) { return maxSize; + } return MAX_DIMENSION; } @@ -798,12 +844,14 @@ public final Dimension getMinimumSize() { */ @Override public Dimension getMinimumSize(int wHint, int hHint) { - if (minSize != null) + if (minSize != null) { return minSize; + } if (getLayoutManager() != null) { Dimension d = getLayoutManager().getMinimumSize(this, wHint, hHint); - if (d != null) + if (d != null) { return d; + } } return getPreferredSize(wHint, hHint); } @@ -829,12 +877,14 @@ public final Dimension getPreferredSize() { */ @Override public Dimension getPreferredSize(int wHint, int hHint) { - if (prefSize != null) + if (prefSize != null) { return prefSize; + } if (getLayoutManager() != null) { Dimension d = getLayoutManager().getPreferredSize(this, wHint, hHint); - if (d != null) + if (d != null) { return d; + } } return getSize(); } @@ -860,8 +910,9 @@ public IFigure getToolTip() { */ @Override public UpdateManager getUpdateManager() { - if (getParent() != null) + if (getParent() != null) { return getParent().getUpdateManager(); + } // Only happens when the figure has not been realized return NO_MANAGER; } @@ -998,8 +1049,9 @@ public void handleMouseReleased(MouseEvent event) { @Override public boolean hasFocus() { EventDispatcher dispatcher = internalGetEventDispatcher(); - if (dispatcher == null) + if (dispatcher == null) { return false; + } return dispatcher.getFocusOwner() == this; } @@ -1008,8 +1060,9 @@ public boolean hasFocus() { */ @Override public EventDispatcher internalGetEventDispatcher() { - if (getParent() != null) + if (getParent() != null) { return getParent().internalGetEventDispatcher(); + } return null; } @@ -1026,8 +1079,9 @@ public boolean intersects(Rectangle rect) { */ @Override public void invalidate() { - if (layoutManager != null) + if (layoutManager != null) { layoutManager.invalidate(); + } setValid(false); } @@ -1037,10 +1091,7 @@ public void invalidate() { @Override public void invalidateTree() { invalidate(); - for (Iterator iter = children.iterator(); iter.hasNext();) { - IFigure child = (IFigure) iter.next(); - child.invalidateTree(); - } + children.forEach(IFigure::invalidateTree); } /** @@ -1085,8 +1136,9 @@ protected boolean isMouseEventTarget() { */ @Override public boolean isMirrored() { - if (getParent() != null) + if (getParent() != null) { return getParent().isMirrored(); + } return false; } @@ -1150,8 +1202,9 @@ public boolean isVisible() { * @since 2.0 */ protected void layout() { - if (layoutManager != null) + if (layoutManager != null) { layoutManager.layout(this); + } } /** @@ -1164,12 +1217,15 @@ protected void layout() { */ @Override public void paint(Graphics graphics) { - if (getLocalBackgroundColor() != null) + if (getLocalBackgroundColor() != null) { graphics.setBackgroundColor(getLocalBackgroundColor()); - if (getLocalForegroundColor() != null) + } + if (getLocalForegroundColor() != null) { graphics.setForegroundColor(getLocalForegroundColor()); - if (font != null) + } + if (font != null) { graphics.setFont(font); + } graphics.pushState(); try { @@ -1190,8 +1246,9 @@ public void paint(Graphics graphics) { * @since 2.0 */ protected void paintBorder(Graphics graphics) { - if (getBorder() != null) + if (getBorder() != null) { getBorder().paint(this, graphics, NO_INSETS); + } } /** @@ -1217,9 +1274,9 @@ protected void paintChildren(Graphics graphics) { clipping = new Rectangle[] { child.getBounds() }; } // child may now paint inside the clipping areas - for (int j = 0; j < clipping.length; j++) { - if (clipping[j].intersects(graphics.getClip(Rectangle.SINGLETON))) { - graphics.clipRect(clipping[j]); + for (Rectangle element : clipping) { + if (element.intersects(graphics.getClip(Rectangle.SINGLETON))) { + graphics.clipRect(element); child.paint(graphics); graphics.restoreState(); } @@ -1238,21 +1295,23 @@ protected void paintChildren(Graphics graphics) { * @since 2.0 */ protected void paintClientArea(Graphics graphics) { - if (children.isEmpty()) + if (children.isEmpty()) { return; + } if (useLocalCoordinates()) { graphics.translate(getBounds().x + getInsets().left, getBounds().y + getInsets().top); - if (!optimizeClip()) + if (!optimizeClip()) { graphics.clipRect(getClientArea(PRIVATE_RECT)); + } graphics.pushState(); paintChildren(graphics); graphics.popState(); graphics.restoreState(); } else { - if (optimizeClip()) + if (optimizeClip()) { paintChildren(graphics); - else { + } else { graphics.clipRect(getClientArea(PRIVATE_RECT)); graphics.pushState(); paintChildren(graphics); @@ -1281,10 +1340,12 @@ protected boolean optimizeClip() { * @since 2.0 */ protected void paintFigure(Graphics graphics) { - if (isOpaque()) + if (isOpaque()) { graphics.fillRectangle(getBounds()); - if (getBorder() instanceof AbstractBackground) - ((AbstractBackground) getBorder()).paintBackground(this, graphics, NO_INSETS); + } + if (getBorder() instanceof AbstractBackground abstractBackground) { + abstractBackground.paintBackground(this, graphics, NO_INSETS); + } } /** @@ -1314,12 +1375,15 @@ protected void primTranslate(int dx, int dy) { */ @Override public void remove(IFigure figure) { - if ((figure.getParent() != this)) + if ((figure.getParent() != this)) { throw new IllegalArgumentException("Figure is not a child"); //$NON-NLS-1$ - if (getFlag(FLAG_REALIZED)) + } + if (getFlag(FLAG_REALIZED)) { figure.removeNotify(); - if (layoutManager != null) + } + if (layoutManager != null) { layoutManager.remove(figure); + } // The updates in the UpdateManager *have* to be // done asynchronously, else will result in // incorrect dirty region corrections. @@ -1396,8 +1460,9 @@ public void removeKeyListener(KeyListener listener) { public void removeLayoutListener(LayoutListener listener) { if (layoutManager instanceof LayoutNotifier notifier) { notifier.listeners.remove(listener); - if (notifier.listeners.isEmpty()) + if (notifier.listeners.isEmpty()) { layoutManager = notifier.realLayout; + } } } @@ -1410,8 +1475,6 @@ public void removeLayoutListener(LayoutListener listener) { * @since 2.0 */ protected void removeListener(Class clazz, Object listener) { - if (eventListeners == null) - return; eventListeners.removeListener(clazz, listener); } @@ -1437,8 +1500,9 @@ public void removeMouseMotionListener(MouseMotionListener listener) { @Override public void removeNotify() { children.forEach(IFigure::removeNotify); - if (internalGetEventDispatcher() != null) + if (internalGetEventDispatcher() != null) { internalGetEventDispatcher().requestRemoveFocus(this); + } setFlag(FLAG_REALIZED, false); } @@ -1447,8 +1511,9 @@ public void removeNotify() { */ @Override public void removePropertyChangeListener(PropertyChangeListener listener) { - if (propertyListeners == null) + if (propertyListeners == null) { return; + } propertyListeners.removePropertyChangeListener(listener); } @@ -1457,8 +1522,9 @@ public void removePropertyChangeListener(PropertyChangeListener listener) { */ @Override public void removePropertyChangeListener(String property, PropertyChangeListener listener) { - if (propertyListeners == null) + if (propertyListeners == null) { return; + } propertyListeners.removePropertyChangeListener(property, listener); } @@ -1475,8 +1541,9 @@ public final void repaint(Rectangle rect) { */ @Override public void repaint(int x, int y, int w, int h) { - if (isVisible()) + if (isVisible()) { getUpdateManager().addDirtyRegion(this, x, y, w, h); + } } /** @@ -1492,11 +1559,13 @@ public void repaint() { */ @Override public final void requestFocus() { - if (!isRequestFocusEnabled() || hasFocus()) + if (!isRequestFocusEnabled() || hasFocus()) { return; + } EventDispatcher dispatcher = internalGetEventDispatcher(); - if (dispatcher == null) + if (dispatcher == null) { return; + } dispatcher.requestFocus(this); } @@ -1506,10 +1575,11 @@ public final void requestFocus() { @Override public void revalidate() { invalidate(); - if (getParent() == null || isValidationRoot()) + if (getParent() == null || isValidationRoot()) { getUpdateManager().addInvalidFigure(this); - else + } else { getParent().revalidate(); + } } /** @@ -1569,8 +1639,9 @@ public void setBounds(Rectangle rect) { boolean resize = (rect.width != bounds.width) || (rect.height != bounds.height), translate = (rect.x != x) || (rect.y != y); - if ((resize || translate) && isVisible()) + if ((resize || translate) && isVisible()) { erase(); + } if (translate) { int dx = rect.x - x; int dy = rect.y - y; @@ -1581,8 +1652,9 @@ public void setBounds(Rectangle rect) { bounds.height = rect.height; if (translate || resize) { - if (resize) + if (resize) { invalidate(); + } fireFigureMoved(); repaint(); } @@ -1598,8 +1670,9 @@ public void setBounds(Rectangle rect) { */ protected void setChildrenDirection(int direction) { getChildrenRevIterable().forEach(child -> { - if (child instanceof Orientable) - ((Orientable) child).setDirection(direction); + if (child instanceof Orientable orientable) { + orientable.setDirection(direction); + } }); } @@ -1624,8 +1697,9 @@ protected void setChildrenEnabled(boolean value) { */ protected void setChildrenOrientation(int orientation) { getChildrenRevIterable().forEach(child -> { - if (child instanceof Orientable) - ((Orientable) child).setOrientation(orientation); + if (child instanceof Orientable orientable) { + orientable.setOrientation(orientation); + } }); } @@ -1634,11 +1708,13 @@ protected void setChildrenOrientation(int orientation) { */ @Override public void setConstraint(IFigure child, Object constraint) { - if (child.getParent() != this) + if (child.getParent() != this) { throw new IllegalArgumentException("Figure must be a child"); //$NON-NLS-1$ + } - if (layoutManager != null) + if (layoutManager != null) { layoutManager.setConstraint(child, constraint); + } revalidate(); } @@ -1659,12 +1735,14 @@ public void setClippingStrategy(IClippingStrategy clippingStrategy) { */ @Override public void setCursor(Cursor cursor) { - if (this.cursor == cursor) + if (this.cursor == cursor) { return; + } this.cursor = cursor; EventDispatcher dispatcher = internalGetEventDispatcher(); - if (dispatcher != null) + if (dispatcher != null) { dispatcher.updateCursor(); + } } /** @@ -1672,8 +1750,9 @@ public void setCursor(Cursor cursor) { */ @Override public void setEnabled(boolean value) { - if (isEnabled() == value) + if (isEnabled() == value) { return; + } setFlag(FLAG_ENABLED, value); } @@ -1685,10 +1764,11 @@ public void setEnabled(boolean value) { * @since 2.0 */ protected final void setFlag(int flag, boolean value) { - if (value) + if (value) { flags |= flag; - else + } else { flags &= ~flag; + } } /** @@ -1696,8 +1776,9 @@ protected final void setFlag(int flag, boolean value) { */ @Override public void setFocusTraversable(boolean focusTraversable) { - if (isFocusTraversable() == focusTraversable) + if (isFocusTraversable() == focusTraversable) { return; + } setFlag(FLAG_FOCUS_TRAVERSABLE, focusTraversable); } @@ -1744,10 +1825,11 @@ public void setForegroundColor(Color fg) { */ @Override public void setLayoutManager(LayoutManager manager) { - if (layoutManager instanceof LayoutNotifier) - ((LayoutNotifier) layoutManager).realLayout = manager; - else + if (layoutManager instanceof LayoutNotifier layoutNotifier) { + layoutNotifier.realLayout = manager; + } else { layoutManager = manager; + } revalidate(); } @@ -1756,8 +1838,9 @@ public void setLayoutManager(LayoutManager manager) { */ @Override public void setLocation(Point p) { - if (getLocation().equals(p)) + if (getLocation().equals(p)) { return; + } Rectangle r = new Rectangle(getBounds()); r.setLocation(p); setBounds(r); @@ -1768,8 +1851,9 @@ public void setLocation(Point p) { */ @Override public void setMaximumSize(Dimension d) { - if (maxSize != null && maxSize.equals(d)) + if (maxSize != null && maxSize.equals(d)) { return; + } maxSize = d; revalidate(); } @@ -1779,8 +1863,9 @@ public void setMaximumSize(Dimension d) { */ @Override public void setMinimumSize(Dimension d) { - if (minSize != null && minSize.equals(d)) + if (minSize != null && minSize.equals(d)) { return; + } minSize = d; revalidate(); } @@ -1790,8 +1875,9 @@ public void setMinimumSize(Dimension d) { */ @Override public void setOpaque(boolean opaque) { - if (isOpaque() == opaque) + if (isOpaque() == opaque) { return; + } setFlag(FLAG_OPAQUE, opaque); repaint(); } @@ -1811,8 +1897,9 @@ public void setParent(IFigure p) { */ @Override public void setPreferredSize(Dimension size) { - if (prefSize != null && prefSize.equals(size)) + if (prefSize != null && prefSize.equals(size)) { return; + } prefSize = size; revalidate(); } @@ -1834,8 +1921,9 @@ public final void setPreferredSize(int w, int h) { */ @Override public void setRequestFocusEnabled(boolean requestFocusEnabled) { - if (isRequestFocusEnabled() == requestFocusEnabled) + if (isRequestFocusEnabled() == requestFocusEnabled) { return; + } setFlag(FLAG_FOCUSABLE, requestFocusEnabled); } @@ -1853,8 +1941,9 @@ public final void setSize(Dimension d) { @Override public void setSize(int w, int h) { Rectangle bounds = getBounds(); - if (bounds.width == w && bounds.height == h) + if (bounds.width == w && bounds.height == h) { return; + } Rectangle r = new Rectangle(getBounds()); r.setSize(w, h); setBounds(r); @@ -1865,8 +1954,9 @@ public void setSize(int w, int h) { */ @Override public void setToolTip(IFigure f) { - if (toolTip == f) + if (toolTip == f) { return; + } toolTip = f; } @@ -1887,13 +1977,16 @@ public void setValid(boolean value) { @Override public void setVisible(boolean visible) { boolean currentVisibility = isVisible(); - if (visible == currentVisibility) + if (visible == currentVisibility) { return; - if (currentVisibility) + } + if (currentVisibility) { erase(); + } setFlag(FLAG_VISIBLE, visible); - if (visible) + if (visible) { repaint(); + } revalidate(); } @@ -1911,8 +2004,9 @@ public final void translate(int x, int y) { */ @Override public void translateFromParent(Translatable t) { - if (useLocalCoordinates()) + if (useLocalCoordinates()) { t.performTranslate(-getBounds().x - getInsets().left, -getBounds().y - getInsets().top); + } } /** @@ -1931,8 +2025,9 @@ public final void translateToAbsolute(Translatable t) { */ @Override public void translateToParent(Translatable t) { - if (useLocalCoordinates()) + if (useLocalCoordinates()) { t.performTranslate(getBounds().x + getInsets().left, getBounds().y + getInsets().top); + } } /** @@ -1962,8 +2057,9 @@ protected boolean useLocalCoordinates() { */ @Override public void validate() { - if (isValid()) + if (isValid()) { return; + } setValid(true); layout(); children.forEach(IFigure::validate); @@ -2014,22 +2110,25 @@ final class LayoutNotifier implements LayoutManager { @Override public Object getConstraint(IFigure child) { - if (realLayout != null) + if (realLayout != null) { return realLayout.getConstraint(child); + } return null; } @Override public Dimension getMinimumSize(IFigure container, int wHint, int hHint) { - if (realLayout != null) + if (realLayout != null) { return realLayout.getMinimumSize(container, wHint, hHint); + } return null; } @Override public Dimension getPreferredSize(IFigure container, int wHint, int hHint) { - if (realLayout != null) + if (realLayout != null) { return realLayout.getPreferredSize(container, wHint, hHint); + } return null; } @@ -2037,8 +2136,9 @@ public Dimension getPreferredSize(IFigure container, int wHint, int hHint) { public void invalidate() { listeners.forEach(listener -> listener.invalidate(Figure.this)); - if (realLayout != null) + if (realLayout != null) { realLayout.invalidate(); + } } @Override @@ -2048,23 +2148,26 @@ public void layout(IFigure container) { consumed |= listener.layout(container); } - if (realLayout != null && !consumed) + if (realLayout != null && !consumed) { realLayout.layout(container); + } listeners.forEach(listener -> listener.postLayout(container)); } @Override public void remove(IFigure child) { listeners.forEach(listener -> listener.remove(child)); - if (realLayout != null) + if (realLayout != null) { realLayout.remove(child); + } } @Override public void setConstraint(IFigure child, Object constraint) { listeners.forEach(listener -> listener.setConstraint(child, constraint)); - if (realLayout != null) + if (realLayout != null) { realLayout.setConstraint(child, constraint); + } } } @@ -2073,8 +2176,9 @@ public void setConstraint(IFigure child, Object constraint) { * * @deprecated use ReverseFigureChildrenIterator instead */ + @Deprecated public static class FigureIterator { - private List list; + private final List list; private int index; /** diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java index 8dc362aea..0f83d9868 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/LightweightSystem.java @@ -107,20 +107,16 @@ public void controlResized(ControlEvent e) { LightweightSystem.this.controlResized(); } }); - canvas.addListener(SWT.Paint, new Listener() { - @Override - public void handleEvent(Event e) { - LightweightSystem.this.paint(e.gc); - } - }); + canvas.addListener(SWT.Paint, e -> paint(e.gc)); } /** * Resizes and revalidates the root figure when the control is resized. */ protected void controlResized() { - if (ignoreResize > 0) + if (ignoreResize > 0) { return; + } Rectangle r = new Rectangle(canvas.getClientArea()); r.setLocation(0, 0); root.setBounds(r); @@ -135,8 +131,9 @@ protected void controlResized() { * @since 2.0 */ protected EventDispatcher getEventDispatcher() { - if (dispatcher == null) + if (dispatcher == null) { setEventDispatcher(new SWTEventDispatcher()); + } return dispatcher; } @@ -213,8 +210,9 @@ public void paint(GC gc) { * @since 2.0 */ public void setContents(IFigure figure) { - if (contents != null) + if (contents != null) { root.remove(contents); + } contents = figure; root.add(contents); } @@ -226,13 +224,15 @@ public void setContents(IFigure figure) { * @since 2.0 */ public void setControl(Canvas c) { - if (canvas == c) + if (canvas == c) { return; + } canvas = c; - if ((c.getStyle() & SWT.DOUBLE_BUFFERED) != 0) + if ((c.getStyle() & SWT.DOUBLE_BUFFERED) != 0) { getUpdateManager().setGraphicsSource(new NativeGraphicsSource(canvas)); - else + } else { getUpdateManager().setGraphicsSource(new BufferedGraphicsSource(canvas)); + } getEventDispatcher().setControl(c); addListeners(); @@ -256,10 +256,11 @@ public void setEventDispatcher(EventDispatcher dispatcher) { } void setIgnoreResize(boolean value) { - if (value) + if (value) { ignoreResize++; - else + } else { ignoreResize--; + } } /** @@ -292,30 +293,36 @@ protected class RootFigure extends Figure { /** @see IFigure#getBackgroundColor() */ @Override public Color getBackgroundColor() { - if (bgColor != null) + if (bgColor != null) { return bgColor; - if (canvas != null) + } + if (canvas != null) { return canvas.getBackground(); + } return null; } /** @see IFigure#getFont() */ @Override public Font getFont() { - if (font != null) + if (font != null) { return font; - if (canvas != null) + } + if (canvas != null) { return canvas.getFont(); + } return null; } /** @see IFigure#getForegroundColor() */ @Override public Color getForegroundColor() { - if (fgColor != null) + if (fgColor != null) { return fgColor; - if (canvas != null) + } + if (canvas != null) { return canvas.getForeground(); + } return null; } @@ -369,8 +376,9 @@ public void focusLost(FocusEvent e) { public void getChild(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getChild(e); + } } /** @see AccessibleControlListener#getChildAtPoint(AccessibleControlEvent) */ @@ -378,8 +386,9 @@ public void getChild(AccessibleControlEvent e) { public void getChildAtPoint(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getChildAtPoint(e); + } } /** @see AccessibleControlListener#getChildCount(AccessibleControlEvent) */ @@ -387,8 +396,9 @@ public void getChildAtPoint(AccessibleControlEvent e) { public void getChildCount(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getChildCount(e); + } } /** @see AccessibleControlListener#getChildren(AccessibleControlEvent) */ @@ -396,8 +406,9 @@ public void getChildCount(AccessibleControlEvent e) { public void getChildren(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getChildren(e); + } } /** @see AccessibleControlListener#getDefaultAction(AccessibleControlEvent) */ @@ -405,8 +416,9 @@ public void getChildren(AccessibleControlEvent e) { public void getDefaultAction(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getDefaultAction(e); + } } /** @see AccessibleListener#getDescription(AccessibleEvent) */ @@ -414,8 +426,9 @@ public void getDefaultAction(AccessibleControlEvent e) { public void getDescription(AccessibleEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getDescription(e); + } } /** @see AccessibleControlListener#getFocus(AccessibleControlEvent) */ @@ -423,8 +436,9 @@ public void getDescription(AccessibleEvent e) { public void getFocus(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getFocus(e); + } } /** @see AccessibleListener#getHelp(AccessibleEvent) */ @@ -432,8 +446,9 @@ public void getFocus(AccessibleControlEvent e) { public void getHelp(AccessibleEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getHelp(e); + } } /** @see AccessibleListener#getKeyboardShortcut(AccessibleEvent) */ @@ -441,8 +456,9 @@ public void getHelp(AccessibleEvent e) { public void getKeyboardShortcut(AccessibleEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getKeyboardShortcut(e); + } } /** @see AccessibleControlListener#getLocation(AccessibleControlEvent) */ @@ -450,8 +466,9 @@ public void getKeyboardShortcut(AccessibleEvent e) { public void getLocation(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getLocation(e); + } } /** @see AccessibleListener#getName(AccessibleEvent) */ @@ -459,8 +476,9 @@ public void getLocation(AccessibleControlEvent e) { public void getName(AccessibleEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getName(e); + } } /** @see AccessibleControlListener#getRole(AccessibleControlEvent) */ @@ -468,8 +486,9 @@ public void getName(AccessibleEvent e) { public void getRole(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getRole(e); + } } /** @see AccessibleControlListener#getSelection(AccessibleControlEvent) */ @@ -477,8 +496,9 @@ public void getRole(AccessibleControlEvent e) { public void getSelection(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getSelection(e); + } } /** @see AccessibleControlListener#getState(AccessibleControlEvent) */ @@ -486,8 +506,9 @@ public void getSelection(AccessibleControlEvent e) { public void getState(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getState(e); + } } /** @see AccessibleControlListener#getValue(AccessibleControlEvent) */ @@ -495,8 +516,9 @@ public void getState(AccessibleControlEvent e) { public void getValue(AccessibleControlEvent e) { EventDispatcher.AccessibilityDispatcher ad; ad = getEventDispatcher().getAccessibilityDispatcher(); - if (ad != null) + if (ad != null) { ad.getValue(e); + } } /** @@ -506,8 +528,9 @@ public void getValue(AccessibleControlEvent e) { @Override public void handleEvent(Event event) { // Mouse wheel events - if (event.type == SWT.MouseWheel) + if (event.type == SWT.MouseWheel) { getEventDispatcher().dispatchMouseWheelScrolled(event); + } } /** @see KeyListener#keyPressed(KeyEvent) */ diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/UpdateManager.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/UpdateManager.java index 82e4b8209..86390795a 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/UpdateManager.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/UpdateManager.java @@ -12,6 +12,8 @@ *******************************************************************************/ package org.eclipse.draw2d; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import org.eclipse.swt.graphics.GC; @@ -43,7 +45,7 @@ */ public abstract class UpdateManager { - private UpdateListener listeners[] = new UpdateListener[0]; + private final List listeners = new ArrayList<>(); private boolean disposed; /** @@ -91,18 +93,10 @@ public void runWithUpdate(Runnable run) { * @param listener the listener to add */ public void addUpdateListener(UpdateListener listener) { - if (listener == null) + if (listener == null) { throw new IllegalArgumentException(); - if (listeners == null) { - listeners = new UpdateListener[1]; - listeners[0] = listener; - } else { - int oldSize = listeners.length; - UpdateListener newListeners[] = new UpdateListener[oldSize + 1]; - System.arraycopy(listeners, 0, newListeners, 0, oldSize); - newListeners[oldSize] = listener; - listeners = newListeners; } + listeners.add(listener); } /** @@ -120,18 +114,14 @@ public void dispose() { * @param dirtyRegions map of dirty regions to figures */ protected void firePainting(Rectangle damage, Map dirtyRegions) { - UpdateListener localListeners[] = listeners; - for (int i = 0; i < localListeners.length; i++) - localListeners[i].notifyPainting(damage, dirtyRegions); + listeners.forEach(localListener -> localListener.notifyPainting(damage, dirtyRegions)); } /** * Notifies listeners that validation is about to occur. */ protected void fireValidating() { - UpdateListener localListeners[] = listeners; - for (int i = 0; i < localListeners.length; i++) - localListeners[i].notifyValidating(); + listeners.forEach(UpdateListener::notifyValidating); } /** @@ -175,22 +165,10 @@ protected void paint(GC gc) { * @param listener the listener to remove */ public void removeUpdateListener(UpdateListener listener) { - if (listener == null) + if (listener == null) { throw new IllegalArgumentException(); - for (int index = 0; index < listeners.length; index++) - if (listeners[index] == listener) { - int newSize = listeners.length - 1; - UpdateListener newListeners[] = null; - if (newSize != 0) { - newListeners = new UpdateListener[newSize]; - System.arraycopy(listeners, 0, newListeners, 0, index); - System.arraycopy(listeners, index + 1, newListeners, index, newSize - index); - } else { - newListeners = new UpdateListener[0]; - } - listeners = newListeners; - return; - } + } + listeners.remove(listener); } /**