001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.diagnostics;
018    
019    import com.intellij.openapi.util.TextRange;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.PsiErrorElement;
022    import com.intellij.psi.PsiFile;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.jet.JetNodeTypes;
025    
026    import java.util.List;
027    
028    public abstract class AbstractDiagnostic<E extends PsiElement> implements ParametrizedDiagnostic<E> {
029        private final E psiElement;
030        private final DiagnosticFactoryWithPsiElement<E> factory;
031        private final Severity severity;
032    
033        public AbstractDiagnostic(@NotNull E psiElement,
034                @NotNull DiagnosticFactoryWithPsiElement<E> factory,
035                @NotNull Severity severity) {
036            this.psiElement = psiElement;
037            this.factory = factory;
038            this.severity = severity;
039        }
040    
041        @NotNull
042        @Override
043        public DiagnosticFactoryWithPsiElement<E> getFactory() {
044            return factory;
045        }
046    
047        @NotNull
048        @Override
049        public PsiFile getPsiFile() {
050            return psiElement.getContainingFile();
051        }
052    
053        @NotNull
054        @Override
055        public Severity getSeverity() {
056            return severity;
057        }
058    
059        @Override
060        @NotNull
061        public E getPsiElement() {
062            return psiElement;
063        }
064    
065        @Override
066        @NotNull
067        public List<TextRange> getTextRanges() {
068            return getFactory().getTextRanges(this);
069        }
070    
071        @Override
072        public boolean isValid() {
073            if (!getFactory().isValid(this)) return false;
074            if (psiElement.getNode().findChildByType(JetNodeTypes.IDE_TEMPLATE_EXPRESSION) != null) return false;
075            return true;
076        }
077    }