View Javadoc

1   /*
2    *  File: AbstractTimeBarModel.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation; either version 2 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.jaret.util.ui.timebars.model;
21  
22  import java.util.List;
23  import java.util.Vector;
24  
25  import de.jaret.util.date.Interval;
26  import de.jaret.util.date.JaretDate;
27  
28  /**
29   * An abstract implementation of the TimeBarModel interface. The storage of the rows has to be added. Care has to be
30   * taken to register with the rows via the TimeBarRowListener interface. If this is ommited changes in the rows will not
31   * autmatic propagate to the TimeBarModelListeners registered with the model. The implementation must also care about
32   * the min/max fields and set them.
33   * 
34   * @author Peter Kliem
35   * @version $Id: AbstractTimeBarModel.java 800 2008-12-27 22:27:33Z kliem $
36   */
37  public abstract class AbstractTimeBarModel implements TimeBarModel, TimeBarRowListener {
38      /** Minimum date of the complete model. */
39      protected JaretDate _minDate;
40  
41      /** Maximum date of the complete model. */
42      protected JaretDate _maxDate;
43  
44      /** List of model listeners. */
45      protected List<TimeBarModelListener> _listenerList;
46  
47      /**
48       * {@inheritDoc}
49       */
50      public JaretDate getMinDate() {
51          if (_minDate == null) {
52              // no mindate -> keep everybody happy
53              return new JaretDate();
54          }
55          return _minDate.copy();
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public JaretDate getMaxDate() {
62          if (_maxDate == null) {
63              // no maxdate -> keep everybody happy
64              return new JaretDate();
65          }
66          return _maxDate.copy();
67      }
68  
69      /**
70       * {@inheritDoc } This default implementation is a brute force implementation. Real implementation should override
71       * the default implementation if the data model contains a reference to improve performance.
72       */
73      public TimeBarRow getRowForInterval(Interval interval) {
74          for (int i = 0; i < getRowCount(); i++) {
75              if (getRow(i).getIntervals().contains(interval)) {
76                  return getRow(i);
77              }
78          }
79          return null;
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      public synchronized void addTimeBarModelListener(TimeBarModelListener tbml) {
86          if (_listenerList == null) {
87              _listenerList = new Vector<TimeBarModelListener>();
88          }
89          _listenerList.add(tbml);
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public void remTimeBarModelListener(TimeBarModelListener tbml) {
96          if (_listenerList != null) {
97              _listenerList.remove(tbml);
98          }
99      }
100 
101     /**
102      * Inform listeners about unspecific or multiple changes in the model. Do use the more specific methods if possible.
103      */
104     protected void fireModelDataChanged() {
105         if (_listenerList != null) {
106             for (TimeBarModelListener listener : _listenerList) {
107                 listener.modelDataChanged(this);
108             }
109         }
110     }
111 
112     /**
113      * Inform listeners about a new row.
114      * 
115      * @param row new row
116      */
117     protected void fireRowAdded(TimeBarRow row) {
118         if (_listenerList != null) {
119             for (TimeBarModelListener listener : _listenerList) {
120                 listener.rowAdded(this, row);
121             }
122         }
123     }
124 
125     /**
126      * Inform listeners about a removed row.
127      * 
128      * @param row removed row
129      */
130     protected void fireRowRemoved(TimeBarRow row) {
131         if (_listenerList != null) {
132             for (TimeBarModelListener listener : _listenerList) {
133                 listener.rowRemoved(this, row);
134             }
135         }
136     }
137 
138     /**
139      * Inform listeners about a changed row.
140      * 
141      * @param row changed row
142      */
143     protected void fireRowDataChanged(TimeBarRow row) {
144         if (_listenerList != null) {
145             for (TimeBarModelListener listener : _listenerList) {
146                 listener.rowDataChanged(this, row);
147             }
148         }
149     }
150 
151     /**
152      * Inform listeners about a changed header.
153      * 
154      * @param row row
155      * @param header header
156      */
157     protected void fireHeaderChanged(TimeBarRow row, TimeBarRowHeader header) {
158         if (_listenerList != null) {
159             for (TimeBarModelListener listener : _listenerList) {
160                 listener.headerChanged(this, row, header);
161             }
162         }
163     }
164 
165     /**
166      * Inform listeners about a changed element in a specific row.
167      * 
168      * @param row row of the element
169      * @param element changed element
170      */
171     protected void fireElementChanged(TimeBarRow row, Interval element) {
172         if (_listenerList != null) {
173             for (TimeBarModelListener listener : _listenerList) {
174                 listener.elementChanged(this, row, element);
175             }
176         }
177     }
178 
179     /**
180      * Inform listeners about a new element in a specific row.
181      * 
182      * @param row row of the element
183      * @param element new element
184      */
185     protected void fireElementAdded(TimeBarRow row, Interval element) {
186         if (_listenerList != null) {
187             for (TimeBarModelListener listener : _listenerList) {
188                 listener.elementAdded(this, row, element);
189             }
190         }
191     }
192 
193     /**
194      * Inform listeners about a removed element in a specific row.
195      * 
196      * @param row row of the element
197      * @param element removed element
198      */
199     protected void fireElementRemoved(TimeBarRow row, Interval element) {
200         if (_listenerList != null) {
201             for (TimeBarModelListener listener : _listenerList) {
202                 listener.elementRemoved(this, row, element);
203             }
204         }
205     }
206 
207     /**
208      * {@inheritDoc}
209      */
210     public void elementAdded(TimeBarRow row, Interval element) {
211         updateMinMax();
212         fireElementAdded(row, element);
213     }
214 
215     /**
216      * {@inheritDoc}
217      */
218     public void elementRemoved(TimeBarRow row, Interval element) {
219         updateMinMax();
220         fireElementRemoved(row, element);
221     }
222 
223     /**
224      * {@inheritDoc}
225      */
226     public void elementChanged(TimeBarRow row, Interval element) {
227         updateMinMax();
228         fireElementChanged(row, element);
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     public void headerChanged(TimeBarRow row, TimeBarRowHeader newHeader) {
235         fireHeaderChanged(row, newHeader);
236     }
237 
238     /**
239      * {@inheritDoc}
240      */
241     public void rowDataChanged(TimeBarRow row) {
242         updateMinMax();
243         fireRowDataChanged(row);
244     }
245 
246     /**
247      * Update the min/max date of the model.
248      * 
249      */
250     protected void updateMinMax() {
251         _minDate = null;
252         _maxDate = null;
253         for (int i = 0; i < getRowCount(); i++) {
254             TimeBarRow r = getRow(i);
255             if ((_minDate == null && r.getMinDate() != null)
256                     || (r.getMinDate() != null && _minDate.compareTo(r.getMinDate()) > 0)) {
257                 _minDate = r.getMinDate().copy();
258             }
259             if ((_maxDate == null && r.getMaxDate() != null)
260                     || (r.getMaxDate() != null && _maxDate.compareTo(r.getMaxDate()) < 0)) {
261                 _maxDate = r.getMaxDate().copy();
262             }
263         }
264     }
265 
266 }