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.component;
032:
033: import java.awt.Color;
034: import java.awt.event.ActionEvent;
035:
036: import javax.swing.*;
037:
038: import com.jgoodies.animation.components.AnimatedLabel;
039: import com.jgoodies.animation.tutorial.TutorialUtils;
040: import com.jgoodies.binding.adapter.BasicComponentFactory;
041: import com.jgoodies.binding.beans.BeanAdapter;
042: import com.jgoodies.binding.beans.Model;
043: import com.jgoodies.forms.builder.ButtonBarBuilder;
044: import com.jgoodies.forms.builder.PanelBuilder;
045: import com.jgoodies.forms.layout.CellConstraints;
046: import com.jgoodies.forms.layout.FormLayout;
047:
048: /**
049: * Demonstrates the features of the {@link AnimatedLabel}.
050: * Consists of a preview panel that displays an AnimatedLabel and a
051: * tools panel to edit its duration, animated state and to change the text.
052: *
053: * @author Karsten Lentzsch
054: * @version $Revision: 1.7 $
055: */
056:
057: public final class AnimatedLabelExample extends Model {
058:
059: private static final Color AMAZON_ORANGE = new Color(255, 142, 12);
060:
061: private static final String[] TEXTS = { "The AnimatedLabel",
062: "Uses an Animation", "That Blends Over", "Texts.",
063: "YOU CAN CONFIGURE", "THE DURATION", "AND ANIMATED STATE" };
064:
065: /**
066: * The current index in the above array of sample texts.
067: */
068: private int textIndex;
069:
070: /**
071: * Refers to the animated label that is demonstrated in this example.
072: */
073: private AnimatedLabel animatedLabel;
074:
075: private JTextField durationField;
076: private JComponent animatedBox;
077: private Action nextTextAction;
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 :: AnimatedLabel");
090: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
091: JComponent panel = new AnimatedLabelExample().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 AnimatedLabelExample() {
101: initComponents();
102: }
103:
104: // Component Creation and Initialization **********************************
105:
106: /**
107: * Creates the animated label, duration text field, a check box
108: * for the animated state and an Action to set the new text.
109: */
110: private void initComponents() {
111: textIndex = 0;
112: // Setup an AnimatedLabel with an Amazon.com organe and a font
113: // 16 pt larger than the dialog font.
114: animatedLabel = new AnimatedLabel(AMAZON_ORANGE, 16,
115: TEXTS[textIndex]);
116:
117: // Create a bean adapter that vends property adapters.
118: BeanAdapter beanAdapter = new BeanAdapter(animatedLabel, true);
119:
120: // Create a text field bound to the label's blend over duration.
121: durationField = BasicComponentFactory
122: .createIntegerField(beanAdapter
123: .getValueModel(AnimatedLabel.PROPERTYNAME_DURATION));
124: durationField.setColumns(5);
125: animatedBox = BasicComponentFactory.createCheckBox(beanAdapter
126: .getValueModel(AnimatedLabel.PROPERTYNAME_ANIMATED),
127: "animated");
128:
129: // Create an Action to set the next text.
130: nextTextAction = new AnimateAction();
131: }
132:
133: // Building *************************************************************
134:
135: /**
136: * Builds and returns a panel with the preview in the top
137: * and the tool panel in the bottom.
138: *
139: * @return the built panel
140: */
141: private JComponent buildPanel() {
142: FormLayout layout = new FormLayout("fill:pref:grow",
143: "fill:p:grow, p, p");
144:
145: PanelBuilder builder = new PanelBuilder(layout);
146: CellConstraints cc = new CellConstraints();
147: builder.add(buildPreviewPanel(), cc.xy(1, 1));
148: builder.addSeparator("", cc.xy(1, 2));
149: builder.add(buildToolsPanel(), cc.xy(1, 3));
150:
151: return builder.getPanel();
152: }
153:
154: public JComponent buildPreviewPanel() {
155: FormLayout layout = new FormLayout(
156: "50dlu, left:200dlu:grow, 50dlu", "fill:100dlu:grow");
157: JPanel panel = new JPanel(layout);
158: panel.setBackground(Color.WHITE);
159: panel.add(animatedLabel, new CellConstraints(2, 1));
160: return panel;
161: }
162:
163: public JComponent buildToolsPanel() {
164: FormLayout layout = new FormLayout(
165: "p, 3dlu, p, 6dlu, p, 24dlu:grow, p", "p");
166:
167: PanelBuilder builder = new PanelBuilder(layout);
168: builder.setDefaultDialogBorder();
169: CellConstraints cc = new CellConstraints();
170: builder.addLabel("Duration:", cc.xy(1, 1));
171: builder.add(durationField, cc.xy(3, 1));
172: builder.add(animatedBox, cc.xy(5, 1));
173: builder.add(buildButtonBar(), cc.xy(7, 1));
174: return builder.getPanel();
175: }
176:
177: private JComponent buildButtonBar() {
178: ButtonBarBuilder builder = new ButtonBarBuilder();
179: builder.addGridded(new JButton(nextTextAction));
180: return builder.getPanel();
181: }
182:
183: // Animation Action *******************************************************
184:
185: private final class AnimateAction extends AbstractAction {
186:
187: private AnimateAction() {
188: super ("Next Text");
189: }
190:
191: public void actionPerformed(ActionEvent e) {
192: if (++textIndex == TEXTS.length)
193: textIndex = 0;
194: animatedLabel.setText(TEXTS[textIndex]);
195: }
196:
197: }
198:
199: }
|