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

Quick Search    Search Deep

Source code: com/arranger/jarl/shell/views/AnimationSeriesView.java


1   package com.arranger.jarl.shell.views;
2   
3   import com.arranger.jarl.Jarl;
4   import com.arranger.jarl.base.IContext;
5   import com.arranger.jarl.base.IJarlObject;
6   import com.arranger.jarl.base.IRenderListener;
7   import com.arranger.jarl.base.IStatusListener;
8   import com.arranger.jarl.shell.models.BaseModel;
9   import com.arranger.jarl.shell.models.JarlAnimationContextModel;
10  import com.arranger.jarl.shell.models.JarlContextModel;
11  import com.arranger.jarl.util.Debug;
12  import com.arranger.jarl.util.JarlInfoUtil;
13  
14  import javax.swing.*;
15  import javax.swing.border.TitledBorder;
16  import java.awt.*;
17  import java.awt.event.KeyEvent;
18  import java.awt.event.ActionListener;
19  import java.awt.event.ActionEvent;
20  
21  /**
22   * AnimationSeriesView created on Apr 21, 2003
23   */
24  public class AnimationSeriesView extends BaseView implements IStatusListener, IRenderListener {
25  
26      protected JarlShellFrame m_jarlShellFrame;
27      protected Jarl m_jarl;
28      protected int m_frameStart, m_frameEnd;
29      protected boolean m_animationAborted = false;
30      protected boolean m_animationCompleted = false;
31  
32      //components
33      protected JTextField m_textField;
34      protected JProgressBar m_progressBar;
35      protected TitledBorder m_renderTitleBorder;
36      protected JLabel m_numFramesLabel;
37      protected JPanel m_renderStatusPanel;
38  
39      public String[] getModelsToSubscribe() {
40          return new String[]{JarlAnimationContextModel.class.getName()};
41      }
42  
43      public void onChange(BaseModel baseModel, String propertyName) {
44          JarlAnimationContextModel jacm = (JarlAnimationContextModel) baseModel;
45          if (JarlAnimationContextModel.RENDER_PREPARING.equals(propertyName)) {
46              prepareRender(jacm);
47          } else if (JarlContextModel.RENDER_PROCESSING_CONFIG.equals(propertyName)) {
48              processingConfig(jacm);
49          } else if (JarlContextModel.INITIALIZED.equals(propertyName)) {
50              initializedAnimation();
51          } else if (JarlContextModel.RENDER_COMPLETED.equals(propertyName)) {
52              completed();
53          } else if (JarlContextModel.ERROR.equals(propertyName)) {
54              onError(jacm.getLastThrowable());
55          }
56      }
57  
58      private void completed() {
59          if (!m_animationAborted) {
60              JOptionPane.showMessageDialog(m_jarlShellFrame.getComponent(),
61                      "Rendering completed",
62                      "Success",
63                      JOptionPane.INFORMATION_MESSAGE);
64              m_animationCompleted = true;
65          }
66          m_jarlShellFrame.dispose();
67      }
68  
69      private void processingConfig(JarlAnimationContextModel jacm) {
70          m_jarl = jacm.getJarl();
71          m_jarl.getContext().addStatusListener(this);
72          m_jarl.getRenderManager().addRenderListener(this);
73          m_progressBar.setIndeterminate(true);
74      }
75  
76      private void initializedAnimation() {
77          m_progressBar.setIndeterminate(false);
78          m_frameStart = m_jarl.getStartTime().getFrame();
79          m_frameEnd = m_jarl.getEndTime().getFrame();
80          m_numFramesLabel.setText("Num Frames: " + (m_frameEnd - m_frameStart));
81      }
82  
83      private void prepareRender(JarlAnimationContextModel jacm) {
84          Container container = getContainer(jacm);
85          m_jarlShellFrame = JarlShellFrame.createJarlShellFrame(m_jarlShell,
86                  container,
87                  container.getWidth(),
88                  container.getHeight(),
89                  "Animation series: " + jacm.getCurrentConfig());
90  
91          m_jarlShellFrame.addWindowClosingListener(new WindowClosingListener());
92          m_jarlShellFrame.packAndShow();
93      }
94  
95      protected class WindowClosingListener implements JarlShellFrame.JarlShellFrameClosingListener {
96  
97          public void onClosing() {
98              if (!m_animationCompleted) {
99                  int result = JOptionPane.showOptionDialog(m_jarlShellFrame.getComponent(),
100                         "Do you want to quit this animation rendering?",
101                         "Closing animation status window",
102                         JOptionPane.YES_NO_OPTION,
103                         JOptionPane.WARNING_MESSAGE,
104                         null,
105                         null,
106                         null);
107                 if (result == JOptionPane.YES_OPTION) {
108                     m_jarl.getEndTime().setFrame(-1); //should kill stuff
109                     m_animationAborted = true;
110                 }
111             }
112         }
113     }
114 
115     protected Container getContainer(JarlAnimationContextModel jacm) {
116         GridBagLayout gridBagLayout = new GridBagLayout();
117         GridBagConstraints c = new GridBagConstraints();
118         c.fill = GridBagConstraints.HORIZONTAL;
119         c.anchor = GridBagConstraints.CENTER;
120 
121         JPanel panel = new JPanel(gridBagLayout);
122 
123         //row 0   Animation options
124         Component topPane = createAnimationOptionsPane(jacm);
125         c.gridx = 0;
126         c.gridy = 0;
127         c.weightx = 5;
128         c.weighty = 2;
129         c.gridwidth = 5;
130         gridBagLayout.setConstraints(topPane, c);
131         panel.add(topPane);
132 
133         //row 2  Render Status pane
134         Component renderStatusPane = createRenderStatusPane(jacm);
135         c.gridx = 0;
136         c.gridy = 2;
137         c.weighty = 2;
138         gridBagLayout.setConstraints(renderStatusPane, c);
139         panel.add(renderStatusPane);
140 
141         //row 3 Abort Button
142         Component abortButtonPane = createAbortButtonPane();
143         c.gridx = 0;
144         c.gridy = 4;
145         c.weighty = 2;
146         gridBagLayout.setConstraints(abortButtonPane, c);
147         panel.add(abortButtonPane);
148 
149         return panel;
150     }
151 
152     protected Component createAnimationOptionsPane(JarlAnimationContextModel jacm) {
153         GridLayout gridLayout = new GridLayout(3, 1, 10, 10);
154         JPanel animationOptions = new JPanel(gridLayout);
155 
156         JLabel jarlFileLabel = new JLabel("File: " + jacm.getCurrentConfig());
157         animationOptions.add(jarlFileLabel);
158 
159         JLabel outputDirLabel = new JLabel("Output Dir: " + jacm.getCurrentOutputDir());
160         animationOptions.add(outputDirLabel);
161 
162         JLabel numFramesLabel = new JLabel("Num Frames: ???");
163         m_numFramesLabel = numFramesLabel;
164         animationOptions.add(m_numFramesLabel);
165 
166         return animationOptions;
167     }
168 
169     protected Component createRenderStatusPane(JarlAnimationContextModel jacm) {
170         m_renderStatusPanel = new JPanel(new GridLayout(2, 1));
171 
172         //status field
173         m_textField = new JTextField("<status>");
174         m_textField.setEditable(false);
175         m_renderStatusPanel.add(m_textField);
176 
177         //progress bar
178         m_progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
179         m_progressBar.setMinimum(0);
180         m_progressBar.setMaximum(100);
181         m_renderStatusPanel.add(m_progressBar);
182 
183         //title
184         m_renderStatusPanel.setBorder(m_renderTitleBorder = BorderFactory.createTitledBorder("Rendering Frame: "));
185         return m_renderStatusPanel;
186     }
187 
188     protected Component createAbortButtonPane() {
189         JButton button = new JButton("Abort");
190         button.addActionListener(new ActionListener() {
191             public void actionPerformed(ActionEvent e) {
192                 int result = JOptionPane.showOptionDialog(m_jarlShellFrame.getComponent(),
193                         "Are you sure you want to quit this animation rendering?",
194                         "Closing animation status window",
195                         JOptionPane.YES_NO_OPTION,
196                         JOptionPane.WARNING_MESSAGE,
197                         null,
198                         null,
199                         null);
200                 if (result == JOptionPane.YES_OPTION) {
201                     m_jarl.getEndTime().setFrame(-1); //should kill stuff
202                     m_animationAborted = true;
203                 }
204             }
205         });
206 
207         button.setMnemonic(KeyEvent.VK_A);
208         return button;
209     }
210 
211     public void onStatus(String message) {
212         m_textField.setText(message);
213     }
214 
215     public void onError(Throwable throwable) {
216         String title = throwable.getMessage();
217         if (title == null) {
218             title = "Null Pointer Exception";
219         }
220         JOptionPane.showMessageDialog(null, Debug.getStackTrace(throwable), title, JOptionPane.ERROR_MESSAGE);
221         m_jarlShellFrame.dispose();
222     }
223 
224     public void onRender(IJarlObject jarlObject, IContext context) {
225         onStatus("Rendering: " + JarlInfoUtil.getName(jarlObject));
226     }
227 
228     public void onRenderFrameStart(IContext context) {
229         final int frame = context.getTime().getFrame();
230         m_renderTitleBorder.setTitle("Rendering Frame: " + frame);
231         m_renderStatusPanel.repaint();
232     }
233 
234     public void onRenderFrameStop(IContext context, Image image) {
235         double current = context.getTime().getFrame();
236 
237         double pct = current / (double) (m_frameEnd - m_frameStart);
238         pct *= 100;
239 
240         m_progressBar.setValue((int) pct);
241     }
242 }