components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/AnimationPanel.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing;
       
    27 
       
    28 import java.awt.CardLayout;
       
    29 import java.awt.image.BufferedImage;
       
    30 import java.util.*;
       
    31 import javax.swing.*;
       
    32 import javax.swing.event.*;
       
    33 
       
    34 public class AnimationPanel extends JPanel implements ChangeListener {
       
    35     //
       
    36     // Static data
       
    37     //
       
    38 
       
    39     private static final String CARD_COMPONENT = "comp";
       
    40 
       
    41     //
       
    42     // Instance data
       
    43     //
       
    44 
       
    45     private JComponent comp;
       
    46     private ComponentAnimation current;
       
    47     private Map<String, ComponentAnimation> map =
       
    48 	new HashMap<String, ComponentAnimation>();
       
    49     private boolean running;
       
    50 
       
    51     //
       
    52     // Constructors
       
    53     //
       
    54 
       
    55     public AnimationPanel(JComponent comp) {
       
    56 	this.comp = comp;
       
    57 	setLayout(new CardLayout());
       
    58 	add(comp, CARD_COMPONENT);
       
    59     }
       
    60 
       
    61     //
       
    62     // ChangeListener methods
       
    63     //
       
    64 
       
    65     @Override
       
    66     public void stateChanged(ChangeEvent e) {
       
    67 	if (e.getSource() == current) {
       
    68 	    animationStopped();
       
    69 	}
       
    70     }
       
    71 
       
    72     //
       
    73     // AnimationPanel methods
       
    74     //
       
    75 
       
    76     public void addAnimation(ComponentAnimation anim) {
       
    77 	String id = anim.getId();
       
    78 	if (!map.containsKey(id)) {
       
    79 	    map.put(id, anim);
       
    80 	    add(anim.getComponent(), id);
       
    81 	    anim.addChangeListener(this);
       
    82 	}
       
    83     }
       
    84 
       
    85     public void init(String id) {
       
    86 	ComponentAnimation anim = map.get(id);
       
    87 	BufferedImage before = GraphicsUtil.paintToImage(comp);
       
    88 	anim.init(before);
       
    89 	((CardLayout)getLayout()).show(this, id);
       
    90 	current = anim;
       
    91 	running = true;
       
    92     }
       
    93 
       
    94     public boolean isRunning() {
       
    95 	return running;
       
    96     }
       
    97 
       
    98     public void removeAnimation(ComponentAnimation anim) {
       
    99 	String id = anim.getId();
       
   100 	if (map.containsKey(id)) {
       
   101 	    map.remove(id);
       
   102 	    remove(anim.getComponent());
       
   103 	    anim.removeChangeListener(this);
       
   104 	}
       
   105     }
       
   106 
       
   107     public void start() {
       
   108 	BufferedImage after = GraphicsUtil.paintToImage(comp);
       
   109 	current.start(after);
       
   110     }
       
   111 
       
   112     public void stop() {
       
   113 	if (isRunning()) {
       
   114 	    current.stop();
       
   115 	    animationStopped();
       
   116 	}
       
   117     }
       
   118 
       
   119     //
       
   120     // Private methods
       
   121     //
       
   122 
       
   123     private void animationStopped() {
       
   124 	((CardLayout)getLayout()).show(this, CARD_COMPONENT);
       
   125 	running = false;
       
   126     }
       
   127 }