View Javadoc

1   /*
2    *  File: DefaultTimeBarRowModel.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.ArrayList;
23  import java.util.Collections;
24  import java.util.Comparator;
25  import java.util.List;
26  
27  import de.jaret.util.date.Interval;
28  
29  /**
30   * A base implementation of the TimeBarRow interface as an extension of the abstract implementation
31   * AbstractTimeBarRowModel. When this default model is used be aware of some possible performance optimizations that can
32   * be made when dealing with a specialized model (see comments).
33   * 
34   * @author Peter Kliem
35   * @version $Id: DefaultTimeBarRowModel.java 858 2009-05-06 22:15:51Z kliem $
36   */
37  public class DefaultTimeBarRowModel extends AbstractTimeBarRowModel {
38      /** list storing the intervals. */
39      protected List<Interval> _intervals = new ArrayList<Interval>();
40  
41      /**
42       * Default constructor.
43       * 
44       */
45      public DefaultTimeBarRowModel() {
46      }
47  
48      /**
49       * Construct a row supplying a header.
50       * 
51       * @param header row header
52       */
53      public DefaultTimeBarRowModel(TimeBarRowHeader header) {
54          setRowHeader(header);
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      public List<Interval> getIntervals() {
61          return _intervals;
62      }
63  
64      /**
65       * Add an interval.
66       * 
67       * @param interval interval to add
68       */
69      public void addInterval(Interval interval) {
70          _intervals.add(interval);
71          // Keep intervals sorted. This is not a performance optimized solution ...
72          // Optimize in cutom implementations
73          Collections.sort(_intervals, new Comparator<Interval>() {
74              public int compare(Interval i1, Interval i2) {
75                  return i1.getBegin().compareTo(i2.getBegin());
76              }
77          });
78          // Check min/max modifications by the added interval
79          if (_minDate == null || _intervals.size() == 1) {
80              _minDate = interval.getBegin().copy();
81              _maxDate = interval.getEnd().copy();
82          } else {
83              if (_minDate.compareTo(interval.getBegin()) > 0) {
84                  _minDate = interval.getBegin().copy();
85              } 
86              if (_maxDate.compareTo(interval.getEnd()) < 0) {
87                  _maxDate = interval.getEnd().copy();
88              }
89          }
90          interval.addPropertyChangeListener(this);
91          fireElementAdded(interval);
92      }
93  
94      /**
95       * Add more than one interval to the row (avoiding unnecessary updates for every interval).
96       * 
97       * @param intervals list of intervals
98       */
99      public void addIntervals(List<Interval> intervals) {
100         _intervals.addAll(intervals);
101         // Keep intervals sorted. This is not a permance optimized solution ...
102         // Optimize in cutom implementations
103         Collections.sort(_intervals, new Comparator<Interval>() {
104             public int compare(Interval i1, Interval i2) {
105                 return i1.getBegin().compareTo(i2.getBegin());
106             }
107         });
108         // Check min/max modifications by the added intervals
109         // min date is easy because we sorted by begin date
110         _minDate = _intervals.get(0).getBegin().copy();
111         // max date is not that siple since a longer interval may begin before the last beginnign interval
112         _maxDate = _minDate.copy(); // safe start
113         for (Interval interval : _intervals) {
114             if (interval.getEnd().compareTo(_maxDate) > 0) {
115                 _maxDate = interval.getEnd();
116             }
117         }
118         // now copy the date because we are holding a reference
119         _maxDate = _maxDate.copy();
120 
121         // be a property change listener on every interval
122         _maxDate = _intervals.get(_intervals.size() - 1).getEnd().copy();
123         for (Interval interval : intervals) {
124             interval.addPropertyChangeListener(this);
125         }
126         // the whole row has become invalid -> tell the world
127         fireRowDataChanged();
128     }
129 
130     /**
131      * Remove an interval.
132      * 
133      * @param interval interval to remove
134      */
135     public void remInterval(Interval interval) {
136         if (_intervals.contains(interval)) {
137             _intervals.remove(interval);
138             // check min/max the hard way (optimize in custom implementations!)
139             updateMinMax();
140             interval.removePropertyChangeListener(this);
141             fireElementRemoved(interval);
142         }
143     }
144 
145     /**
146      * Remove a list of intervals. Please note that the selection can not be adjusted correctly -> do it yourself.
147      * 
148      * @param intervals list of intervals to remove
149      */
150     public void remIntervals(List<Interval> intervals) {
151         boolean hasChanges = false;
152         for (Interval interval : intervals) {
153             if (_intervals.contains(interval)) {
154                 _intervals.remove(interval);
155                 interval.removePropertyChangeListener(this);
156                 hasChanges = true;
157             }
158         }
159         if (hasChanges) {
160             // check min/max the hard way (optimize in custom implementations!)
161             updateMinMax();
162             // the whole row has become invalid -> tell the world
163             fireRowDataChanged();
164         }
165     }
166 
167     /**
168      * Clear the row of all intervals. Please note that the selection can not be adjusted correctly -> do it yourself.
169      */
170     public void clear() {
171         for (Interval interval : _intervals) {
172             interval.removePropertyChangeListener(this);
173         }
174         _intervals.clear();
175         _minDate = null;
176         _maxDate = null;
177         // the whole row has become invalid -> tell the world
178         fireRowDataChanged();
179     }
180 
181 }