001package jmri.util.swing.mdi;
002
003import java.awt.Frame;
004import java.util.List;
005
006import javax.swing.*;
007
008import jmri.util.JmriJFrame;
009
010/**
011 * Display a JmriPanel in a JInternalFrame of its own.
012 *
013 * Dispose() of the panel is invoked when the containing window is fully closed
014 * via a listener installed here.
015 *
016 * @author Bob Jacobsen Copyright 2010
017 * @since 2.9.4
018 */
019public class JmriJInternalFrameInterface implements jmri.util.swing.WindowInterface {
020
021    public JmriJInternalFrameInterface(JmriJFrame mainFrame, JDesktopPane desktop) {
022        this.mainFrame = mainFrame;
023        this.desktop = desktop;
024    }
025
026    JDesktopPane desktop;
027    JmriJFrame mainFrame;
028
029    @Override
030    public void show(final jmri.util.swing.JmriPanel child,
031            jmri.util.swing.JmriAbstractAction act,
032            Hint hint) {
033        // create new internal frame
034        JInternalFrame frame = new JInternalFrame(child.getTitle(),
035                true, //resizable
036                true, //closable
037                true, //maximizable
038                true);//iconifiable
039        frame.setLocation(50, 50);
040
041        // add gui object, responsible for own layout
042        frame.add(child);
043
044        // add menus if requested
045        JMenuBar bar = frame.getJMenuBar();
046        if (bar == null) {
047            bar = new JMenuBar();
048        }
049        List<JMenu> list = child.getMenus();
050        for (JMenu menu : list) {
051            bar.add(menu);
052        }
053
054        // add help menu if requested; this is similar
055        // to code in JmriJFrame
056        if (child.getHelpTarget() != null) {
057            // add Help menu
058            jmri.util.HelpUtil.helpMenu(bar, child.getHelpTarget(), true);
059        }
060        frame.setJMenuBar(bar);
061
062        // set title if available
063        if (child.getTitle() != null) {
064            frame.setTitle(child.getTitle());
065        }
066
067        frame.setMinimumSize(child.getMinimumDimension());
068
069        // arrange to run dispose on close
070        //frame.addWindowListener( new java.awt.event.WindowAdapter(){
071        //    jmri.util.swing.JmriPanel c;
072        //    { c = child; }
073        //    public void windowClosed(java.awt.event.WindowEvent e) {
074        //        c.dispose();
075        //    }
076        //});
077        // add to desktop
078        frame.pack();
079        frame.setVisible(true);
080        desktop.add(frame);
081        frame.moveToFront();
082    }
083
084    @Override
085    public void show(final jmri.util.swing.JmriPanel child,
086            jmri.util.swing.JmriAbstractAction act) {
087
088        show(child, act, Hint.DEFAULT);
089    }
090
091    @Override
092    public void dispose() {
093    }
094
095    /**
096     * Create new windows on each request
097     */
098    @Override
099    public boolean multipleInstances() {
100        return true;
101    }
102
103    @Override
104    public Frame getFrame() {
105        return (this.mainFrame != null) ? this.mainFrame : null;
106    }
107
108}