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 | /**
|
---|
19 | * Synchronizes scrollbar adjustments between a set of
|
---|
20 | * {@see Adjustable}s. Whenever the adjustment of one of
|
---|
21 | * the registerd Adjustables is updated the adjustment of
|
---|
22 | * the other registered Adjustables is adjusted too.
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class AdjustmentSynchronizer implements AdjustmentListener {
|
---|
26 |
|
---|
27 | private final ArrayList<Adjustable> synchronizedAdjustables;
|
---|
28 | private final HashMap<Adjustable, Boolean> enabledMap;
|
---|
29 |
|
---|
30 | private final Observable observable;
|
---|
31 |
|
---|
32 | public AdjustmentSynchronizer() {
|
---|
33 | synchronizedAdjustables = new ArrayList<Adjustable>();
|
---|
34 | enabledMap = new HashMap<Adjustable, Boolean>();
|
---|
35 | observable = new Observable();
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * registers an {@see Adjustable} for participation in synchronized
|
---|
40 | * scrolling.
|
---|
41 | *
|
---|
42 | * @param adjustable the adjustable
|
---|
43 | */
|
---|
44 | public void participateInSynchronizedScrolling(Adjustable adjustable) {
|
---|
45 | if (adjustable == null)
|
---|
46 | return;
|
---|
47 | if (synchronizedAdjustables.contains(adjustable))
|
---|
48 | return;
|
---|
49 | synchronizedAdjustables.add(adjustable);
|
---|
50 | setParticipatingInSynchronizedScrolling(adjustable, true);
|
---|
51 | adjustable.addAdjustmentListener(this);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * event handler for {@see AdjustmentEvent}s
|
---|
56 | *
|
---|
57 | */
|
---|
58 | public void adjustmentValueChanged(AdjustmentEvent e) {
|
---|
59 | if (! enabledMap.get(e.getAdjustable()))
|
---|
60 | return;
|
---|
61 | for (Adjustable a : synchronizedAdjustables) {
|
---|
62 | if (a != e.getAdjustable() && isParticipatingInSynchronizedScrolling(a)) {
|
---|
63 | a.setValue(e.getValue());
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * sets whether adjustable participates in adjustment synchronization
|
---|
70 | * or not
|
---|
71 | *
|
---|
72 | * @param adjustable the adjustable
|
---|
73 | */
|
---|
74 | protected void setParticipatingInSynchronizedScrolling(Adjustable adjustable, boolean isParticipating) {
|
---|
75 | if (adjustable == null)
|
---|
76 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "adjustable"));
|
---|
77 |
|
---|
78 | if (! synchronizedAdjustables.contains(adjustable))
|
---|
79 | throw new IllegalStateException(tr("Adjustable {0} not registered yet. Can't 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 IllegalArgumentException, IllegalStateException {
|
---|
116 | if (adjustable == null)
|
---|
117 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "adjustable"));
|
---|
118 | if (view == null)
|
---|
119 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "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 | 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 | public void update(Observable o, Object arg) {
|
---|
147 | boolean sync = isParticipatingInSynchronizedScrolling(adjustable);
|
---|
148 | if (view.isSelected() != sync) {
|
---|
149 | view.setSelected(sync);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | );
|
---|
154 | setParticipatingInSynchronizedScrolling(adjustable, true);
|
---|
155 | view.setSelected(true);
|
---|
156 | }
|
---|
157 | }
|
---|