Browse Source

Merge pull request #97 from vlsi/fix_reflection

Remove unnecessary reflection in DarkLaf.
pull/105/head
Jannis Weis 5 years ago committed by GitHub
parent
commit
d43a3aa4be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 61
      core/src/main/java/com/github/weisj/darklaf/DarkLaf.java
  2. 52
      core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUIBridge.java
  3. 13
      core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePaneUIBridge.java
  4. 32
      core/src/main/java/com/github/weisj/darklaf/ui/filechooser/MetalUIDefaults.java

61
core/src/main/java/com/github/weisj/darklaf/DarkLaf.java

@ -43,7 +43,6 @@ import java.awt.event.KeyEvent;
import java.awt.font.TextAttribute; import java.awt.font.TextAttribute;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.lang.reflect.Method;
import java.text.AttributedCharacterIterator; import java.text.AttributedCharacterIterator;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -105,12 +104,9 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
@Override @Override
public UIDefaults getDefaults() { public UIDefaults getDefaults() {
final UIDefaults metalDefaults = new MetalLookAndFeel().getDefaults();
final UIDefaults defaults = base.getDefaults();
try { try {
final Method superMethod = BasicLookAndFeel.class.getDeclaredMethod("getDefaults");
superMethod.setAccessible(true);
final UIDefaults metalDefaults = (UIDefaults) superMethod.invoke(new MetalLookAndFeel());
final UIDefaults defaults = (UIDefaults) superMethod.invoke(base);
initInputMapDefaults(defaults); initInputMapDefaults(defaults);
loadThemeDefaults(defaults); loadThemeDefaults(defaults);
initIdeaDefaults(defaults); initIdeaDefaults(defaults);
@ -130,7 +126,7 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.toString(), e.getStackTrace()); LOGGER.log(Level.SEVERE, e.toString(), e.getStackTrace());
} }
return super.getDefaults(); return defaults;
} }
private void patchMacOSFonts(final UIDefaults defaults) { private void patchMacOSFonts(final UIDefaults defaults) {
@ -359,7 +355,7 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
* On macOS the default PopupFactory is overwritten with a custom one, which always uses heavyweight popups. * On macOS the default PopupFactory is overwritten with a custom one, which always uses heavyweight popups.
* This is disadvantageous for the behaviour of custom tooltips. * This is disadvantageous for the behaviour of custom tooltips.
*/ */
call("initialize"); base.initialize();
PopupFactory.setSharedInstance(new DarkPopupFactory()); PopupFactory.setSharedInstance(new DarkPopupFactory());
PropertyLoader.reset(); PropertyLoader.reset();
UIManager.addPropertyChangeListener(this); UIManager.addPropertyChangeListener(this);
@ -367,7 +363,7 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
@Override @Override
public void uninitialize() { public void uninitialize() {
call("uninitialize"); base.uninitialize();
UIManager.removePropertyChangeListener(this); UIManager.removePropertyChangeListener(this);
DarkPopupMenuUI.MouseGrabber mouseGrabber = DarkPopupMenuUI.getMouseGrabber(); DarkPopupMenuUI.MouseGrabber mouseGrabber = DarkPopupMenuUI.getMouseGrabber();
if (mouseGrabber != null) { if (mouseGrabber != null) {
@ -375,63 +371,16 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
} }
} }
@Override
protected void initClassDefaults(final UIDefaults defaults) {
callInit("initClassDefaults", defaults);
}
@Override @Override
public boolean isNativeLookAndFeel() { public boolean isNativeLookAndFeel() {
return true; return true;
} }
@Override
protected void initSystemColorDefaults(final UIDefaults defaults) {
callInit("initSystemColorDefaults", defaults);
}
@Override
protected void loadSystemColors(final UIDefaults defaults, final String[] systemColors,
final boolean useNative) {
try {
final Method superMethod = BasicLookAndFeel.class.getDeclaredMethod(
"loadSystemColors", UIDefaults.class, String[].class, boolean.class);
superMethod.setAccessible(true);
superMethod.invoke(base, defaults, systemColors, useNative);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.toString(), e.getStackTrace());
}
}
public void initComponentDefaults(final UIDefaults defaults) {
callInit("initComponentDefaults", defaults);
}
@Override @Override
public boolean isSupportedLookAndFeel() { public boolean isSupportedLookAndFeel() {
return true; return true;
} }
private void callInit(final String method, final UIDefaults defaults) {
try {
final Method superMethod = BasicLookAndFeel.class.getDeclaredMethod(method, UIDefaults.class);
superMethod.setAccessible(true);
superMethod.invoke(base, defaults);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.toString(), e.getStackTrace());
}
}
private void call(final String method) {
try {
final Method superMethod = BasicLookAndFeel.class.getDeclaredMethod(method);
superMethod.setAccessible(true);
superMethod.invoke(base);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.toString(), e.getStackTrace());
}
}
@Override @Override
public void propertyChange(final PropertyChangeEvent evt) { public void propertyChange(final PropertyChangeEvent evt) {
if (LOOK_AND_FEEL_PROPERTY.equals(evt.getPropertyName())) { if (LOOK_AND_FEEL_PROPERTY.equals(evt.getPropertyName())) {

52
core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFileChooserUIBridge.java

@ -26,7 +26,6 @@ package com.github.weisj.darklaf.ui.filechooser;
import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyKey;
import sun.awt.shell.ShellFolder; import sun.awt.shell.ShellFolder;
import sun.swing.FilePane; import sun.swing.FilePane;
import sun.swing.SwingUtilities2;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
@ -173,36 +172,45 @@ public abstract class DarkFileChooserUIBridge extends BasicFileChooserUI {
Locale l = fc.getLocale(); Locale l = fc.getLocale();
lookInLabelMnemonic = getMnemonic("FileChooser.lookInLabelMnemonic", l); UIDefaults defaults = MetalUIDefaults.DEFAULTS;
lookInLabelText = UIManager.getString("FileChooser.lookInLabelText", l);
saveInLabelText = UIManager.getString("FileChooser.saveInLabelText", l);
fileNameLabelMnemonic = getMnemonic("FileChooser.fileNameLabelMnemonic", l); lookInLabelMnemonic = getMnemonic(defaults, "FileChooser.lookInLabelMnemonic", l);
fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText", l); lookInLabelText = defaults.getString("FileChooser.lookInLabelText", l);
folderNameLabelMnemonic = getMnemonic("FileChooser.folderNameLabelMnemonic", l); saveInLabelText = defaults.getString("FileChooser.saveInLabelText", l);
folderNameLabelText = UIManager.getString("FileChooser.folderNameLabelText", l);
filesOfTypeLabelMnemonic = getMnemonic("FileChooser.filesOfTypeLabelMnemonic", l); fileNameLabelMnemonic = getMnemonic(defaults, "FileChooser.fileNameLabelMnemonic", l);
filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText", l); fileNameLabelText = defaults.getString("FileChooser.fileNameLabelText", l);
folderNameLabelMnemonic = getMnemonic(defaults, "FileChooser.folderNameLabelMnemonic", l);
folderNameLabelText = defaults.getString("FileChooser.folderNameLabelText", l);
upFolderToolTipText = UIManager.getString("FileChooser.upFolderToolTipText", l); filesOfTypeLabelMnemonic = getMnemonic(defaults, "FileChooser.filesOfTypeLabelMnemonic", l);
upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName", l); filesOfTypeLabelText = defaults.getString("FileChooser.filesOfTypeLabelText", l);
homeFolderToolTipText = UIManager.getString("FileChooser.homeFolderToolTipText", l); upFolderToolTipText = defaults.getString("FileChooser.upFolderToolTipText", l);
homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName", l); upFolderAccessibleName = defaults.getString("FileChooser.upFolderAccessibleName", l);
newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText", l); homeFolderToolTipText = defaults.getString("FileChooser.homeFolderToolTipText", l);
newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName", l); homeFolderAccessibleName = defaults.getString("FileChooser.homeFolderAccessibleName", l);
listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText", l); newFolderToolTipText = defaults.getString("FileChooser.newFolderToolTipText", l);
listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName", l); newFolderAccessibleName = defaults.getString("FileChooser.newFolderAccessibleName", l);
detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText", l); listViewButtonToolTipText = defaults.getString("FileChooser.listViewButtonToolTipText", l);
detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName", l); listViewButtonAccessibleName = defaults.getString("FileChooser.listViewButtonAccessibleName", l);
detailsViewButtonToolTipText = defaults.getString("FileChooser.detailsViewButtonToolTipText", l);
detailsViewButtonAccessibleName = defaults.getString("FileChooser.detailsViewButtonAccessibleName", l);
} }
protected Integer getMnemonic(final String key, final Locale l) { protected Integer getMnemonic(UIDefaults defaults, final String key, final Locale l) {
return SwingUtilities2.getUIDefaultsInt(key, l); Object value = defaults.get(key, l);
if (value instanceof Integer) {
return (Integer) value;
}
if (value instanceof String) {
return Integer.parseInt((String) value);
}
return 0;
} }
/* /*

13
core/src/main/java/com/github/weisj/darklaf/ui/filechooser/DarkFilePaneUIBridge.java

@ -455,19 +455,20 @@ public abstract class DarkFilePaneUIBridge extends JPanel implements PropertyCha
readOnly = UIManager.getBoolean("FileChooser.readOnly"); readOnly = UIManager.getBoolean("FileChooser.readOnly");
// TUDU: On windows, get the following localized strings from the OS // TUDU: On windows, get the following localized strings from the OS
UIDefaults metal = MetalUIDefaults.DEFAULTS;
viewMenuLabelText = viewMenuLabelText =
UIManager.getString("FileChooser.viewMenuLabelText", l); metal.getString("FileChooser.viewMenuLabelText", l);
refreshActionLabelText = refreshActionLabelText =
UIManager.getString("FileChooser.refreshActionLabelText", l); metal.getString("FileChooser.refreshActionLabelText", l);
newFolderActionLabelText = newFolderActionLabelText =
UIManager.getString("FileChooser.newFolderActionLabelText", l); metal.getString("FileChooser.newFolderActionLabelText", l);
viewTypeActionNames = new String[VIEWTYPE_COUNT]; viewTypeActionNames = new String[VIEWTYPE_COUNT];
viewTypeActionNames[VIEWTYPE_LIST] = viewTypeActionNames[VIEWTYPE_LIST] =
UIManager.getString("FileChooser.listViewActionLabelText", l); metal.getString("FileChooser.listViewActionLabelText", l);
viewTypeActionNames[VIEWTYPE_DETAILS] = viewTypeActionNames[VIEWTYPE_DETAILS] =
UIManager.getString("FileChooser.detailsViewActionLabelText", l); metal.getString("FileChooser.detailsViewActionLabelText", l);
kiloByteString = UIManager.getString("FileChooser.fileSizeKiloBytes", l); kiloByteString = UIManager.getString("FileChooser.fileSizeKiloBytes", l);
megaByteString = UIManager.getString("FileChooser.fileSizeMegaBytes", l); megaByteString = UIManager.getString("FileChooser.fileSizeMegaBytes", l);
@ -602,7 +603,7 @@ public abstract class DarkFilePaneUIBridge extends JPanel implements PropertyCha
String title = dataItem.getTitle(); String title = dataItem.getTitle();
if (title != null && title.startsWith("FileChooser.") && title.endsWith("HeaderText")) { if (title != null && title.startsWith("FileChooser.") && title.endsWith("HeaderText")) {
// the column must have a string resource that we try to get // the column must have a string resource that we try to get
String uiTitle = UIManager.getString(title, table.getLocale()); String uiTitle = MetalUIDefaults.DEFAULTS.getString(title, table.getLocale());
if (uiTitle != null) { if (uiTitle != null) {
title = uiTitle; title = uiTitle;
} }

32
core/src/main/java/com/github/weisj/darklaf/ui/filechooser/MetalUIDefaults.java

@ -0,0 +1,32 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.weisj.darklaf.ui.filechooser;
import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
// Extra class is for lazy initialization
class MetalUIDefaults {
static final UIDefaults DEFAULTS = new MetalLookAndFeel().getDefaults();
}
Loading…
Cancel
Save