1 package de.jaret.examples.timebars.calendar.model;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Comparator;
6 import java.util.List;
7 import java.util.concurrent.CopyOnWriteArrayList;
8
9 import de.jaret.util.date.Interval;
10 import de.jaret.util.date.JaretDate;
11 import de.jaret.util.ui.timebars.model.DefaultRowHeader;
12 import de.jaret.util.ui.timebars.model.DefaultTimeBarRowModel;
13
14 /***
15 * A day representation as an extenion of the timebar row model. A list of placeholders are used for indicting
16 * appointments that span more than one day (rendereing is done by the header renderer).
17 *
18 * @author Peter Kliem
19 * @version $Id: Day.java 801 2008-12-27 22:44:54Z kliem $
20 */
21 public class Day extends DefaultTimeBarRowModel {
22 /*** the date of the da. */
23 private JaretDate _dayDate;
24
25 /*** List of placeholders. */
26 private List<AppointmentPlaceholder> _placeholders = new ArrayList<AppointmentPlaceholder>();
27
28 /***
29 * Construct the day for a specific date.
30 * @param date the day date
31 */
32 public Day(JaretDate date) {
33 super();
34
35 _intervals = new CopyOnWriteArrayList<Interval>();
36 setRowHeader(new DefaultRowHeader(date.toDisplayStringDate(), this));
37 _dayDate = date;
38 }
39
40 /***
41 * Add an interval.
42 *
43 * @param interval interval to add
44 */
45 public synchronized void addInterval(Interval interval) {
46 insertSorted(interval);
47
48 if (_minDate == null || _intervals.size() == 1) {
49 _minDate = interval.getBegin().copy();
50 _maxDate = interval.getEnd().copy();
51 } else {
52 if (_minDate.compareTo(interval.getBegin()) > 0) {
53 _minDate = interval.getBegin().copy();
54 } else if (_maxDate.compareTo(interval.getEnd()) < 0) {
55 _maxDate = interval.getEnd().copy();
56 }
57 }
58 interval.addPropertyChangeListener(this);
59 fireElementAdded(interval);
60 }
61
62 Comparator<Interval> intervalComparator = new Comparator<Interval>() {
63 public int compare(Interval i1, Interval i2) {
64 return i1.getBegin().compareTo(i2.getBegin());
65 }
66 };
67
68 private void insertSorted(Interval interval) {
69 if (_intervals.size() == 0) {
70 _intervals.add(interval);
71 return;
72 }
73 if (intervalComparator.compare(interval, _intervals.get(0)) < 0) {
74 _intervals.add(0, interval);
75 return;
76 }
77 for (int i = 0; i < _intervals.size() - 1; i++) {
78 if (intervalComparator.compare(_intervals.get(i), interval) <= 0
79 && intervalComparator.compare(interval, _intervals.get(i + 1)) >= 0) {
80 _intervals.add(i + 1, interval);
81 return;
82 }
83 }
84 _intervals.add(interval);
85 }
86
87 public JaretDate getDayDate() {
88 return _dayDate;
89 }
90
91 /***
92 * Look up an appointment on that day by comparing the ids.
93 *
94 * @param id id to check
95 * @return the appointment or <code>null</code>
96 */
97 public Appointment getAppointmentById(String id) {
98 for (Interval interval : _intervals) {
99 Appointment app = (Appointment) interval;
100 if (id.equals(app.getId())) {
101 return app;
102 }
103 }
104 return null;
105 }
106
107 /***
108 * Get all appointments for a day.
109 *
110 * @return list of appointments
111 */
112 public List<Appointment> getAppointments() {
113 List<Appointment> result = new ArrayList<Appointment>(_intervals.size());
114 for (Interval interval : _intervals) {
115 result.add((Appointment) interval);
116 }
117 return result;
118 }
119
120 /***
121 * Retrieve allplaceholders present.
122 * @return the list of placeholders
123 */
124 public List<AppointmentPlaceholder> getPlaceholders() {
125 return _placeholders;
126 }
127
128 /***
129 * Retrive the placeholder for an appointment.
130 * @param appointment appointment
131 * @return the placeholder for the appointment or <code>null</code>
132 */
133 public AppointmentPlaceholder getPlaceholder(Appointment appointment) {
134 for (AppointmentPlaceholder ph : _placeholders) {
135 if (ph.getAppointment().equals(appointment)) {
136 return ph;
137 }
138 }
139 return null;
140 }
141
142 /***
143 * Removes all placeholdes for the given appointment.
144 *
145 * @param appointment appointment
146 */
147 public void removePlaceholder(Appointment appointment) {
148 AppointmentPlaceholder ph = getPlaceholder(appointment);
149 if (ph != null) {
150 _placeholders.remove(ph);
151 }
152 }
153
154 public int getMaxPlaceholderPos() {
155 int max = -1;
156 for (AppointmentPlaceholder ph : _placeholders) {
157 if (ph.getPosition() > max) {
158 max = ph.getPosition();
159 }
160 }
161 return max;
162 }
163
164 public Appointment getPlaceholderForPosition(int pos) {
165 for (AppointmentPlaceholder ph : _placeholders) {
166 if (ph.getPosition() == pos) {
167 return ph.getAppointment();
168 }
169 }
170 return null;
171 }
172
173 public Collection<Appointment> getAppointmentBySynchronizerId(String synchronizerId) {
174 List<Appointment> result = new ArrayList<Appointment>(_intervals.size());
175 for (Interval interval : _intervals) {
176 Appointment app = (Appointment) interval;
177 if (app.getSynchronizerId().equals(synchronizerId)) {
178 result.add(app);
179 }
180 }
181 return result;
182 }
183
184 }