package net.miginfocom.swing;

import java.awt.BasicStroke;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.Label;
import java.awt.List;
import java.awt.Point;
import java.awt.ScrollPane;
import java.awt.Scrollbar;
import java.awt.TextComponent;
import java.awt.TextField;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D.Float;
import java.lang.reflect.Method;
import java.util.IdentityHashMap;
import javax.swing.AbstractButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.JTextComponent;
import net.miginfocom.layout.ComponentWrapper;
import net.miginfocom.layout.ContainerWrapper;
import net.miginfocom.layout.PlatformDefaults;

public class SwingComponentWrapper implements ComponentWrapper {
   private static boolean maxSet = false;
   private static boolean vp = true;
   private static final Color DB_COMP_OUTLINE = new Color(0, 0, 200);
   private final Component c;
   private int compType = -1;
   private Boolean bl = null;
   private boolean prefCalled = false;
   private static final IdentityHashMap<FontMetrics, Float> FM_MAP = new IdentityHashMap(4);
   private static final Font SUBST_FONT = new Font("sansserif", 0, 11);
   private static Method BL_METHOD = null;
   private static Method BL_RES_METHOD = null;
   private static Method IMS_METHOD;

   public SwingComponentWrapper(Component var1) {
      this.c = var1;
   }

   public final int getBaseline(int var1, int var2) {
      if (BL_METHOD == null) {
         return -1;
      } else {
         try {
            Object[] var3 = new Object[]{var1 < 0 ? this.c.getWidth() : var1, var2 < 0 ? this.c.getHeight() : var2};
            return (Integer)BL_METHOD.invoke(this.c, var3);
         } catch (Exception var4) {
            return -1;
         }
      }
   }

   public final Object getComponent() {
      return this.c;
   }

   public final float getPixelUnitFactor(boolean var1) {
      switch(PlatformDefaults.getLogicalPixelBase()) {
      case 100:
         Font var2 = this.c.getFont();
         FontMetrics var3 = this.c.getFontMetrics(var2 != null ? var2 : SUBST_FONT);
         Float var4 = (Float)FM_MAP.get(var3);
         if (var4 == null) {
            Rectangle2D var6 = var3.getStringBounds("X", this.c.getGraphics());
            var4 = new Float((float)var6.getWidth() / 6.0F, (float)var6.getHeight() / 13.277344F);
            FM_MAP.put(var3, var4);
         }

         return var1 ? var4.x : var4.y;
      case 101:
         java.lang.Float var5 = var1 ? PlatformDefaults.getHorizontalScaleFactor() : PlatformDefaults.getVerticalScaleFactor();
         if (var5 != null) {
            return var5;
         }

         return (float)(var1 ? this.getHorizontalScreenDPI() : this.getVerticalScreenDPI()) / (float)PlatformDefaults.getDefaultDPI();
      default:
         return 1.0F;
      }
   }

   public final int getX() {
      return this.c.getX();
   }

   public final int getY() {
      return this.c.getY();
   }

   public final int getHeight() {
      return this.c.getHeight();
   }

   public final int getWidth() {
      return this.c.getWidth();
   }

   public final int getScreenLocationX() {
      Point var1 = new Point();
      SwingUtilities.convertPointToScreen(var1, this.c);
      return var1.x;
   }

   public final int getScreenLocationY() {
      Point var1 = new Point();
      SwingUtilities.convertPointToScreen(var1, this.c);
      return var1.y;
   }

   public final int getMinimumHeight(int var1) {
      if (!this.prefCalled) {
         this.c.getPreferredSize();
         this.prefCalled = true;
      }

      return this.c.getMinimumSize().height;
   }

   public final int getMinimumWidth(int var1) {
      if (!this.prefCalled) {
         this.c.getPreferredSize();
         this.prefCalled = true;
      }

      return this.c.getMinimumSize().width;
   }

   public final int getPreferredHeight(int var1) {
      if (this.c.getWidth() == 0 && this.c.getHeight() == 0 && var1 != -1) {
         this.c.setBounds(this.c.getX(), this.c.getY(), var1, 1);
      }

      return this.c.getPreferredSize().height;
   }

   public final int getPreferredWidth(int var1) {
      if (this.c.getWidth() == 0 && this.c.getHeight() == 0 && var1 != -1) {
         this.c.setBounds(this.c.getX(), this.c.getY(), 1, var1);
      }

      return this.c.getPreferredSize().width;
   }

   public final int getMaximumHeight(int var1) {
      return !this.isMaxSet(this.c) ? 32767 : this.c.getMaximumSize().height;
   }

   public final int getMaximumWidth(int var1) {
      return !this.isMaxSet(this.c) ? 32767 : this.c.getMaximumSize().width;
   }

   private boolean isMaxSet(Component var1) {
      if (IMS_METHOD != null) {
         try {
            return (Boolean)IMS_METHOD.invoke(var1, (Object[])null);
         } catch (Exception var3) {
            IMS_METHOD = null;
         }
      }

      return isMaxSizeSetOn1_4();
   }

   public final ContainerWrapper getParent() {
      Container var1 = this.c.getParent();
      return var1 != null ? new SwingContainerWrapper(var1) : null;
   }

   public final int getHorizontalScreenDPI() {
      return PlatformDefaults.getDefaultDPI();
   }

   public final int getVerticalScreenDPI() {
      return PlatformDefaults.getDefaultDPI();
   }

   public final int getScreenWidth() {
      try {
         return this.c.getToolkit().getScreenSize().width;
      } catch (HeadlessException var2) {
         return 1024;
      }
   }

