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/JarlContextModel.java


1   package com.arranger.jarl.shell.models;
2   
3   import com.arranger.jarl.Jarl;
4   import com.arranger.jarl.base.IStatusListener;
5   import com.arranger.jarl.base.IContext;
6   import com.arranger.jarl.shell.ShellConfig;
7   import com.arranger.jarl.util.StringTools;
8   import com.arranger.jarl.util.Debug;
9   import com.arranger.jarl.util.XMLUtil;
10  
11  import java.awt.*;
12  import java.io.File;
13  import java.util.Stack;
14  
15  /**
16   * JarlContextModel created on Apr 16, 2003
17   */
18  public class JarlContextModel extends BaseModel {
19  
20      public static final String CURRENT_CONFIG = "currentConfig";
21      public static final String CURRENT_OUTPUTDIR = "currentOutputDir";
22      public static final String START_FRAME = "startFrame";
23      public static final String STOP_FRAME = "stopFrame";
24      public static final String CURRENT_FRAME = "currentFrame";
25      public static final String CUSTOM_DIMENSION = "customDimension";
26      public static final String RENDERED_IMAGE = "renderedImage";
27      public static final String ERROR = "error";
28      public static final String INITIALIZED = "initialized";
29      public static final String RENDER_PROCESSING_CONFIG = "renderStarted";
30      public static final String RENDER_PREPARING = "renderPreparing";
31      public static final String RENDER_COMPLETED = "renderCompleted";
32      public static final String RENDER_STARTING = "renderStarting";
33  
34  
35      protected String m_currentConfig;
36      protected String m_currentOutputDir;
37      protected int m_startFrame;
38      protected int m_stopFrame;
39      protected int m_currentFrame;
40      protected Dimension m_dimension;
41  
42      protected transient Jarl m_jarl;
43      protected transient String m_jarlCachedConfigText;
44      protected transient Image m_renderedImage;
45      protected transient Dimension m_originalDimension;
46      protected transient StatusListener m_statusListener = new StatusListener();
47      protected transient Throwable m_lastThrowable;
48  
49      public void load(ShellConfig shellConfig) throws Exception {
50          m_currentConfig = shellConfig.getProperty(CURRENT_CONFIG);
51          m_currentOutputDir = shellConfig.getProperty(CURRENT_OUTPUTDIR);
52          String startFrame = shellConfig.getProperty(START_FRAME);
53          if (!StringTools.isEmpty(startFrame)) {
54              m_startFrame = Integer.parseInt(startFrame);
55          } else {
56              m_startFrame = 0;
57          }
58          String stopFrame = shellConfig.getProperty(STOP_FRAME);
59          if (!StringTools.isEmpty(stopFrame)) {
60              m_stopFrame = Integer.parseInt(stopFrame);
61          } else {
62              m_stopFrame = 90;
63          }
64          String currentFrame = shellConfig.getProperty(CURRENT_FRAME);
65          if (!StringTools.isEmpty(currentFrame)) {
66              m_currentFrame = Integer.parseInt(currentFrame);
67          } else {
68              m_currentFrame = 0;
69          }
70          String customDimension = shellConfig.getProperty(CUSTOM_DIMENSION);
71          if (!StringTools.isEmpty(customDimension)) {
72              String[] dims = StringTools.getStringArrayFromDelimitedString(customDimension, ",");
73              m_dimension = new Dimension(Integer.parseInt(dims[0]), Integer.parseInt(dims[1]));
74          }
75      }
76  
77      public void save(ShellConfig shellConfig) throws Exception {
78          shellConfig.setProperty(CURRENT_CONFIG, m_currentConfig);
79          shellConfig.setProperty(CURRENT_OUTPUTDIR, m_currentOutputDir);
80          shellConfig.setProperty(START_FRAME, String.valueOf(m_startFrame));
81          shellConfig.setProperty(STOP_FRAME, String.valueOf(m_stopFrame));
82          shellConfig.setProperty(CURRENT_FRAME, String.valueOf(m_currentFrame));
83          if (m_dimension != null) {
84              shellConfig.setProperty(CUSTOM_DIMENSION, (int) m_dimension.getWidth() + ", " + (int) m_dimension.getHeight());
85          } else {
86              shellConfig.getProperties().remove(CUSTOM_DIMENSION);
87          }
88      }
89  
90      public String getStatusText() {
91          StringBuffer buffer = new StringBuffer();
92          buffer.append(CURRENT_CONFIG).append(": ").append(m_currentConfig).append(LINE_SEP);
93          buffer.append(CURRENT_OUTPUTDIR).append(": ").append(m_currentOutputDir).append(LINE_SEP);
94          buffer.append(START_FRAME).append(": ").append(m_startFrame).append(LINE_SEP);
95          buffer.append(STOP_FRAME).append(": ").append(m_stopFrame).append(LINE_SEP);
96          buffer.append(CURRENT_FRAME).append(": ").append(m_currentFrame).append(LINE_SEP);
97          buffer.append(CUSTOM_DIMENSION).append(": ").append(m_dimension).append(LINE_SEP);
98          return buffer.toString();
99      }
100 
101     public String getCurrentConfig() {
102         return m_currentConfig;
103     }
104 
105     public void setCurrentConfig(String currentConfig) {
106         if (!currentConfig.equals(m_currentConfig)) {
107             m_currentConfig = currentConfig;
108             raiseEvent(CURRENT_CONFIG);
109         }
110     }
111 
112     public String getCurrentOutputDir() {
113         return m_currentOutputDir;
114     }
115 
116     public void setCurrentOutputDir(String currentOutputDir) {
117         if (!currentOutputDir.equals(m_currentOutputDir)) {
118             m_currentOutputDir = currentOutputDir;
119             raiseEvent(CURRENT_OUTPUTDIR);
120         }
121     }
122 
123     public int getStartFrame() {
124         return m_startFrame;
125     }
126 
127     public void setStartFrame(int startFrame) {
128         if (m_startFrame != startFrame) {
129             m_startFrame = startFrame;
130             raiseEvent(START_FRAME);
131         }
132     }
133 
134     public int getStopFrame() {
135         return m_stopFrame;
136     }
137 
138     public void setStopFrame(int stopFrame) {
139         if (m_stopFrame != stopFrame) {
140             m_stopFrame = stopFrame;
141             raiseEvent(STOP_FRAME);
142         }
143     }
144 
145     public int getCurrentFrame() {
146         return m_currentFrame;
147     }
148 
149     public void setCurrentFrame(int currentFrame) {
150         if (m_currentFrame != currentFrame) {
151             m_currentFrame = currentFrame;
152             raiseEvent(CURRENT_FRAME);
153         }
154     }
155 
156     public Dimension getDimension() {
157         if (m_dimension == null) {
158             if (m_originalDimension != null) {
159                 return m_originalDimension;
160             } else if (m_jarl != null) {
161                 IContext context = m_jarl.getContext();
162                 return new Dimension(context.getWidth(), context.getHeight());
163             }
164         }
165 
166         return m_dimension;
167     }
168 
169     public boolean hasCustomDimension() {
170         return m_dimension != null;
171     }
172 
173     public void setDimension(Dimension dimension) {
174         if (dimension == null) {
175             if (m_dimension != null) {
176                 m_dimension = dimension;
177                 raiseEvent(CUSTOM_DIMENSION);
178             }
179         } else if (!dimension.equals(m_dimension)) {
180             m_dimension = dimension;
181             raiseEvent(CUSTOM_DIMENSION);
182         }
183     }
184 
185     public Throwable getLastThrowable() {
186         return m_lastThrowable;
187     }
188 
189     public Image getRenderedImage() {
190         return m_renderedImage;
191     }
192 
193     public void setRenderedImage(Image renderedImage) {
194         m_renderedImage = renderedImage;
195         raiseEvent(RENDERED_IMAGE);
196     }
197 
198     public Jarl getJarl() {
199         return m_jarl;
200     }
201 
202     public void render() {
203         m_jarlShell.out("Rendering: " + m_currentConfig + " frame: " + m_currentFrame);
204         m_lastThrowable = null;
205 
206         new Thread() {
207             public void run() {
208                 try {
209                     //raise event
210                     raiseEvent(RENDER_PREPARING);
211 
212                     IContext context;
213                     //check the cached jarlConfigText
214                     String rawXmlText = XMLUtil.preprocesXML(new File(m_currentConfig), new Stack());
215                     if (m_jarlCachedConfigText != null && m_jarlCachedConfigText.equals(rawXmlText)) {
216                         context = m_jarl.getContext();
217                     } else {
218                         m_jarl = new Jarl();
219                         context = m_jarl.getContext();
220                         context.addStatusListener(m_statusListener);
221                         raiseEvent(RENDER_PROCESSING_CONFIG);
222 
223                         //prior to init
224                         m_jarl.init(m_currentConfig);
225 
226                         //set start & stop & fire initialized
227                         setStartFrame(m_jarl.getStartTime().getFrame());
228                         setStopFrame(m_jarl.getEndTime().getFrame());
229                         raiseEvent(INITIALIZED);
230 
231                         //store rawXMLText
232                         m_jarlCachedConfigText = rawXmlText;
233                         m_originalDimension = null;
234                     }
235 
236                         //override dimensions
237                     if (m_dimension != null) {
238                         if (m_originalDimension == null) {
239                             m_originalDimension = new Dimension(context.getWidth(), context.getHeight());
240                         }
241                         context.setWidth((int) m_dimension.getWidth());
242                         context.setHeight((int) m_dimension.getHeight());
243                     } else if (m_originalDimension != null) {
244                         context.setWidth((int) m_originalDimension.getWidth());
245                         context.setHeight((int) m_originalDimension.getHeight());
246                     }
247 
248                     raiseEvent(RENDER_STARTING);
249                     Image image = m_jarl.getRenderManager().renderOneFrame(context, m_currentFrame);
250                     setRenderedImage(image);
251                     m_jarlShell.saveModels();
252 
253                     //completed
254                     raiseEvent(RENDER_COMPLETED);
255                 } catch (Throwable t) {
256                     m_lastThrowable = t;
257                     raiseEvent(ERROR);
258                     m_statusListener.onError(t);
259                 }
260             }
261         }.start();
262     }
263 
264     class StatusListener implements IStatusListener {
265 
266         public void onStatus(String message) {
267             if (shouldDebug()) {
268                 m_jarlShell.out(message);
269             }
270         }
271 
272         public void onError(Throwable throwable) {
273             m_jarlShell.out(throwable.getMessage());
274             m_jarlShell.out(Debug.getStackTrace(throwable));
275         }
276 
277         private boolean shouldDebug() {
278             ViewPrefModel viewPrefModel = (ViewPrefModel) m_jarlShell.getModel(ViewPrefModel.class.getName());
279             return viewPrefModel.isDebugRendering();
280         }
281     }
282 }