Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/shell/models/ViewAnimationModel.java


1   package com.arranger.jarl.shell.models;
2   
3   import com.arranger.jarl.shell.ShellConfig;
4   import com.arranger.jarl.util.StringTools;
5   import com.arranger.jarl.util.ImageUtil;
6   
7   import javax.swing.*;
8   import java.awt.*;
9   import java.awt.image.BufferedImage;
10  import java.awt.event.ActionListener;
11  import java.awt.event.ActionEvent;
12  import java.io.File;
13  
14  /**
15   * ViewAnimationModel created on Apr 25, 2003
16   */
17  public class ViewAnimationModel extends BaseModel implements ActionListener {
18  
19      public static final String ANIMATION_DIR = "animationDir";
20      public static final String ANIMATION_FPS = "animationFPS";
21      public static final String CURRENT_IMAGE = "currentImage";
22      public static final String CURRENT_INDEX = "currentIndex";
23  
24      public static final String ANIMATION_INITIALIZING = "animationInitializing";
25      public static final String ANIMATION_STOPPED = "animationStopped";
26      public static final String ANIMATION_STARTED = "animationStarted";
27  
28      protected static final String DEFAULT_SERIES_PREFIX = "image";
29  
30      protected static final float DEFAULT_FPS = 10.0f;
31  
32      protected String m_animationDir;
33      protected float m_fps;
34  
35      protected transient int m_currentImageIndex;
36      protected transient Image m_currentImage;
37      protected transient Timer m_timer;
38      protected transient Dimension m_dimension;
39      protected transient BufferedImage m_initialImage;
40      protected transient boolean m_isBMP = true;
41  
42      public void load(ShellConfig shellConfig) throws Exception {
43          m_animationDir = shellConfig.getProperty(ANIMATION_DIR);
44          String fps = shellConfig.getProperty(ANIMATION_FPS);
45          if (StringTools.isEmpty(fps)) {
46              m_fps = DEFAULT_FPS;
47          } else {
48              m_fps = Float.parseFloat(fps);
49          }
50      }
51  
52      public void save(ShellConfig shellConfig) throws Exception {
53          shellConfig.setProperty(ANIMATION_DIR, m_animationDir);
54          shellConfig.setProperty(ANIMATION_FPS, String.valueOf(m_fps));
55      }
56  
57      public String getStatusText() {
58          StringBuffer buffer = new StringBuffer();
59          buffer.append(ANIMATION_DIR).append(": ").append(m_animationDir).append(LINE_SEP);
60          buffer.append(ANIMATION_FPS).append(": ").append(m_fps).append(LINE_SEP);
61          return buffer.toString();
62      }
63  
64      public String getAnimationDir() {
65          return m_animationDir;
66      }
67  
68      public void setAnimationDir(String animationDir) {
69          if (!animationDir.equals(m_animationDir)) {
70              m_animationDir = animationDir;
71              raiseEvent(ANIMATION_DIR);
72          }
73      }
74  
75      public float getFps() {
76          return m_fps;
77      }
78  
79      public void setFps(float fps) {
80          if (fps != m_fps) {
81              m_fps = fps;
82              raiseEvent(ANIMATION_FPS);
83          }
84      }
85  
86      public int getCurrentImageIndex() {
87          return m_currentImageIndex;
88      }
89  
90      public void setCurrentImageIndex(int currentImageIndex) {
91          if (currentImageIndex != m_currentImageIndex) {
92              m_currentImageIndex = currentImageIndex;
93              raiseEvent(CURRENT_INDEX);
94          }
95      }
96  
97      public Image getCurrentImage() {
98          return m_currentImage;
99      }
100 
101     public void setCurrentImage(Image currentImage) {
102         if (currentImage != m_currentImage) {   //pretty useless
103             m_currentImage = currentImage;
104             raiseEvent(CURRENT_IMAGE);
105         }
106     }
107 
108     public Image getInitialImage() {
109         return m_initialImage;
110     }
111 
112     public Dimension getDimension() {
113         return m_dimension;
114     }
115 
116     public synchronized void viewAnimation() {
117         m_currentImageIndex = 0;
118         m_currentImage = null;
119         startAnimation();
120     }
121 
122     public synchronized void startAnimation() {
123         if (m_timer == null) {
124             m_timer = new Timer((int) (1000 / m_fps), this);
125             m_timer.setInitialDelay(0);
126             m_timer.setCoalesce(true);
127         }
128 
129         initialize();
130         raiseEvent(ANIMATION_INITIALIZING);
131 
132         if (!m_timer.isRunning()) {
133             m_timer.start();
134             raiseEvent(ANIMATION_STARTED);
135         }
136     }
137 
138     public synchronized void stopAnimation() {
139         if (m_timer != null) {
140             raiseEvent(ANIMATION_STOPPED);
141             m_timer.stop();
142         }
143     }
144 
145     /**
146      * Invoked when an action occurs (from the timer).
147      */
148     public synchronized void actionPerformed(ActionEvent e) {
149         //get the current index (increment it)
150         //load and set the image
151         String imageFile = StringTools.appendPath(m_animationDir, DEFAULT_SERIES_PREFIX + m_currentImageIndex++ + ((m_isBMP) ? ".bmp" : ".jpg"));
152         File file = new File(imageFile);
153         if (!file.exists()) {
154             stopAnimation();
155             return;
156         }
157 
158         try {
159             Image image = ImageUtil.loadImage(imageFile);
160             setCurrentImageIndex(m_currentImageIndex);
161             setCurrentImage(image);
162         } catch (Exception e1) {
163             e1.printStackTrace();
164             stopAnimation();
165         }
166     }
167 
168     private void initialize() {
169         String baseImageFile = StringTools.appendPath(m_animationDir, DEFAULT_SERIES_PREFIX + m_currentImageIndex);
170 
171         String imageFile;
172         File file = new File(baseImageFile + ".bmp");
173         if (!file.exists()) {
174             file = new File(baseImageFile + ".jpg");
175             if (!file.exists()) {
176                 throw new IllegalStateException("File doesn't exist: " + baseImageFile + " (.jpg or .bmp), unable to preview animation.");
177             } else {
178                 imageFile = baseImageFile + ".jpg";
179                 m_isBMP = false;
180             }
181         } else {
182             imageFile = baseImageFile + ".bmp";
183         }
184 
185         try {
186             Image image = ImageUtil.loadImage(imageFile);
187             m_initialImage = ImageUtil.toBufferedImage(image);
188             int width = m_initialImage.getWidth();
189             int height = m_initialImage.getHeight();
190             m_dimension = new Dimension(width, height);
191         } catch (Exception e) {
192             e.printStackTrace();
193             throw new IllegalStateException(e.getMessage());
194         }
195     }
196 }