Browse Source

Ensure ViewPropertyChangeListener is uninstalled.

Ensure scrollbar background colors are updated if their ui changes after the ui of the scrollpane.
pull/214/head
weisj 4 years ago
parent
commit
060a463348
  1. 26
      core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollPaneUI.java
  2. 35
      core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/ViewPropertyChangeListener.java

26
core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/DarkScrollPaneUI.java

@ -35,7 +35,7 @@ import com.github.weisj.darklaf.util.PropertyKey;
import com.github.weisj.darklaf.util.PropertyUtil; import com.github.weisj.darklaf.util.PropertyUtil;
/** @author Jannis Weis */ /** @author Jannis Weis */
public class DarkScrollPaneUI extends BasicScrollPaneUI { public class DarkScrollPaneUI extends BasicScrollPaneUI implements PropertyChangeListener {
private final MouseWheelListener verticalMouseWheelListener = e -> { private final MouseWheelListener verticalMouseWheelListener = e -> {
if (!scrollpane.isWheelScrollingEnabled() || e.getModifiersEx() == 0 || !horizontalScrollBarEnabled()) { if (!scrollpane.isWheelScrollingEnabled() || e.getModifiersEx() == 0 || !horizontalScrollBarEnabled()) {
@ -68,6 +68,11 @@ public class DarkScrollPaneUI extends BasicScrollPaneUI {
scrollpane.getComponentOrientation().isLeftToRight()); scrollpane.getComponentOrientation().isLeftToRight());
scrollbar.setValueIsAdjusting(false); scrollbar.setValueIsAdjusting(false);
}; };
private final PropertyChangeListener scrollbarPropertyChangeListener = e -> {
if (PropertyKey.UI.equals(e.getPropertyName())) {
updateScrollBarBackground();
}
};
private ViewPropertyChangeListener viewPropertyChangeListener; private ViewPropertyChangeListener viewPropertyChangeListener;
private ScrollPaneLayout oldLayout; private ScrollPaneLayout oldLayout;
@ -105,9 +110,11 @@ public class DarkScrollPaneUI extends BasicScrollPaneUI {
protected void installListeners(final JScrollPane c) { protected void installListeners(final JScrollPane c) {
super.installListeners(c); super.installListeners(c);
getViewPropertyChangeListener().install(); getViewPropertyChangeListener().install();
scrollpane.addPropertyChangeListener(getPropertyChangeListener()); scrollpane.addPropertyChangeListener(this);
scrollpane.getVerticalScrollBar().addMouseWheelListener(verticalMouseWheelListener); scrollpane.getVerticalScrollBar().addMouseWheelListener(verticalMouseWheelListener);
scrollpane.getVerticalScrollBar().addPropertyChangeListener(scrollbarPropertyChangeListener);
scrollpane.getHorizontalScrollBar().addMouseWheelListener(horizontalMouseWheelListener); scrollpane.getHorizontalScrollBar().addMouseWheelListener(horizontalMouseWheelListener);
scrollpane.getHorizontalScrollBar().addPropertyChangeListener(scrollbarPropertyChangeListener);
} }
protected ViewPropertyChangeListener getViewPropertyChangeListener() { protected ViewPropertyChangeListener getViewPropertyChangeListener() {
@ -125,11 +132,8 @@ public class DarkScrollPaneUI extends BasicScrollPaneUI {
}); });
} }
protected PropertyChangeListener getPropertyChangeListener() { @Override
return this::scrollPanePropertyChange; public void propertyChange(final PropertyChangeEvent e) {
}
protected void scrollPanePropertyChange(final PropertyChangeEvent e) {
if (e.getSource() == scrollpane) { if (e.getSource() == scrollpane) {
String propertyName = e.getPropertyName(); String propertyName = e.getPropertyName();
if ("verticalScrollBar".equals(propertyName)) { if ("verticalScrollBar".equals(propertyName)) {
@ -137,9 +141,11 @@ public class DarkScrollPaneUI extends BasicScrollPaneUI {
Object newVal = e.getNewValue(); Object newVal = e.getNewValue();
if (old instanceof JScrollBar) { if (old instanceof JScrollBar) {
((JScrollBar) old).removeMouseWheelListener(verticalMouseWheelListener); ((JScrollBar) old).removeMouseWheelListener(verticalMouseWheelListener);
((JScrollBar) old).removePropertyChangeListener(scrollbarPropertyChangeListener);
} }
if (newVal instanceof JScrollBar) { if (newVal instanceof JScrollBar) {
((JScrollBar) newVal).addMouseWheelListener(verticalMouseWheelListener); ((JScrollBar) newVal).addMouseWheelListener(verticalMouseWheelListener);
((JScrollBar) newVal).addPropertyChangeListener(scrollbarPropertyChangeListener);
PropertyUtil.installBackground((Component) newVal, getViewBackground()); PropertyUtil.installBackground((Component) newVal, getViewBackground());
} }
} else if ("horizontalScrollBar".equals(propertyName)) { } else if ("horizontalScrollBar".equals(propertyName)) {
@ -147,9 +153,11 @@ public class DarkScrollPaneUI extends BasicScrollPaneUI {
Object newVal = e.getNewValue(); Object newVal = e.getNewValue();
if (old instanceof JScrollBar) { if (old instanceof JScrollBar) {
((JScrollBar) old).removeMouseWheelListener(horizontalMouseWheelListener); ((JScrollBar) old).removeMouseWheelListener(horizontalMouseWheelListener);
((JScrollBar) old).removePropertyChangeListener(scrollbarPropertyChangeListener);
} }
if (newVal instanceof JScrollBar) { if (newVal instanceof JScrollBar) {
((JScrollBar) newVal).addMouseWheelListener(horizontalMouseWheelListener); ((JScrollBar) newVal).addMouseWheelListener(horizontalMouseWheelListener);
((JScrollBar) newVal).addPropertyChangeListener(scrollbarPropertyChangeListener);
PropertyUtil.installBackground((Component) newVal, getViewBackground()); PropertyUtil.installBackground((Component) newVal, getViewBackground());
} }
@ -223,9 +231,11 @@ public class DarkScrollPaneUI extends BasicScrollPaneUI {
protected void uninstallListeners(final JComponent c) { protected void uninstallListeners(final JComponent c) {
super.uninstallListeners(c); super.uninstallListeners(c);
getViewPropertyChangeListener().uninstall(); getViewPropertyChangeListener().uninstall();
scrollpane.addPropertyChangeListener(getPropertyChangeListener()); scrollpane.removePropertyChangeListener(this);
scrollpane.getVerticalScrollBar().removeMouseWheelListener(verticalMouseWheelListener); scrollpane.getVerticalScrollBar().removeMouseWheelListener(verticalMouseWheelListener);
scrollpane.getVerticalScrollBar().removePropertyChangeListener(scrollbarPropertyChangeListener);
scrollpane.getHorizontalScrollBar().removeMouseWheelListener(horizontalMouseWheelListener); scrollpane.getHorizontalScrollBar().removeMouseWheelListener(horizontalMouseWheelListener);
scrollpane.getHorizontalScrollBar().removePropertyChangeListener(scrollbarPropertyChangeListener);
} }
@Override @Override

35
core/src/main/java/com/github/weisj/darklaf/ui/scrollpane/ViewPropertyChangeListener.java

@ -46,6 +46,7 @@ public class ViewPropertyChangeListener implements PropertyChangeListener {
} }
} }
}; };
private JViewport currentViewport;
private Component currentView; private Component currentView;
public ViewPropertyChangeListener(final JScrollPane scrollPane, final PropertyChangeListener listener) { public ViewPropertyChangeListener(final JScrollPane scrollPane, final PropertyChangeListener listener) {
@ -59,13 +60,15 @@ public class ViewPropertyChangeListener implements PropertyChangeListener {
Object source = e.getSource(); Object source = e.getSource();
if (source == scrollPane) { if (source == scrollPane) {
if ("viewport".equals(key)) { if ("viewport".equals(key)) {
Object old = e.getOldValue(); if (currentViewport != null) {
Object newVal = e.getNewValue(); currentViewport.removeContainerListener(containerListener);
if (old instanceof JViewport) {
((JViewport) old).addContainerListener(containerListener);
} }
Object newVal = e.getNewValue();
if (newVal instanceof JViewport) { if (newVal instanceof JViewport) {
((JViewport) newVal).removeContainerListener(containerListener); currentViewport = ((JViewport) newVal);
currentViewport.addContainerListener(containerListener);
} else {
currentViewport = null;
} }
} }
} }
@ -73,25 +76,23 @@ public class ViewPropertyChangeListener implements PropertyChangeListener {
public void install() { public void install() {
scrollPane.addPropertyChangeListener(this); scrollPane.addPropertyChangeListener(this);
JViewport viewport = scrollPane.getViewport(); currentViewport = scrollPane.getViewport();
if (viewport != null) { if (currentViewport != null) {
viewport.addContainerListener(containerListener); currentViewport.addContainerListener(containerListener);
Component view = viewport.getView(); currentView = currentViewport.getView();
if (view != null) { if (currentView != null) {
view.addPropertyChangeListener(listener); currentView.addPropertyChangeListener(listener);
} }
} }
} }
public void uninstall() { public void uninstall() {
scrollPane.removePropertyChangeListener(this); scrollPane.removePropertyChangeListener(this);
JViewport viewport = scrollPane.getViewport(); if (currentViewport != null) {
if (viewport != null) { currentViewport.removeContainerListener(containerListener);
viewport.removeContainerListener(containerListener);
Component view = viewport.getView();
if (view != null) {
view.removePropertyChangeListener(listener);
} }
if (currentView != null) {
currentView.removePropertyChangeListener(listener);
} }
} }
} }

Loading…
Cancel
Save