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

it.tidalwave.netbeans.lookandfeel.ui.BlueMarineToggleButtonUI Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * SolidBlue - open source safe data
 * Copyright (C) 2011-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://solidblue.java.net
 * SCM: https://bitbucket.org/tidalwave/solidblue-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.lookandfeel.ui;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.event.MouseInputAdapter;
import javax.swing.plaf.metal.MetalBorders;
import javax.swing.plaf.metal.MetalToggleButtonUI;
import com.nilo.plaf.nimrod.NimRODBorders; // FIXME: remove dep
import com.nilo.plaf.nimrod.NimRODLookAndFeel; // FIXME: remove dep

/*******************************************************************************
 *
 * TODO: doesn't work.
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public class BlueMarineToggleButtonUI extends MetalToggleButtonUI 
  {
    public class MiML extends MouseInputAdapter 
      {
        private final AbstractButton button;

        MiML (final AbstractButton b) 
          {
            button = b;
          }

        @Override
        public void mouseEntered (final MouseEvent e) 
          {
            button.getModel().setRollover(true);
            button.repaint();
          }

        @Override
        public void mouseExited (final MouseEvent e) 
          {
            button.getModel().setRollover(false);
            button.repaint();
          }
      }
    
    protected MiML miml;
    
    private static BlueMarineToggleButtonUI ui;

    public static BlueMarineToggleButtonUI createUI (JComponent c) 
      {
        if (ui == null) 
          {
            ui = new BlueMarineToggleButtonUI();
          }

        return ui;
      }

    @Override
    public void installDefaults (AbstractButton button) 
      {
        super.installDefaults(button);

        button.setBorder(NimRODBorders.getButtonBorder());

        selectColor = UIManager.getColor("ScrollBar.thumb");
      }

    @Override
    public void uninstallDefaults (AbstractButton button) 
      {
        super.uninstallDefaults(button);
        button.setBorder(MetalBorders.getButtonBorder());
      }

    @Override
    public void installListeners (AbstractButton b) 
      {
        super.installListeners(b);

        miml = new MiML(b);
        b.addMouseListener(miml);
      }

    @Override
    protected void uninstallListeners (AbstractButton b) 
      {
        b.removeMouseListener(miml);
      }

    @Override
    protected void paintButtonPressed (Graphics g, AbstractButton b) 
      {
        if (!b.isOpaque()) 
          {
            return;
          }

        if (b.isContentAreaFilled()) 
          {
            Graphics2D g2D = (Graphics2D) g;
            g2D.setColor(Color.BLUE);
//            g2D.setColor(NimRODUtils.getColorAlfa(selectColor, 100));
            RoundRectangle2D.Float button = hasButton(b);
            g2D.fill(button);
          }
      }

    @Override
    protected void paintFocus (Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) 
      {
        if (!b.isFocusPainted() || !b.isOpaque()) 
          {
            return;
          }
        
        if (b.getParent() instanceof JToolBar) 
          {
            return; 
          }

        g.setColor(getFocusColor());
        g.drawRoundRect(2, 2, b.getWidth() - 6, b.getHeight() - 6, 5, 5);
        g.drawRoundRect(3, 3, b.getWidth() - 6, b.getHeight() - 6, 4, 4);
      }

    @Override
    public void paint (Graphics g, JComponent c) 
      {
        ButtonModel mod = ((AbstractButton)c).getModel();

        if (c.isOpaque()) 
          {
            Graphics2D g2D = (Graphics2D) g;
            RoundRectangle2D.Float button = hasButton(c);

            if (c.getParent() instanceof JToolBar)
              {
                if (mod.isRollover() || mod.isPressed() || mod.isSelected()) 
                  {
                    c.setBorder(NimRODBorders.getGenBorder());
                  }
                else 
                  {
                    c.setBorder(NimRODBorders.getEmptyGenBorder());
                  }

                if (mod.isPressed() || mod.isSelected()) 
                  {
                    g2D.setColor(NimRODLookAndFeel.getFocusColor());
                    g2D.fill(button);
                  }
              }
            
            else 
              {
                GradientPaint grad = null;
                final Color color1 = (mod.isPressed() || mod.isSelected()) ? Color.BLACK : Color.WHITE;
                final Color color2 = (mod.isPressed() || mod.isSelected()) ? Color.WHITE : Color.BLACK;
                grad = new GradientPaint(0, 0, color1, 0, c.getHeight(), color2);
                g2D.setPaint(grad);
                g2D.fill(button);

                if (mod.isRollover()) 
                  {
                    g2D.setColor(Color.RED);
//                    g2D.setColor(NimRODUtils.getRolloverColor());
                    g2D.fill(button);
                  }
              }
          }

        super.paint(g, c);
      }

    private RoundRectangle2D.Float hasButton (JComponent c) 
      {
        RoundRectangle2D.Float boton = new RoundRectangle2D.Float();
        boton.x = 0;
        boton.y = 0;
        boton.width = c.getWidth();
        boton.height = c.getHeight();
        boton.arcwidth = 8;
        boton.archeight = 8;

        return boton;
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy