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 VerticalBarsPanel}.
056: * Consists of a preview panel that displays a BasicTextLabel
057: * as middle component of a VerticalBarsPanel.
058: *
059: * @author Karsten Lentzsch
060: * @version $Revision: 1.12 $
061: */
062: public final class VerticalBarsExample extends Model {
063:
064: private static final Color JAVAONE_BLUE = new Color(46, 49, 146);
065: private static final String PROPERTYNAME_DURATION = "duration";
066:
067: private static final String INITIAL_TEXT = "VerticalBarsPanel";
068:
069: private VerticalBarsPanel verticalBarsPanel;
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 :: Vertical Bars");
090: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
091: JComponent panel = new VerticalBarsExample().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 VerticalBarsExample() {
101: duration = 200;
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 blue, bold Tahoma 24 pt.
126: textLabel = new BasicTextLabel(INITIAL_TEXT);
127: textLabel.setFont(new Font("Tahoma", Font.BOLD, 24));
128: textLabel.setColor(JAVAONE_BLUE);
129:
130: verticalBarsPanel = new VerticalBarsPanel(textLabel);
131: verticalBarsPanel.setBackground(Color.WHITE);
132: verticalBarsPanel.setFraction(0.25);
133:
134: // Create a bean adapter that vends property adapters.
135: BeanAdapter beanAdapter = new BeanAdapter(verticalBarsPanel,
136: true);
137:
138: // Create a slider for the panel's fraction in interval [0.0d, 1.0d]
139: fractionSlider = new JSlider();
140: ValueModel fractionAdapter = beanAdapter
141: .getValueModel(HorizontalBarsPanel.PROPERTYNAME_FRACTION);
142: fractionSlider.setModel(new BoundedRangeAdapter(
143: ConverterFactory.createDoubleToIntegerConverter(
144: fractionAdapter, 100), 0, 0, 100));
145:
146: textField = BasicComponentFactory.createTextField(
147: new PropertyAdapter(textLabel,
148: BasicTextLabel.PROPERTYNAME_TEXT, true), true);
149: // Create a text field bound to the duration.
150: durationField = BasicComponentFactory
151: .createIntegerField(new PropertyAdapter(this ,
152: PROPERTYNAME_DURATION));
153:
154: // Create an Action to animate the bars
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:
176: return builder.getPanel();
177: }
178:
179: public JComponent buildPreviewPanel() {
180: FormLayout layout = new FormLayout("fill:200dlu:grow",
181: "50dlu:grow, fill:50dlu, 50dlu:grow");
182: JPanel panel = new JPanel(layout);
183: panel.setBackground(Color.WHITE);
184: panel.add(verticalBarsPanel, new CellConstraints(1, 2));
185: return panel;
186: }
187:
188: public JComponent buildToolsPanel() {
189: FormLayout layout = new FormLayout("pref, 25dlu, pref",
190: "fill:pref");
191:
192: PanelBuilder builder = new PanelBuilder(layout);
193: builder.setDefaultDialogBorder();
194: CellConstraints cc = new CellConstraints();
195: builder.add(buildPropertiesPanel(), cc.xy(1, 1));
196: builder.add(buildAnimationsPanel(), cc.xy(3, 1));
197: return builder.getPanel();
198: }
199:
200: private JComponent buildPropertiesPanel() {
201: FormLayout layout = new FormLayout(
202: "right:pref, 3dlu, right:pref, 2dlu, 60dlu, 2dlu, right:pref",
203: "p, 6dlu, p, 6dlu, p, 6dlu, p, 6dlu, p");
204:
205: PanelBuilder builder = new PanelBuilder(layout);
206: CellConstraints cc = new CellConstraints();
207: builder.addSeparator("Properties", cc.xyw(1, 1, 7));
208: builder.addLabel("Text:", cc.xy(1, 3));
209: builder.add(textField, cc.xyw(3, 3, 5));
210:
211: addSlider(builder, 5, "Fraction:", fractionSlider, "0", "100");
212:
213: return builder.getPanel();
214: }
215:
216: private JComponent buildAnimationsPanel() {
217: FormLayout layout = new FormLayout(
218: "right:pref, 3dlu, 40dlu, 50dlu:grow",
219: "p, 6dlu, p, 6dlu, p");
220:
221: PanelBuilder builder = new PanelBuilder(layout);
222: CellConstraints cc = new CellConstraints();
223: builder.addSeparator("Animation", cc.xyw(1, 1, 4));
224: builder.addLabel("Duration:", cc.xy(1, 3));
225: builder.add(durationField, cc.xy(3, 3));
226: builder.add(buildButtonBar(), cc.xyw(1, 5, 4, "fill, bottom"));
227: return builder.getPanel();
228: }
229:
230: private JComponent buildButtonBar() {
231: ButtonBarBuilder builder = new ButtonBarBuilder();
232: builder.addGridded(new JButton(animateAction));
233: return builder.getPanel();
234: }
235:
236: private void addSlider(PanelBuilder builder, int row, String title,
237: JSlider slider, String leftText, String rightText) {
238: CellConstraints cc = new CellConstraints();
239: builder.addLabel(title, cc.xy(1, row));
240: builder.addLabel(leftText, cc.xy(3, row));
241: builder.add(slider, cc.xy(5, row));
242: builder.addLabel(rightText, cc.xy(7, row));
243: }
244:
245: // Animation **************************************************************
246:
247: private static final class MoveBarsAnimation extends
248: AbstractAnimation {
249:
250: private final VerticalBarsPanel panel;
251: private final AnimationFunction fractionFunction;
252:
253: private MoveBarsAnimation(VerticalBarsPanel verticalBarsPanel,
254: long duration) {
255: super (duration);
256: panel = verticalBarsPanel;
257: fractionFunction = AnimationFunctions.linear(duration,
258: new Float[] { new Float(0.25f), new Float(0.75d),
259: new Float(0.25f) }, new float[] { 0.0f,
260: 0.5f, 1.0f });
261: }
262:
263: public void applyEffect(long time) {
264: Float fraction = (Float) fractionFunction.valueAt(time);
265: panel.setFraction(fraction.doubleValue());
266: }
267:
268: }
269:
270: // Animation Action *******************************************************
271:
272: private final class AnimateAction extends AbstractAction {
273:
274: private AnimateAction() {
275: super ("Animate");
276: }
277:
278: public void actionPerformed(ActionEvent e) {
279: Animation animation = createAnimation();
280: int fps = 30;
281: animation.addAnimationListener(new StartStopHandler());
282: new Animator(animation, fps).start();
283: }
284:
285: }
286:
287: private Animation createAnimation() {
288: return new MoveBarsAnimation(verticalBarsPanel, getDuration());
289: }
290:
291: /**
292: * Disables the actions at animation start and enables them
293: * when the animation stopped. Also restores the text label's text.
294: */
295: private final class StartStopHandler extends AnimationAdapter {
296:
297: public void animationStarted(AnimationEvent e) {
298: animateAction.setEnabled(false);
299: }
300:
301: public void animationStopped(AnimationEvent e) {
302: animateAction.setEnabled(true);
303: }
304: }
305:
306: }
|