001    package net.sourceforge.mp3dings.utils;
002    
003    /*
004     *  Mp3dings - manage mp3 meta-information
005     *  Copyright (C) 2006 Moritz Ringler
006     *  $Id: BeanSet.java,v 1.11 2007/01/30 15:58:36 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.util.Collection;
023    import java.util.Comparator;
024    import java.util.Iterator;
025    import java.util.TreeSet;
026    import javax.swing.ListModel;
027    import javax.swing.event.ChangeEvent;
028    import javax.swing.event.ChangeListener;
029    import javax.swing.event.EventListenerList;
030    import javax.swing.event.ListDataEvent;
031    import javax.swing.event.ListDataListener;
032    import de.mospace.swing.JDoubleList;
033    
034    /**
035     *  The Set view of a BeanSet always contains all elements that have been added
036     *  to it. In contrast the ListModel view only contains those elements that are
037     *  instances of the current type. *
038     *
039     *@author    ringler
040     */
041    public class BeanSet extends TreeSet {
042        static final long serialVersionUID = -1021726551248209685L;
043    
044        /*
045         *  Event generation and listener management
046         */
047        private EventListenerList listenerList = new EventListenerList();
048        private transient ChangeEvent event = null;
049    
050    
051        /**  Constructs a new BeanSet. */
052        public BeanSet() {
053            super(
054                new Comparator() {
055                    public int compare(Object o1, Object o2) {
056                        return ((Bean) o1).getName().compareTo(((Bean) o2).getName());
057                    }
058                });
059        }
060    
061        /**
062         *  Gets the newListModel attribute of the BeanSet object
063         *
064         *@return    The newListModel value
065         */
066        public ListModel getNewListModel() {
067            return new ListModel(this);
068        }
069    
070    
071        /**
072         *  Description of the Method
073         *
074         *@param  bean     Description of the Parameter
075         *@param  newName  Description of the Parameter
076         *@return          Description of the Return Value
077         */
078        public boolean renameBean(Bean bean, String newName) {
079            boolean result = false;
080            if (newName.equals(bean.getName())) {
081                result = true;
082    
083            } else if (getBeanForName(newName) == null) {
084    
085                /*
086                 *  remove bean from beanset and beanlist
087                 */
088                //beans.remove(bean);
089    
090                /*
091                 *  rename bean
092                 */
093                bean.setName(newName);
094                fireStateChanged(bean.getClass());
095    
096                /*
097                 *  add the bean to the beanset
098                 */
099                //return beans.add(bean);
100    
101                result = true;
102            }
103            return result;
104        }
105    
106    
107        /**
108         *  Gets the beanForName attribute of the BeanSet object
109         *
110         *@param  name  Description of the Parameter
111         *@return       The beanForName value
112         */
113        public Bean getBeanForName(String name) {
114            Iterator it = iterator();
115            Bean otherBean = null;
116    
117            /*
118             *  checkWhetherBeanWithThisNameAlreadyExists
119             */
120            while (it.hasNext()) {
121                otherBean = (Bean) it.next();
122                if (otherBean.getName().equals(name)) {
123                    return otherBean;
124                }
125            }
126            return null;
127        }
128    
129    
130        /**
131         *  Gets the beanForID attribute of the BeanSet object
132         *
133         *@param  id  Description of the Parameter
134         *@return     The beanForID value
135         */
136        public Bean getBeanForID(long id) {
137            Iterator it = iterator();
138            Bean otherBean = null;
139    
140            /*
141             *  checkWhetherBeanWithThisNameAlreadyExists
142             */
143            while (it.hasNext()) {
144                otherBean = (Bean) it.next();
145                if (otherBean.getId() == id) {
146                    return otherBean;
147                }
148            }
149            return null;
150        }
151    
152    
153        /**
154         *  Description of the Method
155         *
156         *@param  o  Description of the Parameter
157         *@return    Description of the Return Value
158         */
159        public boolean add(Object o) {
160            boolean b = super.add((Bean) o);
161            fireStateChanged(o.getClass());
162            return b;
163        }
164    
165    
166        /**
167         *  Adds a feature to the All attribute of the BeanSet object
168         *
169         *@param  c  The feature to be added to the All attribute
170         *@return    Description of the Return Value
171         */
172        public boolean addAll(Collection c) {
173            boolean b = super.addAll(c);
174            fireStateChanged(Bean.class);
175            return b;
176        }
177    
178    
179        /**  Description of the Method */
180        public void clear() {
181            super.clear();
182            fireStateChanged(Bean.class);
183        }
184    
185    
186        /**
187         *  Description of the Method
188         *
189         *@param  o  Description of the Parameter
190         *@return    Description of the Return Value
191         */
192        public boolean remove(Object o) {
193            boolean b = super.remove(o);
194            fireStateChanged(o.getClass());
195            return b;
196        }
197    
198    
199        /**
200         *  Description of the Method
201         *
202         *@param  c  Description of the Parameter
203         *@return    Description of the Return Value
204         */
205        public boolean removeAll(Collection c) {
206            boolean b = false;
207            Iterator it = c.iterator();
208            while (it.hasNext()) {
209                b = b || super.remove((Bean) it.next());
210            }
211            fireStateChanged(Bean.class);
212            return b;
213        }
214    
215    
216        /**
217         *  Adds a feature to the ChangeListener attribute of the BeanSet object
218         *
219         *@param  l  The feature to be added to the ChangeListener attribute
220         */
221        public void addChangeListener(ChangeListener l) {
222            listenerList.add(ChangeListener.class, l);
223        }
224    
225    
226        /**
227         *  Description of the Method
228         *
229         *@param  l  Description of the Parameter
230         */
231        public void removeChangeListener(ChangeListener l) {
232            listenerList.remove(ChangeListener.class, l);
233        }
234    
235    
236        /**
237         *  Informs comboboxes that something has changed.
238         *
239         *@param  beantype  Description of the Parameter
240         */
241        protected void fireStateChanged(Class beantype) {
242            Object[] listeners = listenerList.getListenerList();
243    
244            /*
245             *  Process the listeners last to first, notifying
246             *  those that are interested in this event
247             */
248            for (int i = listeners.length - 2; i >= 0; i -= 2) {
249                if (listeners[i] == ChangeListener.class) {
250                    event = new BeanSetChangeEvent(this, beantype);
251                    ((ChangeListener) listeners[i + 1]).stateChanged(event);
252                }
253                //if
254            }
255            //loop
256        }
257        //method
258    
259    
260    
261        /**
262         *  A ChangeEvent that carries information about the bean type involved in
263         *  the change. If multiple bean types were involved the bean type will be
264         *  <code>Bean.class</code>.
265         *
266         *@author    ringler
267         */
268        private static class BeanSetChangeEvent extends ChangeEvent {
269            private Class beantype;
270    
271    
272            /**
273             *  Constructs a new BeanSetChangeEvent.
274             *
275             *@param  source    Description of the Parameter
276             *@param  beantype  Description of the Parameter
277             */
278            public BeanSetChangeEvent(Object source, Class beantype) {
279                super(source);
280                this.beantype = beantype;
281            }
282    
283    
284            /**
285             *  Gets the beanType attribute of the BeanSetChangeEvent object
286             *
287             *@return    The beanType value
288             */
289            public Class getBeanType() {
290                return beantype;
291            }
292        }
293        //End of class BeanSetChangeEvent
294    
295    
296    
297        /**
298         *  A List Model view of the BeanSet. The beans that appear in the list
299         *  model can be filtered by class. *
300         *
301         *@author    ringler
302         */
303        public static class ListModel implements JDoubleList.SingleListModel, ChangeListener {
304            private Class currentType = Bean.class;
305            private int elementCount = 0;
306            private BeanSet set;
307    
308            /*
309             *  Event generation and listener management
310             */
311            EventListenerList listenerList = new EventListenerList();
312            ListDataEvent event = null;
313    
314    
315            /**
316             *  Constructs a new ListModel.
317             *
318             *@param  set  Description of the Parameter
319             */
320            public ListModel(BeanSet set) {
321                this.set = set;
322                set.addChangeListener(this);
323            }
324    
325    
326            /**
327             *  Description of the Method
328             *
329             *@param  e  Description of the Parameter
330             */
331            public void stateChanged(ChangeEvent e) {
332                if (e.getSource() == set) {
333                    BeanSetChangeEvent bsce = (BeanSetChangeEvent) e;
334                    if (currentType.isAssignableFrom(bsce.getBeanType()) ||
335                            bsce.getBeanType().equals(Bean.class)) {
336                        updateElementCount();
337                        fireContentsChanged();
338                    }
339                }
340            }
341    
342    
343            /**
344             *  Gets the size attribute of the ListModel object
345             *
346             *@return    The size value
347             */
348            public int getSize() {
349                return elementCount;
350            }
351    
352    
353            /**
354             *  Does nothing *
355             *
356             *@param  index  Description of the Parameter
357             *@return        Description of the Return Value
358             */
359            public Object remove(int index) {
360                return (getElementAt(index));
361            }
362    
363    
364            /**
365             *  Does nothing *
366             *
367             *@param  index    Description of the Parameter
368             *@param  element  Description of the Parameter
369             */
370            public void add(int index, Object element) {
371                //set.add(element);
372            }
373    
374    
375            /**
376             *  Gets the beanSet attribute of the ListModel object
377             *
378             *@return    The beanSet value
379             */
380            public BeanSet getBeanSet() {
381                return set;
382            }
383    
384    
385            /**
386             *  Sets the beanSet attribute of the ListModel object
387             *
388             *@param  beans  The new beanSet value
389             */
390            public void setBeanSet(BeanSet beans) {
391                set.removeChangeListener(this);
392                beans.addChangeListener(this);
393                set = beans;
394                fireContentsChanged();
395            }
396    
397    
398            /**
399             *  Gets the elementAt attribute of the ListModel object
400             *
401             *@param  index  Description of the Parameter
402             *@return        The elementAt value
403             */
404            public Object getElementAt(int index) {
405                Iterator it = set.iterator();
406                for (int i = 0; i < index; ) {
407                    if (currentType.isInstance(it.next())) {
408                        i++;
409                    }
410                }
411                Object result = null;
412                while (result == null) {
413                    result = it.next();
414                    if (!currentType.isInstance(result)) {
415                        result = null;
416                    }
417                }
418                return result;
419            }
420    
421    
422            /**
423             *  Description of the Method
424             *
425             *@return    Description of the Return Value
426             */
427            private int updateElementCount() {
428                Iterator it = set.iterator();
429                int j = 0;
430                while (it.hasNext()) {
431                    Object itn = it.next();
432                    if (currentType.isInstance(itn)) {
433                        j++;
434                    }
435                }
436                return (elementCount = j);
437            }
438    
439    
440            /**
441             *  Sets the type attribute of the ListModel object
442             *
443             *@param  c  The new type value
444             */
445            public void setType(Class c) {
446                if (!Bean.class.isAssignableFrom(c)) {
447                    throw new IllegalArgumentException(
448                            "Only classes that extend net.sourceforge.mp3dings.utils.Bean are allowed here.");
449                }
450                currentType = c;
451                updateElementCount();
452                fireContentsChanged();
453            }
454    
455    
456            /**
457             *  Gets the type attribute of the ListModel object
458             *
459             *@return    The type value
460             */
461            public Class getType() {
462                return currentType;
463            }
464    
465    
466            /**
467             *  Adds a feature to the ListDataListener attribute of the ListModel
468             *  object
469             *
470             *@param  l  The feature to be added to the ListDataListener attribute
471             */
472            public void addListDataListener(ListDataListener l) {
473                listenerList.add(ListDataListener.class, l);
474            }
475    
476    
477            /**
478             *  Description of the Method
479             *
480             *@param  l  Description of the Parameter
481             */
482            public void removeListDataListener(ListDataListener l) {
483                listenerList.remove(ListDataListener.class, l);
484            }
485    
486    
487            /**  Informs listeners that something has changed. */
488            protected void fireContentsChanged() {
489                Object[] listeners = listenerList.getListenerList();
490    
491                /*
492                 *  Process the listeners last to first, notifying
493                 *  those that are interested in this event
494                 */
495                for (int i = listeners.length - 2; i >= 0; i -= 2) {
496                    if (listeners[i] == ListDataListener.class) {
497                        event = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize());
498                        ((ListDataListener) listeners[i + 1]).contentsChanged(event);
499                    }
500                    //if
501                }
502                //loop
503            }
504            //method
505    
506        }
507        //End of class BeanSet.ListModel
508    
509    }