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 * UserSelectorPanel.java
024 *
025 * Created on July 20, 2006, 5:30 PM
026 */
027
028 package org.jbudget.gui.user;
029
030 import java.awt.BorderLayout;
031 import java.awt.event.MouseListener;
032 import java.util.LinkedList;
033 import java.util.List;
034 import javax.swing.JList;
035 import javax.swing.JPanel;
036 import javax.swing.JScrollPane;
037 import javax.swing.ListSelectionModel;
038 import javax.swing.event.ListSelectionEvent;
039 import javax.swing.event.ListSelectionListener;
040 import org.jbudget.gui.SelectorRenderer;
041 import org.jbudget.util.Pair;
042
043 /**
044 * A JPanel containing a list of users one of which can be selected.
045 * @author petrov
046 */
047 public class UserSelectorPanel extends JPanel
048 {
049 private final SelectorRenderer renderer =
050 new SelectorRenderer(SelectorRenderer.Type.USER, false);
051 private final UserSelectorModel model;
052 private final JList userList;
053
054 /** Holds id of the selected budget */
055 private int selectedUID = -1;
056
057 /** A list of components that are notified about changes in user selection
058 */
059 private List<UserSelectionListener> listeners =
060 new LinkedList<UserSelectionListener>();
061
062 /**
063 * Creates a new instance of UserSelectorPanel.
064 */
065 public UserSelectorPanel()
066 {
067 super();
068 model = new UserSelectorModel();
069 userList = new JList(model);
070 userList.setCellRenderer(renderer);
071 userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
072
073 setLayout(new BorderLayout());
074 add(new JScrollPane(userList), BorderLayout.CENTER);
075
076 initActions();
077 }
078
079 /**
080 * Creates a new instance of UserSelectorPanel.
081 */
082 public UserSelectorPanel(UserSelectorModel model)
083 {
084 super();
085 this.model = model;
086 userList = new JList(model);
087 userList.setCellRenderer(renderer);
088 userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
089
090 setLayout(new BorderLayout());
091 add(new JScrollPane(userList), BorderLayout.CENTER);
092
093 initActions();
094 }
095
096 /** Initializes gui actions */
097 private void initActions()
098 {
099 userList.addListSelectionListener(new ListSelectionListener(){
100 public void valueChanged(ListSelectionEvent e){
101 Pair<Integer,String> selectedUserInfo =
102 (Pair<Integer,String>)userList.getSelectedValue();
103 if (selectedUserInfo != null)
104 selectedUID = selectedUserInfo.first().intValue();
105 else
106 selectedUID = -1;
107 notifyListeners();
108 }
109 });
110 }
111
112 /** Frees resources. Must be called before closing the widget */
113 public void dispose()
114 {
115 model.dispose();
116 }
117
118 /** Returns the ID of currently selected budget; -1 if no budget is selected*/
119 public int getSelectedUID(){
120 return selectedUID;
121 }
122
123 @Override
124 public void setEnabled(boolean enabled){
125 super.setEnabled(enabled);
126 userList.setEnabled(enabled);
127 }
128
129 //================== Working with budget selection listeners =============//
130
131 public void addListener(UserSelectionListener listener){
132 listeners.add(listener);
133 }
134
135 public void removeListener(UserSelectionListener listener){
136 listeners.remove(listener);
137 }
138
139 public void notifyListeners(){
140 for (UserSelectionListener listener : listeners)
141 listener.userSelected(selectedUID);
142 }
143
144 @Override
145 public void addMouseListener(MouseListener mListener){
146 userList.addMouseListener(mListener);
147 }
148
149 @Override
150 public void removeMouseListener(MouseListener mListener){
151 userList.removeMouseListener(mListener);
152 }
153 //================== Inner classes and interfaces =======================//
154
155 public static interface UserSelectionListener {
156 public void userSelected(int uid);
157 }
158 }