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

com.alee.managers.settings.Configuration Maven / Gradle / Ivy

/*
 * 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.managers.settings;

import com.alee.api.jdk.SerializableSupplier;

import javax.swing.*;
import java.io.Serializable;

/**
 * Configuration provided for each {@link SettingsProcessor}.
 * It can be extended to provide additional settings for custom {@link SettingsProcessor} implementations.
 *
 * @param  {@link Serializable} value type
 * @author Mikle Garin
 * @see How to use SettingsManager
 * @see SettingsProcessor
 * @see UISettingsManager
 * @see SettingsManager
 */
public class Configuration implements Serializable
{
    /**
     * Settings group.
     */
    protected final String group;

    /**
     * Settings key.
     */
    protected final String key;

    /**
     * {@link SerializableSupplier} for default value.
     */
    protected final SerializableSupplier defaultValue;

    /**
     * Whether to load initial available settings into the {@link JComponent} or not.
     */
    protected final boolean loadInitialSettings;

    /**
     * Whether to apply settings changes to the {@link JComponent} or not.
     */
    protected final boolean applySettingsChanges;

    /**
     * Constructs new {@link Configuration}.
     *
     * @param key settings key
     */
    public Configuration ( final String key )
    {
        this ( SettingsManager.getDefaultSettingsGroup (), key, ( SerializableSupplier ) null, true, false );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param key                  settings key
     * @param loadInitialSettings  whether to load initial available settings into the {@link JComponent} or not
     * @param applySettingsChanges whether to apply settings changes to the {@link JComponent} or not
     */
    public Configuration ( final String key, final boolean loadInitialSettings, final boolean applySettingsChanges )
    {
        this ( SettingsManager.getDefaultSettingsGroup (), key, ( SerializableSupplier ) null, true, false );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param key          settings key
     * @param defaultValue default value
     */
    public Configuration ( final String key, final V defaultValue )
    {
        this ( SettingsManager.getDefaultSettingsGroup (), key, new StaticDefaultValue ( defaultValue ), true, false );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param key          settings key
     * @param defaultValue {@link SerializableSupplier} for default value
     */
    public Configuration ( final String key, final SerializableSupplier defaultValue )
    {
        this ( SettingsManager.getDefaultSettingsGroup (), key, defaultValue, true, false );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param key                  settings key
     * @param defaultValue         default value
     * @param loadInitialSettings  whether to load initial available settings into the {@link JComponent} or not
     * @param applySettingsChanges whether to apply settings changes to the {@link JComponent} or not
     */
    public Configuration ( final String key, final V defaultValue,
                           final boolean loadInitialSettings, final boolean applySettingsChanges )
    {
        this ( SettingsManager.getDefaultSettingsGroup (), key, new StaticDefaultValue ( defaultValue ),
                loadInitialSettings, applySettingsChanges );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param key                  settings key
     * @param defaultValue         {@link SerializableSupplier} for default value
     * @param loadInitialSettings  whether to load initial available settings into the {@link JComponent} or not
     * @param applySettingsChanges whether to apply settings changes to the {@link JComponent} or not
     */
    public Configuration ( final String key, final SerializableSupplier defaultValue,
                           final boolean loadInitialSettings, final boolean applySettingsChanges )
    {
        this ( SettingsManager.getDefaultSettingsGroup (), key, defaultValue,
                loadInitialSettings, applySettingsChanges );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param group settings group
     * @param key   settings key
     */
    public Configuration ( final String group, final String key )
    {
        this ( group, key, ( SerializableSupplier ) null, true, false );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param group                settings group
     * @param key                  settings key
     * @param loadInitialSettings  whether to load initial available settings into the {@link JComponent} or not
     * @param applySettingsChanges whether to apply settings changes to the {@link JComponent} or not
     */
    public Configuration ( final String group, final String key,
                           final boolean loadInitialSettings, final boolean applySettingsChanges )
    {
        this ( group, key, ( SerializableSupplier ) null, loadInitialSettings, applySettingsChanges );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param group        settings group
     * @param key          settings key
     * @param defaultValue default value
     */
    public Configuration ( final String group, final String key, final V defaultValue )
    {
        this ( group, key, new StaticDefaultValue ( defaultValue ), true, false );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param group        settings group
     * @param key          settings key
     * @param defaultValue {@link SerializableSupplier} for default value
     */
    public Configuration ( final String group, final String key, final SerializableSupplier defaultValue )
    {
        this ( group, key, defaultValue, true, false );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param group                settings group
     * @param key                  settings key
     * @param defaultValue         default value
     * @param loadInitialSettings  whether to load initial available settings into the {@link JComponent} or not
     * @param applySettingsChanges whether to apply settings changes to the {@link JComponent} or not
     */
    public Configuration ( final String group, final String key, final V defaultValue,
                           final boolean loadInitialSettings, final boolean applySettingsChanges )
    {
        this ( group, key, defaultValue != null ? new StaticDefaultValue ( defaultValue ) : null,
                loadInitialSettings, applySettingsChanges );
    }

    /**
     * Constructs new {@link Configuration}.
     *
     * @param group                settings group
     * @param key                  settings key
     * @param defaultValue         {@link SerializableSupplier} for default value
     * @param loadInitialSettings  whether to load initial available settings into the {@link JComponent} or not
     * @param applySettingsChanges whether to apply settings changes to the {@link JComponent} or not
     */
    public Configuration ( final String group, final String key, final SerializableSupplier defaultValue,
                           final boolean loadInitialSettings, final boolean applySettingsChanges )
    {
        super ();
        this.group = group;
        this.key = key;
        this.defaultValue = defaultValue;
        this.loadInitialSettings = loadInitialSettings;
        this.applySettingsChanges = applySettingsChanges;
    }

    /**
     * Returns settings group.
     *
     * @return settings group
     */
    public String group ()
    {
        return group;
    }

    /**
     * Returns settings key.
     *
     * @return settings key
     */
    public String key ()
    {
        return key;
    }

    /**
     * Returns default value.
     *
     * @return default value
     */
    public V defaultValue ()
    {
        return defaultValue != null ? defaultValue.get () : null;
    }

    /**
     * Returns whether initial available settings should be loaded into the {@link JComponent} or not.
     *
     * @return true if initial available settings should be loaded into the {@link JComponent}, false otherwise
     */
    public boolean isLoadInitialSettings ()
    {
        return loadInitialSettings;
    }

    /**
     * Returns whether settings changes should be applied to the {@link JComponent} or not.
     *
     * @return {@code true} if settings changes should be applied to the {@link JComponent}, {@code false} otherwise
     */
    public boolean isApplySettingsChanges ()
    {
        return applySettingsChanges;
    }

    @Override
    public String toString ()
    {
        final String name = getClass ().getSimpleName ();
        final String settings = "[ " + group + " -> " + key + " ]";
        return name + settings;
    }

    /**
     * Simple {@link SerializableSupplier} for holding static default values.
     *
     * @param  {@link Serializable} value type
     */
    protected static class StaticDefaultValue implements SerializableSupplier
    {
        /**
         * Static value.
         */
        private final V value;

        /**
         * Csontructs new {@link StaticDefaultValue}.
         *
         * @param value static value
         */
        public StaticDefaultValue ( final V value )
        {
            super ();
            this.value = value;
        }

        @Override
        public V get ()
        {
            return value;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy