/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 * $Header:$
 */
package org.apache.beehive.controls.runtime.generator.apt;

import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.declaration.Declaration;
import com.sun.mirror.declaration.AnnotationValue;
import com.sun.mirror.declaration.AnnotationMirror;

/**
 * Wrapper class that allows the Control annotation processors to report diagnostics
 * without calling directly against the {@link AnnotationProcessorEnvironment}.  This
 * class keeps track of whether errors have been reported during annotation processing.
 */
public abstract class Diagnostics
{
    private AnnotationProcessorEnvironment _env;
    private boolean _hasErrors = false;
    
    protected Diagnostics( AnnotationProcessorEnvironment env )
    {
        _env = env;
    }
    
    public void addError( Declaration decl, String messageKey, Object ... args )
    {
        _env.getMessager().printError( decl.getPosition(), getResourceString( messageKey, args ) );
        _hasErrors = true;
    }
    
    public void addError( AnnotationMirror ann, String messageKey, Object ... args )
    {
        _env.getMessager().printError( ann.getPosition(), getResourceString( messageKey, args ) );
        _hasErrors = true;
    }
    
    public void addErrorArrayArgs( AnnotationMirror ann, String messageKey, Object[] args )
    {
        _env.getMessager().printError( ann.getPosition(), getResourceString( messageKey, args ) );
        _hasErrors = true;
    }
    
    public void addError( AnnotationValue annVal, String messageKey, Object ... args )
    {
        _env.getMessager().printError( annVal.getPosition(), getResourceString( messageKey, args ) );
        _hasErrors = true;
    }
    
    public void addWarning( Declaration decl, String messageKey, Object ... args )
    {
        _env.getMessager().printWarning( decl.getPosition(), getResourceString( messageKey, args ) );
    }
    
    public void addWarning( AnnotationMirror ann, String messageKey, Object ... args )
    {
        _env.getMessager().printWarning( ann.getPosition(), getResourceString( messageKey, args ) );
    }
    
    public void addWarning( AnnotationValue annVal, String messageKey, Object ... args )
    {
        _env.getMessager().printWarning( annVal.getPosition(), getResourceString( messageKey, args ) );
    }

    public AnnotationProcessorEnvironment getAnnotationProcessorEnvironment()
    {
        return _env;
    }

    protected abstract String getResourceString( String key, Object ... args );

    public boolean hasErrors()
    {
        return _hasErrors;
    }
    
    protected void setHasErrors( boolean hadErrors )
    {
        _hasErrors = hadErrors;
    }
}
