All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ird.observe.client.util.treetable.JRendererRadioButton Maven / Gradle / Ivy

package fr.ird.observe.client.util.treetable;

/*-
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import org.jdesktop.swingx.painter.Painter;
import org.jdesktop.swingx.renderer.PainterAware;

/**
 * Created by tchemit on 05/08/17.
 *
 * @author Tony Chemit - [email protected]
 * @since 7.0
 */
public class JRendererRadioButton extends JRadioButton implements PainterAware {
    private static final long serialVersionUID = 1841302818246880815L;
    /** the swingx painter */
    private Painter painter;
    /** a flag to prevent ui painting from filling the background. */
    private boolean fakeTransparency;

    JRendererRadioButton() {
        super();
        // fix # 1546-swingx: striping lost in synth-based lafs
        // forcing opaque to enable painting the background
        setOpaque(true);
    }

    @Override
    public Painter getPainter() {
        return painter;
    }

    @Override
    public void setPainter(Painter painter) {
        Painter old = getPainter();
        this.painter = painter;
        firePropertyChange("painter", old, getPainter());
    }

    /**
     * Overridden to return false if painting flag is true.
     */
    @Override
    public boolean isOpaque() {
        return !fakeTransparency && super.isOpaque();
    }

    /**
     * Overridden to return false if painting flag is true.
     */
    @Override
    public boolean isContentAreaFilled() {
        return !fakeTransparency && super.isContentAreaFilled();
    }

    /**
     * Overridden to not automatically de/register itself from/to the ToolTipManager.
     * As rendering component it is not considered to be active in any way, so the
     * manager must not listen.
     */
    @Override
    public void setToolTipText(String text) {
        putClientProperty(TOOL_TIP_TEXT_KEY, text);
    }

    /**
     * Overridden to snatch painting from super if a painter installed or Nimbus
     * detected.
     * 

* The overall logic currently (since 1.6.5) is to simply call super without SwingX * painter. Otherwise, that is with SwingX painter: * if opaque * set a flag which fakes transparency, that is both * contentAreaFilled and * opaque return false * fill background with the component's background color * apply swingx painter * hook into ui.paint(...) * reset the flag else apply swingx painter call super *

* Note that Nimbus is special cased (mainly due to its bug of * even row striping instead of odd) * and handled as if a SwingX painter were set. */ @Override protected void paintComponent(Graphics g) { // JW: hack around for #1178-swingx (core issue) // grab painting if Nimbus detected if ((painter != null) || isNimbus()) { // we have a custom (background) painter // try to inject if possible // there's no guarantee - some LFs have their own background // handling elsewhere if (isOpaque()) { // replace the paintComponent completely fakeTransparency = true; paintComponentWithPainter((Graphics2D) g); fakeTransparency = false; } else { // transparent apply the background painter before calling super paintPainter(g); super.paintComponent(g); } } else { // nothing to worry about - delegate to super super.paintComponent(g); } } /** * Hack around Nimbus not respecting background colors if UIResource. * So by-pass ... */ private boolean isNimbus() { return UIManager.getLookAndFeel().getName().contains("Nimbus"); } /** * Hack around AbstractPainter.paint bug which disposes the Graphics. * So here we give it a scratch to paint on.

* TODO - remove again, the issue is fixed? * * @param g the graphics to paint on */ private void paintPainter(Graphics g) { if (painter == null) return; // fail fast: we assume that g must not be null // which throws an NPE here instead deeper down the bowels // this differs from corresponding core implementation! Graphics2D scratch = (Graphics2D) g.create(); try { painter.paint(scratch, this, getWidth(), getHeight()); } finally { scratch.dispose(); } } private void paintComponentWithPainter(Graphics2D g) { // 1. be sure to fill the background // 2. paint the painter // by-pass ui.update and hook into ui.paint directly if (ui != null) { // fail fast: we assume that g must not be null // which throws an NPE here instead deeper down the bowels // this differs from corresponding core implementation! Graphics scratchGraphics = g.create(); try { scratchGraphics.setColor(getBackground()); scratchGraphics.fillRect(0, 0, getWidth(), getHeight()); paintPainter(g); ui.paint(scratchGraphics, this); } finally { scratchGraphics.dispose(); } } } /** * Overridden for performance reasons. * See the Implementation Note * for more information. * * @since 1.5 */ @Override public void invalidate() { } /** * Overridden for performance reasons. * See the Implementation Note * for more information. */ @Override public void validate() { } /** * Overridden for performance reasons. * See the Implementation Note * for more information. */ @Override public void revalidate() { } /** * Overridden for performance reasons. * See the Implementation Note * for more information. */ @Override public void repaint(long tm, int x, int y, int width, int height) { } /** * Overridden for performance reasons. * See the Implementation Note * for more information. */ @Override public void repaint(Rectangle r) { } /** * Overridden for performance reasons. * See the Implementation Note * for more information. * * @since 1.5 */ @Override public void repaint() { } /** * Overridden for performance reasons. * See the Implementation Note * for more information. */ @Override protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { // Strings get interned... if ("text".equals(propertyName)) { super.firePropertyChange(propertyName, oldValue, newValue); } } /** * Overridden for performance reasons. * See the Implementation Note * for more information. */ @Override public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy