/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * 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.ui;

import com.intellij.util.ui.JBInsets;

import javax.swing.*;
import java.awt.*;

/**
 * Cell renderer CPU optimization.
 * @see javax.swing.table.DefaultTableCellRenderer#invalidate()
 *
 * @author gregsh
 */
public class CellRendererPanel extends JPanel {
  public CellRendererPanel() {
    super(null); // we do the layout ourselves
  }

  // property change support ----------------
  protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  }

  public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
  }

  // isOpaque() optimization ----------------
  public boolean isOpaque() {
    return false;
  }

  @Override
  protected void paintComponent(Graphics g) {
  }

  // BEGIN no validation methods --------------
  @Override
  public void doLayout() {
    if (getComponentCount() == 1) {
      Rectangle bounds = new Rectangle(getWidth(), getHeight());
      JBInsets.removeFrom(bounds, getInsets());
      getComponent(0).setBounds(bounds);
    }
    else {
      super.doLayout();
    }
  }

  @Override
  public Dimension getPreferredSize() {
    if (getComponentCount() != 1) return super.getPreferredSize();
    return getComponent(0).getPreferredSize();
  }

  public void invalidate() {
  }

  public void validate() {
    doLayout();
  }

  public void revalidate() {
  }

  public void repaint(long tm, int x, int y, int width, int height) {
  }

  public void repaint(Rectangle r) {
  }

  public void repaint() {
  }

// END no validation methods --------------
}

@@ -877,16 +877,48 @@
 Panel %7B%0A
+%0A  private boolean mySelected;%0A%0A
   public
@@ -988,24 +988,178 @@
 selves%0A  %7D%0A%0A
+  public final boolean isSelected() %7B%0A    return mySelected;%0A  %7D%0A%0A  public final void setSelected(boolean isSelected) %7B%0A    mySelected = isSelected;%0A  %7D%0A%0A
   // propert
@@ -1540,24 +1540,136 @@
 aphics g) %7B%0A
+    if (mySelected) %7B%0A      g.setColor(getBackground());%0A      g.fillRect(0, 0, getWidth(), getHeight());%0A    %7D%0A
   %7D%0A%0A  // BE
@@ -1745,35 +1745,43 @@
 Layout() %7B%0A    i
-f (
+nt count = 
 getComponentCoun
@@ -1783,16 +1783,31 @@
 tCount()
+;%0A    if (count
  == 1) %7B
@@ -1925,16 +1925,34 @@
 );%0A     
+ Component child =
  getComp
@@ -1959,16 +1959,29 @@
 onent(0)
+;%0A      child
 .setBoun
@@ -2000,44 +2000,446 @@
     
-%7D%0A    else %7B%0A      super.doLayout();
+  if (child instanceof CellRendererPanel) %7B%0A        child.doLayout();%0A      %7D%0A    %7D%0A    else %7B%0A      LayoutManager layout = getLayout();%0A      if (layout instanceof LayoutManager2) %7B%0A        ((LayoutManager2)layout).invalidateLayout(this);%0A      %7D%0A      super.doLayout();%0A      for (int i = 0; i %3C count; i++) %7B%0A        Component c = getComponent(i);%0A        if (c instanceof CellRendererPanel) %7B%0A          c.doLayout();%0A        %7D%0A      %7D
 %0A   

support nesting & box-layout