Source code: com/arranger/jarl/trait/BaseTrait.java
1 package com.arranger.jarl.trait;
2
3 import com.arranger.jarl.base.BaseJarlObject;
4 import com.arranger.jarl.base.IContext;
5 import com.arranger.jarl.widget.IWidget;
6 import org.w3c.dom.Element;
7
8 import java.awt.*;
9 import java.awt.geom.AffineTransform;
10
11 /**
12 * BaseTrait created on Feb 21, 2003
13 */
14 public abstract class BaseTrait extends BaseJarlObject implements ITrait, ITraitDef {
15
16 /**
17 * These are all possible cached objects
18 */
19 protected AffineTransform m_affineTransform;
20 protected Paint m_paint;
21 protected IContext m_context;
22 protected Graphics2D m_graphics2D;
23 protected Composite m_composite;
24
25 /**
26 * Based off of the actual def, get a real instance
27 * @param context
28 * @param element
29 */
30 public ITrait initInstance(IContext context, Element element) {
31 ITrait trait =(ITrait)clone();
32 trait.init(context, element);
33 return trait;
34 }
35
36 protected void store(AffineTransform affineTransform) {
37 m_affineTransform = affineTransform;
38 }
39
40 protected void store(Paint paint) {
41 m_paint = paint;
42 }
43
44 protected void store(IContext context) {
45 m_context = (IContext)context.clone();
46 }
47
48 protected void store(Graphics2D graphics2D) {
49 m_graphics2D = graphics2D;
50 }
51
52 protected void store(Composite composite) {
53 m_composite = composite;
54 }
55
56 protected void restoreTransform(Graphics2D graphics2D) {
57 if (m_affineTransform != null) {
58 graphics2D.setTransform(m_affineTransform);
59 }
60 }
61
62 protected void restorePaint(IWidget widget) {
63 if (m_paint != null) {
64 widget.setColor(m_paint);
65 }
66 }
67
68 protected void restoreContext(IContext context) {
69 if (m_context != null) {
70 context.copy(m_context);
71 }
72 }
73
74 protected void restoreComposite(Graphics2D graphics2D) {
75 if (m_composite != null) {
76 graphics2D.setComposite(m_composite);
77 }
78 }
79
80 protected Graphics2D restoreGraphics() {
81 return m_graphics2D;
82 }
83
84 protected Graphics2D initGraphicsTransform(Graphics2D graphics2D) {
85 store(graphics2D.getTransform());
86 graphics2D.setTransform(new AffineTransform(m_affineTransform));
87 return graphics2D;
88 }
89 }