001package jmri.util.swing.mdi;
002
003import java.awt.BorderLayout;
004import java.awt.Color;
005import java.awt.Dimension;
006import javax.swing.AbstractAction;
007import javax.swing.BorderFactory;
008import javax.swing.JDesktopPane;
009import javax.swing.JMenu;
010import javax.swing.JMenuBar;
011import javax.swing.JScrollPane;
012import javax.swing.JSplitPane;
013import javax.swing.JToolBar;
014import javax.swing.JTree;
015import javax.swing.tree.DefaultMutableTreeNode;
016import javax.swing.tree.TreeNode;
017import javax.swing.tree.TreeSelectionModel;
018import jmri.util.swing.JMenuUtil;
019import jmri.util.swing.JToolBarUtil;
020import jmri.util.swing.JTreeUtil;
021
022/**
023 * Core JMRI JInternalPane GUI window.
024 *
025 * @author Bob Jacobsen Copyright 2010
026 * @since 2.9.4
027 */
028public class MdiMainFrame extends jmri.util.JmriJFrame {
029
030    /**
031     * Create and initialize a multi-pane GUI window.
032     *
033     * @param name        name and title of the frame
034     * @param treeFile    name of file containing XML tree for left pane
035     * @param menubarFile name of file containing XML menu structure
036     * @param toolbarFile name of file containing XML toolbar structure
037     */
038    public MdiMainFrame(String name, String treeFile, String menubarFile, String toolbarFile) {
039        super(name);
040        configureFrame(treeFile);
041        addMainMenuBar(menubarFile);
042        addMainToolBar(toolbarFile);
043        pack();
044    }
045
046    JSplitPane leftRightSplitPane;
047
048    JDesktopPane desktop;
049
050    JmriJInternalFrameInterface rightWI;
051
052    protected void configureFrame(String treeFile) {
053        desktop = new JDesktopPane();
054        desktop.setBorder(BorderFactory.createLineBorder(Color.black));
055
056        leftRightSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, makeLeftTree(treeFile), desktop);
057        leftRightSplitPane.setOneTouchExpandable(true);
058        leftRightSplitPane.setResizeWeight(0.0);  // emphasize right part
059
060        add(leftRightSplitPane, BorderLayout.CENTER);
061    }
062
063    protected JScrollPane makeLeftTree(String treeFile) {
064        final JTree tree;
065        TreeNode topNode;
066
067        rightWI = new JmriJInternalFrameInterface(this, desktop);
068
069        topNode = JTreeUtil.loadTree(treeFile, rightWI, null);  // no context object
070
071        tree = new JTree(topNode);
072        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
073        tree.setRootVisible(false);  // allow multiple roots
074
075        // install listener
076        tree.addTreeSelectionListener((javax.swing.event.TreeSelectionEvent e) -> {
077            DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
078            if (node == null) {
079                return; //Nothing is selected.
080            }
081            if (node.getUserObject() == null) {
082                return; // Not an interesting node
083            }
084            if (node.getUserObject() instanceof AbstractAction) {
085                AbstractAction action = (AbstractAction) node.getUserObject();
086                action.actionPerformed(null);
087            }
088        });
089        // install in scroll area
090        JScrollPane treeView = new JScrollPane(tree);
091        treeView.setMinimumSize(new Dimension(0, 0));
092        treeView.setPreferredSize(new Dimension(150, 600));
093        return treeView;
094    }
095
096    protected void addMainMenuBar(String menuFile) {
097        JMenuBar menuBar = new JMenuBar();
098
099        JMenu[] menus = JMenuUtil.loadMenu(menuFile, rightWI, null); // no central context
100        for (JMenu j : menus) {
101            menuBar.add(j);
102        }
103
104        setJMenuBar(menuBar);
105    }
106
107    protected void addMainToolBar(String toolBarFile) {
108
109        JToolBar toolBar = JToolBarUtil.loadToolBar(toolBarFile, rightWI, null);  // no context
110
111        // this takes up space at the top until pulled to floating
112        add(toolBar, BorderLayout.NORTH);
113    }
114
115    /**
116     * Only close frame, etc, dispose() disposes of all cached panes
117     */
118    @Override
119    public void dispose() {
120        rightWI.dispose();
121        super.dispose();
122    }
123
124}