All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alee.demo.api.example.AbstractExampleGroup Maven / Gradle / Ivy

There is a newer version: 1.2.8
Show newest version
/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see .
 */

package com.alee.demo.api.example;

import com.alee.api.annotations.NotNull;
import com.alee.demo.skin.DemoIcons;
import com.alee.demo.util.ExampleUtils;
import com.alee.utils.ReflectUtils;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Mikle Garin
 */
public abstract class AbstractExampleGroup extends AbstractExampleElement implements ExampleGroup
{
    protected List groups;
    protected List examples;

    @NotNull
    @Override
    public Icon getIcon ()
    {
        final URL resource = getClass ().getResource ( "icons/" + getId () + ".png" );
        return resource != null ? new ImageIcon ( resource ) : DemoIcons.group16;
    }

    @NotNull
    @Override
    public FeatureState getFeatureState ()
    {
        final List groups = getGroups ();
        final List examples = getExamples ();
        final List states = new ArrayList ( groups.size () + examples.size () );
        for ( final ExampleGroup group : groups )
        {
            states.add ( group.getFeatureState () );
        }
        for ( final Example example : examples )
        {
            states.add ( example.getFeatureState () );
        }
        return ExampleUtils.getResultingState ( states );
    }

    @Override
    public List getGroups ()
    {
        if ( groups == null )
        {
            final List groupClasses = getExampleGroupClasses ();
            groups = new ArrayList ( groupClasses.size () );
            for ( final Class groupClass : groupClasses )
            {
                try
                {
                    final ExampleGroup group = ReflectUtils.createInstance ( groupClass );
                    if ( group instanceof AbstractExampleElement )
                    {
                        ( ( AbstractExampleElement ) group ).setGroup ( this );
                    }
                    groups.add ( group );
                }
                catch ( final Exception e )
                {
                    final String msg = "Unable to initialize group: %s";
                    LoggerFactory.getLogger ( AbstractExampleGroup.class ).error ( String.format ( msg, groupClass ), e );
                }
            }
        }
        return groups;
    }

    /**
     * Returns list of example sub group classes for this example group.
     *
     * @return list of example sub group classes for this example group
     */
    protected List getExampleGroupClasses ()
    {
        return Collections.emptyList ();
    }

    @Override
    public List getExamples ()
    {
        if ( examples == null )
        {
            final List exampleClasses = getExampleClasses ();
            examples = new ArrayList ( exampleClasses.size () );
            for ( final Class exampleClass : exampleClasses )
            {
                try
                {
                    final Example example = ReflectUtils.createInstance ( exampleClass );
                    if ( example instanceof AbstractExampleElement )
                    {
                        ( ( AbstractExampleElement ) example ).setGroup ( this );
                    }
                    examples.add ( example );
                }
                catch ( final Exception e )
                {
                    final String msg = "Unable to initialize example: %s";
                    LoggerFactory.getLogger ( AbstractExampleGroup.class ).error ( String.format ( msg, exampleClass ), e );
                }
            }
        }
        return examples;
    }

    /**
     * Returns list of example classes for this example group.
     *
     * @return list of example classes for this example group
     */
    protected List getExampleClasses ()
    {
        return Collections.emptyList ();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy