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 * BudgetSelectorModel.java
024 *
025 * Created on June 5, 2006, 7:08 PM
026 */
027
028 package org.jbudget.gui.user;
029
030 import java.util.List;
031 import javax.swing.AbstractListModel;
032 import org.jbudget.Core.User;
033 import org.jbudget.io.DataManager;
034 import org.jbudget.util.Pair;
035
036 /**
037 * A list model for selecting a user.
038 *
039 * @author petrov
040 */
041 public class UserSelectorModel extends AbstractListModel
042 {
043 /** List of displayed users */
044 private List<Pair<Integer, String>> shownUsers;
045 /** If true not all users are shown and the users are not added to the
046 * list when they are added to the DataManager.
047 */
048
049 private final boolean showAllUsers;
050
051 private DataManager.UserListener listener =
052 new DataManager.UserListener()
053 {
054
055 public void userAdded(User user)
056 {
057 if (!showAllUsers)
058 {
059 return;
060 }
061 initialize();
062 int uid = user.getUID();
063 int i = 0;
064 for (Pair<Integer, String> userInfo : shownUsers)
065 {
066 if (userInfo.first().intValue() == uid)
067 {
068 fireIntervalAdded(this, i, i);
069 break;
070 }
071 i++;
072 }
073 }
074
075 public void userRemoved(int uid)
076 {
077 int i = 0;
078 for (Pair<Integer, String> userInfo : shownUsers)
079 {
080 if (userInfo.first().intValue() == uid)
081 {
082 break;
083 }
084 i++;
085 }
086 if (i == shownUsers.size())
087 {
088 return;
089 }
090 if (showAllUsers)
091 {
092 initialize();
093 }
094 fireIntervalRemoved(this, i, i);
095 }
096
097 public void userChanged(User user)
098 {
099 }
100 };
101
102 /** Guard variable */
103 private boolean wasDisposed = false;
104
105 /** Creates a new instance of UserSelectorModel. Configures the model to
106 * show all users known to the
107 * {@link org.petrov.dmitri.jbudget.io.DataManager DataManager} except for
108 * the <b>system</b> and <b>unknown</b> users.
109 */
110 public UserSelectorModel()
111 {
112 showAllUsers = true;
113 DataManager.getInstance().addUserListener(listener);
114 initialize();
115 }
116
117 /**
118 * Initializes the data model to show only users that are in the given list.
119 */
120 public UserSelectorModel(List<Pair<Integer, String>> shownUsers)
121 {
122 showAllUsers = false;
123 DataManager.getInstance().addUserListener(listener);
124 this.shownUsers = shownUsers;
125 }
126
127 /** Add a user to the list. Should only be used when showAllUsers is true */
128 public void addToList(User user)
129 {
130 Pair<Integer, String> userInfo =
131 new Pair<Integer, String>(new Integer(user.getUID()), user.getName());
132 addToList(userInfo);
133 }
134
135 /** Add a user to the list. Should only be used when showAllUsers is true */
136 public void addToList(Pair<Integer, String> userInfo)
137 {
138 int uid = userInfo.first().intValue();
139
140 int i = 0;
141 for (Pair<Integer, String> ui : shownUsers)
142 {
143 int listUid = ui.first().intValue();
144 if (listUid > uid)
145 {
146 break;
147 } else if (listUid == uid)
148 {
149 return;
150 }
151 i++;
152 }
153 shownUsers.add(i, userInfo);
154 fireIntervalAdded(this, i, i);
155 }
156
157 /** Add a user to the list. Should only be used when showAllUsers is true */
158 public void removeFromList(User user)
159 {
160 Pair<Integer, String> userInfo =
161 new Pair<Integer, String>(new Integer(user.getUID()), user.getName());
162 removeFromList(userInfo);
163 }
164
165 /** Add a user to the list. Should only be used when showAllUsers is true */
166 public void removeFromList(Pair<Integer, String> userInfo)
167 {
168 int uid = userInfo.first().intValue();
169
170 int i = 0;
171 for (Pair<Integer, String> ui : shownUsers)
172 {
173 if (ui.first().intValue() == uid)
174 {
175 break;
176 }
177 i++;
178 }
179 shownUsers.remove(i);
180 fireIntervalRemoved(this, i, i);
181 }
182
183 /** Frees resources */
184 public void dispose()
185 {
186 DataManager.getInstance().removeUserListener(listener);
187 wasDisposed = true;
188 }
189
190 @Override
191 public void finalize()
192 {
193 if (!wasDisposed)
194 {
195 System.err.println("Dispose was not called for UserSelectorModel");
196 dispose();
197 }
198 }
199
200 /** Gets a list of available budgets from the DataManager */
201 private void initialize()
202 {
203 shownUsers = DataManager.getInstance().getValidUsers(false, false);
204 }
205
206 /** Returns the element at the appropriate location */
207 public Object getElementAt(int i)
208 {
209 return shownUsers.get(i);
210 }
211
212 /** Returns the number of available budgets */
213 public int getSize()
214 {
215 return shownUsers.size();
216 }
217 }