My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*******************************************************************************
* Copyright (c) 2011 Zeljko Zirikovic.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GPL which
* accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
******************************************************************************/
package com.revolucion.secretwit.ui.timeline;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.plaf.basic.BasicListUI;

import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.TimingTargetAdapter;
import org.jdesktop.animation.timing.interpolation.PropertySetter;

public class TimelineListUI extends BasicListUI {

private Animator animator;

// private boolean firstTimeInitialization = true;
private float cellAlpha = 1.0f;

private int minAnimIndex;
private int maxAnimIndex;

private RolloverListener rolloverListener;
private ComponentAdapter highlightComponentListener;
private ListDataListener dataListener;

private int rolledOverIndex = -1;

@Override
protected void installDefaults() {
super.installDefaults();

animator = new Animator(800);
animator.setAcceleration(0.2f);
animator.setDeceleration(0.6f);
animator.addTarget(new PropertySetter(this, "cellAlpha", 0.0f));
animator.addTarget(new TimingTargetAdapter() {
@Override
public void end() {
minAnimIndex = -1;
maxAnimIndex = -1;
// firstTimeInitialization = false;
}
});
}

@Override
protected void uninstallDefaults() {
super.uninstallDefaults();

animator = null;
}

@Override
protected void installListeners() {
super.installListeners();

rolloverListener = new RolloverListener();

highlightComponentListener = new ComponentAdapter() {
public void componentMoved(ComponentEvent e) {
fadeOutRolloverIndication();
resetRolloverIndex();
};
};

dataListener = new ListDataListener() {
@Override
public void intervalAdded(ListDataEvent e) {
if (!animator.isRunning()) {
minAnimIndex = e.getIndex0();
maxAnimIndex = e.getIndex1();
cellAlpha = 1.0f;
animator.start();
}
}

@Override
public void intervalRemoved(ListDataEvent e) {}

@Override
public void contentsChanged(ListDataEvent e) {}
};

list.addMouseListener(rolloverListener);
list.addMouseMotionListener(rolloverListener);
list.addComponentListener(highlightComponentListener);

if (list.getModel() != null)
list.getModel().addListDataListener(dataListener);
}

@Override
protected void uninstallListeners() {
super.uninstallListeners();

list.removeMouseListener(rolloverListener);
list.removeMouseMotionListener(rolloverListener);
list.removeComponentListener(highlightComponentListener);

if (list.getModel() != null)
list.getModel().removeListDataListener(dataListener);

rolloverListener = null;
highlightComponentListener = null;
}

@Override
protected void paintCell(Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, ListModel dataModel, ListSelectionModel selModel, int leadIndex) {
// The effect is applied for all cells if it's the first time they
// appear, and always for newly inserted first cell
// if (animator.isRunning() && (firstTimeInitialization || row == 0)) {
if (animator.isRunning() && row >= minAnimIndex && row <= maxAnimIndex) {
// Calculate cell position, set offset and paint it
Rectangle newRowBounds = new Rectangle(rowBounds);
newRowBounds.x = (int) (-newRowBounds.width * cellAlpha);

super.paintCell(g, row, newRowBounds, cellRenderer, dataModel, selModel, leadIndex);

// Paint overlay for fade in effect
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, cellAlpha));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.white);
g2.fillRect(rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height);
g2.dispose();
}
else
super.paintCell(g, row, rowBounds, cellRenderer, dataModel, selModel, leadIndex);
}

private void fadeOutRolloverIndication() {
if (rolledOverIndex < 0)
return;

Rectangle rect = list.getCellBounds(rolledOverIndex, rolledOverIndex);
list.repaint(rect);
}

private void fadeInRolloverIndication() {
if (rolledOverIndex < 0)
return;

Rectangle rect = list.getCellBounds(rolledOverIndex, rolledOverIndex);
list.repaint(rect);
}

public float getCellAlpha() {
return cellAlpha;
}

public void setCellAlpha(float cellAlpha) {
this.cellAlpha = cellAlpha;
list.repaint();
}

public int getRolledOverIndex() {
return rolledOverIndex;
}

public void setRolledOverIndex(int rolledOverIndex) {
this.rolledOverIndex = rolledOverIndex;
}

public void resetRolloverIndex() {
rolledOverIndex = -1;
}

private final class RolloverListener implements MouseListener, MouseMotionListener {

private void handleMove(MouseEvent e) {
int roIndex = list.locationToIndex(e.getPoint());
if ((roIndex >= 0) && (roIndex < list.getModel().getSize())) {
// test actual hit
if (!list.getCellBounds(roIndex, roIndex).contains(e.getPoint())) {
roIndex = -1;
}
}
if ((roIndex < 0) || (roIndex >= list.getModel().getSize())) {
fadeOutRolloverIndication();
resetRolloverIndex();
}
else {
// check if this is the same index
if ((rolledOverIndex >= 0) && (rolledOverIndex == roIndex))
return;

fadeOutRolloverIndication();

// rollover on a new row
rolledOverIndex = roIndex;
fadeInRolloverIndication();
}
}

@Override
public void mouseDragged(MouseEvent e) {
if (!list.isEnabled())
return;
handleMove(e);
}

@Override
public void mouseMoved(MouseEvent e) {
if (!list.isEnabled())
return;
handleMove(e);
}

@Override
public void mouseClicked(MouseEvent e) {}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {
fadeOutRolloverIndication();
resetRolloverIndex();
}

}

}

Change log

r5 by joshefin on Jun 12, 2011   Diff
Update licence header
Go to: 
Project members, sign in to write a code review

Older revisions

r2 by joshefin on Jun 11, 2011   Diff
First commit
All revisions of this file

File info

Size: 7030 bytes, 250 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting