KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > data > DataEvent


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.data;
20
21 /**
22  * Event to be sent to a {@link DataListener}.
23  *
24  * @author Franz-Josef Elmer
25  */

26 public class DataEvent {
27   private final DataContainer _container;
28   private final DataEventType _type;
29   private final int _index;
30   private final DataElement _deletedElement;
31
32   /**
33    * Creates an instance for the specified parameters.
34    * @param container The container which has been changed.
35    * @param type Type of change.
36    * @param index Index of the element which has been added, inserted,
37    * replaced, or removed.
38    * @param deletedElement Element which has been replaced or removed.
39    */

40   private DataEvent(DataContainer container, DataEventType type, int index,
41                     DataElement deletedElement) {
42     _container = container;
43     _type = type;
44     _index = index;
45     _deletedElement = deletedElement;
46   }
47
48   /**
49    * Creates an event of type {@link DataEventType#ELEMENT_ADDED} for the
50    * specified container.
51    * @param container Container where an element has been added.
52    * @return <tt>ELEMENT_ADDED</tt> event.
53    */

54   public static final DataEvent createAddEvent(DataContainer container) {
55     return new DataEvent(container, DataEventType.ELEMENT_ADDED,
56                          container.getNumberOfElements() - 1, null);
57   }
58
59   /**
60    * Creates an event of type {@link DataEventType#ELEMENT_INSERTED} for the
61    * specified container.
62    * @param container Container where an element has been inserted.
63    * @param index Index at which an element has been inserted.
64    * @return <tt>ELEMENT_INSERTED</tt> event.
65    */

66   public static final DataEvent createInsertEvent(DataContainer container,
67                                                   int index) {
68     return new DataEvent(container, DataEventType.ELEMENT_INSERTED, index,
69                          null);
70   }
71
72   /**
73    * Creates an event of type {@link DataEventType#ELEMENT_REPLACED} for the
74    * specified container.
75    * @param container Container where an element has been replaced.
76    * @param index Index of the replaced element.
77    * @param replacedElement The previous element at <tt>index</tt>.
78    * @return <tt>ELEMENT_REPLACED</tt> event.
79    */

80   public static final DataEvent createReplaceEvent(DataContainer container,
81                                       int index, DataElement replacedElement) {
82     return new DataEvent(container, DataEventType.ELEMENT_REPLACED, index,
83                          replacedElement);
84   }
85
86   /**
87    * Creates an event of type {@link DataEventType#ELEMENT_REMOVED} for the
88    * specified container.
89    * @param container Container where an element has been removed.
90    * @param index Index of the removed element.
91    * @param removedElement The previous element at <tt>index</tt>.
92    * @return <tt>ELEMENT_REMOVED</tt> event.
93    */

94   public static final DataEvent createRemoveEvent(DataContainer container,
95                                       int index, DataElement removedElement) {
96     return new DataEvent(container, DataEventType.ELEMENT_REMOVED, index,
97                          removedElement);
98   }
99
100   /** Returns the container. */
101   public DataContainer getContainer() {
102     return _container;
103   }
104
105   /**
106    * Returns the event type. Will be one of the constants
107    * {@link DataEventType#ELEMENT_ADDED},
108    * {@link DataEventType#ELEMENT_INSERTED},
109    * {@link DataEventType#ELEMENT_REMOVED}, or
110    * {@link DataEventType#ELEMENT_REPLACED}.
111    */

112   public DataEventType getType() {
113     return _type;
114   }
115
116   /** Returns the index. */
117   public int getIndex() {
118     return _index;
119   }
120
121   /**
122    * Returns the deleted element.
123    * @return <tt>null</tt> if either an element has been added or inserted.
124    */

125   public DataElement getDeletedElement() {
126     return _deletedElement;
127   }
128 }
129
Popular Tags