001package jmri.jmrit.catalog;
002
003import java.io.File;
004import javax.swing.BoxLayout;
005import javax.swing.JLabel;
006import javax.swing.JPanel;
007import javax.swing.JScrollPane;
008import javax.swing.JTree;
009import javax.swing.event.TreeSelectionEvent;
010import javax.swing.event.TreeSelectionListener;
011import javax.swing.tree.DefaultMutableTreeNode;
012import javax.swing.tree.DefaultTreeSelectionModel;
013import javax.swing.tree.TreePath;
014import jmri.InstanceManager;
015import org.slf4j.Logger;
016import org.slf4j.LoggerFactory;
017
018/**
019 * Create a JPanel containing a tree of resources.
020 * <p>
021 * The tree has two top-level visible nodes. One, "icons", represents the
022 * contents of the icons directory in the resources tree in the .jar file. The
023 * other, "files", is all files found in the "resources" filetree in the
024 * preferences directory. Note that this means that files in the distribution
025 * directory are _not_ included.
026 *
027 * @author Bob Jacobsen Copyright 2002
028 */
029public class CatalogPane extends JPanel {
030
031    JLabel preview = new JLabel();
032
033    public CatalogPane() {
034
035        super(true);
036
037        // create basic GUI
038        dTree = new JTree(InstanceManager.getDefault(CatalogTreeModel.class));
039        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
040
041        // build the tree GUI
042        add(new JScrollPane(dTree));
043        dTree.setRootVisible(false);
044        dTree.setShowsRootHandles(true);
045        dTree.setScrollsOnExpand(true);
046        dTree.setExpandsSelectedPaths(true);
047
048        dTree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
049
050        dTree.addTreeSelectionListener(new TreeSelectionListener() {
051            @Override
052            public void valueChanged(TreeSelectionEvent e) {
053                if (!dTree.isSelectionEmpty() && dTree.getSelectionPath() != null) {
054                    // somebody has been selected
055                    preview.setIcon(getSelectedIcon());
056                } else {
057                    preview.setIcon(null);
058                }
059            }
060        });
061
062        // add a listener for debugging
063        if (log.isDebugEnabled()) {
064            dTree.addTreeSelectionListener(new TreeSelectionListener() {
065                @Override
066                public void valueChanged(TreeSelectionEvent e) {
067                    if (!dTree.isSelectionEmpty() && dTree.getSelectionPath() != null) {
068                        // somebody has been selected
069                        log.debug("Selection event with {}", dTree.getSelectionPath());
070                        log.debug("          icon: {}", getSelectedIcon());
071                    }
072                }
073            });
074        }
075        JPanel previewPanel = new JPanel();
076        previewPanel.setLayout(new BoxLayout(previewPanel, BoxLayout.X_AXIS));
077        previewPanel.add(new JLabel(Bundle.getMessage("FilePreviewLabel")));
078        JScrollPane js = new JScrollPane(preview);
079        previewPanel.add(js);
080        add(previewPanel);
081    }
082
083    public NamedIcon getSelectedIcon() {
084        if (dTree.isSelectionEmpty() || dTree.getSelectionPath() == null) {
085            return null;
086        }
087        // somebody has been selected
088        if (log.isDebugEnabled()) log.debug("getSelectedIcon with {}", dTree.getSelectionPath());
089        TreePath path = dTree.getSelectionPath();
090        int level = path.getPathCount();
091        if (level < 3) {
092            return null;
093        }
094        StringBuilder buf;
095        String name;
096        if (((DefaultMutableTreeNode) path.getPathComponent(1)).getUserObject().equals("resources")) {
097            // process a .jar icon
098            buf = new StringBuilder(CatalogTreeModel.resourceRoot);
099            for (int i = 2; i < level; i++) {
100                buf.append("/");
101                buf.append((String) ((DefaultMutableTreeNode) path.getPathComponent(i)).getUserObject());
102            }
103        } else if (((DefaultMutableTreeNode) path.getPathComponent(1)).getUserObject().equals("files")) {
104            // process a file
105            buf = new StringBuilder(CatalogTreeModel.fileRoot);
106            buf.append(File.separator);   // Separator need between the root and the user directory/file
107            buf.append((String) ((DefaultMutableTreeNode) path.getPathComponent(2)).getUserObject());
108            for (int i = 3; i < level; i++) {
109                buf.append(File.separator);
110                buf.append((String) ((DefaultMutableTreeNode) path.getPathComponent(i)).getUserObject());
111            }
112        } else {
113            log.error("unexpected first element on getSelectedIcon: {}", path.getPathComponent(1));
114            return null;
115        }
116        name = buf.toString();
117        if (log.isDebugEnabled()) log.debug("attempt to load file from {}", name);
118        return NamedIcon.getIconByName(name);
119    }
120
121    JTree dTree;
122
123    private final static Logger log = LoggerFactory.getLogger(CatalogPane.class);
124
125}