001    /*
002     *  Copyright (C) 2003 Adam Olsen
003     *  This program is free software; you can redistribute it and/or modify
004     *  it under the terms of the GNU General Public License as published by
005     *  the Free Software Foundation; either version 1, or (at your option)
006     *  any later version.
007     *  This program is distributed in the hope that it will be useful,
008     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
009     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
010     *  GNU General Public License for more details.
011     *  You should have received a copy of the GNU General Public License
012     *  along with this program; if not, write to the Free Software
013     *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
014     */
015    package com.valhalla.jbother.preferences;
016    
017    import java.awt.Component;
018    import java.awt.Dimension;
019    import java.awt.event.MouseAdapter;
020    import java.awt.event.MouseEvent;
021    import java.util.Iterator;
022    import java.util.Locale;
023    import java.util.ResourceBundle;
024    import java.util.TreeMap;
025    
026    import javax.swing.BoxLayout;
027    import javax.swing.JPanel;
028    import javax.swing.JScrollPane;
029    import javax.swing.JTree;
030    import javax.swing.tree.DefaultMutableTreeNode;
031    import javax.swing.tree.DefaultTreeCellRenderer;
032    import javax.swing.tree.DefaultTreeModel;
033    import javax.swing.tree.TreePath;
034    
035    /**
036     * Displays the different preference panels available in a JTree
037     * 
038     * @author Adam Olsen
039     * @created March 3, 2005
040     * @version 1.0
041     */
042    public class PreferencesTree extends JPanel {
043        private ResourceBundle resources = ResourceBundle.getBundle(
044                "JBotherBundle", Locale.getDefault());
045    
046        private PreferencesDialog prefsDialog;
047    
048        private JTree tree;
049    
050        private DefaultMutableTreeNode root, topNode, pluginsNode;
051    
052        private JScrollPane scroll = new JScrollPane();
053    
054        /**
055         * Sets up the preferences tree
056         * 
057         * @param prefsDialog
058         *            the enclosing preferences dialog
059         */
060        public PreferencesTree(PreferencesDialog prefsDialog) {
061            this.prefsDialog = prefsDialog;
062    
063            setupTree();
064    
065            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
066            add(scroll);
067            setPreferredSize(new Dimension(150, 350));
068        }
069    
070        /**
071         * Description of the Method
072         */
073        public void updateUI() {
074            super.updateUI();
075    
076            if (resources != null)
077                setupTree();
078        }
079    
080        /**
081         * Sets the tree up
082         */
083        private void setupTree() {
084            root = new DefaultMutableTreeNode(resources.getString("preferences"));
085            topNode = new DefaultMutableTreeNode(resources.getString("preferences"));
086            root.add(topNode);
087    
088            DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
089                public Component getTreeCellRendererComponent(JTree tree,
090                        Object value, boolean sel, boolean expanded, boolean leaf,
091                        int row, boolean hasFocus) {
092                    super.getTreeCellRendererComponent(tree, value, sel, expanded,
093                            leaf, row, hasFocus);
094    
095                    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
096                    setText(((String) node.getUserObject()).replaceAll("^a\\d+ ",
097                            ""));
098    
099                    return this;
100                }
101            };
102            renderer.setLeafIcon(null);
103    
104            Iterator i = prefsDialog.getPanels().keySet().iterator();
105    
106            while (i.hasNext()) {
107                topNode.add(new DefaultMutableTreeNode((String) i.next()));
108            }
109    
110            TreeMap plugins = PreferencesDialog.getPluginPanels();
111            if (plugins.size() > 0) {
112                pluginsNode = new DefaultMutableTreeNode(resources
113                        .getString("pluginPreferences"));
114                i = plugins.keySet().iterator();
115                while (i.hasNext()) {
116                    pluginsNode.add(new DefaultMutableTreeNode((String) i.next()));
117                }
118                root.add(pluginsNode);
119            }
120    
121            tree = new JTree(root);
122            tree.setRootVisible(false);
123            tree.setShowsRootHandles(true);
124    
125            for (int a = 0; a < tree.getRowCount(); a++) {
126                tree.expandRow(a);
127            }
128    
129            tree.setSelectionRow(1);
130            tree.setCellRenderer(renderer);
131            tree.addMouseListener(new DoubleClickListener());
132            scroll.setViewportView(tree);
133            validate();
134        }
135    
136        /**
137         * Gets the index of the selected item
138         * 
139         * @return the selected index
140         */
141        public int getSelectedIndex() {
142            TreePath path = tree.getSelectionPath();
143            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
144                    .getLastPathComponent();
145            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
146            return model.getIndexOfChild(root, node);
147        }
148    
149        /**
150         * Gets the number of rows
151         * 
152         * @return the row count
153         */
154        public int getRowCount() {
155            DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
156            return model.getChildCount(root);
157        }
158    
159        /**
160         * Listens for a click on one of the tree items
161         * 
162         * @author synic
163         * @created March 3, 2005
164         */
165        class DoubleClickListener extends MouseAdapter {
166            /**
167             * Description of the Method
168             * 
169             * @param e
170             *            Description of the Parameter
171             */
172            public void mouseClicked(MouseEvent e) {
173                JTree tree = (JTree) e.getComponent();
174                TreePath path = tree.getSelectionPath();
175                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
176                        .getLastPathComponent();
177                String string = (String) node.getUserObject();
178                if (string != null) {
179                    prefsDialog.switchPanel(string);
180                }
181            }
182        }
183    }
184