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