View Javadoc

1   /*
2    *  File: AddingTimeBarRowModel.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.List;
24  
25  import de.jaret.util.date.Interval;
26  import de.jaret.util.date.IntervalImpl;
27  import de.jaret.util.date.JaretDate;
28  
29  /**
30   * Timebar row model that adds up (merges) all intervals added.
31   * 
32   * @author Peter Kliem
33   * @version $Id: AddingTimeBarRowModel.java 592 2007-10-14 22:02:30Z olk $
34   */
35  public class AddingTimeBarRowModel extends AbstractTimeBarRowModel implements TimeBarRowListener {
36      /** intervals in this row (the one added up merged interval, actually). */
37      protected List<Interval> _intervals = new ArrayList<Interval>();
38  
39      /** the merged interval. */
40      protected MergedInterval _mergedInterval;
41  
42      /** rows beeing "add up". */
43      protected List<TimeBarRow> _rows = new ArrayList<TimeBarRow>();
44  
45      /**
46       * Create an adding row model.
47       * 
48       * @param header the headr of the row
49       */
50      public AddingTimeBarRowModel(TimeBarRowHeader header) {
51          super(header);
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      public List<Interval> getIntervals() {
58          return _intervals;
59      }
60  
61      /**
62       * Add a row.
63       * 
64       * @param row row to add
65       */
66      public void addRow(TimeBarRow row) {
67          _rows.add(row);
68          if (row.getMinDate() != null) {
69              checkMergedInterval();
70              if (_mergedInterval.getBegin() == null || row.getMinDate().compareTo(_mergedInterval.getBegin()) < 0) {
71                  _mergedInterval.setBeginX(row.getMinDate().copy());
72                  _minDate = _mergedInterval.getBegin().copy();
73              }
74              if (_mergedInterval.getEnd() == null || row.getMaxDate().compareTo(_mergedInterval.getEnd()) > 0) {
75                  _mergedInterval.setEndX(row.getMaxDate().copy());
76                  _maxDate = _mergedInterval.getEnd().copy();
77              }
78          }
79          row.addTimeBarRowListener(this);
80      }
81  
82      /**
83       * Remove a row from the list of rows adding up.
84       * 
85       * @param row row to remove
86       */
87      public void remRow(TimeBarRow row) {
88          if (_rows.remove(row)) {
89              row.remTimeBarRowListener(this);
90              checkBounds();
91          }
92      }
93  
94      /**
95       * Check existence of merged interval and create one if necessary.
96       * 
97       */
98      protected void checkMergedInterval() {
99          if (_mergedInterval == null) {
100             _mergedInterval = new MergedInterval();
101             _intervals.add(_mergedInterval);
102         }
103     }
104 
105     // ---------------------- TimeBarRowListener
106     /**
107      * {@inheritDoc} Check whether the bounds are still correct.
108      */
109     public void rowDataChanged(TimeBarRow row) {
110         checkBounds();
111     }
112 
113     /**
114      * {@inheritDoc} Check whether the bounds are still correct.
115      */
116     public void elementAdded(TimeBarRow row, Interval element) {
117         checkBounds();
118     }
119 
120     /**
121      * {@inheritDoc} Check whether the bounds are still correct.
122      */
123     public void elementRemoved(TimeBarRow row, Interval element) {
124         checkBounds();
125     }
126 
127     /**
128      * {@inheritDoc} Check whether the bounds are still correct.
129      */
130     public void elementChanged(TimeBarRow row, Interval element) {
131         checkBounds();
132     }
133 
134     /**
135      * Check the bounds of the merged interval against all added rows.
136      */
137     protected void checkBounds() {
138         // get min and max date of all rows in the row list
139         JaretDate min = null;
140         JaretDate max = null;
141         for (TimeBarRow row : _rows) {
142             if (row.getMinDate() != null && row.getIntervals().size() > 0) {
143                 if (min == null || row.getMinDate().compareTo(min) < 0) {
144                     min = row.getMinDate().copy();
145                 }
146             }
147             if (row.getMaxDate() != null && row.getIntervals().size() > 0) {
148                 if (max == null || row.getMaxDate().compareTo(max) > 0) {
149                     max = row.getMaxDate().copy();
150                 }
151             }
152         }
153         // if min ist still null no row contains an interval
154         if (min == null) {
155             // no interval in any row
156             if (_mergedInterval != null) {
157                 _intervals.remove(_mergedInterval);
158                 fireElementRemoved(_mergedInterval);
159                 _mergedInterval = null;
160             }
161 
162         } else {
163             checkMergedInterval(); // make sure the merged interval exists and
164             // is added to the intervals list
165             boolean changed = false;
166             if (_mergedInterval != null
167                     && (_mergedInterval.getBegin() == null || !_mergedInterval.getBegin().equals(min))) {
168                 _mergedInterval.setBeginX(min);
169                 _minDate = _mergedInterval.getBegin().copy();
170                 changed = true;
171             }
172             if (_mergedInterval != null && (_mergedInterval.getEnd() == null || !_mergedInterval.getEnd().equals(max))) {
173                 _mergedInterval.setEndX(max);
174                 _maxDate = _mergedInterval.getEnd().copy();
175                 changed = true;
176             }
177             if (changed) {
178                 fireElementChanged(_mergedInterval);
179             }
180         }
181     }
182 
183     /**
184      * {@inheritDoc} Do nothing!
185      */
186     public void headerChanged(TimeBarRow row, TimeBarRowHeader newHeader) {
187         // never mind
188     }
189 
190     // -------------------------- END TimeBarRowListener
191 
192     /**
193      * Simple extension of the basic interval implementation to make it read-only on the standard setters.
194      */
195     public class MergedInterval extends IntervalImpl {
196         /**
197          * Set the begin.
198          * 
199          * @param date begin date
200          */
201         public void setBeginX(JaretDate date) {
202             super.setBegin(date);
203         }
204 
205         /**
206          * Set the end date.
207          * 
208          * @param date end date
209          */
210         public void setEndX(JaretDate date) {
211             super.setEnd(date);
212         }
213 
214         /**
215          * Overridden to have no function. ({@inheritDoc})
216          */
217         public void setBegin(JaretDate begin) {
218         }
219 
220         /**
221          * Overridden to have no function. ({@inheritDoc})
222          */
223         public void setEnd(JaretDate end) {
224         }
225     }
226 
227 }