001    
002    /*****************************************************************************
003     *
004     * jBudget.
005     * Copyright (C) 2006  Dmitri Petrov
006     *
007     * This program is free software: you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     *
020     * /*****************************************************************************
021     *
022     * /*
023     * SelectorRenderer.java
024     *
025     * Created on June 2, 2006, 5:30 PM
026     */
027    
028    package org.jbudget.gui;
029    
030    import java.awt.BorderLayout;
031    import java.awt.Color;
032    import java.awt.Component;
033    import java.awt.Dimension;
034    import java.awt.Font;
035    import javax.swing.Icon;
036    import javax.swing.ImageIcon;
037    import javax.swing.JLabel;
038    import javax.swing.JList;
039    import javax.swing.JPanel;
040    import javax.swing.ListCellRenderer;
041    import javax.swing.UIManager;
042    import org.jbudget.Core.Account;
043    import org.jbudget.Core.Budget;
044    import org.jbudget.Core.User;
045    import org.jbudget.util.Pair;
046    
047    /**
048     * Renderer for lists of Accounts, Budgets, Users, or Pairs<Integer,String>.
049     *
050     * @author petrov
051     */
052    public class SelectorRenderer extends JPanel
053        implements ListCellRenderer
054    {
055        private final JLabel idLabel = new JLabel();
056        private final JLabel nameLabel = new JLabel();
057        
058        private Color bgColor1;
059        private Color bgColor2;
060        private Color fgColor;
061        private Color sBgColor;
062        private Color sFgColor;
063        
064        private Font font;
065        private Icon budgetIcon;
066        private Icon accountIcon;
067        private Icon userIcon;
068        private Icon userSystemIcon;
069        
070        private final Type type;
071        private final boolean showID;
072        
073        /**
074         * Creates a new instance of SelectorRenderer
075         */
076        public SelectorRenderer(Type type)
077        {
078            this(type, true);
079        }
080        
081        /**
082         * Creates a new instance of SelectorRenderer
083         */
084        public SelectorRenderer(Type type, boolean showID)
085        {
086            this.type = type;
087            this.showID = showID;
088            updateUI();
089            
090            idLabel.setMaximumSize(new Dimension(100, 25));
091            idLabel.setMinimumSize(new Dimension(100,25));
092            idLabel.setPreferredSize(new Dimension(100,25));
093            
094            nameLabel.setPreferredSize(new Dimension(200,25));
095            
096            setLayout(new BorderLayout());
097            
098            if (showID)
099                add(idLabel, BorderLayout.WEST);
100            add(nameLabel, BorderLayout.CENTER);
101            
102            setOpaque(true);
103            idLabel.setOpaque(true);
104            nameLabel.setOpaque(true);
105        }
106        
107        public void updateUI()
108        {
109            if (idLabel == null)
110                return;
111            
112            font = UIManager.getFont(GuiPreferences.tableFontKey);
113            
114            bgColor1 = UIManager.getColor(GuiPreferences.tableBgKey);
115            bgColor2 = UIManager.getColor(GuiPreferences.tableAltBgKey);
116            
117            fgColor = UIManager.getColor(GuiPreferences.tableFgKey);
118            
119            sBgColor = UIManager.getColor(GuiPreferences.tableSelectionBgKey);
120            sFgColor = UIManager.getColor(GuiPreferences.tableSelectionFgKey);
121            
122            budgetIcon = UIManager.getIcon(GuiPreferences.iconBudgetKey);
123            accountIcon = UIManager.getIcon(GuiPreferences.iconAccountKey);
124            userIcon = UIManager.getIcon(GuiPreferences.iconUserKey);
125            userSystemIcon = UIManager.getIcon(GuiPreferences.iconUserSysKey);
126            
127            idLabel.setFont(font);
128            nameLabel.setFont(font);
129        }
130        
131        public Component getListCellRendererComponent(
132            JList list, Object value, int index,
133            boolean isSelected, boolean cellHasFocus)
134        {   
135            if (isSelected)
136            {
137                nameLabel.setBackground(sBgColor);
138                nameLabel.setForeground(sFgColor);
139                idLabel.setBackground(sBgColor);
140                idLabel.setForeground(sFgColor);
141                setBackground(sBgColor);
142            }
143            else if (index % 2 == 0)
144            {
145                nameLabel.setBackground(bgColor1);
146                nameLabel.setForeground(fgColor);
147                idLabel.setBackground(bgColor1);
148                idLabel.setForeground(fgColor);
149                setBackground(bgColor1);
150            }
151            else
152            {
153                nameLabel.setBackground(bgColor2);
154                nameLabel.setForeground(fgColor);
155                idLabel.setBackground(bgColor2);
156                idLabel.setForeground(fgColor);
157                setBackground(bgColor2);
158            }
159            if (value instanceof Pair)
160            {
161                Pair<Integer,String> p = (Pair<Integer,String>)value;
162                if (showID)
163                {
164                    idLabel.setText("ID: " + p.first().toString());
165                    nameLabel.setText("Name: " + p.second());
166                }
167                else
168                    nameLabel.setText(p.second());
169                
170                Icon icon = null;
171                if (type == Type.BUDGET)
172                    icon = budgetIcon;
173                else if (type == Type.ACCOUNT)
174                    icon = accountIcon;
175                else if (type == Type.USER)
176                {
177                    if (p.first().intValue() == User.SYSTEM.getUID())
178                        icon = userSystemIcon;
179                    else
180                        icon = userIcon;
181                }
182                
183                if (showID)
184                    idLabel.setIcon(icon);
185                else
186                    nameLabel.setIcon(icon);
187                
188            }
189            else if (value instanceof Account)
190            {
191                Account account = (Account)value;
192                if (showID)
193                {
194                    idLabel.setText("ID: " + Integer.toString(account.getID()));
195                    nameLabel.setText("Name: " + account.getName());
196                }
197                else
198                    nameLabel.setText(account.getName());
199                idLabel.setIcon(accountIcon);
200            }
201            else if (value instanceof Budget)
202            {
203                Budget budget = (Budget)value;
204                if (showID)
205                {
206                    idLabel.setText("ID: " + Integer.toString(budget.getID()));
207                    nameLabel.setText("Name: " + budget.getName());
208                }
209                else
210                    nameLabel.setText(budget.getName());
211                idLabel.setIcon(budgetIcon);
212            }
213            else if (value instanceof User)
214            {
215                User user = (User)value;
216                if (showID)
217                {
218                    idLabel.setText("UID: " + Integer.toString(user.getUID()));
219                    nameLabel.setText("Name: " + user.getName());
220                }
221                else
222                    nameLabel.setText(user.getName());
223                idLabel.setIcon(userIcon);
224            }
225            
226            return this;
227        }
228        
229        public static class Type
230        {
231            private Type()
232            {};
233            
234            public static final Type BUDGET = new Type();
235            public static final Type ACCOUNT = new Type();
236            public static final Type USER = new Type();
237        }
238        
239    }