View Javadoc

1   /*
2    *  File: DefaultTimeBarModel.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  /**
26   * A default implementation of the TimeBarModel interface. Extends the AbstractTimeBarModel.
27   * 
28   * @author Peter Kliem
29   * @version $Id: DefaultTimeBarModel.java 886 2009-10-08 22:08:27Z kliem $
30   */
31  public class DefaultTimeBarModel extends AbstractTimeBarModel {
32      /** list of the rows. */
33      protected List<TimeBarRow> _rows = new ArrayList<TimeBarRow>();
34  
35      /**
36       * {@inheritDoc}
37       */
38      public TimeBarRow getRow(int row) {
39          return (TimeBarRow) _rows.get(row);
40      }
41  
42      /**
43       * {@inheritDoc}
44       */
45      public int getRowCount() {
46          return _rows.size();
47      }
48  
49      /**
50       * Add a row.
51       * 
52       * @param row row to add.
53       */
54      public void addRow(TimeBarRow row) {
55          addRow(-1, row);
56      }
57  
58      /**
59       * Add a row.
60       * 
61       * @param index index the row should be inserted. -1 marks append to the end.
62       * @param row row to add.
63       */
64      public void addRow(int index, TimeBarRow row) {
65          if (index == -1) {
66              _rows.add(row);
67          } else {
68              _rows.add(index, row);
69          }
70          if (_minDate == null) {
71              _minDate = row.getMinDate();
72              _maxDate = row.getMaxDate();
73          } else if (row.getMinDate() != null && row.getMaxDate() != null) {
74              if (_minDate.compareTo(row.getMinDate()) > 0) {
75                  _minDate = row.getMinDate();
76              } 
77              if (_maxDate.compareTo(row.getMaxDate()) < 0) {
78                  _maxDate = row.getMaxDate();
79              }
80          }
81          row.addTimeBarRowListener(this);
82          fireRowAdded(row);
83      }
84  
85      /**
86       * Remove a row from the model.
87       * 
88       * @param row row to remove
89       */
90      public void remRow(TimeBarRow row) {
91          if (_rows.contains(row)) {
92              row.remTimeBarRowListener(this);
93              _rows.remove(row);
94              updateMinMax();
95              fireRowRemoved(row);
96          }
97      }
98  
99      /**
100      * Retrieve the model index of a given row.
101      * 
102      * @param row row to check
103      * @return index or -1
104      */
105     public int getIndexForRow(TimeBarRow row) {
106         return _rows.indexOf(row);
107     }
108 
109 }