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.Font;
035: import java.awt.event.ActionEvent;
036:
037: import javax.swing.*;
038:
039: import com.jgoodies.animation.Animation;
040: import com.jgoodies.animation.AnimationAdapter;
041: import com.jgoodies.animation.AnimationEvent;
042: import com.jgoodies.animation.Animator;
043: import com.jgoodies.animation.animations.GlyphAnimation;
044: import com.jgoodies.animation.components.BasicTextLabel;
045: import com.jgoodies.animation.components.GlyphLabel;
046: import com.jgoodies.animation.tutorial.TutorialUtils;
047: import com.jgoodies.binding.adapter.BasicComponentFactory;
048: import com.jgoodies.binding.adapter.BoundedRangeAdapter;
049: import com.jgoodies.binding.beans.BeanAdapter;
050: import com.jgoodies.binding.beans.Model;
051: import com.jgoodies.binding.value.ConverterFactory;
052: import com.jgoodies.binding.value.ValueModel;
053: import com.jgoodies.forms.builder.ButtonBarBuilder;
054: import com.jgoodies.forms.builder.PanelBuilder;
055: import com.jgoodies.forms.layout.CellConstraints;
056: import com.jgoodies.forms.layout.FormLayout;
057:
058: /**
059: * Demonstrates the features of the {@link GlyphLabel}.
060: * Consists of a preview panel that displays a GlyphLabel,
061: * and a tool panel for configuration the label's properties
062: * and for running animations on that label.
063: *
064: * @author Karsten Lentzsch
065: * @version $Revision: 1.10 $
066: */
067:
068: public final class GlyphLabelExample extends Model {
069:
070: private static final String PROPERTYNAME_DURATION = "duration";
071:
072: private static final String INITIAL_TEXT = "GlyphLabel";
073:
074: private GlyphLabel glyphLabel;
075:
076: private JComponent textField;
077: private JSlider timeSlider;
078:
079: private int duration;
080: private Action animateAction;
081:
082: // Self Starter ***********************************************************
083:
084: public static void main(String[] args) {
085: try {
086: UIManager
087: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
088: } catch (Exception e) {
089: // Likely PlasticXP is not in the class path; ignore.
090: }
091: JFrame frame = new JFrame();
092: frame.setTitle("Animation Tutorial :: Glyph Label");
093: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
094: JComponent panel = new GlyphLabelExample().buildPanel();
095: frame.getContentPane().add(panel);
096: frame.pack();
097: TutorialUtils.locateOnOpticalScreenCenter(frame);
098: frame.setVisible(true);
099: }
100:
101: // Instance Creation ******************************************************
102:
103: public GlyphLabelExample() {
104: duration = 4000;
105: initComponents();
106: }
107:
108: // Bound Properties *******************************************************
109:
110: public int getDuration() {
111: return duration;
112: }
113:
114: public void setDuration(int newDuration) {
115: int oldDuration = getDuration();
116: duration = newDuration;
117: firePropertyChange(PROPERTYNAME_DURATION, oldDuration,
118: newDuration);
119: }
120:
121: // Component Creation and Initialization **********************************
122:
123: /**
124: * Creates the text label, sliders and animation Actions.
125: * Binds sliders to bound properties of the text label.
126: */
127: private void initComponents() {
128: // Setup a GlyphLabel with dark gray, bold Tahoma 24 pt.
129: glyphLabel = new GlyphLabel(INITIAL_TEXT, 4000, 150);
130: glyphLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
131: //glyphLabel.setColor(Color.DARK_GRAY);
132:
133: // Create a bean adapter that vends property adapters.
134: BeanAdapter beanAdapter = new BeanAdapter(glyphLabel, true);
135:
136: // Create a slider for the label's time in interval [0, 2000]
137: timeSlider = new JSlider();
138: ValueModel timeAdapter = beanAdapter
139: .getValueModel(GlyphLabel.PROPERTYNAME_TIME);
140: timeSlider
141: .setModel(new BoundedRangeAdapter(ConverterFactory
142: .createLongToIntegerConverter(timeAdapter), 0,
143: 0, 2000));
144:
145: textField = BasicComponentFactory
146: .createTextField(
147: beanAdapter
148: .getValueModel(BasicTextLabel.PROPERTYNAME_TEXT),
149: false);
150: // Create a text field bound to the duration.
151: //durationField = BasicComponentFactory.createIntegerField(
152: // new PropertyAdapter(this, PROPERTYNAME_DURATION));
153:
154: // Create an Action to animate the GlyphLabel
155: animateAction = new AnimateAction();
156: }
157:
158: // Building *************************************************************
159:
160: /**
161: * Builds and returns a panel with the preview in the top
162: * and the tool panel in the bottom.
163: *
164: * @return the built panel
165: */
166: private JComponent buildPanel() {
167: FormLayout layout = new FormLayout("fill:pref:grow",
168: "fill:pref:grow, p, p");
169:
170: PanelBuilder builder = new PanelBuilder(layout);
171: CellConstraints cc = new CellConstraints();
172: builder.add(buildPreviewPanel(), cc.xy(1, 1));
173: builder.addSeparator("", cc.xy(1, 2));
174: builder.add(buildToolsPanel(), cc.xy(1, 3));
175: return builder.getPanel();
176: }
177:
178: public JComponent buildPreviewPanel() {
179: FormLayout layout = new FormLayout("fill:200dlu:grow",
180: "fill:100dlu:grow");
181: JPanel panel = new JPanel(layout);
182: panel.setBackground(Color.WHITE);
183: panel.add(glyphLabel, 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, "Time:", timeSlider, "0", "4000");
211: return builder.getPanel();
212: }
213:
214: private JComponent buildAnimationsPanel() {
215: FormLayout layout = new FormLayout(
216: "right:pref, 3dlu, 40dlu, 50dlu:grow",
217: "p, 6dlu, p, 9dlu, p:grow");
218:
219: PanelBuilder builder = new PanelBuilder(layout);
220: CellConstraints cc = new CellConstraints();
221: builder.addSeparator("Animations", cc.xyw(1, 1, 4));
222: //builder.addLabel("Duration:", cc.xy (1, 3));
223: //builder.add(durationField, cc.xy (3, 3));
224: builder.add(buildButtonBar(), cc.xyw(1, 3, 4, "fill, bottom"));
225: return builder.getPanel();
226: }
227:
228: private JComponent buildButtonBar() {
229: ButtonBarBuilder builder = new ButtonBarBuilder();
230: builder.addGridded(new JButton(animateAction));
231: return builder.getPanel();
232: }
233:
234: private void addSlider(PanelBuilder builder, int row, String title,
235: JSlider slider, String leftText, String rightText) {
236: CellConstraints cc = new CellConstraints();
237: builder.addLabel(title, cc.xy(1, row));
238: builder.addLabel(leftText, cc.xy(3, row));
239: builder.add(slider, cc.xy(5, row));
240: builder.addLabel(rightText, cc.xy(7, row));
241: }
242:
243: // Animation Action *******************************************************
244:
245: private final class AnimateAction extends AbstractAction {
246:
247: private AnimateAction() {
248: super ("Animate");
249: }
250:
251: public void actionPerformed(ActionEvent e) {
252: Animation animation = createAnimation();
253: int fps = 30;
254: animation.addAnimationListener(new StartStopHandler());
255: new Animator(animation, fps).start();
256: }
257:
258: private Animation createAnimation() {
259: return new GlyphAnimation(glyphLabel, getDuration(), 150, // Ignored
260: glyphLabel.getText());
261: }
262:
263: }
264:
265: /**
266: * Disables the actions at animation start and enables them
267: * when the animation stopped. Also restores the text label's text.
268: */
269: private class StartStopHandler extends AnimationAdapter {
270:
271: private String text;
272:
273: public void animationStarted(AnimationEvent e) {
274: animateAction.setEnabled(false);
275: text = glyphLabel.getText();
276: }
277:
278: public void animationStopped(AnimationEvent e) {
279: animateAction.setEnabled(true);
280: glyphLabel.setText(text);
281: }
282: }
283:
284: }
|