Example usage for javax.swing UIManager getLookAndFeelDefaults

List of usage examples for javax.swing UIManager getLookAndFeelDefaults

Introduction

In this page you can find the example usage for javax.swing UIManager getLookAndFeelDefaults.

Prototype

public static UIDefaults getLookAndFeelDefaults() 

Source Link

Document

Returns the UIDefaults from the current look and feel, that were obtained at the time the look and feel was installed.

Usage

From source file:org.apache.log4j.chainsaw.LogUI.java

private static void loadLookAndFeelUsingPluginClassLoader(String lookAndFeelClassName) {
    ClassLoader classLoader = PluginClassLoaderFactory.getInstance().getClassLoader();
    ClassLoader previousTCCL = Thread.currentThread().getContextClassLoader();
    try {/*from  w ww .j av a  2  s .  c  o m*/
        // we temporarily swap the TCCL so that plugins can find resources
        Thread.currentThread().setContextClassLoader(classLoader);
        UIManager.setLookAndFeel(lookAndFeelClassName);
        UIManager.getLookAndFeelDefaults().put("ClassLoader", classLoader);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // now switch it back...
        Thread.currentThread().setContextClassLoader(previousTCCL);
    }
}

From source file:org.kuali.test.ui.base.BaseTreeCellRenderer.java

/**
 *
 *///  ww  w .  j  a  va 2  s  . c om
protected void setSelectionDisplay() {
    if (this.selected) {
        super.setBackground(getBackgroundSelectionColor());
        setForeground(getTextSelectionColor());

        if (this.hasFocus) {
            setBorderSelectionColor(UIManager.getLookAndFeelDefaults().getColor("Tree.selectionBorderColor"));
        } else {
            setBorderSelectionColor(null);
        }
    } else {
        setBackground(getBackgroundNonSelectionColor());
        setForeground(getTextNonSelectionColor());
        setBorderSelectionColor(null);
    }
}

From source file:org.notebook.gui.widget.LookAndFeelSelector.java

private static void fixFontBug() {
    int sizeOffset = 0;
    Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
    while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof Font) {
            Font oldFont = (Font) value;
            // logger.info(oldFont.getName());
            Font newFont = new Font("Dialog", oldFont.getStyle(), oldFont.getSize() + sizeOffset);
            UIManager.put(key, newFont);
        }/*from w w w. ja  va  2s . c om*/
    }
}

From source file:org.pgptool.gui.ui.tools.UiUtils.java

