/*
 * Copyright 2000-2015 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.util.xml.ui.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.xml.*;
import com.intellij.util.xml.reflect.DomCollectionChildDescription;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.lang.reflect.Type;

public abstract class DefaultAddAction<T extends DomElement> extends AnAction {

  public DefaultAddAction() {
    super(ApplicationBundle.message("action.add"));
  }

  public DefaultAddAction(String text) {
    super(text);
  }

  public DefaultAddAction(String text, String description, Icon icon) {
    super(text, description, icon);
  }


  protected Type getElementType() {
    return getDomCollectionChildDescription().getType();
  }

  protected void tuneNewValue(T t) {
  }

  protected abstract DomCollectionChildDescription getDomCollectionChildDescription();

  protected abstract DomElement getParentDomElement();

  protected void afterAddition(@NotNull T newElement) {
  }

  @Override
  public final void actionPerformed(@NotNull final AnActionEvent e) {
    final T result = performElementAddition();
    if (result != null) {
      afterAddition(result);
    }
  }

  @Nullable
  protected T performElementAddition() {
    final DomElement parent = getParentDomElement();
    final DomManager domManager = parent.getManager();
    final TypeChooser[] oldChoosers = new TypeChooser[]{null};
    final Type[] aClass = new Type[]{null};
    final StableElement<T> result = WriteCommandAction.writeCommandAction(domManager.getProject(), DomUtil.getFile(parent)).compute(() -> {
      final DomElement parentDomElement = getParentDomElement();
      final T t = (T)getDomCollectionChildDescription().addValue(parentDomElement, getElementType());
      tuneNewValue(t);
      aClass[0] = parent.getGenericInfo().getCollectionChildDescription(t.getXmlElementName()).getType();
      oldChoosers[0] = domManager.getTypeChooserManager().getTypeChooser(aClass[0]);
      final SmartPsiElementPointer pointer =
        SmartPointerManager.getInstance(domManager.getProject()).createSmartPsiElementPointer(t.getXmlTag());
      domManager.getTypeChooserManager().registerTypeChooser(aClass[0], new TypeChooser() {
        @Override
        public Type chooseType(final XmlTag tag) {
          if (tag == pointer.getElement()) {
            return getElementType();
          }
          return oldChoosers[0].chooseType(tag);
        }

        @Override
        public void distinguishTag(final XmlTag tag, final Type aClass) throws IncorrectOperationException {
          oldChoosers[0].distinguishTag(tag, aClass);
        }

        @Override
        public Type[] getChooserTypes() {
          return oldChoosers[0].getChooserTypes();
        }
      });
      return t.createStableCopy();
    });
    if (result != null) {
      domManager.getTypeChooserManager().registerTypeChooser(aClass[0], oldChoosers[0]);
      return result.getWrappedElement();
    }
    return null;
  }
}
@@ -1,13 +1,10 @@
 /
-*%0A *
+/
  Copyrig
@@ -18,9 +18,9 @@
 -201
-5
+9
  Jet
@@ -36,567 +36,109 @@
 r.o.
-%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
+ Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 %0Apac
@@ -678,24 +678,62 @@
 escription;%0A
+import org.jetbrains.annotations.Nls;%0A
 import org.j
@@ -1046,32 +1046,80 @@
 efaultAddAction(
+@Nls(capitalization = Nls.Capitalization.Title) 
 String text) %7B%0A 
@@ -1169,20 +1169,145 @@
 ion(
-String text,
+@Nls(capitalization = Nls.Capitalization.Title) String text,%0A                          @Nls(capitalization = Nls.Capitalization.Sentence)
  Str

add @Nls capitalization rules