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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 5.8 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
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19
20/**
21 * Synchronizes scrollbar adjustments between a set of
22 * {@link Adjustable}s. Whenever the adjustment of one of
23 * the registerd Adjustables is updated the adjustment of
24 * the other registered Adjustables is adjusted too.
25 *
26 */
27public class AdjustmentSynchronizer implements AdjustmentListener {
28
29 private final ArrayList<Adjustable> synchronizedAdjustables;
30 private final HashMap<Adjustable, Boolean> enabledMap;
31
32 private final Observable observable;
33
34 public AdjustmentSynchronizer() {
35 synchronizedAdjustables = new ArrayList<Adjustable>();
36 enabledMap = new HashMap<Adjustable, Boolean>();
37 observable = new Observable();
38 }
39
40 /**
41 * registers an {@link 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 {@link AdjustmentEvent}s
58 *
59 */
60 @Override
61 public void adjustmentValueChanged(AdjustmentEvent e) {
62 if (! enabledMap.get(e.getAdjustable()))
63 return;
64 for (Adjustable a : synchronizedAdjustables) {
65 if (a != e.getAdjustable() && isParticipatingInSynchronizedScrolling(a)) {
66 a.setValue(e.getValue());
67 }
68 }
69 }
70
71 /**
72 * sets whether adjustable participates in adjustment synchronization
73 * or not
74 *
75 * @param adjustable the adjustable
76 */
77 protected void setParticipatingInSynchronizedScrolling(Adjustable adjustable, boolean isParticipating) {
78 CheckParameterUtil.ensureParameterNotNull(adjustable, "adjustable");
79 if (! synchronizedAdjustables.contains(adjustable))
80 throw new IllegalStateException(tr("Adjustable {0} not registered yet. Cannot set participation in synchronized adjustment.", adjustable));
81
82 enabledMap.put(adjustable, isParticipating);
83 observable.notifyObservers();
84 }
85
86 /**
87 * returns true if an adjustable is participating in synchronized scrolling
88 *
89 * @param adjustable the adjustable
90 * @return true, if the adjustable is participating in synchronized scrolling, false otherwise
91 * @throws IllegalStateException thrown, if adjustable is not registered for synchronized scrolling
92 */
93 protected boolean isParticipatingInSynchronizedScrolling(Adjustable adjustable) throws IllegalStateException {
94 if (! synchronizedAdjustables.contains(adjustable))
95 throw new IllegalStateException(tr("Adjustable {0} not registered yet.", adjustable));
96
97 return enabledMap.get(adjustable);
98 }
99
100 /**
101 * wires a {@link JCheckBox} to the adjustment synchronizer, in such a way that:
102 * <li>
103 * <ol>state changes in the checkbox control whether the adjustable participates
104 * in synchronized adjustment</ol>
105 * <ol>state changes in this {@link AdjustmentSynchronizer} are reflected in the
106 * {@link JCheckBox}</ol>
107 * </li>
108 *
109 *
110 * @param view the checkbox to control whether an adjustable participates in synchronized
111 * adjustment
112 * @param adjustable the adjustable
113 * @exception IllegalArgumentException thrown, if view is null
114 * @exception IllegalArgumentException thrown, if adjustable is null
115 */
116 protected void adapt(final JCheckBox view, final Adjustable adjustable) throws IllegalStateException {
117 CheckParameterUtil.ensureParameterNotNull(adjustable, "adjustable");
118 CheckParameterUtil.ensureParameterNotNull(view, "view");
119
120 if (! synchronizedAdjustables.contains(adjustable)) {
121 participateInSynchronizedScrolling(adjustable);
122 }
123
124 // register an item lister with the check box
125 //
126 view.addItemListener(new ItemListener() {
127 @Override
128 public void itemStateChanged(ItemEvent e) {
129 switch(e.getStateChange()) {
130 case ItemEvent.SELECTED:
131 if (!isParticipatingInSynchronizedScrolling(adjustable)) {
132 setParticipatingInSynchronizedScrolling(adjustable, true);
133 }
134 break;
135 case ItemEvent.DESELECTED:
136 if (isParticipatingInSynchronizedScrolling(adjustable)) {
137 setParticipatingInSynchronizedScrolling(adjustable, false);
138 }
139 break;
140 }
141 }
142 });
143
144 observable.addObserver(
145 new Observer() {
146 @Override
147 public void update(Observable o, Object arg) {
148 boolean sync = isParticipatingInSynchronizedScrolling(adjustable);
149 if (view.isSelected() != sync) {
150 view.setSelected(sync);
151 }
152 }
153 }
154 );
155 setParticipatingInSynchronizedScrolling(adjustable, true);
156 view.setSelected(true);
157 }
158}
Note: See TracBrowser for help on using the repository browser.