/*
 * Copyright 2003-2006 Dave Griffith, Bas Leijdekkers
 *
 * 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.siyeh.ig.logging;

import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.find.FindUtil;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.ClassInspection;
import com.siyeh.ig.RegExInputVerifier;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.text.Document;
import java.awt.*;

public class ClassWithoutLoggerInspection extends ClassInspection {

    /** @noinspection PublicField*/
    public String loggerClassName = "java.util.logging.Logger";

    /** @noinspection PublicField*/
    public boolean ignoreSuperLoggers = false;

    public String getDisplayName() {
        return InspectionGadgetsBundle.message("no.logger.display.name");
    }

    public String getGroupDisplayName() {
        return GroupNames.LOGGING_GROUP_NAME;
    }

    @NotNull
    public String buildErrorString(Object... infos) {
        return InspectionGadgetsBundle.message("no.logger.problem.descriptor");
    }

    public JComponent createOptionsPanel() {
        final Form form = new Form();
        return form.getContentPanel();
    }

    public BaseInspectionVisitor buildVisitor() {
        return new ClassWithoutLoggerVisitor();
    }

    private class ClassWithoutLoggerVisitor extends BaseInspectionVisitor {

        public void visitClass(@NotNull PsiClass aClass) {
            //no recursion to avoid drilldown
            if (aClass.isInterface() || aClass.isEnum()||
                    aClass.isAnnotationType()){
                return;
            }
            if (aClass instanceof PsiTypeParameter) {
                return;
            }
            if (aClass.getContainingClass()!=null) {
                return;
            }
            final PsiField[] fields;
            if (ignoreSuperLoggers) {
                fields = aClass.getAllFields();
            } else {
                fields = aClass.getFields();
            }
            for (PsiField field : fields) {
                if (isLogger(field)) {
                    if(PsiUtil.isAccessible(field, aClass, aClass)) {
                        return;
                    }
                }
            }
            registerClassError(aClass);
        }

        private boolean isLogger(PsiField field) {
            final PsiType type = field.getType();
            final String text = type.getCanonicalText();
            return text.equals(loggerClassName);
        }
    }

    private class Form {

        private JPanel contentPanel;
        private JTextField loggerClassNameTextField;
        private JCheckBox ignoreCheckBox;

        Form() {
            super();
            loggerClassNameTextField.setText(loggerClassName);
            loggerClassNameTextField.setInputVerifier(new RegExInputVerifier());
            final DocumentListener listener = new DocumentListener() {

                public void changedUpdate(DocumentEvent e) {
                    textChanged();
                }

                public void insertUpdate(DocumentEvent e) {
                    textChanged();
                }

                public void removeUpdate(DocumentEvent e) {
                    textChanged();
                }

                private void textChanged() {
                    loggerClassName =  loggerClassNameTextField.getText();
                }
            };
            final Document document = loggerClassNameTextField.getDocument();
            document.addDocumentListener(listener);
            ignoreCheckBox.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e) {
                    ignoreSuperLoggers = ignoreCheckBox.isSelected();
                }
            });
        }

        public JPanel getContentPanel() {
            return contentPanel;
        }
    }
}
@@ -714,17 +714,24 @@
 lij.psi.
-*
+PsiClass
 ;%0Aimport
@@ -752,55 +752,171 @@
 psi.
-util.PsiUtil;%0Aimport com.intellij.find.FindUtil
+PsiField;%0Aimport com.intellij.psi.PsiType;%0Aimport com.intellij.psi.PsiTypeParameter;%0Aimport com.intellij.psi.util.PsiUtil;%0Aimport com.siyeh.InspectionGadgetsBundle
 ;%0Aim
@@ -1037,50 +1037,8 @@
 er;%0A
-import com.siyeh.InspectionGadgetsBundle;%0A
 impo
@@ -1095,17 +1095,25 @@
 x.swing.
-*
+JCheckBox
 ;%0Aimport
@@ -1129,27 +1129,76 @@
 ing.
-event.DocumentEvent
+JComponent;%0Aimport javax.swing.JPanel;%0Aimport javax.swing.JTextField
 ;%0Aim
@@ -1220,32 +1220,27 @@
 g.event.
-DocumentListener
+ChangeEvent
 ;%0Aimport
@@ -1291,38 +1291,40 @@
 vax.swing.event.
-Change
+Document
 Event;%0Aimport ja
@@ -1333,19 +1333,20 @@
 x.swing.
-tex
+even
 t.Docume
@@ -1347,16 +1347,24 @@
 Document
+Listener
 ;%0Aimport
@@ -1372,14 +1372,29 @@
 java
-.awt.*
+x.swing.text.Document
 ;%0A%0Ap

fix imports