/*
 * Copyright 2013-2014 must-be.org
 *
 * 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.
 */
package com.intellij.ide.ui.laf.modern;

import com.intellij.openapi.ui.GraphicsConfig;
import com.intellij.util.ui.GraphicsUtil;
import com.intellij.util.ui.JBUI;
import sun.swing.SwingUtilities2;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.IconUIResource;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicRadioButtonUI;
import javax.swing.text.View;
import java.awt.*;

/**
 * @author VISTALL
 * @since 02.08.14
 *
 * Based on {@link com.intellij.ide.ui.laf.darcula.ui.DarculaRadioButtonUI}
 */
public class ModernRadioButtonUI extends BasicRadioButtonUI {
  private MouseEnterHandler myMouseEnterHandler;

  public ModernRadioButtonUI(final JComponent c) {
    myMouseEnterHandler = new MouseEnterHandler(c);
  }

  @SuppressWarnings("MethodOverridesStaticMethodOfSuperclass")
  public static ComponentUI createUI(JComponent c) {
    return new ModernRadioButtonUI(c);
  }

  @Override
  public synchronized void paint(Graphics g2d, JComponent c) {
    Graphics2D g = (Graphics2D)g2d;
    AbstractButton b = (AbstractButton) c;
    ButtonModel model = b.getModel();

    Dimension size = c.getSize();
    Font f = c.getFont();
    g.setFont(f);
    FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);

    Rectangle viewRect = new Rectangle(size);
    Rectangle iconRect = new Rectangle();
    Rectangle textRect = new Rectangle();

    Insets i = c.getInsets();
    viewRect.x += i.left;
    viewRect.y += i.top;
    viewRect.width -= (i.right + viewRect.x);
    viewRect.height -= (i.bottom + viewRect.y);


    String text = SwingUtilities.layoutCompoundLabel(
            c, fm, b.getText(), getDefaultIcon(),
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
            viewRect, iconRect, textRect, b.getIconTextGap());

    // fill background
    if(c.isOpaque()) {
      g.setColor(b.getBackground());
      g.fillRect(0,0, size.width, size.height);
    }

    int rad = 5;

    // Paint the radio button
    final int x = iconRect.x + (rad-1)/2;
    final int y = iconRect.y + (rad-1)/2;
    final int w = iconRect.width - (rad + 5) / 2;
    final int h = iconRect.height - (rad + 5) / 2;

    g.translate(x, y);

    //setup AA for lines
    final GraphicsConfig config = GraphicsUtil.setupAAPainting(g);
    g.setColor(c.getBackground());
    g.fillOval(0, 1, w - 1, h - 1);

    if (myMouseEnterHandler.isMouseEntered()) {
      g.setColor(ModernUIUtil.getSelectionBackground());
      g.drawOval(0, 1, w - 1, h - 1);
    }
    else {
      g.setPaint(ModernUIUtil.getBorderColor(c));
      g.drawOval(0, 1, w - 1, h - 1);
    }

    if (b.isSelected()) {
      final boolean enabled = b.isEnabled();
      g.setColor(UIManager.getColor(enabled ? "RadioButton.darcula.selectionEnabledShadowColor" : "RadioButton.darcula.selectionDisabledShadowColor"));// ? Gray._30 : Gray._60);
      g.fillOval(w/2 - rad/2, h/2 , rad, rad);
      g.setColor(UIManager.getColor(enabled ? "RadioButton.darcula.selectionEnabledColor" : "RadioButton.darcula.selectionDisabledColor")); //Gray._170 : Gray._120);
      g.fillOval(w/2 - rad/2, h/2 - 1, rad, rad);
    }
    config.restore();
    g.translate(-x, -y);

    // Draw the Text
    if(text != null) {
      View v = (View) c.getClientProperty(BasicHTML.propertyKey);
      if (v != null) {
        v.paint(g, textRect);
      } else {
        int mnemIndex = b.getDisplayedMnemonicIndex();
        if(model.isEnabled()) {
          // *** paint the text normally
          g.setColor(b.getForeground());
        } else {
          // *** paint the text disabled
          g.setColor(UIManager.getColor("RadioButton.disabledText"));
        }
        SwingUtilities2.drawStringUnderlineCharAt(c, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
      }
    }
  }

  @Override
  public Icon getDefaultIcon() {
    return new IconUIResource(JBUI.emptyIcon(20));
  }
}

@@ -2633,17 +2633,29 @@
 t rad = 
-5
+JBUI.scale(5)
 ;%0A%0A    /
@@ -2710,28 +2710,44 @@
 ect.x + (rad
--1)/
+ - JBUI.scale(1)) / 
 2;%0A    final
@@ -2776,12 +2776,28 @@
 (rad
--1)/
+ - JBUI.scale(1)) / 
 2;%0A 
@@ -2830,29 +2830,19 @@
 width - 
-(
 rad
- + 5) / 2
 ;%0A    fi
@@ -2875,21 +2875,11 @@
 t - 
-(
 rad
- + 5) / 2
 ;%0A%0A 
@@ -3046,31 +3046,67 @@
 Oval(0, 
-1, w - 1, h - 1
+JBUI.scale(1), w - JBUI.scale(1), h - JBUI.scale(1)
 );%0A%0A    
@@ -3222,39 +3222,75 @@
 drawOval(0, 
-1, w - 1, h - 1
+JBUI.scale(1), w - JBUI.scale(1), h - JBUI.scale(1)
 );%0A    %7D%0A   
@@ -3371,23 +3371,59 @@
 (0, 
-1, w - 1, h - 1
+JBUI.scale(1), w - JBUI.scale(1), h - JBUI.scale(1)
 );%0A 
@@ -3503,233 +3503,8 @@
 ();%0A
-      g.setColor(UIManager.getColor(enabled ? %22RadioButton.darcula.selectionEnabledShadowColor%22 : %22RadioButton.darcula.selectionDisabledShadowColor%22));// ? Gray._30 : Gray._60);%0A      g.fillOval(w/2 - rad/2, h/2 , rad, rad);%0A
     
@@ -3700,14 +3700,41 @@
 2, h
-/
+ / 
 2 - 
-1
+Math.round(JBUI.scale(1f))
 , ra

update radio button ui for hidpi