View Javadoc

1   /*
2    *  File: DistributeOverlapStrategy.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.examples.timebars.timeline;
21  
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import de.jaret.util.date.Interval;
28  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
29  import de.jaret.util.ui.timebars.model.TimeBarRow;
30  import de.jaret.util.ui.timebars.strategy.IOverlapStrategy;
31  import de.jaret.util.ui.timebars.strategy.OverlapInfo;
32  
33  /***
34   * Implementation of an overlap strategy simply distributing the intervals over a number of rows.
35   * 
36   * @author kliem
37   * @version $Id: $
38   */
39  public class DistributeOverlapStrategy implements IOverlapStrategy{
40      /*** the delegate the stragey works for. */
41      protected TimeBarViewerDelegate _delegate;
42      /*** Cache for overlap infos. */
43      protected Map<TimeBarRow, Map<Interval, OverlapInfo>> _oiRowCache = new HashMap<TimeBarRow, Map<Interval, OverlapInfo>>();
44      
45      protected int _numRows; 
46      
47      
48      /***
49       * Construct the strategy.
50       * 
51       * @param delegate the delegate the strategy works for
52       * @param numRows number of rows to distribute the intervals on
53       */
54      public DistributeOverlapStrategy(TimeBarViewerDelegate delegate, int numRows) {
55          _delegate = delegate;
56          _numRows = numRows;
57      }
58  
59      /***
60       * {@inheritDoc}
61       */
62      public OverlapInfo getOverlapInfo(TimeBarRow row, Interval interval) {
63          Map<Interval, OverlapInfo> oiIntervalMap = _oiRowCache.get(row);
64          if (oiIntervalMap == null) {
65              oiIntervalMap = updateOICache(row);
66          }
67          OverlapInfo oi = oiIntervalMap.get(interval);
68          return oi;
69      }
70  
71      /***
72       * {@inheritDoc}
73       */
74      public int getMaxOverlapCount(TimeBarRow row) {
75          // TODO caching
76          Map<Interval, OverlapInfo> oiIntervalMap = _oiRowCache.get(row);
77          if (oiIntervalMap == null) {
78              oiIntervalMap = updateOICache(row);
79          }
80          int max = 0;
81          Collection<OverlapInfo> infos = oiIntervalMap.values();
82          for (OverlapInfo overlapInfo : infos) {
83              if (overlapInfo.maxOverlapping > max) {
84                  max = overlapInfo.maxOverlapping;
85              }
86          }
87          return max + 1;
88      }
89  
90      /***
91       * {@inheritDoc} Assumes sorted Intervals.
92       */
93      public Map<Interval, OverlapInfo> updateOICache(TimeBarRow row) {
94          List<Interval> intervals = _delegate.filterIntervals(row.getIntervals());
95          Map<Interval, OverlapInfo> result = new HashMap<Interval, OverlapInfo>();
96          // todo: assumes sorted intervals!
97          for (int i = 0; i < intervals.size(); i++) {
98              Interval interval = intervals.get(i);
99              OverlapInfo oi = new OverlapInfo();
100             oi.interval = interval;
101             result.put(interval, oi);
102             oi.maxOverlapping = _numRows;
103             oi.overlappingCount=_numRows;
104             oi.pos = i % _numRows;
105         }
106             
107             // put to cache
108         _oiRowCache.put(row, result);
109         // dump(result);
110         return result;
111     }
112 
113     /***
114      * {@inheritDoc}
115      */
116     public void clearCachedData() {
117         _oiRowCache.clear(); // clear oi cache
118     }
119 
120     /***
121      * {@inheritDoc} Simply helps the garbage collector.
122      */
123     public void dispose() {
124         _delegate = null;
125         _oiRowCache = null;
126     }
127 
128 
129 }