001// License: GPL. For details, see Readme.txt file.
002package org.openstreetmap.gui.jmapviewer.checkBoxTree;
003
004import java.awt.Color;
005import java.awt.Component;
006import java.awt.Font;
007import java.awt.event.MouseAdapter;
008
009import javax.swing.JTree;
010import javax.swing.UIManager;
011import javax.swing.tree.DefaultMutableTreeNode;
012import javax.swing.tree.DefaultTreeCellRenderer;
013import javax.swing.tree.TreeCellRenderer;
014
015/**
016 * Renderer for checkBox Tree
017 * 
018 * @author galo
019 */
020public class CheckBoxNodeRenderer implements TreeCellRenderer {
021
022    private final CheckBoxNodePanel panel = new CheckBoxNodePanel();
023    private final DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
024    private final Color selectionForeground;
025    private final Color selectionBackground;
026    private final Color textForeground;
027    private final Color textBackground;
028
029    /**
030     * Constructs a new {@code CheckBoxNodeRenderer}.
031     */
032    public CheckBoxNodeRenderer() {
033        final Font fontValue = UIManager.getFont("Tree.font");
034        if (fontValue != null) panel.getLabel().setFont(fontValue);
035
036        final Boolean focusPainted =
037            (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
038        panel.check.setFocusPainted(focusPainted != null && focusPainted);
039
040        selectionForeground = UIManager.getColor("Tree.selectionForeground");
041        selectionBackground = UIManager.getColor("Tree.selectionBackground");
042        textForeground = UIManager.getColor("Tree.textForeground");
043        textBackground = UIManager.getColor("Tree.textBackground");
044    }
045
046    protected CheckBoxNodePanel getPanel() {
047        return panel;
048    }
049
050    public void addNodeListener(MouseAdapter listener) {
051        panel.addMouseListener(listener);
052    }
053
054    // -- TreeCellRenderer methods --
055
056    @Override
057    public Component getTreeCellRendererComponent(final JTree tree,
058        final Object value, final boolean selected, final boolean expanded,
059        final boolean leaf, final int row, final boolean hasFocus) {
060        CheckBoxNodeData data = null;
061        if (value instanceof DefaultMutableTreeNode) {
062            final DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
063            final Object userObject = node.getUserObject();
064            if (userObject instanceof CheckBoxNodeData) {
065                data = (CheckBoxNodeData) userObject;
066            }
067        }
068
069        //final String stringValue =
070        //    tree.convertValueToText(value, selected, expanded, leaf, row, false);
071        //panel.label.setText(stringValue);
072
073        panel.setSelected(Boolean.FALSE);
074
075        panel.setEnabled(tree.isEnabled());
076
077        if (selected) {
078            panel.setForeground(selectionForeground);
079            panel.setBackground(selectionBackground);
080            panel.getLabel().setForeground(selectionForeground);
081            panel.getLabel().setBackground(selectionBackground);
082        } else {
083            panel.setForeground(textForeground);
084            panel.setBackground(textBackground);
085            panel.getLabel().setForeground(textForeground);
086            panel.getLabel().setBackground(textBackground);
087        }
088
089        if (data == null) {
090            // not a check box node; return default cell renderer
091            return defaultRenderer.getTreeCellRendererComponent(tree, value,
092                selected, expanded, leaf, row, hasFocus);
093        }
094
095        //panel.label.setText(data.getText());
096        panel.setData(data);
097        panel.setSelected(data.isSelected());
098
099        return panel;
100    }
101/*
102    private JPopupMenu createPopupMenu(final AbstractLayer layer) {
103        JMenuItem menuItem;
104 
105        //Create the popup menu.
106        if (layer.isVisibleTexts()) menuItem = new JMenuItem("hide texts");
107        else menuItem = new JMenuItem("show texts");
108        JPopupMenu popup = new JPopupMenu();
109        popup.add(menuItem);
110        menuItem.addActionListener(new ActionListener(){
111            @Override
112            public void actionPerformed(ActionEvent arg0) {
113                setVisibleTexts(layer, !layer.isVisibleTexts());
114            }
115        });
116 
117        return popup;
118    }
119
120    private void setVisibleTexts(AbstractLayer layer, boolean visible) {
121        layer.setVisibleTexts(visible);
122        if (layer instanceof LayerGroup) {
123            LayerGroup group = (LayerGroup) layer;
124            if (group.getLayers() != null)
125                for (AbstractLayer al : group.getLayers()) {
126                    setVisibleTexts(al, visible);
127                }
128        }
129    }
130*/
131}