Source code: com/arranger/jarl/base/Context.java
1 package com.arranger.jarl.base;
2
3 import com.arranger.jarl.Jarl;
4
5 import java.awt.*;
6 import java.awt.image.BufferedImage;
7 import java.util.*;
8
9 /**
10 * Context is the concrete implementation of {@link com.arranger.jarl.base.IContext}
11 */
12 public class Context implements IContext, Cloneable {
13
14 protected Jarl m_jarl;
15 protected IRenderManager m_renderManager;
16 protected IGradientManager m_gradientManager;
17 protected RenderingHints m_renderingHints;
18 protected int m_height;
19 protected int m_width;
20 protected Time m_time;
21 protected Time m_absoluteTime;
22 protected float m_strokeWidth;
23 protected Stroke m_defaultStroke = new BasicStroke(5);
24 protected Set m_statusListeners = new HashSet();
25 protected Map m_attributes = new HashMap();
26
27 public Context() {
28 }
29
30 /**
31 * @return the currently configured rendering hints
32 */
33 public RenderingHints getRenderingHints() {
34 return m_renderingHints;
35 }
36
37 public void setRenderingHints(RenderingHints renderingHints) {
38 m_renderingHints = renderingHints;
39 }
40
41 /**
42 * @return the current rendering height
43 */
44 public int getHeight() {
45 return m_height;
46 }
47
48 public void setHeight(int height) {
49 m_height = height;
50 }
51
52 /**
53 * @return the current rendering width
54 */
55 public int getWidth() {
56 return m_width;
57 }
58
59 public void setWidth(int width) {
60 m_width = width;
61 }
62
63 public float getStrokeWidth() {
64 return m_strokeWidth;
65 }
66
67 public void setStrokeWidth(float strokeWidth) {
68 m_strokeWidth = strokeWidth;
69 m_defaultStroke = new BasicStroke(m_strokeWidth);
70 }
71
72 public Stroke getDefaultStroke() {
73 return m_defaultStroke;
74 }
75
76 public void setDefaultStroke(Stroke defaultStroke) {
77 m_defaultStroke = defaultStroke;
78 }
79
80 /**
81 * Create a properly initialized image
82 * @return an initialized image
83 */
84 public Image createImage() {
85 return new BufferedImage(getWidth(),
86 getHeight(),
87 BufferedImage.TYPE_INT_RGB);
88 }
89
90 /**
91 * Get the current rendering manager
92 * @return the current {@link IRenderManager}
93 */
94 public IRenderManager getRenderManager() {
95 return m_renderManager;
96 }
97
98 /**
99 * sets the current render manager
100 * @param renderManager
101 */
102 public void setRenderManager(IRenderManager renderManager) {
103 m_renderManager = renderManager;
104 }
105
106 /**
107 * Get the current gradient manager
108 * @return the current {@link IGradientManager}
109 */
110 public IGradientManager getGradientManager() {
111 return m_gradientManager;
112 }
113
114 /**
115 * sets the current gradient manager
116 * @param gradientManager
117 */
118 public void setGradientManager(IGradientManager gradientManager) {
119 m_gradientManager = gradientManager;
120 }
121
122 /**
123 * @return the current instance of Jarl
124 */
125 public Jarl getJarl() {
126 return m_jarl;
127 }
128
129 /**
130 * Set the current instance of jarl
131 * @param jarl
132 */
133 public void setJarl(Jarl jarl) {
134 m_jarl = jarl;
135 }
136
137 /**
138 * Get the current time
139 * @return the current rendering time
140 */
141 public Time getTime() {
142 return m_time;
143 }
144
145 /**
146 * Get the absolute current time that won't be affected by holds or echos
147 * @return the absolutely current rendering time
148 */
149 public Time getAbsoluteTime() {
150 return m_absoluteTime;
151 }
152
153 /**
154 * Set the absolute current time that won't be affected by holds or echos
155 */
156 public void setAbsoluteTime(Time time) {
157 setTime(time);
158 m_absoluteTime = new Time(time.getFrame());
159 }
160
161 /**
162 * sets the current time
163 * @param time
164 */
165 public void setTime(Time time) {
166 m_time = time;
167 }
168
169 public Object clone() {
170 try {
171 return super.clone();
172 } catch (CloneNotSupportedException e) {
173 e.printStackTrace();
174 return this;
175 }
176 }
177
178 /**
179 * Copy the params from the other context
180 * @param context
181 */
182 public void copy(IContext context) {
183 m_renderManager = context.getRenderManager();
184 m_renderingHints = context.getRenderingHints();
185 m_jarl = context.getJarl();
186 m_height = context.getHeight();
187 m_width = context.getWidth();
188 m_time = context.getTime();
189 }
190
191 /**
192 * Adds a status Listener
193 * @param statusListener
194 */
195 public void addStatusListener(IStatusListener statusListener) {
196 m_statusListeners.add(statusListener);
197 }
198
199 /**
200 * Removes a status Listener
201 * @param statusListener
202 */
203 public void removeStatusListener(IStatusListener statusListener) {
204 m_statusListeners.remove(statusListener);
205 }
206
207 /**
208 * A simple message
209 * @param message
210 */
211 public void onStatus(String message) {
212 for (Iterator it = m_statusListeners.iterator(); it.hasNext();) {
213 ((IStatusListener) it.next()).onStatus(message);
214 }
215 }
216
217 /**
218 * An error occurred
219 * @param throwable
220 */
221 public void onError(Throwable throwable) {
222 for (Iterator it = m_statusListeners.iterator(); it.hasNext();) {
223 ((IStatusListener) it.next()).onError(throwable);
224 }
225 }
226
227 /**
228 * @return an alterable map
229 */
230 public Map getAttributes() {
231 return m_attributes;
232 }
233 }
234
235