001: /*
002: * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.animation.tutorial.panel;
032:
033: import java.awt.Color;
034: import java.awt.Font;
035: import java.awt.event.ActionEvent;
036:
037: import javax.swing.*;
038:
039: import com.jgoodies.animation.*;
040: import com.jgoodies.animation.components.BasicTextLabel;
041: import com.jgoodies.animation.tutorial.TutorialUtils;
042: import com.jgoodies.binding.adapter.BasicComponentFactory;
043: import com.jgoodies.binding.adapter.BoundedRangeAdapter;
044: import com.jgoodies.binding.beans.BeanAdapter;
045: import com.jgoodies.binding.beans.Model;
046: import com.jgoodies.binding.beans.PropertyAdapter;
047: import com.jgoodies.binding.value.ConverterFactory;
048: import com.jgoodies.binding.value.ValueModel;
049: import com.jgoodies.forms.builder.ButtonBarBuilder;
050: import com.jgoodies.forms.builder.PanelBuilder;
051: import com.jgoodies.forms.layout.CellConstraints;
052: import com.jgoodies.forms.layout.FormLayout;
053:
054: /**
055: * Demonstrates the features of the {@link HorizontalBarsPanel}.
056: * Consists of a preview panel that displays a BasicTextLabel
057: * as center component of a HorizontalBarPanel.
058: *
059: * @author Karsten Lentzsch
060: * @version $Revision: 1.12 $
061: */
062: public final class HorizontalBarsExample extends Model {
063:
064: private static final Color JAVAONE_RED = new Color(209, 33, 36);
065: private static final String PROPERTYNAME_DURATION = "duration";
066:
067: private static final String INITIAL_TEXT = "HorizontalBarsPanel";
068:
069: private HorizontalBarsPanel horizontalBarsPanel;
070: private BasicTextLabel textLabel;
071:
072: private JComponent textField;
073: private JSlider fractionSlider;
074:
075: private int duration;
076: private JComponent durationField;
077: private Action animateAction;
078:
079: // Self Starter ***********************************************************
080:
081: public static void main(String[] args) {
082: try {
083: UIManager
084: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
085: } catch (Exception e) {
086: // Likely PlasticXP is not in the class path; ignore.
087: }
088: JFrame frame = new JFrame();
089: frame.setTitle("Animation Tutorial :: Horizontal Bars");
090: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
091: JComponent panel = new HorizontalBarsExample().buildPanel();
092: frame.getContentPane().add(panel);
093: frame.pack();
094: TutorialUtils.locateOnOpticalScreenCenter(frame);
095: frame.setVisible(true);
096: }
097:
098: // Instance Creation ******************************************************
099:
100: public HorizontalBarsExample() {
101: duration = 500;
102: initComponents();
103: }
104:
105: // Bound Properties *******************************************************
106:
107: public int getDuration() {
108: return duration;
109: }
110:
111: public void setDuration(int newDuration) {
112: int oldDuration = getDuration();
113: duration = newDuration;
114: firePropertyChange(PROPERTYNAME_DURATION, oldDuration,
115: newDuration);
116: }
117:
118: // Component Creation and Initialization **********************************
119:
120: /**
121: * Creates the text label, sliders and animation Actions.
122: * Binds sliders to bound properties of the text label.
123: */
124: private void initComponents() {
125: // Setup a BasicTextLabel with read, bold Tahoma 24 pt.
126: textLabel = new BasicTextLabel(INITIAL_TEXT);
127: textLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
128: textLabel.setColor(JAVAONE_RED);
129:
130: horizontalBarsPanel = new HorizontalBarsPanel(textLabel);
131: horizontalBarsPanel.setBackground(Color.WHITE);
132:
133: // Create a bean adapter that vends property adapters.
134: BeanAdapter beanAdapter = new BeanAdapter(horizontalBarsPanel,
135: true);
136:
137: // Create a slider for the panel's fraction in interval [0.0d, 1.0d]
138: fractionSlider = new JSlider();
139: ValueModel fractionAdapter = beanAdapter
140: .getValueModel(HorizontalBarsPanel.PROPERTYNAME_FRACTION);
141: fractionSlider.setModel(new BoundedRangeAdapter(
142: ConverterFactory.createDoubleToIntegerConverter(
143: fractionAdapter, 100), 0, 0, 100));
144:
145: textField = BasicComponentFactory.createTextField(
146: new PropertyAdapter(textLabel,
147: BasicTextLabel.PROPERTYNAME_TEXT, true), true);
148: // Create a text field bound to the duration.
149: durationField = BasicComponentFactory
150: .createIntegerField(new PropertyAdapter(this ,
151: PROPERTYNAME_DURATION));
152:
153: // Create an Action to animate the bars
154: animateAction = new AnimateAction();
155: }
156:
157: // Building *************************************************************
158:
159: /**
160: * Builds and returns a panel with the preview in the top
161: * and the tool panel in the bottom.
162: *
163: * @return the built panel
164: */
165: private JComponent buildPanel() {
166: FormLayout layout = new FormLayout("fill:pref:grow",
167: "fill:pref:grow, p, p");
168:
169: PanelBuilder builder = new PanelBuilder(layout);
170: CellConstraints cc = new CellConstraints();
171: builder.add(buildPreviewPanel(), cc.xy(1, 1));
172: builder.addSeparator("", cc.xy(1, 2));
173: builder.add(buildToolsPanel(), cc.xy(1, 3));
174:
175: return builder.getPanel();
176: }
177:
178: public JComponent buildPreviewPanel() {
179: FormLayout layout = new FormLayout("fill:200dlu:grow",
180: "fill:150dlu:grow");
181: JPanel panel = new JPanel(layout);
182: panel.setBackground(Color.WHITE);
183: panel.add(horizontalBarsPanel, new CellConstraints());
184: return panel;
185: }
186:
187: public JComponent buildToolsPanel() {
188: FormLayout layout = new FormLayout("pref, 25dlu, pref",
189: "fill:pref");
190:
191: PanelBuilder builder = new PanelBuilder(layout);
192: builder.setDefaultDialogBorder();
193: CellConstraints cc = new CellConstraints();
194: builder.add(buildPropertiesPanel(), cc.xy(1, 1));
195: builder.add(buildAnimationsPanel(), cc.xy(3, 1));
196: return builder.getPanel();
197: }
198:
199: private JComponent buildPropertiesPanel() {
200: FormLayout layout = new FormLayout(
201: "right:pref, 3dlu, right:pref, 2dlu, 60dlu, 2dlu, right:pref",
202: "p, 6dlu, p, 6dlu, p, 6dlu, p, 6dlu, p");
203:
204: PanelBuilder builder = new PanelBuilder(layout);
205: CellConstraints cc = new CellConstraints();
206: builder.addSeparator("Properties", cc.xyw(1, 1, 7));
207: builder.addLabel("Text:", cc.xy(1, 3));
208: builder.add(textField, cc.xyw(3, 3, 5));
209:
210: addSlider(builder, 5, "Fraction:", fractionSlider, "0", "100");
211:
212: return builder.getPanel();
213: }
214:
215: private JComponent buildAnimationsPanel() {
216: FormLayout layout = new FormLayout(
217: "right:pref, 3dlu, 40dlu, 50dlu:grow",
218: "p, 6dlu, p, 6dlu, p");
219:
220: PanelBuilder builder = new PanelBuilder(layout);
221: CellConstraints cc = new CellConstraints();
222: builder.addSeparator("Animation", cc.xyw(1, 1, 4));
223: builder.addLabel("Duration:", cc.xy(1, 3));
224: builder.add(durationField, cc.xy(3, 3));
225: builder.add(buildButtonBar(), cc.xyw(1, 5, 4, "fill, bottom"));
226: return builder.getPanel();
227: }
228:
229: private JComponent buildButtonBar() {
230: ButtonBarBuilder builder = new ButtonBarBuilder();
231: builder.addGridded(new JButton(animateAction));
232: return builder.getPanel();
233: }
234:
235: private void addSlider(PanelBuilder builder, int row, String title,
236: JSlider slider, String leftText, String rightText) {
237: CellConstraints cc = new CellConstraints();
238: builder.addLabel(title, cc.xy(1, row));
239: builder.addLabel(leftText, cc.xy(3, row));
240: builder.add(slider, cc.xy(5, row));
241: builder.addLabel(rightText, cc.xy(7, row));
242: }
243:
244: // Animation **************************************************************
245:
246: private static final class MoveBarsAnimation extends
247: AbstractAnimation {
248:
249: private final HorizontalBarsPanel panel;
250: private final AnimationFunction fractionFunction;
251:
252: private MoveBarsAnimation(
253: HorizontalBarsPanel horizontalBarsPanel, long duration) {
254: super (duration);
255: panel = horizontalBarsPanel;
256: fractionFunction = AnimationFunctions.linear(duration,
257: new Float[] { new Float(0.45f), new Float(1.0d),
258: new Float(0.45f) }, new float[] { 0.0f,
259: 0.4f, 1.0f });
260: }
261:
262: public void applyEffect(long time) {
263: Float fraction = (Float) fractionFunction.valueAt(time);
264: panel.setFraction(fraction.doubleValue());
265: }
266:
267: }
268:
269: // Animation Action *******************************************************
270:
271: private final class AnimateAction extends AbstractAction {
272:
273: private AnimateAction() {
274: super ("Animate");
275: }
276:
277: public void actionPerformed(ActionEvent e) {
278: Animation animation = createAnimation();
279: int fps = 30;
280: animation.addAnimationListener(new StartStopHandler());
281: new Animator(animation, fps).start();
282: }
283:
284: }
285:
286: private Animation createAnimation() {
287: return new MoveBarsAnimation(horizontalBarsPanel, getDuration());
288: }
289:
290: /**
291: * Disables the actions at animation start and enables them
292: * when the animation stopped. Also restores the text label's text.
293: */
294: private final class StartStopHandler extends AnimationAdapter {
295:
296: public void animationStarted(AnimationEvent e) {
297: animateAction.setEnabled(false);
298: }
299:
300: public void animationStopped(AnimationEvent e) {
301: animateAction.setEnabled(true);
302: }
303: }
304:
305: }
|