package j3deditor.bin;


import j3deditor.bin.components.ComponentUtils;

import j3deditor.bin.util.Utilities;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;


/**
 * Modified version of <code>JInternalFrame</code>.
 * Contains methods for attaching this <code>JInternalFrame</code>
 * to an instance of <code>JDesktopPane</code>,
 * for applying user preferences to it and
 * disposing it correctly.
 *
 * @author Risto Seene
 */
public class InnerFrame extends JInternalFrame{

	/**
	 * Creates an instance of <code>InnerFrame</code>.
	 * Calls the constructor of <code>JInternalFrame</code>
	 * to create resizable, closable, maximizable and iconifiable frame with no title.
	 * 
	 * @see javax.swing.JInternalFrame#JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)
	 */
	public InnerFrame(){
		super("", true, true, true, true);
	}


	/**
	 * Attaches this internal frame to a
	 * <code>JDesktopPane</code> and selects this JInternalFrame.
	 *
	 * @param parent
	 *	<code>JDesktopPane</code> this internal frame
	 *	will be attached to
	 */
	public void showWindow(JDesktopPane parent){
		parent.add(this);

		applyPreferences();
		this.setVisible(true);
		try{
			this.setSelected(true);
		} catch(Throwable t){
		}
	}


	/**
	 * Applies user preferences to this internal frame.
	 */
	private void applyPreferences(){
		try{
			setMaximum(Utilities.userdata.getBooleanData("internalframe_maximized"));
		} catch(Throwable t){
		}
	}

	/**
	 * Closes this frame correctly.
	 * If overridden, <code>super.doDefaultCloseAction()</code> must be called
	 * in overriding method!
	 */
	@Override
	public void doDefaultCloseAction(){
		this.cleanUp();
		super.doDefaultCloseAction();
	}

	/**
	 * Removes all components from this frame.
	 * If overridden, <code>super.cleanUp()</code> must be called
	 * in overriding method!
	 */
	public void cleanUp(){
		ComponentUtils.removeChildren(this);
	}

}
