001 package net.sourceforge.mp3dings.utils;
002
003 /*
004 * Mp3dings - manage mp3 meta-information
005 * Copyright (C) 2006 Moritz Ringler
006 * $Id: BeanList.java,v 1.8 2006/07/03 10:59:40 ringler Exp $
007 *
008 * This program is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU General Public License
010 * as published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021 */
022 import java.awt.Component;
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 import java.util.Collection;
026 import java.util.HashMap;
027 import java.util.Iterator;
028 import java.util.List;
029 import java.util.Map;
030 import java.util.Vector; //Must use Vector here!
031 import java.util.ArrayList;
032 import javax.swing.DefaultComboBoxModel;
033 import javax.swing.DefaultListCellRenderer;
034 import javax.swing.JComboBox;
035 import javax.swing.JList;
036 import javax.swing.ListCellRenderer;
037 import javax.swing.ListModel;
038 import net.sourceforge.mp3dings.Globals;
039
040 /**
041 * Description of the Class
042 *
043 *@author ringler
044 */
045 public class BeanList extends JList {
046 private Vector regTypes = new Vector(); //Must be a Vector!
047 private DefaultComboBoxModel types = new DefaultComboBoxModel(regTypes);
048 private JComboBox typeSelection = new JComboBox(types);
049 private static DisplayNames displayNames = new DisplayNames();
050 private static ListCellRenderer beanListCellRenderer =
051 new DefaultListCellRenderer() {
052 public Component getListCellRendererComponent(JList list,
053 Object value, int index, boolean isSelected,
054 boolean cellHasFocus) {
055 return super.getListCellRendererComponent(list,
056 ((Bean) value).getName(), index, isSelected,
057 cellHasFocus);
058 }
059 };
060
061
062 /** Constructs a new BeanList. */
063 public BeanList() {
064 this(new BeanSet());
065 }
066
067
068 /**
069 * Constructs a new BeanList.
070 *
071 *@param beans Description of the Parameter
072 */
073 public BeanList(BeanSet beans) {
074 super(beans.getNewListModel());
075 init();
076 }
077
078
079 /**
080 * Constructs a new BeanList.
081 *
082 *@param model Description of the Parameter
083 */
084 public BeanList(BeanSet.ListModel model) {
085 super(model);
086 init();
087 }
088
089
090 /**
091 * Sets the model attribute of the BeanList object
092 *
093 *@param model The new model value
094 */
095 public void setModel(ListModel model) {
096 if (!(model instanceof BeanSet.ListModel)) {
097 throw new IllegalArgumentException("Model must be a BeanSet.ListModel.");
098 }
099 super.setModel(model);
100 }
101
102
103 /** Description of the Method */
104 private void init() {
105 setCellRenderer(beanListCellRenderer);
106 setFixedCellWidth(256);
107
108 typeSelection.setRenderer(displayNames);
109 typeSelection.addActionListener(
110 new ActionListener() {
111 public void actionPerformed(ActionEvent e) {
112 Class c = (Class) ((JComboBox) e.getSource()).getSelectedItem();
113 if (c != null) {
114 setVisibleBeanType(c);
115 }
116 }
117 });
118 }
119
120
121 /**
122 * Gets the beanListCellRenderer attribute of the BeanList class
123 *
124 *@return The beanListCellRenderer value
125 */
126 public static ListCellRenderer getBeanListCellRenderer() {
127 return beanListCellRenderer;
128 }
129
130
131
132 /**
133 * Description of the Method
134 *
135 *@param bean Description of the Parameter
136 */
137 public void remove(Bean bean) {
138 int selection = getMaxSelectionIndex();
139 clearSelection();
140
141 ((BeanSet.ListModel) getModel()).getBeanSet().remove(bean);
142
143 int listsize = getModel().getSize();
144
145 if (listsize > 0) {
146 /*
147 * if there remain elements select the next one
148 */
149 if (selection >= 0) {
150 setSelectedIndex((listsize > selection) ? selection : listsize - 1);
151 }
152 repaint();
153 }
154 }
155
156
157 /**
158 * Description of the Method
159 *
160 *@param beanClass Description of the Parameter
161 */
162 public void registerType(Class beanClass) {
163 if (!Bean.class.isAssignableFrom(beanClass)) {
164 throw new IllegalArgumentException("beanClass must inherit from Bean.");
165 }
166 if (!regTypes.contains(beanClass)) {
167 types.addElement(beanClass);
168 }
169 }
170
171
172 /**
173 * Gets the registeredTypes attribute of the BeanList object
174 *
175 *@return The registeredTypes value
176 */
177 public List getRegisteredTypes() {
178 return new ArrayList(regTypes);
179 }
180
181
182 /** Description of the Method */
183 public void clearRegisteredTypes() {
184 regTypes.clear();
185 }
186
187
188 /**
189 * Description of the Method
190 *
191 *@param beans Description of the Parameter
192 */
193 public void registerBeanTypes(Collection beans) {
194 Iterator it = beans.iterator();
195 while (it.hasNext()) {
196 registerType(it.next().getClass());
197 }
198 }
199
200
201 /**
202 * Sets the visibleBeanType attribute of the BeanList object
203 *
204 *@param beanClass The new visibleBeanType value
205 */
206 public void setVisibleBeanType(Class beanClass) {
207 clearSelection();
208 ((BeanSet.ListModel) getModel()).setType(beanClass);
209 registerType(beanClass);
210 }
211
212
213 /**
214 * Gets the typeSelector attribute of the BeanList object
215 *
216 *@return The typeSelector value
217 */
218 public JComboBox getTypeSelector() {
219 return typeSelection;
220 }
221
222
223 /**
224 * Description of the Method
225 *
226 *@param beanClass Description of the Parameter
227 *@return Description of the Return Value
228 */
229 public String nameForClass(Class beanClass) {
230 return displayNames.nameForClass(beanClass);
231 }
232
233
234
235 /**
236 * Description of the Class
237 *
238 *@author ringler
239 */
240 private static class DisplayNames extends DefaultListCellRenderer {
241
242 /*
243 * caches display names
244 */
245 private Map names = new HashMap();
246
247
248 /** Constructs a new DisplayNames. */
249 public DisplayNames() {
250 super();
251 }
252
253
254 /**
255 * Gets the listCellRendererComponent attribute of the DisplayNames
256 * object
257 *
258 *@param list Description of the Parameter
259 *@param value Description of the Parameter
260 *@param index Description of the Parameter
261 *@param isSelected Description of the Parameter
262 *@param cellHasFocus Description of the Parameter
263 *@return The listCellRendererComponent value
264 */
265 public Component getListCellRendererComponent(JList list,
266 Object value, int index, boolean isSelected,
267 boolean cellHasFocus) {
268 return super.getListCellRendererComponent(
269 list,
270 nameForClass((Class) value),
271 index,
272 isSelected,
273 cellHasFocus);
274 }
275
276
277 /**
278 * Description of the Method
279 *
280 *@param beanClass Description of the Parameter
281 *@return Description of the Return Value
282 */
283 public String nameForClass(Class beanClass) {
284
285 String name = beanClass.getName();
286
287 /*
288 * try to retrieve a display name from the cache
289 */
290 String newname = (String) names.get(name);
291
292 /*
293 * if no display name is cached ...
294 */
295 if (newname == null) {
296 if (Globals.resourceExists(name)) {
297
298 /*
299 * look it up in the ResourceBundle
300 */
301 newname = Globals.getString(name);
302 } else {
303
304 /*
305 * or use the classname without package and final "Bean"
306 */
307 newname = name.replaceFirst(
308 "\\Q" + beanClass.getPackage().getName() + ".\\E",
309 ""
310 );
311 newname = newname.replaceFirst("Bean$", "");
312 }
313
314 /*
315 * put the new name into the cache
316 */
317 names.put(name, newname);
318 }
319
320 return newname;
321 }
322
323 }
324 }