1 /*
2 * Copyright (c) 2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
11 *
12 */
13 package org.abstracthorizon.aequo;
14
15 import java.util.ArrayList;
16
17 import javax.swing.ListModel;
18 import javax.swing.event.ListDataEvent;
19 import javax.swing.event.ListDataListener;
20
21 /**
22 * {@link ListModel} adapter. This class wraps {@link CompareModel} providing place of simple extension
23 * of {@link ListModel}.
24 *
25 *
26 * @author Daniel Sendula
27 */
28 public class ListModelAdapter implements ListModel, ListDataListener {
29
30 /** Model */
31 protected final CompareModel<?, ?> model;
32
33 /** Listeneres */
34 protected ArrayList<ListDataListener> listeners = new ArrayList<ListDataListener>();
35
36 /**
37 * Constructor
38 *
39 * @param compareModel model
40 */
41 public ListModelAdapter(CompareModel<?, ?> compareModel) {
42 this.model = compareModel;
43 }
44
45 /**
46 * Returns the number of entries in the model
47 * @return the number of entries in the model
48 */
49 public int getSize() {
50 return this.model.getSize();
51 }
52
53 /**
54 * Adds listener. If this is the first listener it will register
55 * itself with the model
56 * @param l listener
57 */
58 public void addListDataListener(ListDataListener l) {
59 listeners.add(l);
60 if (listeners.size() == 1) {
61 this.model.addListDataListener(this);
62 }
63 }
64
65 /**
66 * Removes listener. If this is the last listener it will de-register
67 * itself with the model
68 * @param l listener
69 */
70 public void removeListDataListener(ListDataListener l) {
71 listeners.remove(l);
72 if (listeners.size() == 0) {
73 this.model.removeListDataListener(this);
74 }
75 }
76
77 /**
78 * Listener's callback
79 * @param e event
80 */
81 public void contentsChanged(ListDataEvent e) {
82 for (ListDataListener listener : listeners) {
83 listener.contentsChanged(e);
84 }
85 }
86
87 /**
88 * Listener's callback
89 * @param e event
90 */
91 public void intervalAdded(ListDataEvent e) {
92 for (ListDataListener listener : listeners) {
93 listener.intervalAdded(e);
94 }
95 }
96
97 /**
98 * Listener's callback
99 * @param e event
100 */
101 public void intervalRemoved(ListDataEvent e) {
102 for (ListDataListener listener : listeners) {
103 listener.intervalRemoved(e);
104 }
105 }
106
107 /**
108 * Returns entry at the given index
109 * @return entry at the given index
110 */
111 public Object getElementAt(int index) {
112 return model.getElementAt(index);
113 }
114 }