// Swing Desktop Pane idea (RJM Programming - October, 2014) ... based on ...
//   Introduction to Java 2 
//    WEA Training Manual
//    by Phil Cogar 
//    pp. 111-113
// ... of huge help (thanks) was ...
// http://stackoverflow.com/questions/7218971/java-method-works-on-windows-but-not-macintosh

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class JDeskTop extends JFrame implements ActionListener {

	JButton ourbutton; // instance variables
	Container ourcontainer;
	JDesktopPane ourdesktop;
	JInternalFrame iFrame;

	static int frameCount = 1;
	static final int xoffset = 25;	// offsets are constants
	static final int yoffset = 25;

	public JDeskTop() {	// constructor
		super("DesktopPane Example") ; 
		ourcontainer = this.getContentPane();
		ourbutton = new JButton("Click to Create More Internal Frames"); 
		ourbutton.addActionListener(this); 
		// create desktop pane, add internal pane
		ourcontainer.add(ourbutton, BorderLayout.SOUTH); 
		ourdesktop = new JDesktopPane(); 
		ourcontainer.add(ourdesktop);
		createInternalFrame();  //configure frame
		addWindowListener(new  WindowEventHandler());
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setSize(800, 600);  // width, height  2x
		setVisible(true);  // display frame
	}

    // Add internal frame to desktop pane 

	public void createInternalFrame() {
		iFrame = new JInternalFrame("Frame " +  (frameCount++),
			true,	// can be resized 
			true,	// can be closed 
			true,	// can be maximised 
			true);  // can be iconified
		// set location of frame
		ourdesktop.add(iFrame); // allow frame to be selected
		iFrame.setLocation(xoffset*(frameCount	- 2),
			yoffset*(frameCount - 2));
		iFrame.setSize(400,	300);  // 2 times bigger
		iFrame.setFrameIcon(new ImageIcon("droppunt.gif"));

        if (System.getProperty("os.name").startsWith("Mac OS")) {
            iFrame.putClientProperty("JInternalFrame.isPalette", true);
        } else {
            ((BasicInternalFrameUI) iFrame.getUI()).setNorthPane(null);
        }
        iFrame.add(createTabbedPane());
        iFrame.pack();
        iFrame.setVisible(true);

		try {
			iFrame.setSelected(true);
		} catch (java.beans.PropertyVetoException ex) { 
			System.out.println("Exception selecting internal frame.");
		}
	}


	// take up some space
	private JTabbedPane createTabbedPane() {
        JTabbedPane jtp = new JTabbedPane();
        createTab(jtp, "One");
        createTab(jtp, "Two");
        return jtp;
    }

    private void createTab(JTabbedPane jtp, String s) {
        jtp.add(s, new JLabel("TabbedPane " + s, JLabel.CENTER));
    }

  	public void actionPerformed(ActionEvent oure) {
    	createInternalFrame();
  	}


  	class WindowEventHandler extends WindowAdapter {
   		public void windowClosing(WindowEvent ourevt) {
    		System.exit(0);
   		}
  	}

  	public static void main(String[] args) {
   		JDeskTop ourframe = new JDeskTop();
  	}

}