   public final int getScreenHeight() {
      try {
         return this.c.getToolkit().getScreenSize().height;
      } catch (HeadlessException var2) {
         return 768;
      }
   }

   public final boolean hasBaseline() {
      if (this.bl == null) {
         try {
            if (BL_RES_METHOD != null && !BL_RES_METHOD.invoke(this.c).toString().equals("OTHER")) {
               Dimension var1 = this.c.getMinimumSize();
               this.bl = this.getBaseline(var1.width, var1.height) > -1;
            } else {
               this.bl = Boolean.FALSE;
            }
         } catch (Throwable var2) {
            this.bl = Boolean.FALSE;
         }
      }

      return this.bl;
   }

   public final String getLinkId() {
      return this.c.getName();
   }

   public final void setBounds(int var1, int var2, int var3, int var4) {
      this.c.setBounds(var1, var2, var3, var4);
   }

   public boolean isVisible() {
      return this.c.isVisible();
   }

   public final int[] getVisualPadding() {
      return vp && this.c instanceof JTabbedPane && UIManager.getLookAndFeel().getClass().getName().endsWith("WindowsLookAndFeel") ? new int[]{-1, 0, 2, 2} : null;
   }

   public static boolean isMaxSizeSetOn1_4() {
      return maxSet;
   }

   public static void setMaxSizeSetOn1_4(boolean var0) {
      maxSet = var0;
   }

   public static boolean isVisualPaddingEnabled() {
      return vp;
   }

   public static void setVisualPaddingEnabled(boolean var0) {
      vp = var0;
   }

   public final void paintDebugOutline() {
      if (this.c.isShowing()) {
         Graphics2D var1 = (Graphics2D)this.c.getGraphics();
         if (var1 != null) {
            var1.setPaint(DB_COMP_OUTLINE);
            var1.setStroke(new BasicStroke(1.0F, 2, 0, 10.0F, new float[]{2.0F, 4.0F}, 0.0F));
            var1.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
         }
      }
   }

   public int getComponetType(boolean var1) {
      if (this.compType == -1) {
         this.compType = this.checkType(var1);
      }

      return this.compType;
   }

   public int getLayoutHashCode() {
      Dimension var1 = this.c.getMaximumSize();
      int var2 = var1.width + (var1.height << 5);
      var1 = this.c.getPreferredSize();
      var2 += (var1.width << 10) + (var1.height << 15);
      var1 = this.c.getMinimumSize();
      var2 += (var1.width << 20) + (var1.height << 25);
      if (this.c.isVisible()) {
         var2 += 1324511;
      }

      String var3 = this.getLinkId();
      if (var3 != null) {
         var2 += var3.hashCode();
      }

      return var2;
   }

   private int checkType(boolean var1) {
      Component var2 = this.c;
      if (var1) {
         if (var2 instanceof JScrollPane) {
            var2 = ((JScrollPane)var2).getViewport().getView();
         } else if (var2 instanceof ScrollPane) {
            var2 = ((ScrollPane)var2).getComponent(0);
         }
      }

      if (!(var2 instanceof JTextField) && !(var2 instanceof TextField)) {
         if (!(var2 instanceof JLabel) && !(var2 instanceof Label)) {
            if (!(var2 instanceof JToggleButton) && !(var2 instanceof Checkbox)) {
               if (!(var2 instanceof AbstractButton) && !(var2 instanceof Button)) {
                  if (!(var2 instanceof JComboBox) && !(var2 instanceof Choice)) {
                     if (!(var2 instanceof JTextComponent) && !(var2 instanceof TextComponent)) {
                        if (!(var2 instanceof JPanel) && !(var2 instanceof Canvas)) {
                           if (!(var2 instanceof JList) && !(var2 instanceof List)) {
                              if (var2 instanceof JTable) {
                                 return 7;
                              } else if (var2 instanceof JSeparator) {
                                 return 18;
                              } else if (var2 instanceof JSpinner) {
                                 return 13;
                              } else if (var2 instanceof JProgressBar) {
                                 return 14;
                              } else if (var2 instanceof JSlider) {
                                 return 12;
                              } else if (var2 instanceof JScrollPane) {
                                 return 8;
                              } else if (!(var2 instanceof JScrollBar) && !(var2 instanceof Scrollbar)) {
                                 return var2 instanceof Container ? 1 : 0;
                              } else {
                                 return 17;
                              }
                           } else {
                              return 6;
                           }
                        } else {
                           return 10;
                        }
                     } else {
                        return 4;
                     }
                  } else {
                     return 2;
                  }
               } else {
                  return 5;
               }
            } else {
               return 16;
            }
         } else {
            return 2;
         }
      } else {
         return 3;
      }
   }

   public final int hashCode() {
      return this.getComponent().hashCode();
   }

   public final boolean equals(Object var1) {
      return !(var1 instanceof ComponentWrapper) ? false : this.getComponent().equals(((ComponentWrapper)var1).getComponent());
   }

   static {
      try {
         BL_METHOD = Component.class.getDeclaredMethod("getBaseline", Integer.TYPE, Integer.TYPE);
         BL_RES_METHOD = Component.class.getDeclaredMethod("getBaselineResizeBehavior");
      } catch (Throwable var2) {
      }

      IMS_METHOD = null;

      try {
         IMS_METHOD = Component.class.getDeclaredMethod("isMaximumSizeSet", (Class[])null);
      } catch (Throwable var1) {
      }

   }
}
