package org.vaadin.spring.navigator;

import java.lang.annotation.Annotation;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.vaadin.spring.events.EventBus;
import org.vaadin.spring.events.EventBusScope;
import org.vaadin.spring.events.EventScope;

import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewProvider;

/**
 * Base class for presentation.
 * Derivatives must be annotated with {@link VaadinPresenter}.
 * Works with {@link EventBus} to subscribe to events and set model data
 * on an implementor of {@link com.vaadin.navigator.View}.
 *
 * @author Chris Phillipson (fastnsilver@gmail.com)
 *
 * @param <V> an implementation of {@link com.vaadin.navigator.View}
 */
public abstract class Presenter<V extends View> {

    private static Logger logger = LoggerFactory.getLogger(Presenter.class);

    @Autowired
    private SpringViewProvider viewProvider;

    @Autowired
    @EventBusScope(EventScope.APPLICATION)
    private EventBus eventBus;

    @PostConstruct
    protected void init() {
        eventBus.subscribe(this);
    }

    public EventBus getEventBus() {
        return this.eventBus;
    }

    /**
     * Hands back the View that this Presenter works with
     * A match is made if the ViewProvider finds a VaadinView annotated View whose name matches Presenter's viewName
     * @return an implementor of {@link View}
     */
    public V getView() {
        V result = null;
        Annotation[] annotations = getClass().getDeclaredAnnotations();
        if (annotations != null && annotations.length > 0) {
            int i = 0;
            for (Annotation a: annotations) {
                if (a.annotationType().equals(VaadinPresenter.class)) {
                    VaadinPresenter vp = (VaadinPresenter) a;
                    result = (V) viewProvider.getView(vp.viewName());
                    i++;
                    break;
                }
            }
            if (i == 0) {
                logger.error("Presenter does not have a @VaadinPresenter annotation!");
            }
        } else {
            logger.error("Presenter does not have any annotations!");
        }
        return result;
    }


    /**
     * Allows this Presenter to (possibly) work with other views
     * @return a ViewProvider
     */
    public ViewProvider getViewProvider() {
        return this.viewProvider;
    }

    @PreDestroy
    // It's good manners to do this, even though we should be automatically unsubscribed
    // when the UI is garbage collected
    void destroy() {
        getEventBus().unsubscribe(this);
    }

}

@@ -35,49 +35,8 @@
 r;%0A%0A
-import java.lang.annotation.Annotation;%0A%0A
 impo
@@ -1540,247 +1540,74 @@
     
-Annotation%5B%5D annotations = getClass().getDeclaredAnnotations();%0A        if (annotations != null && annotations.length %3E 0) %7B%0A            int i = 0;%0A            for (Annotation a: annotations) %7B%0A                if (a.annotationType().equals
+Class%3C?%3E clazz = getClass();%0A        if (clazz.isAnnotationPresent
 (Vaa
@@ -1641,24 +1641,16 @@
         
-        
 VaadinPr
@@ -1662,16 +1662,35 @@
 er vp = 
+clazz.getAnnotation
 (VaadinP
@@ -1701,21 +1701,17 @@
 nter
-) a;%0A        
+.class);%0A
     
@@ -1780,114 +1780,17 @@
     
-            i++;%0A                    break;%0A                %7D%0A            %7D%0A            if (i == 0) %7B%0A    
+%7D else %7B%0A
     
@@ -1812,32 +1812,37 @@
 error(%22Presenter
+ %5B%7B%7D%5D
  does not have a
@@ -1875,109 +1875,31 @@
 on!%22
-);%0A            %7D%0A        %7D else %7B%0A            logger.error(%22Presenter does not have any annotations!%22
+, clazz.getSimpleName()
 );%0A 

Simplify Presenter#getView internals.