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

mmb.menu.components.Placeholder Maven / Gradle / Ivy

Go to download

Dependency for the MultiMachineBuilder, a voxel game about building an industrial empire in a finite world. THIS RELEASE IS NOT PLAYABLE. To play the game, donwload from >ITCH.IO LINK HERE< or >GH releases link here<

There is a newer version: 0.6
Show newest version
/**
 * 
 */
package mmb.menu.components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.function.Consumer;

import javax.swing.JComponent;

import mmb.NN;
import mmb.Nil;
import mmb.content.modular.gui.SafeCloseable;
import mmb.data.reactive.ListenableProperty;

/**
 * A placeholder displays a component provided by a property
 * @author oskar
 */
public class Placeholder extends JComponent implements SafeCloseable {
	private static final long serialVersionUID = -2374883511304183860L;
	
	/**
	 * Creates a placeholder with a property
	 * @param property source of components to use
	 */
	public Placeholder(ListenableProperty property) {
		setProperty(property);
		setLayout(new BorderLayout());
	}
	/**
	 * Creates a placeholder without a property
	 */
	public Placeholder() {
		setLayout(new BorderLayout());
	}
	
	//Component properties
	@Override
	public void close(){
		setProperty(null);
	}
	
	//The underlying component
	@Nil private transient ListenableProperty property;
	@Nil Component oldComponent;
	/** 
	 * Sets up the component after closing 
	 * @param newp  new property
	 */
	public void setProperty(@Nil ListenableProperty newp) {
		ListenableProperty old = property;
		if(old != null) old.unlistenadd(listener);
		
		if(newp != null) {
			newp.listenadd(listener);
			listener(newp.get());
		}else {
			listener(null);
		}
		property = newp;
		
	}
	/** @return current underlying property */
	public @Nil ListenableProperty getProperty(){
		return property;
	}
	
	//Listener
	@NN private final transient Consumer listener = this::listener;
	private void listener(@Nil Component newComponent) {
		if(oldComponent != null) remove(oldComponent);
		setLayout(new BorderLayout());
		if(newComponent != null) add(newComponent, BorderLayout.CENTER);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy