source: josm/trunk/src/org/openstreetmap/josm/gui/history/AdjustmentSynchronizer.java@ 1989

Last change on this file since 1989 was 1709, checked in by Gubaer, 15 years ago

new: history feature implemented

File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Adjustable;
7import java.awt.event.AdjustmentEvent;
8import java.awt.event.AdjustmentListener;
9import java.awt.event.ItemEvent;
10import java.awt.event.ItemListener;
11import java.util.ArrayList;
12import java.util.HashMap;
13import java.util.Observable;
14import java.util.Observer;
15
16import javax.swing.JCheckBox;
17
18
19/**
20 * Synchronizes scrollbar adjustments between a set of
21 * {@see Adjustable}s. Whenever the adjustment of one of
22 * the registerd Adjustables is updated the adjustment of
23 * the other registered Adjustables is adjusted too.
24 *
25 */
26public class AdjustmentSynchronizer implements AdjustmentListener {
27
28 private final ArrayList<Adjustable> synchronizedAdjustables;
29 private final HashMap<Adjustable, Boolean> enabledMap;
30
31 private final Observable observable;
32
33 public AdjustmentSynchronizer() {
34 synchronizedAdjustables = new ArrayList<Adjustable>();
35 enabledMap = new HashMap<Adjustable, Boolean>();
36 observable = new Observable();
37 }
38
39
40 /**
41 * registers an {@see Adjustable} for participation in synchronized
42 * scrolling.
43 *
44 * @param adjustable the adjustable
45 */
46 public void participateInSynchronizedScrolling(Adjustable adjustable) {
47 if (adjustable == null)
48 return;
49 if (synchronizedAdjustables.contains(adjustable))
50 return;
51 synchronizedAdjustables.add(adjustable);
52 setParticipatingInSynchronizedScrolling(adjustable, true);
53 adjustable.addAdjustmentListener(this);
54 }
55
56 /**
57 * event handler for {@see AdjustmentEvent}s
58 *
59 */
60 public void adjustmentValueChanged(AdjustmentEvent e) {
61 if (! enabledMap.get(e.getAdjustable()))
62 return;
63 for (Adjustable a : synchronizedAdjustables) {
64 if (a != e.getAdjustable() && isParticipatingInSynchronizedScrolling(a)) {
65 a.setValue(e.getValue());
66 }
67 }
68 }
69
70 /**
71 * sets whether adjustable participates in adjustment synchronization
72 * or not
73 *
74 * @param adjustable the adjustable
75 */
76 protected void setParticipatingInSynchronizedScrolling(Adjustable adjustable, boolean isParticipating) {
77 if (adjustable == null)
78 throw new IllegalArgumentException(tr("parameter '{0}' must not be null", "adjustable"));
79
80 if (! synchronizedAdjustables.contains(adjustable))
81 throw new IllegalStateException(tr("adjustable {0} not registered yet. Can't set participation in synchronized adjustment",adjustable));
82
83 enabledMap.put(adjustable, isParticipating);
84 observable.notifyObservers();
85 }
86
87 /**
88 * returns true if an adjustable is participating in synchronized scrolling
89 *
90 * @param adjustable the adjustable
91 * @return true, if the adjustable is participating in synchronized scrolling, false otherwise
92 * @throws IllegalStateException thrown, if adjustable is not registered for synchronized scrolling
93 */
94 protected boolean isParticipatingInSynchronizedScrolling(Adjustable adjustable) throws IllegalStateException {
95 if (! synchronizedAdjustables.contains(adjustable))
96 throw new IllegalStateException(tr("adjustable {0} not registered yet",adjustable));
97
98 return enabledMap.get(adjustable);
99 }
100
101 /**
102 * wires a {@see JCheckBox} to the adjustment synchronizer, in such a way that:
103 * <li>
104 * <ol>state changes in the checkbox control whether the adjustable participates
105 * in synchronized adjustment</ol>
106 * <ol>state changes in this {@see AdjustmentSynchronizer} are reflected in the
107 * {@see JCheckBox}</ol>
108 * </li>
109 *
110 *
111 * @param view the checkbox to control whether an adjustable participates in synchronized
112 * adjustment
113 * @param adjustable the adjustable
114 * @exception IllegalArgumentException thrown, if view is null
115 * @exception IllegalArgumentException thrown, if adjustable is null
116 */
117 protected void adapt(final JCheckBox view, final Adjustable adjustable) throws IllegalArgumentException, IllegalStateException {
118 if (adjustable == null)
119 throw new IllegalArgumentException(tr("parameter '{0}' must not be null", "adjustable"));
120 if (view == null)
121 throw new IllegalArgumentException(tr("parameter '{0}' must not be null", "view"));
122
123 if (! synchronizedAdjustables.contains(adjustable)) {
124 participateInSynchronizedScrolling(adjustable);
125 }
126
127 // register an item lister with the check box
128 //
129 view.addItemListener(new ItemListener() {
130 public void itemStateChanged(ItemEvent e) {
131 switch(e.getStateChange()) {
132 case ItemEvent.SELECTED:
133 if (!isParticipatingInSynchronizedScrolling(adjustable)) {
134 setParticipatingInSynchronizedScrolling(adjustable, true);
135 }
136 break;
137 case ItemEvent.DESELECTED:
138 if (isParticipatingInSynchronizedScrolling(adjustable)) {
139 setParticipatingInSynchronizedScrolling(adjustable, false);
140 }
141 break;
142 }
143 }
144 });
145
146 observable.addObserver(
147 new Observer() {
148 public void update(Observable o, Object arg) {
149 boolean sync = isParticipatingInSynchronizedScrolling(adjustable);
150 if (view.isSelected() != sync) {
151 view.setSelected(sync);
152 }
153 }
154 }
155 );
156 setParticipatingInSynchronizedScrolling(adjustable, true);
157 view.setSelected(true);
158 }
159}
Note: See TracBrowser for help on using the repository browser.