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

37
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;
public ViewPropertyChangeListener(final JScrollPane scrollPane, final PropertyChangeListener listener) {
@ -59,13 +60,15 @@ public class ViewPropertyChangeListener implements PropertyChangeListener {
Object source = e.getSource();
if (source == scrollPane) {
if ("viewport".equals(key)) {
Object old = e.getOldValue();
Object newVal = e.getNewValue();
if (old instanceof JViewport) {
((JViewport) old).addContainerListener(containerListener);
if (currentViewport != null) {
currentViewport.removeContainerListener(containerListener);
}
Object newVal = e.getNewValue();
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() {
scrollPane.addPropertyChangeListener(this);
JViewport viewport = scrollPane.getViewport();
if (viewport != null) {
viewport.addContainerListener(containerListener);
Component view = viewport.getView();
if (view != null) {
view.addPropertyChangeListener(listener);
currentViewport = scrollPane.getViewport();
if (currentViewport != null) {
currentViewport.addContainerListener(containerListener);
currentView = currentViewport.getView();
if (currentView != null) {
currentView.addPropertyChangeListener(listener);
}
}
}
public void uninstall() {
scrollPane.removePropertyChangeListener(this);
JViewport viewport = scrollPane.getViewport();
if (viewport != null) {
viewport.removeContainerListener(containerListener);
Component view = viewport.getView();
if (view != null) {
view.removePropertyChangeListener(listener);
}
if (currentViewport != null) {
currentViewport.removeContainerListener(containerListener);
}
if (currentView != null) {
currentView.removePropertyChangeListener(listener);
}
}
}

Loading…
Cancel
Save