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

Last change on this file since 2986 was 2850, checked in by mjulius, 14 years ago

fix messages for gui

File size: 5.7 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 * {@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 */
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 {@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}
Note: See TracBrowser for help on using the repository browser.