001: package org.zilonis.tool.ext.aerith.ui;
002:
003: import java.awt.AlphaComposite;
004: import java.awt.BorderLayout;
005: import java.awt.Color;
006: import java.awt.Composite;
007: import java.awt.Font;
008: import java.awt.Graphics;
009: import java.awt.Graphics2D;
010: import java.awt.Point;
011: import java.awt.Rectangle;
012: import java.awt.RenderingHints;
013: import java.awt.font.FontRenderContext;
014: import java.awt.font.TextLayout;
015: import java.awt.geom.Rectangle2D;
016: import java.awt.image.BufferedImage;
017:
018: import javax.swing.JComponent;
019: import javax.swing.JPanel;
020: import javax.swing.SwingUtilities;
021:
022: import org.jdesktop.animation.timing.Animator;
023: import org.jdesktop.animation.timing.TimingTarget;
024: import org.jdesktop.animation.timing.Animator.RepeatBehavior;
025: import org.jdesktop.animation.timing.interpolation.KeyValues;
026: import org.jdesktop.animation.timing.interpolation.PropertySetter;
027: import org.jdesktop.fuse.InjectedResource;
028: import org.jdesktop.fuse.ResourceInjector;
029: import org.zilonis.tool.ext.aerith.util.Bundles;
030: import org.zilonis.tool.ui.TransitionManager;
031:
032: public final class WaitOverlay extends JPanel {
033: private String text;
034: private int iconHeight;
035: private int iconWidth;
036: private double loadingRotation;
037: private Animator loadingTimer;
038: private Animator glowTimer;
039: private JComponent clip;
040: private float fade = 0.0f;
041: private float opacity = 1.0f;
042: private float glowFactor = 0.0f;
043:
044: ////////////////////////////////////////////////////////////////////////////
045: // THEME SPECIFIC FIELDS
046: ////////////////////////////////////////////////////////////////////////////
047: @InjectedResource
048: private BufferedImage waitCircle;
049: @InjectedResource
050: private BufferedImage javaCup;
051: @InjectedResource
052: private BufferedImage javaCupGlow;
053: @InjectedResource
054: private Color color;
055: @InjectedResource
056: private Font messageFont;
057: @InjectedResource
058: private Color messageColor;
059:
060: public WaitOverlay(JComponent clip) {
061: ResourceInjector.get().inject(this );
062:
063: setLayout(new BorderLayout());
064: setOpaque(false);
065:
066: this .clip = clip;
067: this .text = Bundles.getMessage(getClass(), "TXT_PleaseWait");
068: this .iconWidth = waitCircle.getWidth();
069: this .iconHeight = waitCircle.getHeight();
070: }
071:
072: @Override
073: public void setVisible(boolean visible) {
074: if (visible) {
075: super .setVisible(visible);
076:
077: loadingTimer = new Animator(1500, 4f, RepeatBehavior.LOOP,
078: new LoadingMessenger());
079: loadingTimer.start();
080:
081: glowTimer = new Animator(800, 0f, RepeatBehavior.REVERSE,
082: new TimingTarget() {
083: public void begin() {
084: }
085:
086: public void end() {
087: }
088:
089: public void repeat() {
090:
091: }
092:
093: public void timingEvent(float f) {
094: glowFactor = f;
095: repaint();
096: }
097: });
098: glowTimer.start();
099:
100: startFadeIn();
101: } else if (loadingTimer != null) {
102: TransitionManager.showTransitionPanel();
103: startFadeOut();
104: }
105: }
106:
107: private void startFadeIn() {
108: Animator timer = PropertySetter.createAnimator(2000, this ,
109: "fade", KeyValues.create(new float[] { 0f, 1f }));
110: timer.setAcceleration(0.7f);
111: timer.setDeceleration(0.3f);
112: timer.start();
113: }
114:
115: private void startFadeOut() {
116: Animator timer = PropertySetter.createAnimator(2000, this ,
117: "fade", KeyValues.create(new float[] { 1f, 0f }));
118: timer.setAcceleration(0.7f);
119: timer.setDeceleration(0.3f);
120: timer.start();
121: timer = PropertySetter.createAnimator(2000, this , "opacity",
122: KeyValues.create(new float[] { 1f, 0f }));
123: timer.setAcceleration(0.7f);
124: timer.setDeceleration(0.3f);
125: timer.addTarget(new TimingTarget() {
126: public void end() {
127: WaitOverlay.super .setVisible(false);
128: if (loadingTimer != null) {
129: loadingTimer.stop();
130: loadingTimer = null;
131: }
132: if (glowTimer != null) {
133: glowTimer.stop();
134: glowTimer = null;
135: }
136: TransitionManager.killOverlay();
137: }
138:
139: public void begin() {
140: }
141:
142: public void timingEvent(float arg2) {
143: }
144:
145: public void repeat() {
146:
147: }
148: });
149: timer.start();
150: }
151:
152: public void setFade(float fade) {
153: this .fade = fade;
154: repaint();
155: }
156:
157: public float getFade() {
158: return fade;
159: }
160:
161: public void setOpacity(float opacity) {
162: this .opacity = opacity;
163: repaint();
164: }
165:
166: public float getOpacity() {
167: return opacity;
168: }
169:
170: @Override
171: protected void paintComponent(Graphics g) {
172: Graphics2D g2 = (Graphics2D) g.create();
173: setupGraphics(g2);
174:
175: paintBackground(g2);
176:
177: Composite composite = g2.getComposite();
178: g2.setComposite(AlphaComposite.getInstance(
179: AlphaComposite.SRC_OVER, fade));
180:
181: int y = (getHeight() - javaCup.getHeight()) / 3
182: - waitCircle.getHeight();
183:
184: Composite composite2 = g2.getComposite();
185: g2.setComposite(AlphaComposite.getInstance(
186: AlphaComposite.SRC_OVER, (float) (fade * glowFactor)));
187: g2.drawImage(javaCupGlow,
188: (getWidth() - javaCup.getWidth()) / 2, y
189: + javaCup.getHeight() / 3, null);
190: g2.setComposite(composite2);
191: g2.drawImage(javaCup, (getWidth() - javaCup.getWidth()) / 2, y
192: + javaCup.getHeight() / 3, null);
193: float x = paintText(g2, y);
194: paintIcon(g2, x, y);
195:
196: g2.setComposite(composite);
197: }
198:
199: private void paintIcon(Graphics2D g2, float x, int y) {
200: Rectangle rect = getClipBounds();
201: Graphics2D g2d = (Graphics2D) g2.create();
202: g2d.rotate(loadingRotation * 2.0 * Math.PI,
203: x + iconWidth / 2.0, y + rect.getY() + rect.getHeight()
204: / 2.0);
205: g2d
206: .drawImage(
207: waitCircle,
208: (int) x,
209: y
210: + (int) (rect.getY() + (rect
211: .getHeight() - iconHeight) / 2.0),
212: null);
213: }
214:
215: private Rectangle getClipBounds() {
216: Point point = clip.getLocation();
217: point = SwingUtilities.convertPoint(clip, point, this );
218: return new Rectangle(point.x, point.y, clip.getWidth(), clip
219: .getHeight());
220: }
221:
222: private float paintText(Graphics2D g2, int y) {
223: g2.setFont(messageFont);
224: FontRenderContext context = g2.getFontRenderContext();
225: TextLayout layout = new TextLayout(text, messageFont, context);
226: Rectangle2D bounds = layout.getBounds();
227:
228: Rectangle rect = getClipBounds();
229: float x = rect.x
230: + (rect.width - waitCircle.getWidth(this ) - (float) bounds
231: .getWidth()) / 2.0f;
232:
233: g2.setColor(messageColor);
234: layout.draw(g2, x + iconWidth + 10, y
235: + layout.getAscent()
236: + rect.y
237: + (rect.height - layout.getAscent() - layout
238: .getDescent()) / 2.0f);
239:
240: return x;
241: }
242:
243: private void paintBackground(Graphics2D g2) {
244: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
245: RenderingHints.VALUE_ANTIALIAS_OFF);
246: Composite composite = g2.getComposite();
247: g2.setComposite(AlphaComposite.getInstance(
248: AlphaComposite.SRC_OVER, opacity));
249: g2.setColor(color);
250: Rectangle rect = g2.getClipBounds().intersection(
251: getClipBounds());
252: g2.fillRect(rect.x, rect.y, rect.width, rect.height);
253: g2.setComposite(composite);
254: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
255: RenderingHints.VALUE_ANTIALIAS_ON);
256: }
257:
258: private static void setupGraphics(Graphics2D g2) {
259: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
260: RenderingHints.VALUE_ANTIALIAS_ON);
261: g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
262: RenderingHints.VALUE_INTERPOLATION_BILINEAR);
263: }
264:
265: private class LoadingMessenger implements TimingTarget {
266: public void timingEvent(float fraction) {
267: loadingRotation = fraction - fraction % (1.0 / 12.0);
268:
269: // if (x < 0.0f) {
270: // repaint();
271: // } else {
272: // Rectangle rect = clip.getBounds();
273: // repaint((int) x - 2, rect.y - 2 + (rect.height - iconHeight) / 2,
274: // iconWidth + 4, iconHeight + 4);
275: // }
276: }
277:
278: public void repeat() {
279:
280: }
281:
282: public void begin() {
283: }
284:
285: public void end() {
286: }
287: }
288: }
|