public static void setLookAndFeel() {
    // NOTE: We doing it this way to prevent dead=locks that is sometimes
    // happens if do it in main thread
    Edt.invokeOnEdtAndWait(new Runnable() {
        @Override/*from  w  ww .  java 2s. co m*/
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                fixCheckBoxMenuItemForeground();
                fixFontSize();
            } catch (Throwable t) {
                log.error("Failed to set L&F", t);
            }
        }

        /**
         * In some cases (depends on OS theme) check menu item foreground is same as
         * bacground - thus it;'s invisible when cheked
         */
        private void fixCheckBoxMenuItemForeground() {
            UIDefaults defaults = UIManager.getDefaults();
            Color selectionForeground = defaults.getColor("CheckBoxMenuItem.selectionForeground");
            Color foreground = defaults.getColor("CheckBoxMenuItem.foreground");
            Color background = defaults.getColor("CheckBoxMenuItem.background");
            if (colorsDiffPercentage(selectionForeground, background) < 10) {
                // TODO: That doesn't actually affect defaults. Need to find out how to fix it
                defaults.put("CheckBoxMenuItem.selectionForeground", foreground);
            }
        }

        private int colorsDiffPercentage(Color c1, Color c2) {
            int diffRed = Math.abs(c1.getRed() - c2.getRed());
            int diffGreen = Math.abs(c1.getGreen() - c2.getGreen());
            int diffBlue = Math.abs(c1.getBlue() - c2.getBlue());

            float pctDiffRed = (float) diffRed / 255;
            float pctDiffGreen = (float) diffGreen / 255;
            float pctDiffBlue = (float) diffBlue / 255;

            return (int) ((pctDiffRed + pctDiffGreen + pctDiffBlue) / 3 * 100);
        }

        private void fixFontSize() {
            if (isJreHandlesScaling()) {
                log.info("JRE handles font scaling, won't change it");
                return;
            }
            log.info("JRE doesnt't seem to support font scaling");

            Toolkit toolkit = Toolkit.getDefaultToolkit();
            int dpi = toolkit.getScreenResolution();
            if (dpi == 96) {
                if (log.isDebugEnabled()) {
                    Font font = UIManager.getDefaults().getFont("TextField.font");
                    String current = font != null ? Integer.toString(font.getSize()) : "unknown";
                    log.debug(
                            "Screen dpi seem to be 96. Not going to change font size. Btw current size seem to be "
                                    + current);
                }
                return;
            }
            int targetFontSize = 12 * dpi / 96;
            log.debug("Screen dpi = " + dpi + ", decided to change font size to " + targetFontSize);
            setDefaultSize(targetFontSize);
        }

        private boolean isJreHandlesScaling() {
            try {
                JreVersion noNeedToScaleForVer = JreVersion.parseString("9");
                String jreVersionStr = System.getProperty("java.version");
                if (jreVersionStr != null) {
                    JreVersion curVersion = JreVersion.parseString(jreVersionStr);
                    if (noNeedToScaleForVer.compareTo(curVersion) <= 0) {
                        return true;
                    }
                }

                return false;
            } catch (Throwable t) {
                log.warn("Failed to see oif JRE can handle font scaling. Will assume it does. JRE version: "
                        + System.getProperty("java.version"), t);
                return true;
            }
        }

        public void setDefaultSize(int size) {
            Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
            Object[] keys = keySet.toArray(new Object[keySet.size()]);
            for (Object key : keys) {
                if (key != null && key.toString().toLowerCase().contains("font")) {
                    Font font = UIManager.getDefaults().getFont(key);
                    if (font != null) {
                        Font changedFont = font.deriveFont((float) size);
                        UIManager.put(key, changedFont);
                        Font doubleCheck = UIManager.getDefaults().getFont(key);
                        log.debug("Font size changed for " + key + ". From " + font.getSize() + " to "
                                + doubleCheck.getSize());
                    }
                }
            }
        }
    });
    log.info("L&F set");
}

From source file:se.trixon.jota.client.ui.MainFrame.java

private void loadClientOption(ClientOptionsEvent clientOptionEvent) {
    switch (clientOptionEvent) {
    case LOOK_AND_FEEL:
        if (mOptions.isForceLookAndFeel()) {
            SwingUtilities.invokeLater(() -> {
                try {
                    UIManager.setLookAndFeel(SwingHelper.getLookAndFeelClassName(mOptions.getLookAndFeel()));
                    SwingUtilities.updateComponentTreeUI(MainFrame.this);
                    SwingUtilities.updateComponentTreeUI(sPopupMenu);

                    if (mOptions.getLookAndFeel().equalsIgnoreCase("Darcula")) {
                        int iconSize = 32;
                        UIDefaults uiDefaults = UIManager.getLookAndFeelDefaults();
                        uiDefaults.put("OptionPane.informationIcon",
                                MaterialIcon.Action.INFO_OUTLINE.get(iconSize, IconColor.WHITE));
                        uiDefaults.put("OptionPane.errorIcon",
                                MaterialIcon.Alert.ERROR_OUTLINE.get(iconSize, IconColor.WHITE));
                        uiDefaults.put("OptionPane.questionIcon",
                                MaterialIcon.Action.HELP_OUTLINE.get(iconSize, IconColor.WHITE));
                        uiDefaults.put("OptionPane.warningIcon",
                                MaterialIcon.Alert.WARNING.get(iconSize, IconColor.WHITE));
                    }//  ww  w .j  ava  2s.co m
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                        | UnsupportedLookAndFeelException ex) {
                    //Xlog.timedErr(ex.getMessage());
                }
            });
        }
        break;

    case MENU_ICONS:
        ActionMap actionMap = getRootPane().getActionMap();
        for (Object allKey : actionMap.allKeys()) {
            Action action = actionMap.get(allKey);
            Icon icon = null;
            if (mOptions.isDisplayMenuIcons()) {
                icon = (Icon) action.getValue(ActionManager.JOTA_SMALL_ICON_KEY);
            }
            action.putValue(Action.SMALL_ICON, icon);
        }
        break;

    default:
        throw new AssertionError();
    }

}

