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

bibliothek.gui.dock.util.AbstractUIValue Maven / Gradle / Ivy

The newest version!
/*
 * Bibliothek - DockingFrames
 * Library built on Java/Swing, allows the user to "drag and drop"
 * panels containing any Swing-Component the developer likes to add.
 * 
 * Copyright (C) 2007 Benjamin Sigg
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Benjamin Sigg
 * [email protected]
 * CH - Switzerland
 */
package bibliothek.gui.dock.util;

import bibliothek.gui.dock.util.color.AbstractDockColor;
import bibliothek.util.Path;

/**
 * An abstract implementation of {@link UIValue}. This class contains more than
 * just one resource:
 * 
    *
  • override: is a value that can be set from outside and overrides all other values
  • *
  • value: is the value obtained through the {@link UIProperties}
  • *
  • backup: is a value used when all other values are unavailable
  • *

* This class also has methods to add or remove itself from a {@link UIProperties}. * * @author Benjamin Sigg * @param the kind of values this {@link UIValue} handles * @param the kind of {@link UIValue} that the {@link UIProperties} will handle. * This class is either abstract, or a subclass of U. */ public abstract class AbstractUIValue> implements UIValue { /** the value set by a client */ private V override; /** the value set by a {@link UIProperties} */ private V value; /** the backup value for emergencies */ private V backup; /** the id for which this {@link UIValue} should listen */ private String id; /** the kind of this {@link UIValue} */ private Path kind; /** the current owner of this {@link UIValue} */ private UIProperties manager; /** an override value of null is returned by {@link #value()} */ private boolean overrideNull = false; /** * Creates a new {@link UIValue}. * @param id the id of the resource for which this should listen for */ public AbstractUIValue( String id ){ this( id, null, null ); } /** * Creates a new {@link UIValue}. * @param id the id of the resource for which this should listen for * @param kind the kind of {@link UIValue} this is */ public AbstractUIValue( String id, Path kind ){ this( id, kind, null ); } /** * Creates a new {@link UIValue}. * @param id the id of the resource for which this should listen for * @param backup a backup resource, can be null */ public AbstractUIValue( String id, V backup ){ this( id, null, backup ); } /** * Creates a new {@link UIValue}. * @param id the id of the resource for which this should listen for * @param kind the kind of {@link UIValue} this is, can be null * @param backup a backup resource, can be null */ public AbstractUIValue( String id, Path kind, V backup ){ if( id == null ) throw new IllegalArgumentException( "id must no be null" ); if( kind == null ) throw new IllegalArgumentException( "kind must not be null" ); this.id = id; this.kind = kind; this.backup = backup; } /** * Returns this. This method can only be implemented when * the generic boundaries are met, so this methods ensures that this * is really an U, or abstract. * @return this */ protected abstract U me(); /** * Changes the identifier of this value. * @param id the new id, must not be null */ public void setId( String id ) { if( id == null ) throw new IllegalArgumentException( "id must not be null" ); this.id = id; if( this.manager != null ){ U me = me(); this.manager.remove( me ); this.manager.add( id, kind, me ); } } /** * Gets the identifier of this value. * @return the identifier, never null */ public String getId() { return id; } /** * Changes the kind of this value. The kind is used by the {@link UIProperties} * to find out, which {@link UIBridge} should be used to interact with * this {@link UIValue}. * @param kind the new kind, not null. The kind should be * a class or interfaces that is implemented by this {@link UIValue}. */ public void setKind( Path kind ) { if( kind == null ) throw new IllegalArgumentException( "kind must not be null" ); this.kind = kind; if( this.manager != null ){ U me = me(); this.manager.remove( me ); this.manager.add( id, kind, me ); } } /** * Gets the kind of this value. See {@link #setKind(Path)}. * @return the kind, never null */ public Path getKind() { return kind; } /** * Sets the manager which owns this {@link UIValue}, this will * automatically be added or removed from the current manager. * @param manager the new manager, can be null */ public void setManager( UIProperties manager ){ if( manager != this.manager ){ U me = me(); if( this.manager != null ) this.manager.remove( me ); this.manager = manager; if( this.manager != null ) this.manager.add( id, kind, me ); } } public void set( V value ) { V oldValue = value(); this.value = value; V newValue = value(); if( oldValue != newValue ){ changed( oldValue, newValue ); } } /** * Updates the value of this {@link UIValue} without actually installing this * on manager. * @param manager the manager from which to read the value */ public void update( UIProperties manager ){ manager.get( id, kind, me() ); } /** * Gets the first non-null value of the list * override, value, backup. * @return a resource or null */ public V value(){ if( overrideNull || override != null ) return override; if( value != null ) return value; return backup; } /** * Called when the resource of this {@link AbstractDockColor} has changed * @param oldValue the old value, can be null * @param newValue the new value, can be null */ protected abstract void changed( V oldValue, V newValue ); /** * Sets the override value. * @param value the new override or null */ public void setValue( V value ) { setValue( value, false ); } /** * Sets the override value. Please note that some modules won't work properly if {@link #value()} returns * null, use forceNull with care. * @param value the new value, can be null * @param forceNull if true and value is null, then * the result of {@link #value()} is null too */ public void setValue( V value, boolean forceNull ){ V oldValue = value(); this.override = value; this.overrideNull = forceNull; V newValue = value(); if( oldValue != newValue ){ changed( oldValue, newValue ); } } /** * Gets the override value. * @return the override or null */ public V getValue() { return override; } /** * Sets the backup value. * @param backup the backup or null */ public void setBackup( V backup ) { V oldColor = value(); this.backup = backup; V newColor = value(); if( oldColor != newColor ){ changed( oldColor, newColor ); } } /** * Gets the backup value. * @return the backup or null */ public V getBackup() { return backup; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy