001    /*
002        Copyright (C) 2003 Adam Olsen
003    
004            This program is free software; you can redistribute it and/or modify
005        it under the terms of the GNU General Public License as published by
006        the Free Software Foundation; either version 1, or (at your option)
007        any later version.
008    
009        This program is distributed in the hope that it will be useful,
010        but WITHOUT ANY WARRANTY; without even the implied warranty of
011        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012        GNU General Public License for more details.
013    
014        You should have received a copy of the GNU General Public License
015        along with this program; if not, write to the Free Software
016        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017    */
018    
019    package com.valhalla.jbother.preferences;
020    
021    import java.awt.Dimension;
022    import java.awt.event.MouseAdapter;
023    import java.awt.event.MouseEvent;
024    
025    import java.util.*;
026    import com.valhalla.jbother.*;
027    
028    import javax.swing.*;
029    import javax.swing.tree.*;
030    
031    /**
032     * Displays the different preference panels available in a JTree
033     * @author Adam Olsen
034     * @version 1.0
035    */
036    public class PreferencesTree extends JPanel
037    {
038            private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );
039            private PreferencesDialog prefsDialog;
040            private JTree tree;
041            private DefaultMutableTreeNode root;
042            private Properties panels = new Properties();
043    
044            /**
045             * Sets up the preferences tree
046             * @param prefsDialog the enclosing preferences dialog
047            */
048            public PreferencesTree( PreferencesDialog prefsDialog )
049            {
050                    this.prefsDialog = prefsDialog;
051    
052                    panels.setProperty( resources.getString( "general" ), "General" );
053                    panels.setProperty( resources.getString( "applications" ), "Applications" );
054                    panels.setProperty( resources.getString( "sounds" ), "Sounds" );
055                    panels.setProperty( resources.getString( "appearance" ), "Appearance" );
056                    panels.setProperty( resources.getString( "privacy" ), "Privacy" );
057    
058                    setupTree();
059    
060    
061                    setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
062                    add( new JScrollPane( tree ) );
063                    setPreferredSize( new Dimension( 150, 350 ) );
064            }
065    
066            /**
067             * Sets the tree up
068            */
069            private void setupTree()
070            {
071                    root = new DefaultMutableTreeNode( resources.getString( "preferences" ) );
072    
073                    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
074                    renderer.setLeafIcon( null );
075    
076                    String items[] = new String[] {
077                            resources.getString( "general" ),
078                            resources.getString( "applications" ),
079                            resources.getString( "sounds" ),
080                            resources.getString( "appearance" ),
081                            resources.getString( "privacy" ) };
082    
083    
084                    for( int i = 0; i < items.length; i++ )
085                    {
086                            root.add( new DefaultMutableTreeNode( items[i] ) );
087                    }
088    
089                    tree = new JTree( root );
090                    tree.setShowsRootHandles( true );
091                    tree.setSelectionRow( 1 );
092                    tree.setCellRenderer( renderer );
093                    tree.addMouseListener( new DoubleClickListener() );
094            }
095    
096            /**
097             * Gets the index of the selected item
098             * @return the selected index
099            */
100            public int getSelectedIndex()
101            {
102                    TreePath path = tree.getSelectionPath();
103                    DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
104                    DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
105                    return model.getIndexOfChild( root, node );
106            }
107    
108            /**
109             * Gets the number of rows
110             * @return the row count
111            */
112            public int getRowCount()
113            {
114                    DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
115                    return model.getChildCount( root );
116            }
117    
118            /**
119             * Listens for a click on one of the tree items
120            */
121            class DoubleClickListener extends MouseAdapter
122            {
123                    public void mouseClicked( MouseEvent e )
124                    {
125                            JTree tree = (JTree)e.getComponent();
126                            TreePath path = tree.getSelectionPath();
127                            DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
128                            String string = panels.getProperty( (String)node.getUserObject() );
129                            if( string != null ) prefsDialog.switchPanel( string );
130                    }
131            }
132    }