package org.jtheque.ui.utils.builded;

import org.jtheque.core.utils.OSGiUtils;
import org.jtheque.errors.able.IError;
import org.jtheque.errors.able.IErrorService;
import org.jtheque.i18n.able.ILanguageService;
import org.jtheque.spring.utils.SwingSpringProxy;
import org.jtheque.ui.able.IFilthyUtils;

import org.osgi.framework.BundleContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.osgi.context.BundleContextAware;

import java.util.ArrayList;
import java.util.Collection;

/*
 * Copyright JTheque (Baptiste Wicht)
 *
 * 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.
 */

/**
 * An filthy panel builded with a panel builder in osgi spring context.
 *
 * @author Baptiste Wicht
 */
public abstract class OSGIFilthyBuildedPanel extends FilthyBuildedPanel implements BundleContextAware,
        ApplicationContextAware {
    private BundleContext bundleContext;
    private ApplicationContext applicationContext;

    private IFilthyUtils utils;

    @Override
    public void setBundleContext(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
    }

    /**
     * Return the service of the given class.
     *
     * @param classz The class to get the service.
     * @param <T>    The type of service.
     *
     * @return The service of the given class if it's exists otherwise null.
     */
    protected <T> T getService(Class<T> classz) {
        return OSGiUtils.getService(bundleContext, classz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * Return the bean of the given class using the application context.
     *
     * @param classz The classz of the bean to get from application context.
     * @param <T>    The type of bean to get.
     *
     * @return The bean of the given class or null if it doesn't exist.
     */
    protected <T> T getBean(Class<T> classz) {
        return applicationContext.getBean(classz);
    }

    /**
     * Return the bean of the given class using the application context. The bean will be retrieved in the EDT, so it
     * can be used for a Swing bean.
     *
     * @param classz The classz of the bean to get from application context.
     * @param <T>    The type of bean to get.
     *
     * @return The bean of the given class or null if it doesn't exist.
     */
    protected <T> T getBeanFromEDT(Class<T> classz) {
        return new SwingSpringProxy<T>(classz, applicationContext).get();
    }

    @Override
    protected ILanguageService getLanguageService() {
        return getService(ILanguageService.class);
    }

    @Override
    protected IFilthyUtils getFilthyUtils() {
        if (utils == null) {
            utils = getService(IFilthyUtils.class);
        }

        return utils;
    }

    @Override
    public boolean validateContent() {
        Collection<IError> errors = new ArrayList<IError>(5);

        validate(errors);

        for (IError error : errors) {
            getService(IErrorService.class).addError(error);
        }

        return errors.isEmpty();
    }

    /**
     * Validate the panel.
     *
     * @param errors The errors.
     */
    public void validate(Collection<IError> errors) {
        //Default empty implementation
    }
}
@@ -1740,38 +1740,36 @@
 *%0A     * @param 
-classz
+type
  The class to ge
@@ -1792,34 +1792,32 @@
     * @param %3CT%3E
-  
   The type of se
@@ -1947,38 +1947,36 @@
 ervice(Class%3CT%3E 
-classz
+type
 ) %7B%0A        retu
@@ -2014,22 +2014,20 @@
 ontext, 
-classz
+type
 );%0A    %7D
@@ -2276,38 +2276,475 @@
 *%0A     * @param 
-classz
+type The type of the bean to get from application context.%0A     * @param %3CT%3E  The type of bean to get.%0A     *%0A     * @return The bean of the given class or null if it doesn't exist.%0A     */%0A    protected %3CT%3E T getBean(Class%3CT%3E type) %7B%0A        return applicationContext.getBean(type);%0A    %7D%0A%0A    /**%0A     * Return the bean of the given class using the application context.%0A     *%0A     * @param id   The in of the Spring bean.%0A     * @param type
  The classz of t
@@ -2793,34 +2793,32 @@
     * @param %3CT%3E
-  
   The type of be
@@ -2935,32 +2935,43 @@
 d %3CT%3E T getBean(
+String id, 
 Class%3CT%3E classz)
@@ -2955,38 +2955,36 @@
 ng id, Class%3CT%3E 
-classz
+type
 ) %7B%0A        retu
@@ -3013,22 +3013,24 @@
 getBean(
-classz
+id, type
 );%0A    %7D
@@ -3215,22 +3215,20 @@
  @param 
-classz
+type
  The cla
@@ -3293,18 +3293,16 @@
 aram %3CT%3E
-  
   The ty
@@ -3451,22 +3451,20 @@
 lass%3CT%3E 
-classz
+type
 ) %7B%0A    
@@ -3498,22 +3498,20 @@
 roxy%3CT%3E(
-classz
+type
 , applic

Improve utility methods of application context