View Javadoc

1   /*
2    *  File: TimeBarSelectionModelImpl.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  import java.util.Vector;
25  
26  import de.jaret.util.date.Interval;
27  
28  /**
29   * Implementation of TimeBarSelectionModel: straight forward.
30   * 
31   * @author Peter Kliem
32   * @version $Id: TimeBarSelectionModelImpl.java 757 2008-04-27 22:42:37Z kliem $
33   */
34  public class TimeBarSelectionModelImpl implements TimeBarSelectionModel {
35      /** listener list. */
36      protected List<TimeBarSelectionListener> _listenerList;
37  
38      /** list of selecetd intervals. */
39      protected List<Interval> _selectedIntervals = new ArrayList<Interval>();
40  
41      /** list of selecetd rows. */
42      protected List<TimeBarRow> _selectedRows = new ArrayList<TimeBarRow>();
43  
44      /** list of selecetd relations. */
45      protected List<IIntervalRelation> _selectedRelations = new ArrayList<IIntervalRelation>();
46  
47      /** flag indicating that row selection is allowed. */
48      protected boolean _rowSelectAllowed = true;
49  
50      /** flag indicatig that the selection of intervals is allowed. */
51      protected boolean _intervalSelectAllow = true;
52  
53      /** flag indicatig that the selection of relations is allowed. */
54      protected boolean _relationSelectAllow = true;
55  
56      /** flag indicating that the selection of multiple elements is allowed. */
57      protected boolean _multiAllowed = true;
58  
59      /** flag indicating activated toggle mode for row selections. */
60      protected boolean _rowSelectionToggleMode = false;
61  
62      /**
63       * {@inheritDoc}
64       */
65      public boolean isEmpty() {
66          return _selectedIntervals.size() == 0 && _selectedRows.size() == 0 && _selectedRelations.size() == 0;
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      public boolean hasRowSelection() {
73          return _selectedRows.size() != 0;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      public boolean hasIntervalSelection() {
80          return _selectedIntervals.size() != 0;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public boolean hasRelationSelection() {
87          return _selectedRelations.size() != 0;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public List<TimeBarRow> getSelectedRows() {
94          return _selectedRows;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public List<Interval> getSelectedIntervals() {
101         return _selectedIntervals;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public List<IIntervalRelation> getSelectedRelations() {
108         return _selectedRelations;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     public void setRowSelectionAllowed(boolean allowed) {
115         _rowSelectAllowed = allowed;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     public boolean isRowSelectionAllowed() {
122         return _rowSelectAllowed;
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     public void setIntervalSelectionAllowed(boolean allowed) {
129         _intervalSelectAllow = allowed;
130     }
131 
132     /**
133      * {@inheritDoc}
134      */
135     public boolean isIntervalSelectionAllowed() {
136         return _intervalSelectAllow;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     public void setRelationSelectionAllowed(boolean allowed) {
143         _relationSelectAllow = allowed;
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     public boolean isRelationSelectionAllowed() {
150         return _relationSelectAllow;
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     public void setMultipleSelectionAllowed(boolean allowed) {
157         _multiAllowed = allowed;
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     public boolean getMultipleSelectionAllowed() {
164         return _multiAllowed;
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     public void clearSelection() {
171         if (!isEmpty()) {
172             _selectedIntervals.clear();
173             _selectedRows.clear();
174             _selectedRelations.clear();
175             fireSelectionChanged();
176         }
177     }
178 
179     /**
180      * {@inheritDoc}
181      */
182     public void clearIntervalSelection() {
183         if (hasIntervalSelection()) {
184             _selectedIntervals.clear();
185             fireSelectionChanged();
186         }
187     }
188 
189     /**
190      * {@inheritDoc}
191      */
192     public void clearRowSelection() {
193         if (hasRowSelection()) {
194             _selectedRows.clear();
195             fireSelectionChanged();
196         }
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     public void clearRelationSelection() {
203         if (hasRelationSelection()) {
204             _selectedRelations.clear();
205             fireSelectionChanged();
206         }
207     }
208 
209     /**
210      * {@inheritDoc}
211      */
212     public void setSelectedRow(TimeBarRow row) {
213         if (_rowSelectAllowed) {
214             _selectedRows.clear();
215             _selectedRows.add(row);
216             fireSelectionChanged();
217         }
218     }
219 
220     /**
221      * {@inheritDoc}
222      */
223     public void addSelectedRow(TimeBarRow row) {
224         if (_rowSelectAllowed) {
225             if (!_multiAllowed) {
226                 _selectedRows.clear();
227                 fireSelectionChanged();
228             }
229             if (!_selectedRows.contains(row)) {
230                 _selectedRows.add(row);
231                 fireElementAdded(row);
232             }
233         }
234     }
235 
236     /**
237      * {@inheritDoc}
238      */
239     public void remSelectedRow(TimeBarRow row) {
240         if (isSelected(row)) {
241             _selectedRows.remove(row);
242             fireElementRemoved(row);
243         }
244     }
245 
246     /**
247      * {@inheritDoc}
248      */
249     public boolean isSelected(TimeBarRow row) {
250         return _selectedRows.contains(row);
251     }
252 
253     /**
254      * {@inheritDoc}
255      */
256     public void setSelectedInterval(Interval interval) {
257         if (_intervalSelectAllow) {
258             boolean hasSelection = hasIntervalSelection();
259             _selectedIntervals.clear();
260             _selectedIntervals.add(interval);
261             if (hasSelection) {
262                 fireSelectionChanged();
263             } else {
264                 fireElementAdded(interval);
265             }
266         }
267     }
268 
269     /**
270      * {@inheritDoc}
271      */
272     public void addSelectedInterval(Interval interval) {
273         if (_intervalSelectAllow) {
274             if (!_multiAllowed) {
275                 _selectedIntervals.clear();
276                 fireSelectionChanged();
277             }
278             if (!_selectedIntervals.contains(interval)) {
279                 _selectedIntervals.add(interval);
280                 fireElementAdded(interval);
281             }
282         }
283     }
284 
285     /**
286      * {@inheritDoc}
287      */
288     public void remSelectedInterval(Interval interval) {
289         if (isSelected(interval)) {
290             _selectedIntervals.remove(interval);
291             fireElementRemoved(interval);
292         }
293     }
294 
295     /**
296      * {@inheritDoc}
297      */
298     public void remSelectedIntervals(List<Interval> intervals) {
299         _selectedIntervals.removeAll(intervals);
300         fireSelectionChanged();
301     }
302 
303     /**
304      * {@inheritDoc}
305      */
306     public boolean isSelected(Interval interval) {
307         return _selectedIntervals.contains(interval);
308     }
309 
310     /**
311      * {@inheritDoc}
312      */
313     public void setSelectedRelation(IIntervalRelation relation) {
314         if (_relationSelectAllow) {
315             _selectedRelations.clear();
316             _selectedRelations.add(relation);
317             fireSelectionChanged();
318         }
319     }
320 
321     /**
322      * {@inheritDoc}
323      */
324     public void addSelectedRelation(IIntervalRelation relation) {
325         if (_relationSelectAllow) {
326             if (!_multiAllowed) {
327                 _selectedRelations.clear();
328                 fireSelectionChanged();
329             }
330             if (!_selectedRelations.contains(relation)) {
331                 _selectedRelations.add(relation);
332                 fireElementAdded(relation);
333             }
334         }
335     }
336 
337     /**
338      * {@inheritDoc}
339      */
340     public void remSelectedRelation(IIntervalRelation relation) {
341         if (isSelected(relation)) {
342             _selectedRelations.remove(relation);
343             fireElementRemoved(relation);
344         }
345     }
346 
347     /**
348      * {@inheritDoc}
349      */
350     public void remSelectedRelations(List<IIntervalRelation> relations) {
351         _selectedRelations.removeAll(relations);
352         fireSelectionChanged();
353     }
354 
355     /**
356      * {@inheritDoc}
357      */
358     public boolean isSelected(IIntervalRelation relation) {
359         return _selectedRelations.contains(relation);
360     }
361 
362     /**
363      * {@inheritDoc}
364      */
365     public synchronized void addTimeBarSelectionListener(TimeBarSelectionListener tbsl) {
366         if (_listenerList == null) {
367             _listenerList = new Vector<TimeBarSelectionListener>();
368         }
369         _listenerList.add(tbsl);
370     }
371 
372     /**
373      * {@inheritDoc}
374      */
375     public void remTimeBarSelectionListener(TimeBarSelectionListener tbsl) {
376         if (_listenerList != null) {
377             _listenerList.remove(tbsl);
378         }
379     }
380 
381     /**
382      * Inform listeners about a general change of the selection.
383      * 
384      */
385     protected void fireSelectionChanged() {
386         if (_listenerList != null) {
387             for (TimeBarSelectionListener listener : _listenerList) {
388                 listener.selectionChanged(this);
389             }
390         }
391     }
392 
393     /**
394      * Inform listeners about the addition of a new element in the selection.
395      * 
396      * @param element newly selected element
397      */
398     protected void fireElementAdded(Object element) {
399         if (_listenerList != null) {
400             for (TimeBarSelectionListener listener : _listenerList) {
401                 listener.elementAddedToSelection(this, element);
402             }
403         }
404     }
405 
406     /**
407      * Inform listeners about the removal of an element from the selection.
408      * 
409      * @param element the removed element
410      */
411     protected void fireElementRemoved(Object element) {
412         if (_listenerList != null) {
413             for (TimeBarSelectionListener listener : _listenerList) {
414                 listener.elementRemovedFromSelection(this, element);
415             }
416         }
417     }
418 
419     /**
420      * {@inheritDoc}
421      */
422     public boolean getRowSelectionToggleMode() {
423         return _rowSelectionToggleMode;
424     }
425 
426     /**
427      * {@inheritDoc}
428      */
429     public void setRowSelectionToggleMode(boolean activated) {
430         _rowSelectionToggleMode = activated;
431     }
432 
433 }