View Javadoc

1   /*
2    *  File: ITimeBarViewStateListener.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.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Vector;
26  
27  import de.jaret.util.ui.timebars.TimeBarViewerDelegate;
28  
29  /**
30   * The default implementation of a ITimeBarViewState.
31   * 
32   * @author kliem
33   * @version $Id: DefaultTimeBarViewState.java 800 2008-12-27 22:27:33Z kliem $
34   */
35  public class DefaultTimeBarViewState implements ITimeBarViewState {
36      /** vies state listeners. */
37      protected List<ITimeBarViewStateListener> _tbvsListeners = new Vector<ITimeBarViewStateListener>();
38  
39      /** map holding the heights. */
40      protected Map<TimeBarRow, Integer> _heights = new HashMap<TimeBarRow, Integer>();
41  
42      /** map holding the individual overlapping properties for rows. */
43      protected Map<TimeBarRow, Boolean> _drawOverlapping = new HashMap<TimeBarRow, Boolean>();
44  
45      /** true if variable row heights should be used, false if fixed heights should be used. */
46      protected boolean _useVariableRowHeights = false;
47  
48      /** the default height for rows. */
49      protected int _defaultHeight;
50  
51      /** strategy for calculatng row heights. */
52      protected IRowHeightStrategy _rowHeightStrategy;
53  
54      /** delegate this viewstate is assigned to. */
55      protected TimeBarViewerDelegate _delegate;
56  
57      /**
58       * Consruct the default view state for a specific delegate.
59       * 
60       * @param delegate the delegate
61       */
62      public DefaultTimeBarViewState(TimeBarViewerDelegate delegate) {
63          _delegate = delegate;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public int getRowHeight(TimeBarRow row) {
70          if (!_useVariableRowHeights) {
71              return _defaultHeight;
72          }
73          Integer h = _heights.get(row);
74          if (h == null || (_rowHeightStrategy != null) && _rowHeightStrategy.overrideDefault()) {
75              if (_rowHeightStrategy == null) {
76                  // use the default
77                  return _defaultHeight;
78              } else {
79                  return _rowHeightStrategy.calculateRowHeight(_delegate, this, row);
80              }
81          }
82          return h;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      public void setRowHeight(TimeBarRow row, int height) {
89          int curHeight = getRowHeight(row);
90          if (curHeight != height) {
91              _heights.put(row, height);
92              fireRowHeightChanged(row, height);
93          }
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void setRowHeightStrategy(IRowHeightStrategy rowHeightStrategy) {
100         _rowHeightStrategy = rowHeightStrategy;
101         fireViewStateChanged();
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public IRowHeightStrategy getRowHeightStrategy() {
108         return _rowHeightStrategy;
109     }
110 
111     /**
112      * Inform the listeners about the new height of a row.
113      * 
114      * @param row row
115      * @param height new height
116      */
117     public void fireRowHeightChanged(TimeBarRow row, int height) {
118         for (ITimeBarViewStateListener listener : _tbvsListeners) {
119             listener.rowHeightChanged(row, height);
120         }
121     }
122 
123     /**
124      * Inform listeners about a massive change in the view state.
125      */
126     public void fireViewStateChanged() {
127         for (ITimeBarViewStateListener listener : _tbvsListeners) {
128             listener.viewStateChanged();
129         }
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     public void removeTimeBarViewStateListener(ITimeBarViewStateListener listener) {
136         _tbvsListeners.remove(listener);
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public void addTimeBarViewStateListener(ITimeBarViewStateListener listener) {
143         _tbvsListeners.add(listener);
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     public void setDefaultRowHeight(int height) {
150         _defaultHeight = height;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     public int getDefaultRowHeight() {
157         return _defaultHeight;
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     public boolean getUseVariableRowHeights() {
164         return _useVariableRowHeights;
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     public void setUseVariableRowHeights(boolean useVariableRowHeights) {
171         if (useVariableRowHeights != _useVariableRowHeights) {
172             _useVariableRowHeights = useVariableRowHeights;
173             fireViewStateChanged();
174         }
175     }
176 
177     /**
178      * {@inheritDoc}
179      */
180     public boolean getDrawOverlapping(TimeBarRow row) {
181         Boolean result = _drawOverlapping.get(row);
182         if (result == null) {
183             return _delegate.isDrawOverlapping();
184         }
185         return result.booleanValue();
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     public void setDrawOverlapping(TimeBarRow row, boolean drawOverlapping) {
192         _drawOverlapping.put(row, drawOverlapping);
193     }
194 
195 }