JInternalFrame

An answer to a mail I received.

The following code is a proof of concept that parts of DF can be used together with JInternalFrames/JDesktopPane. The necessary factories are already in the repository and will be part of version 1.0.8p5f (which I’ll upload in this week). No support for ScreenDockStation yet.


import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Point;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import bibliothek.gui.dock.FlapDockStation;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.station.flap.ButtonPane;
import bibliothek.gui.dock.station.flap.DefaultFlapWindow;
import bibliothek.gui.dock.station.flap.DefaultFlapWindowFactory;
import bibliothek.gui.dock.station.flap.FlapWindow;
import bibliothek.gui.dock.util.ComponentWindowProvider;

public class Dock14 extends JInternalFrame{
	public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        JDesktopPane desktop = new JDesktopPane();
        frame.add( desktop );
        
        Dock14 dock = new Dock14( desktop );
        desktop.add( dock );
        dock.setBounds( 20, 20, 400, 400 );
        dock.setVisible( true );
        
        frame.setBounds( 20, 20, 500, 500 );
        frame.setVisible( true );
    }
    
	private JDesktopPane desktop;
    private CControl control;
    
    public Dock14( JDesktopPane desktop ){
    	this.desktop = desktop;
    	setSize( 600, 500 );
    	setDefaultCloseOperation( EXIT_ON_CLOSE );

    	control = new CControl( new ComponentWindowProvider( this ) );
    	control.putProperty( FlapDockStation.WINDOW_FACTORY, new DefaultFlapWindowFactory(){
    		@Override
    		public FlapWindow create( FlapDockStation station, ButtonPane buttonPane ){
    			return new DefaultFlapWindow( station, buttonPane, new InternalDialog( Dock14.this.desktop ) );
    		}
    	});
    	
    	add(control.getContentArea());
    	
    	CGrid grid = new CGrid( control );
    	grid.add(  0,  0, 10, 10, get( "red", Color.RED ) );
    	grid.add(  0, 10, 10, 10, get( "green", Color.GREEN ) );
    	grid.add( 10,  0, 10, 10, get( "blue", Color.BLUE ) );
    	grid.add( 10, 10, 10, 10, get( "yellow", Color.YELLOW ) );
    	control.getContentArea().deploy( grid );
    }
    
    private DefaultSingleCDockable get(String id, Color color){
    	JPanel panel = new JPanel();
    	panel.setBackground( color );
    	panel.setOpaque( true );
    	
    	DefaultSingleCDockable result = new DefaultSingleCDockable( id, id, panel );
    	result.setCloseable( true );
    	return result;
    }
    
    private static class InternalDialog extends JPanel implements DefaultFlapWindow.Parent{
    	private JDesktopPane desktop;
    	
    	public InternalDialog( JDesktopPane desktop ){
    		this.desktop = desktop;
    		setLayout( new GridLayout( 1, 1 ) );
    		desktop.add( this );
    		desktop.setLayer( this, JDesktopPane.MODAL_LAYER );
    		setVisible( false );
//    		setLayer( JDesktopPane.MODAL_LAYER );
    	}
    	
    	@Override
    	public Container getContentPane(){
    		if( getComponentCount() == 0 ){
    			return null;
    		}
    		return (Container)getComponent( 0 );
    	}
    	
    	@Override
    	public void setContentPane( Container content ){
	    	removeAll();
	    	if( content != null ){
	    		add( content );
	    	}
    	}
    	
		public Component asComponent(){
			return this;
		}

		public void destroy(){
			setVisible( false );
			desktop.remove( this );
		}

		public boolean isParentValid(){
			return true;
		}
		
		public void setParentLocation( Point location ){
			Point delta = new Point( 0, 0 );
			SwingUtilities.convertPointToScreen( delta, desktop );
			
			Point finalResult = new Point( location.x - delta.x, location.y - delta.y );
			setLocation( finalResult );
		}
    }
}