Browse Source

Memory: Further ensure no memory leaks for with shared resources and wrappers can occur.

pull/245/head
weisj 3 years ago
parent
commit
acc4aaf7c2
No known key found for this signature in database
GPG Key ID: 31124CB75461DA2A
  1. 26
      core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java
  2. 1
      core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkEditorPaneUIBridge.java
  3. 4
      core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkTextFieldUIBridge.java
  4. 43
      core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyEditorPane.java
  5. 9
      core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyTextFieldUI.java
  6. 5
      utils/src/main/java/com/github/weisj/darklaf/util/value/WeakShared.java

26
core/src/main/java/com/github/weisj/darklaf/settings/ThemeSettings.java

@ -41,7 +41,7 @@ import com.github.weisj.darklaf.theme.info.PreferredThemeStyle;
import com.github.weisj.darklaf.util.DarkUIUtil; import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.LazyValue; import com.github.weisj.darklaf.util.LazyValue;
import com.github.weisj.darklaf.util.LogUtil; import com.github.weisj.darklaf.util.LogUtil;
import com.github.weisj.darklaf.util.MutableLazyValue; import com.github.weisj.darklaf.util.value.WeakShared;
public class ThemeSettings implements ThemePreferenceListener { public class ThemeSettings implements ThemePreferenceListener {
@ -49,7 +49,8 @@ public class ThemeSettings implements ThemePreferenceListener {
private static final LazyValue<ThemeSettings> instance = new LazyValue<>(ThemeSettings::new); private static final LazyValue<ThemeSettings> instance = new LazyValue<>(ThemeSettings::new);
private static final LazyValue<Icon> icon = new LazyValue<>(() -> UIManager.getIcon("ThemeSettings.icon")); private static final LazyValue<Icon> icon = new LazyValue<>(() -> UIManager.getIcon("ThemeSettings.icon"));
private final MutableLazyValue<ThemeSettingsPanel> settingsPanel; private final WeakShared<ThemeSettingsPanel> settingsPanel;
private ThemeSettingsPanel customThemeSettingsPanel;
private JDialog dialog; private JDialog dialog;
@ -89,7 +90,7 @@ public class ThemeSettings implements ThemePreferenceListener {
LafManager.addThemePreferenceChangeListener(this); LafManager.addThemePreferenceChangeListener(this);
currentConfiguration = new DefaultSettingsConfiguration(); currentConfiguration = new DefaultSettingsConfiguration();
savedConfiguration = new DefaultSettingsConfiguration(); savedConfiguration = new DefaultSettingsConfiguration();
settingsPanel = new MutableLazyValue<>(() -> { settingsPanel = new WeakShared<>(() -> {
ThemeSettingsPanel panel = new ThemeSettingsPanel(); ThemeSettingsPanel panel = new ThemeSettingsPanel();
panel.loadConfiguration(currentConfiguration); panel.loadConfiguration(currentConfiguration);
return panel; return panel;
@ -155,8 +156,10 @@ public class ThemeSettings implements ThemePreferenceListener {
*/ */
public void setThemeSettingsPanel(final ThemeSettingsPanel panel) { public void setThemeSettingsPanel(final ThemeSettingsPanel panel) {
if (panel != null) { if (panel != null) {
settingsPanel.set(panel); customThemeSettingsPanel = panel;
panel.loadConfiguration(currentConfiguration); panel.loadConfiguration(currentConfiguration);
} else {
customThemeSettingsPanel = null;
} }
} }
@ -381,9 +384,7 @@ public class ThemeSettings implements ThemePreferenceListener {
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING)); dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
}); });
JButton apply = new JButton(UIManager.getString("dialog_apply", l)); JButton apply = new JButton(UIManager.getString("dialog_apply", l));
apply.addActionListener(e -> { apply.addActionListener(e -> apply());
apply();
});
Box box = Box.createHorizontalBox(); Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue()); box.add(Box.createHorizontalGlue());
@ -404,12 +405,19 @@ public class ThemeSettings implements ThemePreferenceListener {
updateSettingsPanel(); updateSettingsPanel();
} }
private ThemeSettingsPanel getThemeSettingsPanelWeak() {
if (customThemeSettingsPanel != null) return customThemeSettingsPanel;
return settingsPanel.getIfPresent();
}
private void updateSettingsPanel() { private void updateSettingsPanel() {
settingsPanel.ifPresent(p -> p.loadConfiguration(currentConfiguration)); ThemeSettingsPanel panel = getThemeSettingsPanelWeak();
if (panel != null) panel.loadConfiguration(currentConfiguration);
} }
private void fetchFromSettingsPanel() { private void fetchFromSettingsPanel() {
settingsPanel.ifPresent(ThemeSettingsPanel::updateConfiguration); ThemeSettingsPanel panel = getThemeSettingsPanelWeak();
if (panel != null) panel.updateConfiguration();
} }
/** Saves the settings. */ /** Saves the settings. */

1
core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkEditorPaneUIBridge.java

@ -80,6 +80,7 @@ public abstract class DarkEditorPaneUIBridge extends DarkTextUI {
} }
protected void updateDisplayProperties() { protected void updateDisplayProperties() {
editorPane.setEditorPane((JEditorPane) editor);
updateDisplayProperties((JEditorPane) getComponent(), updateDisplayProperties((JEditorPane) getComponent(),
new PropertyChangeEvent(editorPane, PropertyKey.FONT, editorPane.getFont(), editorPane.getFont())); new PropertyChangeEvent(editorPane, PropertyKey.FONT, editorPane.getFont(), editorPane.getFont()));
} }

4
core/src/main/java/com/github/weisj/darklaf/ui/text/bridge/DarkTextFieldUIBridge.java

@ -69,7 +69,9 @@ public abstract class DarkTextFieldUIBridge extends DarkTextUI {
@Override @Override
public int getBaseline(final JComponent c, final int width, final int height) { public int getBaseline(final JComponent c, final int width, final int height) {
basicTextFieldUI.setRootView(getRootView((JTextComponent) c)); basicTextFieldUI.setRootView(getRootView((JTextComponent) c));
return basicTextFieldUI.getBaseline(c, width, height); int baseLine = basicTextFieldUI.getBaseline(c, width, height);
basicTextFieldUI.setRootView(null);
return baseLine;
} }
@Override @Override

43
core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyEditorPane.java

@ -27,6 +27,7 @@ import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener; import java.awt.event.MouseWheelListener;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.lang.ref.WeakReference;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
@ -38,18 +39,18 @@ import javax.swing.text.Highlighter;
public class DummyEditorPane extends JEditorPane { public class DummyEditorPane extends JEditorPane {
private JEditorPane editorPane; private WeakReference<JEditorPane> editorPane;
private PropertyChangeListener propertyChangeListener; private PropertyChangeListener propertyChangeListener;
public void setEditorPane(final JEditorPane editorPane) { public void setEditorPane(final JEditorPane editor) {
this.editorPane = editorPane; this.editorPane = new WeakReference<>(editor);
if (editorPane != null) { if (editor != null) {
copyProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES); copyProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, editor);
copyProperty(JEditorPane.W3C_LENGTH_UNITS); copyProperty(JEditorPane.W3C_LENGTH_UNITS, editor);
} }
} }
protected void copyProperty(final String key) { protected void copyProperty(final String key, final JEditorPane editorPane) {
putClientProperty(key, editorPane.getClientProperty(key)); putClientProperty(key, editorPane.getClientProperty(key));
} }
@ -57,33 +58,43 @@ public class DummyEditorPane extends JEditorPane {
return propertyChangeListener; return propertyChangeListener;
} }
private JEditorPane getEditor() {
return editorPane != null ? editorPane.get() : null;
}
@Override @Override
public Document getDocument() { public Document getDocument() {
if (editorPane == null) return super.getDocument(); JEditorPane editor = getEditor();
return editorPane.getDocument(); if (editor == null) return super.getDocument();
return editor.getDocument();
} }
@Override @Override
public Font getFont() { public Font getFont() {
if (editorPane == null) return null; JEditorPane editor = getEditor();
return editorPane.getFont(); if (editor == null) return null;
return editor.getFont();
} }
@Override @Override
public Color getForeground() { public Color getForeground() {
if (editorPane == null) return null; JEditorPane editor = getEditor();
return editorPane.getForeground(); if (editor == null) return null;
return editor.getForeground();
} }
@Override @Override
public Color getBackground() { public Color getBackground() {
if (editorPane == null) return null; JEditorPane editor = getEditor();
return editorPane.getBackground(); if (editor == null) return null;
return editor.getBackground();
} }
@Override @Override
public boolean isEditable() { public boolean isEditable() {
return editorPane.isEditable(); JEditorPane editor = getEditor();
if (editor == null) return false;
return editor.isEditable();
} }
@Override @Override

9
core/src/main/java/com/github/weisj/darklaf/ui/text/dummy/DummyTextFieldUI.java

@ -21,20 +21,23 @@
*/ */
package com.github.weisj.darklaf.ui.text.dummy; package com.github.weisj.darklaf.ui.text.dummy;
import java.lang.ref.WeakReference;
import javax.swing.plaf.basic.BasicTextFieldUI; import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
import javax.swing.text.View; import javax.swing.text.View;
public class DummyTextFieldUI extends BasicTextFieldUI { public class DummyTextFieldUI extends BasicTextFieldUI {
private View rootView; private WeakReference<View> rootView;
@Override @Override
public View getRootView(final JTextComponent tc) { public View getRootView(final JTextComponent tc) {
return rootView != null ? rootView : super.getRootView(tc); View view = rootView != null ? rootView.get() : null;
return view != null ? view : super.getRootView(tc);
} }
public void setRootView(final View rootView) { public void setRootView(final View rootView) {
this.rootView = rootView; this.rootView = new WeakReference<>(rootView);
} }
} }

5
utils/src/main/java/com/github/weisj/darklaf/util/value/WeakShared.java

@ -56,4 +56,9 @@ public class WeakShared<T> {
} }
return value; return value;
} }
public T getIfPresent() {
WeakReference<T> ref = reference;
return ref != null ? ref.get() : null;
}
} }

Loading…
Cancel
Save