Debian Codegraph @

src/com/jidesoft/range/RangeMorpher.java

raw
1 /**
2 * Copyright (c) Catalysoft Ltd, 2005-2013 All Rights Reserved
3 * Created: 23/06/2013 at 19:11
4 */
5 package com.jidesoft.range;
6
7 import javax.swing.*;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.beans.PropertyChangeListener;
11 import java.beans.PropertyChangeSupport;
12
13 public class RangeMorpher {
14 public static final String PROPERTY_MORPH_STARTED = "Morph Started";
15 public static final String PROPERTY_MORPH_ENDED = "Morph Ended";
16 public static final String PROPERTY_MORPH_RANGES = "Morph Ranges";
17 private double position = 1.0;
18 private int numSteps = 10;
19 private int delay = 40;
20 private int stepsLeft = numSteps;
21 private Timer timer;
22 private Range<?>[] ranges = null;
23 private PropertyChangeSupport support = new PropertyChangeSupport(this);
24
25 /**
26 * This constructor is provided mainly for Java Bean compatibility. If you use it, make sure you call
27 * <code>setChart</code> to inform the object of the chart on which it operates.
28 */
29 public RangeMorpher() {
30
31 }
32
33 /**
34 * Create a RangeMorpher instance on the supplied <code>Chart</code>.
35 *
36 * @param numSteps the number of steps in a transition
37 * @param delay the delay between the steps, in milliseconds
38 */
39 public RangeMorpher(int numSteps, int delay) {
40 this.numSteps = numSteps;
41 stepsLeft = numSteps;
42 this.delay = delay;
43 }
44
45 /**
46 * Add a property change listener to this object. Property events are fired at the beginning and at the end of
47 * a transition.
48 *
49 * @param listener the property change listener
50 */
51 public void addPropertyChangeListener(PropertyChangeListener listener) {
52 support.addPropertyChangeListener(listener);
53 }
54
55 /**
56 * Remove a property change listener from this object.
57 *
58 * @param listener the property change listener to remove
59 */
60 public void removePropertyChangeListener(PropertyChangeListener listener) {
61 support.removePropertyChangeListener(listener);
62 }
63
64 /**
65 * Returns true if a morph is currently in progress
66 *
67 * @return a boolean to indicate whether a morph is in progress.
68 */
69 public boolean isMorphing() {
70 return position < 1.0;
71 }
72
73 public void morph(final Range<?>[] sourceRanges, final Range<?>[] destinationRanges) {
74 // Make sure the timer is not running
75 stopAnimation();
76 ActionListener listener = new ActionListener() {
77 public void actionPerformed(ActionEvent e) {
78 stepsLeft--;
79 position = ((double) (numSteps - stepsLeft)) / numSteps;
80 if (stepsLeft == 0) {
81 stopAnimation();
82 support.firePropertyChange(PROPERTY_MORPH_ENDED, ranges, destinationRanges);
83 } else {
84 Range<?>[] oldRanges = ranges;
85 ranges = createIntermediate(sourceRanges, destinationRanges, position);
86 support.firePropertyChange(PROPERTY_MORPH_RANGES, oldRanges, ranges);
87 }
88 }
89 };
90 timer = new Timer(delay, listener);
91 timer.start();
92 support.firePropertyChange(PROPERTY_MORPH_STARTED, null, sourceRanges);
93 }
94
95 /**
96 * Creates an intermediate array of ranges to be used during the transition
97 *
98 * @param sources the source ranges
99 * @param targets the parallel array of corresponding target ranges
100 * @param position the position on a scale from 0 (source) to 1 (target)
101 * @return a Chartable point representing the given position during the transition
102 */
103 Range<?>[] createIntermediate(Range<?>[] sources, Range<?>[] targets, double position) {
104 Range<?>[] ranges = new Range<?>[sources.length];
105 for (int i=0; i<ranges.length; i++) {
106 if (sources[i] instanceof AbstractRange) {
107 AbstractRange<?> ar = (AbstractRange) sources[i];
108 ranges[i] = ar.createIntermediate((AbstractRange) targets[i], position);
109 } else {
110 throw new UnsupportedOperationException("Cannot morph "+sources[i].getClass().getName());
111 }
112 }
113 return ranges;
114 }
115
116
117 Range<?> createIntermediate(NumericRange source, NumericRange target, double position) {
118 double sourceMin = source.minimum();
119 double sourceMax = source.maximum();
120 double targetMin = target.minimum();
121 double targetMax = target.maximum();
122 double min = sourceMin + position * (targetMin - sourceMin);
123 double max= sourceMax + position * (targetMax - sourceMax);
124 return new NumericRange(min, max);
125 }
126
127 Range<?> createIntermediate(CategoryRange<?> source, CategoryRange<?> target, double position) {
128 double sourceMin = source.minimum();
129 double sourceMax = source.maximum();
130 double targetMin = target.minimum();
131 double targetMax = target.maximum();
132 double min = sourceMin + position * (targetMin - sourceMin);
133 double max= sourceMax + position * (targetMax - sourceMax);
134 CategoryRange r = position < 0.5 ? new CategoryRange(source) : new CategoryRange(target);
135 r.setMinimum(min);
136 r.setMaximum(max);
137 return r;
138 }
139
140 Range<?> createIntermediate(TimeRange source, TimeRange target, double position) {
141 double sourceMin = source.minimum();
142 double sourceMax = source.maximum();
143 double targetMin = target.minimum();
144 double targetMax = target.maximum();
145 double min = sourceMin + position * (targetMin - sourceMin);
146 double max= sourceMax + position * (targetMax - sourceMax);
147 return new TimeRange((long) min, (long) max);
148 }
149
150 /**
151 * Stops the morphing.
152 */
153 private void stopAnimation() {
154 if (timer != null && timer.isRunning()) {
155 timer.stop();
156 stepsLeft = numSteps;
157 }
158 }
159
160 }
161