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 * BudgetSelectorPanel.java
024 *
025 * Created on June 2, 2006, 5:30 PM
026 */
027
028 package org.jbudget.gui.budget;
029
030 import java.awt.BorderLayout;
031 import java.util.LinkedList;
032 import java.util.List;
033 import javax.swing.JList;
034 import javax.swing.JPanel;
035 import javax.swing.JScrollPane;
036 import javax.swing.ListSelectionModel;
037 import javax.swing.event.ListSelectionEvent;
038 import javax.swing.event.ListSelectionListener;
039 import org.jbudget.gui.SelectorRenderer;
040 import org.jbudget.util.Pair;
041
042 /**
043 * A JPanel containing a list of budgets one of which can be selected.
044 * @author petrov
045 */
046 public class BudgetSelectorPanel extends JPanel
047 {
048 private final SelectorRenderer renderer =
049 new SelectorRenderer(SelectorRenderer.Type.BUDGET);
050 private final BudgetSelectorModel model;
051 private final JList budgetList;
052
053 /** Holds id of the selected budget */
054 private int selectedBudgetID = -1;
055
056 /** A list of components that are notified about changes in budget selection
057 */
058 private List<BudgetSelectionListener> listeners =
059 new LinkedList<BudgetSelectionListener>();
060
061 /** Creates a new instance of BudgetSelectorPanel */
062 public BudgetSelectorPanel(boolean showUnused)
063 {
064 super();
065 model = new BudgetSelectorModel(showUnused);
066 budgetList = new JList(model);
067 budgetList.setCellRenderer(renderer);
068 budgetList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
069 setLayout(new BorderLayout());
070 add(new JScrollPane(budgetList), BorderLayout.CENTER);
071
072 budgetList.addListSelectionListener(new ListSelectionListener(){
073 public void valueChanged(ListSelectionEvent e){
074 Pair<Integer,String> selectedBudgetInfo =
075 (Pair<Integer,String>)budgetList.getSelectedValue();
076 if (selectedBudgetInfo != null)
077 selectedBudgetID = selectedBudgetInfo.first().intValue();
078 else
079 selectedBudgetID = -1;
080 notifyListeners();
081 }
082 });
083 }
084
085 /** Frees resources. Must be called before closing the widget */
086 public void dispose(){
087 model.dispose();
088 }
089
090 /** Returns the ID of currently selected budget; -1 if no budget is selected*/
091 public int getSelectedBudgetID(){
092 return selectedBudgetID;
093 }
094
095 //================== Working with budget selection listeners =============//
096
097 public void addListener(BudgetSelectionListener listener){
098 listeners.add(listener);
099 }
100
101 public void removeListener(BudgetSelectionListener listener){
102 listeners.remove(listener);
103 }
104
105 public void notifyListeners(){
106 for (BudgetSelectionListener listener : listeners)
107 listener.budgetSelected(selectedBudgetID);
108 }
109 //================== Inner classes and interfaces =======================//
110
111 public static interface BudgetSelectionListener {
112 public void budgetSelected(long budgetID);
113 }
114 }