/*
 * Copyright 2000-2016 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.codeInspection.ex;

import com.intellij.codeInsight.AnnotationTargetUtil;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInspection.util.SpecialAnnotationsUtil;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.VerticalFlowLayout;
import com.intellij.openapi.util.Condition;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import org.jdom.Element;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@State(name = "EntryPointsManager")
public class EntryPointsManagerImpl extends EntryPointsManagerBase implements PersistentStateComponent<Element> {
  public EntryPointsManagerImpl(Project project) {
    super(project);
  }

  @Override
  public void configureAnnotations() {
    final List<String> list = new ArrayList<>(ADDITIONAL_ANNOTATIONS);
    final List<String> writeList = new ArrayList<>(myWriteAnnotations);

    final JPanel listPanel = SpecialAnnotationsUtil.createSpecialAnnotationsListControl(list, "Mark as entry point if annotated by", true);
    Condition<PsiClass> applicableToField = psiClass -> {
      Set<PsiAnnotation.TargetType> annotationTargets = AnnotationTargetUtil.getAnnotationTargets(psiClass);
      return annotationTargets != null && annotationTargets.contains(PsiAnnotation.TargetType.FIELD);
    };
    final JPanel writtenAnnotationsPanel = SpecialAnnotationsUtil.createSpecialAnnotationsListControl(writeList, "Mark field as implicitly written if annotated by", false, applicableToField);
    new DialogWrapper(myProject) {
      {
        init();
        setTitle("Configure Annotations");
      }

      @Override
      protected JComponent createCenterPanel() {
        final JPanel panel = new JPanel(new VerticalFlowLayout());
        panel.add(listPanel);
        panel.add(writtenAnnotationsPanel);
        return panel;
      }

      @Override
      protected void doOKAction() {
        ADDITIONAL_ANNOTATIONS.clear();
        ADDITIONAL_ANNOTATIONS.addAll(list);

        myWriteAnnotations.clear();
        myWriteAnnotations.addAll(writeList);

        DaemonCodeAnalyzer.getInstance(myProject).restart();
        super.doOKAction();
      }
    }.show();
  }

  @Override
  public JButton createConfigureAnnotationsBtn() {
    return createConfigureAnnotationsButton();
  }

  public static JButton createConfigureAnnotationsButton() {
    final JButton configureAnnotations = new JButton("Annotations...");
    configureAnnotations.setHorizontalAlignment(SwingConstants.LEFT);
    configureAnnotations.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        getInstance(ProjectUtil.guessCurrentProject(configureAnnotations)).configureAnnotations();
      }
    });
    return configureAnnotations;
  }

  public static JButton createConfigureClassPatternsButton() {
    final JButton configureClassPatterns = new JButton("Code patterns...");
    configureClassPatterns.setHorizontalAlignment(SwingConstants.LEFT);
    configureClassPatterns.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        final Project project = ProjectUtil.guessCurrentProject(configureClassPatterns);
        final EntryPointsManagerBase entryPointsManagerBase = getInstance(project);
        final ArrayList<ClassPattern> list = new ArrayList<>();
        for (ClassPattern pattern : entryPointsManagerBase.getPatterns()) {
          list.add(new ClassPattern(pattern));
        }
        final ClassPatternsPanel panel = new ClassPatternsPanel(list);
        new DialogWrapper(entryPointsManagerBase.myProject) {

          {
            init();
            setTitle("Configure Code Patterns");
          }

          @Nullable
          @Override
          protected JComponent createCenterPanel() {
            return panel;
          }

          @Override
          protected void doOKAction() {
            final String error = panel.getValidationError(project);
            if (error != null) {
              Messages.showErrorDialog(panel, error);
              return;
            }
            final LinkedHashSet<ClassPattern> patterns = entryPointsManagerBase.getPatterns();
            patterns.clear();
            patterns.addAll(list);
            DaemonCodeAnalyzer.getInstance(entryPointsManagerBase.myProject).restart();
            super.doOKAction();
          }
        }.show();
      }
    });
    return configureClassPatterns;
  }
}
@@ -1,13 +1,10 @@
 /
-*%0A *
+/
  Copyrig
@@ -18,9 +18,9 @@
 -201
-6
+8
  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
@@ -840,51 +840,8 @@
 ent;
-%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aim
@@ -3005,78 +3005,8 @@
 %22);%0A
-    configureAnnotations.setHorizontalAlignment(SwingConstants.LEFT);%0A
     
@@ -3430,80 +3430,8 @@
 %22);%0A
-    configureClassPatterns.setHorizontalAlignment(SwingConstants.LEFT);%0A
     
@@ -4163,28 +4163,8 @@
  %7D%0A%0A
-          @Nullable%0A
     

fix button alignment in "Unused declaration" inspection (IDEA-174261)