View Javadoc

1   /*
2    *  File: CollectingTimeBarRow.java 
3    *  Copyright (c) 2004-2008  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.eventmonitoring.model;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Vector;
25  
26  import de.jaret.util.date.Interval;
27  import de.jaret.util.ui.timebars.model.AbstractTimeBarRowModel;
28  import de.jaret.util.ui.timebars.model.TimeBarNode;
29  import de.jaret.util.ui.timebars.model.TimeBarNodeListener;
30  import de.jaret.util.ui.timebars.model.TimeBarRowHeader;
31  
32  /***
33   * An implementation of a TimeBarNode that adds up all children. The node itself can carry it's own intervals.
34   * 
35   * @author Peter Kliem
36   * @version $Id: CollectingTimeBarNode.java 801 2008-12-27 22:44:54Z kliem $
37   */
38  public class CollectingTimeBarNode extends AbstractTimeBarRowModel implements TimeBarNode {
39      /*** children. */
40      protected List<TimeBarNode> _children = new ArrayList<TimeBarNode>();
41  
42      protected List<Interval> _ownIntervals = new ArrayList<Interval>();
43  
44      /*** level of the node. */
45      protected int _level = -1;
46  
47      /*** NodeListeners regsitered. */
48      protected List<TimeBarNodeListener> _nodeListeners;
49  
50      /*** true if all intervals of the children shall be collected. */
51      protected boolean _collectChildIntervals = true;
52  
53      /***
54       * Constructor.
55       * 
56       * @param header required header
57       */
58      public CollectingTimeBarNode(TimeBarRowHeader header) {
59          super(header);
60      }
61  
62      public void addInterval(Interval interval) {
63          _ownIntervals.add(interval);
64          updateMinMax();
65      }
66  
67      /***
68       * {@inheritDoc}
69       */
70      public List<TimeBarNode> getChildren() {
71          return _children;
72      }
73  
74      /***
75       * {@inheritDoc}
76       */
77      public void addNode(TimeBarNode node) {
78          node.setLevel(getLevel() + 1); // set the level of the added node to be under the current node
79          _children.add(node);
80          fireNodeAdded(node);
81      }
82  
83      /***
84       * {@inheritDoc}
85       */
86      public void remNode(TimeBarNode node) {
87          if (_children.remove(node)) {
88              fireNodeRemoved(node);
89          }
90      }
91  
92      /***
93       * {@inheritDoc}
94       */
95      public int getLevel() {
96          return _level;
97      }
98  
99      /***
100      * {@inheritDoc}
101      */
102     public void setLevel(int level) {
103         _level = level;
104         if (_children != null) {
105             for (TimeBarNode node : _children) {
106                 node.setLevel(level + 1);
107             }
108         }
109     }
110 
111     /***
112      * {@inheritDoc}
113      */
114     public synchronized void addTimeBarNodeListener(TimeBarNodeListener tbnl) {
115         if (_nodeListeners == null) {
116             _nodeListeners = new Vector<TimeBarNodeListener>();
117         }
118         _nodeListeners.add(tbnl);
119         super.addTimeBarRowListener(tbnl);
120     }
121 
122     /***
123      * {@inheritDoc}
124      */
125     public void removeTimeBarNodeListener(TimeBarNodeListener tbnl) {
126         if (_nodeListeners == null) {
127             return;
128         }
129         _nodeListeners.remove(tbnl);
130         super.remTimeBarRowListener(tbnl);
131     }
132 
133     /***
134      * Inform listeners about a new child.
135      * 
136      * @param newNode new child node
137      */
138     protected void fireNodeAdded(TimeBarNode newNode) {
139         if (_nodeListeners != null) {
140             for (TimeBarNodeListener listener : _nodeListeners) {
141                 listener.nodeAdded(this, newNode);
142             }
143         }
144     }
145 
146     /***
147      * Inform listeners about a removed child.
148      * 
149      * @param removedNode removed child node
150      */
151     protected void fireNodeRemoved(TimeBarNode removedNode) {
152         if (_nodeListeners != null) {
153             for (TimeBarNodeListener listener : _nodeListeners) {
154                 listener.nodeRemoved(this, removedNode);
155             }
156         }
157     }
158 
159     @Override
160     public List<Interval> getIntervals() {
161         List<Interval> result = new ArrayList<Interval>();
162         result.addAll(_ownIntervals);
163         if (_collectChildIntervals) {
164             for (TimeBarNode child : _children) {
165                 result.addAll(child.getIntervals());
166             }
167         }
168 
169         return result;
170     }
171 
172     public boolean getCollectChildIntervals() {
173         return _collectChildIntervals;
174     }
175 
176     public void setCollectChildIntervals(boolean collectChildIntervals) {
177         if (_collectChildIntervals != collectChildIntervals) {
178             _collectChildIntervals = collectChildIntervals;
179             fireRowDataChanged();
180         }
181     }
182 
183 }