public Color getColor () { SynthStyle style = getSynthStyle (aRegion); if (Boolean.TRUE.equals(functioning)) { try { Color result = (Color) synthStyle_getColorForState.invoke (style, new Object [] { getSynthContext (), aColorType }); if (result == null) { result = (Color) fallback; } if (darken) { result = result.darker(); } return result; } catch (Exception e) { functioning = Boolean.FALSE; if (log) { e.printStackTrace(); } } } //This will only happen once, after which functioning will be false return null; }
private static void fixGtkPopupStyle() { if (!UIUtil.isUnderGTKLookAndFeel()) return; final SynthStyleFactory original = SynthLookAndFeel.getStyleFactory(); SynthLookAndFeel.setStyleFactory(new SynthStyleFactory() { @Override public SynthStyle getStyle(final JComponent c, final Region id) { final SynthStyle style = original.getStyle(c, id); if (id == Region.POPUP_MENU) { final Integer x = ReflectionUtil.getField(style.getClass(), style, int.class, "xThickness"); if (x != null && x == 0) { // workaround for Sun bug #6636964 ReflectionUtil.setField(style.getClass(), style, int.class, "xThickness", 1); ReflectionUtil.setField(style.getClass(), style, int.class, "yThickness", 3); } } return style; } }); new JBPopupMenu(); // invokes updateUI() -> updateStyle() SynthLookAndFeel.setStyleFactory(original); }
private static void patchGtkDefaults(UIDefaults defaults) { if (!UIUtil.isUnderGTKLookAndFeel()) return; Map<String, Icon> map = ContainerUtil.newHashMap( Arrays.asList("OptionPane.errorIcon", "OptionPane.informationIcon", "OptionPane.warningIcon", "OptionPane.questionIcon"), Arrays.asList(AllIcons.General.ErrorDialog, AllIcons.General.InformationDialog, AllIcons.General.WarningDialog, AllIcons.General.QuestionDialog)); // GTK+ L&F keeps icons hidden in style SynthStyle style = SynthLookAndFeel.getStyle(new JOptionPane(""), Region.DESKTOP_ICON); for (String key : map.keySet()) { if (defaults.get(key) != null) continue; Object icon = style == null ? null : style.get(null, key); defaults.put(key, icon instanceof Icon ? icon : map.get(key)); } Color fg = defaults.getColor("Label.foreground"); Color bg = defaults.getColor("Label.background"); if (fg != null && bg != null) { defaults.put("Label.disabledForeground", UIUtil.mix(fg, bg, 0.5)); } }
/** * A convenience method that handles painting of the background. All SynthUI * implementations should override update and invoke this method. * * @param context must not be null * @param g must not be null * @param the bounds to fill, may be null to indicate the complete size */ public static void update(SynthContext context, Graphics g, Rectangle bounds) { JComponent c = context.getComponent(); SynthStyle style = context.getStyle(); int x, y, width, height; if (bounds == null) { x = 0; y = 0; width = c.getWidth(); height = c.getHeight(); } else { x = bounds.x; y = bounds.y; width = bounds.width; height = bounds.height; } // Fill in the background, if necessary. boolean subregion = context.getRegion().isSubregion(); if ((subregion && style.isOpaque(context)) || (!subregion && c.isOpaque())) { g.setColor(style.getColor(context, ColorType.BACKGROUND)); g.fillRect(x, y, width, height); } }
/** * Called by UIManager when this look and feel is installed. */ @Override public void initialize() { super.initialize(); // create synth style factory setStyleFactory(new SynthStyleFactory() { @Override public SynthStyle getStyle(JComponent c, Region r) { SynthStyle style = getSeaGlassStyle(c, r); if (!(style instanceof SeaGlassStyle)) { style = new SeaGlassStyleWrapper(style); } return style; } }); }
/** * A convience method that will reset the Style of StyleContext if * necessary. * * @return newStyle */ public static SynthStyle updateSeaglassStyle(SynthContext context, SeaglassUI ui) { SynthStyle newStyle = getStyle(context.getComponent(), context.getRegion()); // TODO rossi 04.07.2011 this code is now private in the Synth L&F // SynthStyle oldStyle = context.getStyle(); // // if (newStyle != oldStyle) { // if (oldStyle != null) { // oldStyle.uninstallDefaults(context); // } // context.setStyle(newStyle); // newStyle.installDefaults(context, ui); // } return newStyle; }
/** * A convience method that will reset the Style of StyleContext if * necessary. * * @param context the SynthContext corresponding to the current state. * @param ui the UI delegate. * * @return the new, updated style. */ public static SynthStyle updateStyle(SeaGlassContext context, SeaglassUI ui) { SynthStyle newStyle = SynthLookAndFeel.getStyle(context.getComponent(), context.getRegion()); SynthStyle oldStyle = context.getStyle(); if (newStyle != oldStyle) { if (oldStyle != null) { oldStyle.uninstallDefaults(context); } context.setStyle(newStyle); if (newStyle instanceof SeaGlassStyle) { ((SeaGlassStyle) newStyle).installDefaults(context, ui); } } return newStyle; }
private void updateStyle(JTextComponent comp) { SeaGlassContext context = getContext(comp, ENABLED); SynthStyle oldStyle = style; style = SeaGlassLookAndFeel.updateStyle(context, this); if (style != oldStyle) { SeaGlassTextFieldUI.updateStyle(comp, context, getPropertyPrefix()); if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } } context.dispose(); }
private void updateStyle(JComponent c) { SeaGlassContext context = getContext(c, ENABLED); // Note: JViewport is special cased as it does not allow for // a border to be set. JViewport.setBorder is overriden to throw // an IllegalArgumentException. Refer to SynthScrollPaneUI for // details of this. SynthStyle newStyle = SynthLookAndFeel.getStyle(context.getComponent(), context.getRegion()); SynthStyle oldStyle = context.getStyle(); if (newStyle != oldStyle) { if (oldStyle != null) { oldStyle.uninstallDefaults(context); } context.setStyle(newStyle); newStyle.installDefaults(context); } this.style = newStyle; context.dispose(); }
/** * @see javax.swing.plaf.basic.BasicButtonUI#getMinimumSize(javax.swing.JComponent) */ public Dimension getMinimumSize(JComponent c) { if (c.getComponentCount() > 0 && c.getLayout() != null) { return null; } AbstractButton b = (AbstractButton) c; SeaGlassContext ss = getContext(c); final SynthStyle style2 = ss.getStyle(); Dimension size = style2.getGraphicsUtils(ss).getMinimumSize(ss, style2.getFont(ss), b.getText(), getSizingIcon(b), b.getHorizontalAlignment(), b.getVerticalAlignment(), b.getHorizontalTextPosition(), b.getVerticalTextPosition(), b.getIconTextGap(), b.getDisplayedMnemonicIndex()); ss.dispose(); return size; }
/** * @see javax.swing.plaf.basic.BasicButtonUI#getPreferredSize(javax.swing.JComponent) */ public Dimension getPreferredSize(JComponent c) { if (c.getComponentCount() > 0 && c.getLayout() != null) { return null; } AbstractButton b = (AbstractButton) c; SeaGlassContext ss = getContext(c); SynthStyle style2 = ss.getStyle(); SynthGraphicsUtils graphicsUtils = style2.getGraphicsUtils(ss); Dimension size = graphicsUtils.getPreferredSize(ss, style2.getFont(ss), b.getText(), getSizingIcon(b), b.getHorizontalAlignment(), b.getVerticalAlignment(), b.getHorizontalTextPosition(), b.getVerticalTextPosition(), b.getIconTextGap(), b.getDisplayedMnemonicIndex()); ss.dispose(); // Make height odd. size.height &= ~1; return size; }
/** * @see javax.swing.plaf.basic.BasicButtonUI#getMaximumSize(javax.swing.JComponent) */ public Dimension getMaximumSize(JComponent c) { if (c.getComponentCount() > 0 && c.getLayout() != null) { return null; } AbstractButton b = (AbstractButton) c; SeaGlassContext ss = getContext(c); final SynthStyle style2 = ss.getStyle(); Dimension size = style2.getGraphicsUtils(ss).getMaximumSize(ss, style2.getFont(ss), b.getText(), getSizingIcon(b), b.getHorizontalAlignment(), b.getVerticalAlignment(), b.getHorizontalTextPosition(), b.getVerticalTextPosition(), b.getIconTextGap(), b.getDisplayedMnemonicIndex()); ss.dispose(); return size; }
private void updateStyle(JToolBar c) { SeaGlassContext context = getContext(c, Region.TOOL_BAR_CONTENT, null, ENABLED); contentStyle = SeaGlassLookAndFeel.updateStyle(context, this); context.getComponent().setOpaque(false); context.dispose(); context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, null, ENABLED); context.getComponent().setOpaque(false); dragWindowStyle = SeaGlassLookAndFeel.updateStyle(context, this); context.dispose(); context = getContext(c, ENABLED); context.getComponent().setOpaque(false); SynthStyle oldStyle = style; style = SeaGlassLookAndFeel.updateStyle(context, this); if (oldStyle != style) { handleIcon = style.getIcon(context, "ToolBar.handleIcon"); if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } } context.dispose(); }
private void updateStyle(JComponent c) { SeaGlassContext context = getContext(c, ENABLED); Window window = SwingUtilities.getWindowAncestor(popupMenu); if (PlatformUtils.isMac() && window != null) { WindowUtils.makeWindowNonOpaque(window); } SeaGlassStyle oldStyle = style; SynthStyle s = SeaGlassLookAndFeel.updateStyle(context, this); if (s instanceof SeaGlassStyle) { style = (SeaGlassStyle) s; if (style != oldStyle) { if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } } } context.dispose(); }
private void updateStyle(JScrollPane c) { SeaGlassContext context = getContext(c, ENABLED); SynthStyle oldStyle = style; style = SeaGlassLookAndFeel.updateStyle(context, this); if (style != oldStyle) { Border vpBorder = scrollpane.getViewportBorder(); if ((vpBorder == null) || (vpBorder instanceof UIResource)) { scrollpane.setViewportBorder(new ViewportBorder(context)); } if (oldStyle != null) { uninstallKeyboardActions(c); installKeyboardActions(c); } } context.dispose(); }
private void updateStyle(JComponent c) { SeaGlassContext context = getContext(c, ENABLED); SynthStyle oldStyle = style; style = SeaGlassLookAndFeel.updateStyle(context, this); if (style != oldStyle) { Icon frameIcon = frame.getFrameIcon(); if (frameIcon == null || frameIcon instanceof UIResource) { frame.setFrameIcon(context.getStyle().getIcon(context, "InternalFrame.icon")); } if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } } context.dispose(); }
public void propertyChange(PropertyChangeEvent evt) { SynthStyle oldStyle = style; JInternalFrame f = (JInternalFrame) evt.getSource(); String prop = evt.getPropertyName(); if (SeaGlassLookAndFeel.shouldUpdateStyle(evt)) { updateStyle(f); } if (style == oldStyle && (prop == JInternalFrame.IS_MAXIMUM_PROPERTY || prop == JInternalFrame.IS_SELECTED_PROPERTY)) { // Border (and other defaults) may need to change SeaGlassContext context = getContext(f, ENABLED); style.uninstallDefaults(context); ((SeaGlassStyle) style).installDefaults(context, this); } }
/** * Create a SynthContext for the component, subregion, and state. * * @param c the component. * @param subregion the subregion. * @param state the state. * * @return the newly created SynthContext. */ private SeaGlassContext getContext(JComponent c, Region subregion, int state) { SynthStyle style = null; Class klass = SeaGlassContext.class; if (subregion == Region.TABBED_PANE_TAB) { style = tabStyle; } else if (subregion == Region.TABBED_PANE_TAB_AREA) { style = tabAreaStyle; } else if (subregion == Region.TABBED_PANE_CONTENT) { style = tabContentStyle; } else if (subregion == SeaGlassRegion.TABBED_PANE_TAB_CLOSE_BUTTON) { style = tabCloseStyle; } return SeaGlassContext.getContext(klass, c, subregion, style, state); }
private void updateStyle(JComboBox comboBox) { SeaGlassStyle oldStyle = style; SeaGlassContext context = getContext(comboBox, ENABLED); SynthStyle s = SeaGlassLookAndFeel.updateStyle(context, this); if (s instanceof SeaGlassStyle) { style = (SeaGlassStyle) s; if (style != oldStyle) { popupInsets = (Insets) style.get(context, "ComboBox.popupInsets"); useListColors = style.getBoolean(context, "ComboBox.rendererUseListColors", true); buttonWhenNotEditable = style.getBoolean(context, "ComboBox.buttonWhenNotEditable", false); pressedWhenPopupVisible = style.getBoolean(context, "ComboBox.pressedWhenPopupVisible", false); if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } forceOpaque = style.getBoolean(context, "ComboBox.forceOpaque", false); } } context.dispose(); }
private void updateStyle(JComponent c) { SeaGlassContext context = getContext(c, ENABLED); SynthStyle oldStyle = style; style = SeaGlassLookAndFeel.updateStyle(context, this); if (style != oldStyle) { minimumSize = (Dimension) style.get(context, "OptionPane.minimumSize"); if (minimumSize == null) { minimumSize = new Dimension(262, 90); } if (oldStyle != null) { uninstallKeyboardActions(); installKeyboardActions(); } } context.dispose(); }
/** * Creates a SeaGlassContext with the specified values. This is meant for * subclasses and custom UI implementors. You very rarely need to construct * a SeaGlassContext, though some methods will take one. * * @param component JComponent * @param region Identifies the portion of the JComponent * @param style Style associated with the component * @param state State of the component as defined in SynthConstants. */ public SeaGlassContext(JComponent component, Region region, SynthStyle style, int state) { super(component, region, style, state); if (component == fakeComponent) { this.component = null; this.region = null; this.style = null; return; } if (component == null || region == null || style == null) { throw new NullPointerException("You must supply a non-null component, region and style"); } reset(component, region, style, state); }
public static void paint(SynthContext context, SynthContext accContext, Graphics g, Icon checkIcon, Icon arrowIcon, String acceleratorDelimiter, int defaultTextIconGap, String propertyPrefix) { JMenuItem mi = (JMenuItem) context.getComponent(); SynthStyle style = context.getStyle(); g.setFont(style.getFont(context)); Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight()); boolean leftToRight = SeaGlassLookAndFeel.isLeftToRight(mi); applyInsets(viewRect, mi.getInsets(), leftToRight); SeaGlassMenuItemLayoutHelper lh = new SeaGlassMenuItemLayoutHelper(context, accContext, mi, checkIcon, arrowIcon, viewRect, defaultTextIconGap, acceleratorDelimiter, leftToRight, MenuItemLayoutHelper.useCheckAndArrow(mi), propertyPrefix); MenuItemLayoutHelper.LayoutResult lr = lh.layoutMenuItem(); paintMenuItem(g, lh, lr); }
/** * Gets the style. Creates it if necessary. * @return the style */ SynthStyle getStyle(JComponent c) { // if the component has overrides, it gets its own unique style // instead of the shared style. if (c.getClientProperty("Nimbus.Overrides") != null) { if (overridesCache == null) overridesCache = new WeakHashMap<JComponent, WeakReference<OpaqueNimbusStyle>>(); WeakReference<OpaqueNimbusStyle> ref = overridesCache.get(c); OpaqueNimbusStyle s = ref == null ? null : ref.get(); if (s == null) { s = new OpaqueNimbusStyle(prefix, c); overridesCache.put(c, new WeakReference<OpaqueNimbusStyle>(s)); } return s; } // lazily create the style if necessary if (style == null) style = new OpaqueNimbusStyle(prefix, null); // return the style return style; }
/** Paint bumps to specific Graphics. */ @Override public void paint (Graphics g) { Icon icon = UIManager.getIcon("ToolBar.handleIcon"); Region region = Region.TOOL_BAR; SynthStyleFactory sf = SynthLookAndFeel.getStyleFactory(); SynthStyle style = sf.getStyle(toolbar, region); SynthContext context = new SynthContext(toolbar, region, style, SynthConstants.DEFAULT); SynthGraphicsUtils sgu = context.getStyle().getGraphicsUtils(context); sgu.paintText(context, g, null, icon, SwingConstants.LEADING, SwingConstants.LEADING, 0, 0, 0, -1, 0); }
public Font getFont() { SynthStyle style = getSynthStyle (aRegion); if (Boolean.TRUE.equals(functioning)) { try { Font result = (Font) synthStyle_getFontForState.invoke (style, new Object [] { getSynthContext () }); if (result == null) { result = (Font) fallback; } return result; } catch (Exception e) { functioning = Boolean.FALSE; if (log) { e.printStackTrace(); } } } //This will only happen once, after which functioning will be false return null; }
private static void checkFunctioning() { functioning = Boolean.FALSE; try { gtkColorType = UIUtils.classForName ("com.sun.java.swing.plaf.gtk.GTKColorType"); //NOI18N synthStyle_getColorForState = SynthStyle.class.getDeclaredMethod ("getColorForState", //NOI18N SynthContext.class, ColorType.class ); synthStyle_getColorForState.setAccessible(true); synthStyle_getFontForState = SynthStyle.class.getDeclaredMethod ("getFontForState", //NOI18N SynthContext.class ); synthStyle_getFontForState.setAccessible(true); LIGHT = (ColorType) valueOfField (gtkColorType, "LIGHT"); //NOI18N DARK = (ColorType) valueOfField (gtkColorType, "DARK"); //NOI18N MID = (ColorType) valueOfField (gtkColorType, "MID"); //NOI18N BLACK = (ColorType) valueOfField (gtkColorType, "BLACK"); //NOI18N WHITE = (ColorType) valueOfField (gtkColorType, "WHITE"); //NOI18N functioning = Boolean.TRUE; } catch (Exception e) { System.err.println ("Cannot initialize GTK colors - using hardcoded defaults: " + e); //NOI18N if (log) { e.printStackTrace(); } return; } }
/** * <p>Locate the style associated with the given region, and component. * This is called from NimbusLookAndFeel in the SynthStyleFactory * implementation.</p> * * <p>Lookup occurs as follows:<br/> * Check the map of styles <code>m</code>. If the map contains no styles at * all, then simply return the defaultStyle. If the map contains styles, * then iterate over all of the styles for the Region <code>r</code> looking * for the best match, based on prefix. If a match was made, then return * that SynthStyle. Otherwise, return the defaultStyle.</p> * * @param comp The component associated with this region. For example, if * the Region is Region.Button then the component will be a JButton. * If the Region is a subregion, such as ScrollBarThumb, then the * associated component will be the component that subregion belongs * to, such as JScrollBar. The JComponent may be named. It may not be * null. * @param r The region we are looking for a style for. May not be null. */ SynthStyle getStyle(JComponent comp, Region r) { //validate method arguments if (comp == null || r == null) { throw new IllegalArgumentException( "Neither comp nor r may be null"); } //if there are no lazy styles registered for the region r, then return //the default style List<LazyStyle> styles = m.get(r); if (styles == null || styles.size() == 0) { return defaultStyle; } //Look for the best SynthStyle for this component/region pair. LazyStyle foundStyle = null; for (LazyStyle s : styles) { if (s.matches(comp)) { //replace the foundStyle if foundStyle is null, or //if the new style "s" is more specific (ie, its path was //longer), or if the foundStyle was "simple" and the new style //was not (ie: the foundStyle was for something like Button and //the new style was for something like "MyButton", hence, being //more specific.) In all cases, favor the most specific style //found. if (foundStyle == null || (foundStyle.parts.length < s.parts.length) || (foundStyle.parts.length == s.parts.length && foundStyle.simple && !s.simple)) { foundStyle = s; } } } //return the style, if found, or the default style if not found return foundStyle == null ? defaultStyle : foundStyle.getStyle(comp, r); }
/** * Gets the style. Creates it if necessary. * @return the style */ SynthStyle getStyle(JComponent c, Region r) { // if the component has overrides, it gets its own unique style // instead of the shared style. if (c.getClientProperty("Nimbus.Overrides") != null) { Map<Region, SynthStyle> map = overridesCache.get(c); SynthStyle s = null; if (map == null) { map = new HashMap<Region, SynthStyle>(); overridesCache.put(c, map); } else { s = map.get(r); } if (s == null) { s = new NimbusStyle(prefix, c); map.put(r, s); } return s; } // lazily create the style if necessary if (style == null) style = new NimbusStyle(prefix, null); // return the style return style; }
/** Called by UIManager when this look and feel is installed. */ @Override public void initialize() { super.initialize(); defaults.initialize(); // create synth style factory setStyleFactory(new SynthStyleFactory() { @Override public SynthStyle getStyle(JComponent c, Region r) { return defaults.getStyle(c, r); } }); }
private static void testSynthIcon() { if (!checkAndSetNimbusLookAndFeel()) { return; } JMenuItem menu = new JMenuItem(); Icon subMenuIcon = UIManager.getIcon("Menu.arrowIcon"); if (!(subMenuIcon instanceof SynthIcon)) { throw new RuntimeException("Icon is not a SynthIcon!"); } Region region = SynthLookAndFeel.getRegion(menu); SynthStyle style = SynthLookAndFeel.getStyle(menu, region); SynthContext synthContext = new SynthContext(menu, region, style, SynthConstants.ENABLED); int width = SynthGraphicsUtils.getIconWidth(subMenuIcon, synthContext); int height = SynthGraphicsUtils.getIconHeight(subMenuIcon, synthContext); paintAndCheckIcon(subMenuIcon, synthContext, width, height); int newWidth = width * 17; int newHeight = height * 37; Icon centeredIcon = new CenteredSynthIcon((SynthIcon) subMenuIcon, newWidth, newHeight); paintAndCheckIcon(centeredIcon, synthContext, newWidth, newHeight); }
/** * Locate the style associated with the given region and component. This is * called from SeaGlassLookAndFeel in the SynthStyleFactory implementation. * * <p>Lookup occurs as follows:<br/> * Check the map of styles <code>styleMap</code>. If the map contains no * styles at all, then simply return the defaultStyle. If the map contains * styles, then iterate over all of the styles for the Region <code>r</code> * looking for the best match, based on prefix. If a match was made, then * return that SynthStyle. Otherwise, return the defaultStyle.</p> * * @param c The component associated with this region. For example, if the * Region is Region.Button then the component will be a JButton. * If the Region is a subregion, such as ScrollBarThumb, then the * associated component will be the component that subregion * belongs to, such as JScrollBar. The JComponent may be named. It * may not be null. * @param r The region we are looking for a style for. May not be null. * * @return the style associated with the given region and component. */ SynthStyle getSeaGlassStyle(JComponent c, Region r) { // validate method arguments if (c == null || r == null) { throw new IllegalArgumentException("Neither comp nor r may be null"); } // if there are no lazy styles registered for the region r, then return // the default style List<LazyStyle> styles = styleMap.get(r); if (styles == null || styles.size() == 0) { return getDefaultStyle(); } // Look for the best SynthStyle for this component/region pair. LazyStyle foundStyle = null; for (LazyStyle s : styles) { if (s.matches(c)) { /* * Replace the foundStyle if foundStyle is null, or if the new * style "s" is more specific (ie, its path was longer), or if * the foundStyle was "simple" and the new style was not (ie: * the foundStyle was for something like Button and the new * style was for something like "MyButton", hence, being more * specific). In all cases, favor the most specific style found. */ if (foundStyle == null || (foundStyle.parts.length < s.parts.length) || (foundStyle.parts.length == s.parts.length && foundStyle.simple && !s.simple)) { foundStyle = s; } } } // return the style, if found, or the default style if not found return foundStyle == null ? getDefaultStyle() : foundStyle.getStyle(c); }