1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.history;
|
---|
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.ArrayList;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.Observable;
|
---|
14 | import java.util.Observer;
|
---|
15 |
|
---|
16 | import javax.swing.JCheckBox;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Synchronizes scrollbar adjustments between a set of
|
---|
22 | * {@see 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 | */
|
---|
27 | public 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 {@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 | CheckParameterUtil.ensureParameterNotNull(adjustable, "adjustable");
|
---|
78 | if (! synchronizedAdjustables.contains(adjustable))
|
---|
79 | throw new IllegalStateException(tr("Adjustable {0} not registered yet. Cannot set participation in synchronized adjustment.", adjustable));
|
---|
80 |
|
---|
81 | enabledMap.put(adjustable, isParticipating);
|
---|
82 | observable.notifyObservers();
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * returns true if an adjustable is participating in synchronized scrolling
|
---|
87 | *
|
---|
88 | * @param adjustable the adjustable
|
---|
89 | * @return true, if the adjustable is participating in synchronized scrolling, false otherwise
|
---|
90 | * @throws IllegalStateException thrown, if adjustable is not registered for synchronized scrolling
|
---|
91 | */
|
---|
92 | protected boolean isParticipatingInSynchronizedScrolling(Adjustable adjustable) throws IllegalStateException {
|
---|
93 | if (! synchronizedAdjustables.contains(adjustable))
|
---|
94 | throw new IllegalStateException(tr("Adjustable {0} not registered yet.", adjustable));
|
---|
95 |
|
---|
96 | return enabledMap.get(adjustable);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * wires a {@see JCheckBox} to the adjustment synchronizer, in such a way that:
|
---|
101 | * <li>
|
---|
102 | * <ol>state changes in the checkbox control whether the adjustable participates
|
---|
103 | * in synchronized adjustment</ol>
|
---|
104 | * <ol>state changes in this {@see AdjustmentSynchronizer} are reflected in the
|
---|
105 | * {@see JCheckBox}</ol>
|
---|
106 | * </li>
|
---|
107 | *
|
---|
108 | *
|
---|
109 | * @param view the checkbox to control whether an adjustable participates in synchronized
|
---|
110 | * adjustment
|
---|
111 | * @param adjustable the adjustable
|
---|
112 | * @exception IllegalArgumentException thrown, if view is null
|
---|
113 | * @exception IllegalArgumentException thrown, if adjustable is null
|
---|
114 | */
|
---|
115 | protected void adapt(final JCheckBox view, final Adjustable adjustable) throws IllegalStateException {
|
---|
116 | CheckParameterUtil.ensureParameterNotNull(adjustable, "adjustable");
|
---|
117 | CheckParameterUtil.ensureParameterNotNull(view, "view");
|
---|
118 |
|
---|
119 | if (! synchronizedAdjustables.contains(adjustable)) {
|
---|
120 | participateInSynchronizedScrolling(adjustable);
|
---|
121 | }
|
---|
122 |
|
---|
123 | // register an item lister with the check box
|
---|
124 | //
|
---|
125 | view.addItemListener(new ItemListener() {
|
---|
126 | public void itemStateChanged(ItemEvent e) {
|
---|
127 | switch(e.getStateChange()) {
|
---|
128 | case ItemEvent.SELECTED:
|
---|
129 | if (!isParticipatingInSynchronizedScrolling(adjustable)) {
|
---|
130 | setParticipatingInSynchronizedScrolling(adjustable, true);
|
---|
131 | }
|
---|
132 | break;
|
---|
133 | case ItemEvent.DESELECTED:
|
---|
134 | if (isParticipatingInSynchronizedScrolling(adjustable)) {
|
---|
135 | setParticipatingInSynchronizedScrolling(adjustable, false);
|
---|
136 | }
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | });
|
---|
141 |
|
---|
142 | observable.addObserver(
|
---|
143 | new Observer() {
|
---|
144 | public void update(Observable o, Object arg) {
|
---|
145 | boolean sync = isParticipatingInSynchronizedScrolling(adjustable);
|
---|
146 | if (view.isSelected() != sync) {
|
---|
147 | view.setSelected(sync);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | );
|
---|
152 | setParticipatingInSynchronizedScrolling(adjustable, true);
|
---|
153 | view.setSelected(true);
|
---|
154 | }
|
---|
155 | }
|
---|