1 /*
2 * File: TimeBarViewerSynchronizer.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;
21
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import de.jaret.util.date.JaretDate;
28
29 /**
30 * A synchronizer for two or more TimeBarViewers.
31 *
32 * @author Peter Kliem
33 * @version $Id: TimeBarViewerSynchronizer.java 863 2009-06-22 20:06:19Z kliem $
34 */
35 public class TimeBarViewerSynchronizer implements PropertyChangeListener {
36 /** attribute marking that rowheigths should be synced. */
37 protected boolean _syncRowHeight = false;
38
39 /** attribute marking that start dates should be synced. */
40 protected boolean _syncStartDate = true;
41
42 /** attribute marking that the scales should be synced. */
43 protected boolean _syncTimeScale = true;
44
45 /** attribute marking that the y axis width should be synced. */
46 protected boolean _syncYAxisWidth = true;
47
48 /** List of viewers to be synchronized. */
49 protected List<TimeBarViewerInterface> _viewers;
50
51 /**
52 * Constructs a new Synchronizer. It will synchronize the timescale (optional), min/max dates of the scale, optional
53 * rowheight, optional startdate, optional y axis width
54 *
55 * @param syncRowHeight if true, row heights will be synchronized
56 * @param syncStartDate if true, the start dates will be synchronized
57 * @param syncTimeScale if true the scaling of the time axis will be synchronized
58 * @param syncYAxisWidth if true sync the width of the y axis
59 */
60 public TimeBarViewerSynchronizer(boolean syncRowHeight, boolean syncStartDate, boolean syncTimeScale, boolean syncYAxisWidth) {
61 _syncRowHeight = syncRowHeight;
62 _syncStartDate = syncStartDate;
63 _syncTimeScale = syncTimeScale;
64 _syncYAxisWidth = syncYAxisWidth;
65 }
66
67
68 /**
69 * Constructs a new Synchronizer. It will synchronize the timescale (optional), min/max dates of the scale, optional
70 * rowheight, optional startdate
71 *
72 * @param syncRowHeight if true, row heights will be synchronized
73 * @param syncStartDate if true, the start dates will be synchronized
74 * @param syncTimeScale if true the scaling of the time axis will be synchronized
75 */
76 public TimeBarViewerSynchronizer(boolean syncRowHeight, boolean syncStartDate, boolean syncTimeScale) {
77 this(syncRowHeight, syncStartDate, syncTimeScale, true);
78 }
79
80 /**
81 * Add a viewer to the set of synchronized viewers.
82 *
83 * @param viewer the viewer to be added
84 */
85 public void addViewer(TimeBarViewerInterface viewer) {
86 if (_viewers == null) {
87 _viewers = new ArrayList<TimeBarViewerInterface>();
88 }
89 _viewers.add(viewer);
90 viewer.addPropertyChangeListener(this);
91 }
92
93 /**
94 * Remove a viewer from the set of synchronized viewers.
95 *
96 * @param viewer the viewer to be removed
97 */
98 public void remViewer(TimeBarViewerInterface viewer) {
99 if (_viewers != null) {
100 _viewers.remove(viewer);
101 viewer.removePropertyChangeListener(this);
102 }
103 }
104
105 /**
106 * Handle property changes from the different viewers.
107 *
108 * @param evt propertyChange event
109 */
110 public void propertyChange(PropertyChangeEvent evt) {
111 // System.out.println("propChange "+evt.getPropertyName());
112 TimeBarViewerInterface emitting = (TimeBarViewerInterface) evt.getSource();
113 if (evt.getPropertyName().equals(TimeBarViewerInterface.PROPERTYNAME_PIXELPERSECOND) && _syncTimeScale) {
114 setPixPerSecond(emitting, emitting.getPixelPerSecond());
115 } else if (evt.getPropertyName().equals(TimeBarViewerInterface.PROPERTYNAME_ROWHEIGHT) && _syncRowHeight) {
116 setRowHeight(emitting, emitting.getRowHeight());
117 } else if (evt.getPropertyName().equals(TimeBarViewerInterface.PROPERTYNAME_STARTDATE) && _syncStartDate) {
118 setStartDate(emitting, emitting.getStartDate());
119 } else if (evt.getPropertyName().equals(TimeBarViewerInterface.PROPERTYNAME_MINDATE)) {
120 setMinDate(emitting, emitting.getMinDate());
121 } else if (evt.getPropertyName().equals(TimeBarViewerInterface.PROPERTYNAME_MAXDATE)) {
122 setMaxDate(emitting, emitting.getMaxDate());
123 } else if (evt.getPropertyName().equals(TimeBarViewerInterface.PROPERTYNAME_YAXISWIDTH) && _syncYAxisWidth) {
124 setYAxisWidth(emitting, emitting.getYAxisWidth());
125 }
126 }
127
128 /**
129 * Set the pix per second value for all viewers but the emitting viewer.
130 *
131 * @param emitting emitting viewer
132 * @param pixPerSecond value
133 */
134 private void setPixPerSecond(TimeBarViewerInterface emitting, double pixPerSecond) {
135 for (TimeBarViewerInterface viewer : _viewers) {
136 if (!emitting.equals(viewer)) { // do not set on the emitting viewer
137 viewer.removePropertyChangeListener(this);
138 viewer.setPixelPerSecond(pixPerSecond);
139 viewer.addPropertyChangeListener(this);
140 }
141 }
142 }
143
144 /**
145 * Set the rowheight value for all viewers but the emitting viewer.
146 *
147 * @param emitting emitting viewer
148 * @param rowHeight value
149 */
150 private void setRowHeight(TimeBarViewerInterface emitting, int rowHeight) {
151 for (TimeBarViewerInterface viewer : _viewers) {
152 if (!emitting.equals(viewer)) { // do not set on the emitting viewer
153 viewer.removePropertyChangeListener(this);
154 viewer.setRowHeight(rowHeight);
155 viewer.addPropertyChangeListener(this);
156 }
157 }
158 }
159
160 /**
161 * Set the minimium date value for all viewers but the emitting viewer.
162 *
163 * @param emitting emitting viewer
164 * @param minDate value
165 */
166 private void setMinDate(TimeBarViewerInterface emitting, JaretDate minDate) {
167 for (TimeBarViewerInterface viewer : _viewers) {
168 if (!emitting.equals(viewer)) { // do not set on the emitting viewer
169 viewer.removePropertyChangeListener(this);
170 viewer.setMinDate(minDate.copy());
171 viewer.addPropertyChangeListener(this);
172 }
173 }
174 }
175
176 /**
177 * Set the maximum date value for all viewers but the emitting viewer.
178 *
179 * @param emitting emitting viewer
180 * @param maxDate value
181 */
182 private void setMaxDate(TimeBarViewerInterface emitting, JaretDate maxDate) {
183 for (TimeBarViewerInterface viewer : _viewers) {
184 if (!emitting.equals(viewer)) { // do not set on the emitting viewer
185 viewer.removePropertyChangeListener(this);
186 viewer.setMaxDate(maxDate.copy());
187 viewer.addPropertyChangeListener(this);
188 }
189 }
190 }
191
192 /**
193 * Set the start date value for all viewers but the emitting viewer.
194 *
195 * @param emitting emitting viewer
196 * @param startDate value
197 */
198 private void setStartDate(TimeBarViewerInterface emitting, JaretDate startDate) {
199 for (TimeBarViewerInterface viewer : _viewers) {
200 if (!emitting.equals(viewer)) { // do not set on the emitting viewer
201 viewer.removePropertyChangeListener(this);
202 viewer.setStartDate(startDate.copy());
203 viewer.addPropertyChangeListener(this);
204 }
205 }
206 }
207
208 /**
209 * Set the y axis width value for all viewers but the emitting viewer.
210 *
211 * @param emitting emitting viewer
212 * @param yAxisWidth the width of the y axis
213 */
214 private void setYAxisWidth(TimeBarViewerInterface emitting, int yAxisWIdth) {
215 for (TimeBarViewerInterface viewer : _viewers) {
216 if (!emitting.equals(viewer)) { // do not set on the emitting viewer
217 viewer.removePropertyChangeListener(this);
218 viewer.setYAxisWidth(yAxisWIdth);
219 viewer.addPropertyChangeListener(this);
220 }
221 }
222 }
223
224 /**
225 * Produce a string representation.
226 *
227 * @return simple string representation
228 */
229 public String toString() {
230 StringBuilder buf = new StringBuilder();
231 buf.append("TimeBarViewerSynchronizer:\n");
232 for (TimeBarViewerInterface viewer : _viewers) {
233 buf.append(viewer.toString() + "\n");
234 }
235 return buf.toString();
236 }
237
238 }