/*
 * Copyright 2000-2009 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.uiDesigner.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.designSurface.GuiEditor;
import com.intellij.uiDesigner.propertyInspector.properties.IndentProperty;
import com.intellij.uiDesigner.radComponents.RadComponent;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yole
 */
public class IncreaseIndentAction extends AbstractGuiEditorAction {
  private final IndentProperty myIndentProperty = new IndentProperty();

  public IncreaseIndentAction() {
    super(true);
  }

  protected void actionPerformed(final GuiEditor editor, final List<RadComponent> selection, final AnActionEvent e) {
    for(RadComponent c: selection) {
      int indent = myIndentProperty.getValue(c).intValue();
      myIndentProperty.setValueEx(c, adjustIndent(indent));
    }
  }

  protected void update(@NotNull GuiEditor editor, final ArrayList<RadComponent> selection, final AnActionEvent e) {
    e.getPresentation().setVisible(canAdjustIndent(selection));
  }

  private boolean canAdjustIndent(final ArrayList<RadComponent> selection) {
    for(RadComponent c: selection) {
      if (canAdjustIndent(c)) {
        return true;
      }
    }
    return false;
  }

  protected int adjustIndent(final int indent) {
    return indent + 1;
  }

  protected boolean canAdjustIndent(final RadComponent component) {
    return component.getParent().getLayout() instanceof GridLayoutManager;
  }
}

@@ -688,24 +688,72 @@
 ctionEvent;%0A
+import com.intellij.openapi.wm.IdeFocusManager;%0A
 import com.i
@@ -1029,16 +1029,35 @@
 tNull;%0A%0A
+import java.awt.*;%0A
 import j
@@ -1732,64 +1732,314 @@
     
-e.getPresentation().setVisible(canAdjustIndent(selection
+final boolean applicable = canAdjustIndent(selection);%0A    e.getPresentation().setVisible(applicable);%0A    final Component focusOwner = IdeFocusManager.findInstanceByComponent(editor).getFocusOwner();%0A    e.getPresentation().setEnabled(applicable && (focusOwner == editor %7C%7C editor.isAncestorOf(focusOwner)
 ));%0A

indent actions work only when the editor is focused (IDEA-66035)