From source file:shuffle.fwk.ShuffleController.java

/**
 * Creates a ShuffleController with the given configuration paths for the primary configuration
 * (which tells other managers where to get their configurations). If there are none passed, then
 * "config/main.txt" is assumed./*ww w. j  ava 2  s . c  om*/
 * 
 * @param configPaths
 *           The paths as Strings
 */
public ShuffleController(String... configPaths) {
    if (configPaths.length > 0 && configPaths[0] != null) {
        factory = new ConfigFactory(configPaths[0]);
    } else {
        factory = new ConfigFactory();
    }
    Integer menuFontOverride = getPreferencesManager().getIntegerValue(KEY_FONT_SIZE_SCALING);
    if (menuFontOverride != null && menuFontOverride != 100 && menuFontOverride >= 1
            && menuFontOverride <= 10000) {
        float scale = menuFontOverride.floatValue() / 100.0f;
        try {
            // This is the cleanest and most bug-free way to do this hack.
            Set<Object> allKeys = new HashSet<Object>();
            allKeys.add("JMenu.font");
            // Yes we're not supposed to use this, but it is the only one that works with Nimbus LAF
            allKeys.addAll(UIManager.getLookAndFeelDefaults().keySet());
            Object value = UIManager.get("defaultFont");
            if (value != null && value instanceof FontUIResource) {
                FontUIResource fromFont = (javax.swing.plaf.FontUIResource) value;
                FontUIResource toFont = new FontUIResource(fromFont.deriveFont(fromFont.getSize() * scale));
                // This one is necessary
                UIManager.getLookAndFeel().getDefaults().put("defaultFont", toFont);
                // And this one allows other LAF to be used in the future
                UIManager.getDefaults().put("defaultFont", toFont);
            }

            // Needed for Nimbus's JTable row height adjustment
            Object tableFontValue = UIManager.getLookAndFeel().getDefaults().get("Table.font");
            Number bestRowHeight = null;
            if (tableFontValue != null && tableFontValue instanceof FontUIResource) {
                FontUIResource fromFont = (FontUIResource) tableFontValue;
                bestRowHeight = fromFont.getSize();
            }
            Object rowHeightValue = UIManager.getLookAndFeel().getDefaults().get("Table.rowHeight");
            if (rowHeightValue != null && rowHeightValue instanceof Number) {
                Number rowHeight = (Number) rowHeightValue;
                rowHeight = rowHeight.doubleValue() * scale;
                if (bestRowHeight == null || bestRowHeight.intValue() < rowHeight.intValue()) {
                    bestRowHeight = rowHeight;
                }
            }
            if (bestRowHeight != null) {
                bestRowHeight = bestRowHeight.doubleValue() * (4.0 / 3.0);
            }
            if (bestRowHeight != null && bestRowHeight.intValue() > 0) {
                UIManager.getLookAndFeel().getDefaults().put("Table.rowHeight", bestRowHeight.intValue());
            }
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            LOG.log(Level.SEVERE, "Cannot override menu font sizes!", e);
        }
    }
    try {
        setModel(new ShuffleModel(this));
        setView(new ShuffleView(this));
        getModel().checkLocaleConfig();
        loadFrame();
    } catch (Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        LOG.log(Level.SEVERE, "Failure on start:", e);
    }
}