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

Last change on this file since 2512 was 2512, checked in by stoecker, 14 years ago

i18n updated, fixed files to reduce problems when applying patches, fix #4017

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
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 */
25public 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}
Note: See TracBrowser for help on using the repository browser.