001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.swing;
018:
019: import java.awt.Component;
020: import java.awt.Container;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.swing.Timer;
028:
029: /**
030: * Animates a PercentLayout
031: */
032: public class PercentLayoutAnimator implements ActionListener {
033:
034: private Timer animatorTimer;
035: private List tasks = new ArrayList();
036: private PercentLayout layout;
037: private Container container;
038:
039: public PercentLayoutAnimator(Container container,
040: PercentLayout layout) {
041: this .container = container;
042: this .layout = layout;
043: }
044:
045: public void setTargetPercent(Component component, float percent) {
046: PercentLayout.Constraint oldConstraint = layout
047: .getConstraint(component);
048: if (oldConstraint instanceof PercentLayout.PercentConstraint) {
049: setTargetPercent(component,
050: ((PercentLayout.PercentConstraint) oldConstraint)
051: .floatValue(), percent);
052: }
053: }
054:
055: public void setTargetPercent(Component component,
056: float startPercent, float endPercent) {
057: tasks.add(new PercentTask(component, startPercent, endPercent));
058: }
059:
060: public void start() {
061: animatorTimer = new Timer(15, this );
062: animatorTimer.start();
063: }
064:
065: public void stop() {
066: animatorTimer.stop();
067: }
068:
069: protected void complete() {
070: animatorTimer.stop();
071: }
072:
073: public void actionPerformed(ActionEvent e) {
074: boolean allCompleted = true;
075:
076: for (Iterator iter = tasks.iterator(); iter.hasNext();) {
077: PercentTask element = (PercentTask) iter.next();
078: if (!element.isCompleted()) {
079: allCompleted = false;
080: element.execute();
081: }
082: }
083:
084: container.invalidate();
085: container.doLayout();
086: container.repaint();
087:
088: if (allCompleted) {
089: complete();
090: }
091: }
092:
093: class PercentTask {
094:
095: Component component;
096:
097: float targetPercent;
098: float currentPercent;
099:
100: boolean completed;
101: boolean incrementing;
102: float delta;
103:
104: public PercentTask(Component component, float currentPercent,
105: float targetPercent) {
106: this .component = component;
107: this .currentPercent = currentPercent;
108: this .targetPercent = targetPercent;
109:
110: float diff = targetPercent - currentPercent;
111: incrementing = diff > 0;
112: delta = diff / 10;
113: }
114:
115: public void execute() {
116: currentPercent += delta;
117: if (incrementing) {
118: if (currentPercent > targetPercent) {
119: currentPercent = targetPercent;
120: completed = true;
121: }
122: } else {
123: if (currentPercent < targetPercent) {
124: currentPercent = targetPercent;
125: completed = true;
126: }
127: }
128:
129: layout
130: .setConstraint(component,
131: new PercentLayout.PercentConstraint(
132: currentPercent));
133: }
134:
135: public boolean isCompleted() {
136: return completed;
137: }
138: }
139:
140: }
|