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.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.reflect.Method;
import java.text.AttributedCharacterIterator;
import java.util.Collections;
import java.util.HashMap;
@ -105,12 +104,9 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
@Override
public UIDefaults getDefaults() {
final UIDefaults metalDefaults = new MetalLookAndFeel().getDefaults();
final UIDefaults defaults = base.getDefaults();
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);
loadThemeDefaults(defaults);
initIdeaDefaults(defaults);
@ -130,7 +126,7 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.toString(), e.getStackTrace());
}
return super.getDefaults();
return 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.
* This is disadvantageous for the behaviour of custom tooltips.
*/
call("initialize");
base.initialize();
PopupFactory.setSharedInstance(new DarkPopupFactory());
PropertyLoader.reset();
UIManager.addPropertyChangeListener(this);
@ -367,7 +363,7 @@ public class DarkLaf extends BasicLookAndFeel implements PropertyChangeListener
@Override
public void uninitialize() {
call("uninitialize");
base.uninitialize();
UIManager.removePropertyChangeListener(this);
DarkPopupMenuUI.MouseGrabber mouseGrabber = DarkPopupMenuUI.getMouseGrabber();
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
public boolean isNativeLookAndFeel() {
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
public boolean isSupportedLookAndFeel() {
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
public void propertyChange(final PropertyChangeEvent evt) {
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 sun.awt.shell.ShellFolder;
import sun.swing.FilePane;
import sun.swing.SwingUtilities2;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
@ -173,36 +172,45 @@ public abstract class DarkFileChooserUIBridge extends BasicFileChooserUI {
Locale l = fc.getLocale();
lookInLabelMnemonic = getMnemonic("FileChooser.lookInLabelMnemonic", l);
lookInLabelText = UIManager.getString("FileChooser.lookInLabelText", l);
saveInLabelText = UIManager.getString("FileChooser.saveInLabelText", l);
UIDefaults defaults = MetalUIDefaults.DEFAULTS;
fileNameLabelMnemonic = getMnemonic("FileChooser.fileNameLabelMnemonic", l);
fileNameLabelText = UIManager.getString("FileChooser.fileNameLabelText", l);
folderNameLabelMnemonic = getMnemonic("FileChooser.folderNameLabelMnemonic", l);
folderNameLabelText = UIManager.getString("FileChooser.folderNameLabelText", l);
lookInLabelMnemonic = getMnemonic(defaults, "FileChooser.lookInLabelMnemonic", l);
lookInLabelText = defaults.getString("FileChooser.lookInLabelText", l);
saveInLabelText = defaults.getString("FileChooser.saveInLabelText", l);
filesOfTypeLabelMnemonic = getMnemonic("FileChooser.filesOfTypeLabelMnemonic", l);
filesOfTypeLabelText = UIManager.getString("FileChooser.filesOfTypeLabelText", l);
fileNameLabelMnemonic = getMnemonic(defaults, "FileChooser.fileNameLabelMnemonic", 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);
upFolderAccessibleName = UIManager.getString("FileChooser.upFolderAccessibleName", l);
filesOfTypeLabelMnemonic = getMnemonic(defaults, "FileChooser.filesOfTypeLabelMnemonic", l);
filesOfTypeLabelText = defaults.getString("FileChooser.filesOfTypeLabelText", l);
homeFolderToolTipText = UIManager.getString("FileChooser.homeFolderToolTipText", l);
homeFolderAccessibleName = UIManager.getString("FileChooser.homeFolderAccessibleName", l);
upFolderToolTipText = defaults.getString("FileChooser.upFolderToolTipText", l);
upFolderAccessibleName = defaults.getString("FileChooser.upFolderAccessibleName", l);
newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText", l);
newFolderAccessibleName = UIManager.getString("FileChooser.newFolderAccessibleName", l);
homeFolderToolTipText = defaults.getString("FileChooser.homeFolderToolTipText", l);
homeFolderAccessibleName = defaults.getString("FileChooser.homeFolderAccessibleName", l);
listViewButtonToolTipText = UIManager.getString("FileChooser.listViewButtonToolTipText", l);
listViewButtonAccessibleName = UIManager.getString("FileChooser.listViewButtonAccessibleName", l);
newFolderToolTipText = defaults.getString("FileChooser.newFolderToolTipText", l);
newFolderAccessibleName = defaults.getString("FileChooser.newFolderAccessibleName", l);
detailsViewButtonToolTipText = UIManager.getString("FileChooser.detailsViewButtonToolTipText", l);
detailsViewButtonAccessibleName = UIManager.getString("FileChooser.detailsViewButtonAccessibleName", l);
listViewButtonToolTipText = defaults.getString("FileChooser.listViewButtonToolTipText", 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) {
return SwingUtilities2.getUIDefaultsInt(key, l);
protected Integer getMnemonic(UIDefaults defaults, final String key, final Locale 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");
// TUDU: On windows, get the following localized strings from the OS
UIDefaults metal = MetalUIDefaults.DEFAULTS;
viewMenuLabelText =
UIManager.getString("FileChooser.viewMenuLabelText", l);
metal.getString("FileChooser.viewMenuLabelText", l);
refreshActionLabelText =
UIManager.getString("FileChooser.refreshActionLabelText", l);
metal.getString("FileChooser.refreshActionLabelText", l);
newFolderActionLabelText =
UIManager.getString("FileChooser.newFolderActionLabelText", l);
metal.getString("FileChooser.newFolderActionLabelText", l);
viewTypeActionNames = new String[VIEWTYPE_COUNT];
viewTypeActionNames[VIEWTYPE_LIST] =
UIManager.getString("FileChooser.listViewActionLabelText", l);
metal.getString("FileChooser.listViewActionLabelText", l);
viewTypeActionNames[VIEWTYPE_DETAILS] =
UIManager.getString("FileChooser.detailsViewActionLabelText", l);
metal.getString("FileChooser.detailsViewActionLabelText", l);
kiloByteString = UIManager.getString("FileChooser.fileSizeKiloBytes", l);
megaByteString = UIManager.getString("FileChooser.fileSizeMegaBytes", l);
@ -602,7 +603,7 @@ public abstract class DarkFilePaneUIBridge extends JPanel implements PropertyCha
String title = dataItem.getTitle();
if (title != null && title.startsWith("FileChooser.") && title.endsWith("HeaderText")) {
// 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) {
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