📄 synthlookandfeel.java
字号:
/** * Returns the defaults for this SynthLookAndFeel. * * @return Defaults table. */ @Override public UIDefaults getDefaults() { UIDefaults table = new UIDefaults(60, 0.75f); Region.registerUIs(table); table.setDefaultLocale(Locale.getDefault()); table.addResourceBundle( "com.sun.swing.internal.plaf.basic.resources.basic" ); table.addResourceBundle("com.sun.swing.internal.plaf.synth.resources.synth"); // SynthTabbedPaneUI supports rollover on tabs, GTK does not table.put("TabbedPane.isTabRollover", Boolean.TRUE); // These need to be defined for JColorChooser to work. table.put("ColorChooser.swatchesRecentSwatchSize", new Dimension(10, 10)); table.put("ColorChooser.swatchesDefaultRecentColor", Color.RED); table.put("ColorChooser.swatchesSwatchSize", new Dimension(10, 10)); // These are needed for PopupMenu. table.put("PopupMenu.selectedWindowInputMapBindings", new Object[] { "ESCAPE", "cancel", "DOWN", "selectNext", "KP_DOWN", "selectNext", "UP", "selectPrevious", "KP_UP", "selectPrevious", "LEFT", "selectParent", "KP_LEFT", "selectParent", "RIGHT", "selectChild", "KP_RIGHT", "selectChild", "ENTER", "return", "SPACE", "return" }); table.put("PopupMenu.selectedWindowInputMapBindings.RightToLeft", new Object[] { "LEFT", "selectChild", "KP_LEFT", "selectChild", "RIGHT", "selectParent", "KP_RIGHT", "selectParent", }); // enabled antialiasing depending on desktop settings flushUnreferenced(); Object aaTextInfo = getAATextInfo(); table.put(SwingUtilities2.AA_TEXT_PROPERTY_KEY, aaTextInfo); new AATextListener(this); if (defaultsMap != null) { table.putAll(defaultsMap); } return table; } /** * Returns true, SynthLookAndFeel is always supported. * * @return true. */ @Override public boolean isSupportedLookAndFeel() { return true; } /** * Returns false, SynthLookAndFeel is not a native look and feel. * * @return false */ @Override public boolean isNativeLookAndFeel() { return false; } /** * Returns a textual description of SynthLookAndFeel. * * @return textual description of synth. */ @Override public String getDescription() { return "Synth look and feel"; } /** * Return a short string that identifies this look and feel. * * @return a short string identifying this look and feel. */ @Override public String getName() { return "Synth look and feel"; } /** * Return a string that identifies this look and feel. * * @return a short string identifying this look and feel. */ @Override public String getID() { return "Synth"; } /** * Returns whether or not the UIs should update their * <code>SynthStyles</code> from the <code>SynthStyleFactory</code> * when the ancestor of the <code>JComponent</code> changes. A subclass * that provided a <code>SynthStyleFactory</code> that based the * return value from <code>getStyle</code> off the containment hierarchy * would override this method to return true. * * @return whether or not the UIs should update their * <code>SynthStyles</code> from the <code>SynthStyleFactory</code> * when the ancestor changed. */ public boolean shouldUpdateStyleOnAncestorChanged() { return false; } /** * Returns the antialiasing information as specified by the host desktop. * Antialiasing might be forced off if the desktop is GNOME and the user * has set his locale to Chinese, Japanese or Korean. This is consistent * with what GTK does. See com.sun.java.swing.plaf.gtk.GtkLookAndFeel * for more information about CJK and antialiased fonts. * * @return the text antialiasing information associated to the desktop */ private static Object getAATextInfo() { String language = Locale.getDefault().getLanguage(); String desktop = (String) AccessController.doPrivileged(new GetPropertyAction("sun.desktop")); boolean isCjkLocale = (Locale.CHINESE.getLanguage().equals(language) || Locale.JAPANESE.getLanguage().equals(language) || Locale.KOREAN.getLanguage().equals(language)); boolean isGnome = "gnome".equals(desktop); boolean isLocal = SwingUtilities2.isLocalDisplay(); boolean setAA = isLocal && (!isGnome || !isCjkLocale); Object aaTextInfo = SwingUtilities2.AATextInfo.getAATextInfo(setAA); return aaTextInfo; } private static ReferenceQueue queue = new ReferenceQueue(); private static void flushUnreferenced() { AATextListener aatl; while ((aatl = (AATextListener) queue.poll()) != null) { aatl.dispose(); } } private static class AATextListener extends WeakReference implements PropertyChangeListener { private String key = SunToolkit.DESKTOPFONTHINTS; AATextListener(LookAndFeel laf) { super(laf, queue); Toolkit tk = Toolkit.getDefaultToolkit(); tk.addPropertyChangeListener(key, this); } @Override public void propertyChange(PropertyChangeEvent pce) { UIDefaults defaults = UIManager.getLookAndFeelDefaults(); if (defaults.getBoolean("Synth.doNotSetTextAA")) { dispose(); return; } LookAndFeel laf = (LookAndFeel) get(); if (laf == null || laf != UIManager.getLookAndFeel()) { dispose(); return; } Object aaTextInfo = getAATextInfo(); defaults.put(SwingUtilities2.AA_TEXT_PROPERTY_KEY, aaTextInfo); updateUI(); } void dispose() { Toolkit tk = Toolkit.getDefaultToolkit(); tk.removePropertyChangeListener(key, this); } /** * Updates the UI of the passed in window and all its children. */ private static void updateWindowUI(Window window) { updateStyles(window); Window ownedWins[] = window.getOwnedWindows(); for (int i = 0; i < ownedWins.length; i++) { updateWindowUI(ownedWins[i]); } } /** * Updates the UIs of all the known Frames. */ private static void updateAllUIs() { Frame appFrames[] = Frame.getFrames(); for (int i = 0; i < appFrames.length; i++) { updateWindowUI(appFrames[i]); } } /** * Indicates if an updateUI call is pending. */ private static boolean updatePending; /** * Sets whether or not an updateUI call is pending. */ private static synchronized void setUpdatePending(boolean update) { updatePending = update; } /** * Returns true if a UI update is pending. */ private static synchronized boolean isUpdatePending() { return updatePending; } protected void updateUI() { if (!isUpdatePending()) { setUpdatePending(true); Runnable uiUpdater = new Runnable() { @Override public void run() { updateAllUIs(); setUpdatePending(false); } }; SwingUtilities.invokeLater(uiUpdater); } } } private void writeObject(java.io.ObjectOutputStream out) throws IOException { throw new NotSerializableException(this.getClass().getName()); } private class Handler implements PropertyChangeListener { @Override public void propertyChange(PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); Object newValue = evt.getNewValue(); Object oldValue = evt.getOldValue(); if ("focusOwner" == propertyName) { if (oldValue instanceof JComponent) { repaintIfBackgroundsDiffer((JComponent)oldValue); } if (newValue instanceof JComponent) { repaintIfBackgroundsDiffer((JComponent)newValue); } } else if ("managingFocus" == propertyName) { // De-register listener on old keyboard focus manager and // register it on the new one. KeyboardFocusManager manager = (KeyboardFocusManager)evt.getSource(); if (((Boolean)newValue).equals(Boolean.FALSE)) { manager.removePropertyChangeListener(_handler); } else { manager.addPropertyChangeListener(_handler); } } } /** * This is a support method that will check if the background colors of * the specified component differ between focused and unfocused states. * If the color differ the component will then repaint itself. * * @comp the component to check */ private void repaintIfBackgroundsDiffer(JComponent comp) { ComponentUI ui = (ComponentUI)comp.getClientProperty( SwingUtilities2.COMPONENT_UI_PROPERTY_KEY); if (ui instanceof SynthUI) { SynthUI synthUI = (SynthUI)ui; SynthContext context = synthUI.getContext(comp); SynthStyle style = context.getStyle(); int state = context.getComponentState(); // Get the current background color. Color currBG = style.getColor(context, ColorType.BACKGROUND); // Get the last background color. state ^= SynthConstants.FOCUSED; context.setComponentState(state); Color lastBG = style.getColor(context, ColorType.BACKGROUND); // Reset the component state back to original. state ^= SynthConstants.FOCUSED; context.setComponentState(state); // Repaint the component if the backgrounds differed. if (currBG != null && !currBG.equals(lastBG)) { comp.repaint(); } context.dispose(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -