1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.awt;
20
21 import java.awt.Component;
22 import java.awt.Dimension;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25
26 import net.sourceforge.jeuclid.MutableLayoutContext;
27 import net.sourceforge.jeuclid.context.LayoutContextImpl;
28 import net.sourceforge.jeuclid.context.Parameter;
29 import net.sourceforge.jeuclid.layout.JEuclidView;
30
31 import org.w3c.dom.Document;
32
33
34
35
36
37
38
39 public class MathComponent extends Component {
40
41
42
43
44
45
46
47
48
49
50 private static final long serialVersionUID = 1L;
51
52
53
54
55 private transient JEuclidView view;
56
57 private Document document;
58
59 private MutableLayoutContext parameters = new LayoutContextImpl(
60 LayoutContextImpl.getDefaultLayoutContext());
61
62
63
64
65 public MathComponent() {
66
67 }
68
69
70
71
72
73
74
75 public final void setParameters(final MutableLayoutContext newParameters) {
76 this.parameters = newParameters;
77 }
78
79
80
81
82 public Document getDocument() {
83 return this.document;
84 }
85
86
87
88
89
90
91 @Override
92 public Dimension getMinimumSize() {
93 if (this.view == null) {
94 return new Dimension(1, 1);
95 } else {
96 return new Dimension((int) Math.ceil(this.view.getWidth()),
97 (int) Math.ceil(this.view.getAscentHeight()
98 + (int) Math.ceil(this.view.getDescentHeight())));
99 }
100 }
101
102
103
104
105
106
107 @Override
108 public Dimension getPreferredSize() {
109 return this.getMinimumSize();
110 }
111
112
113
114
115
116
117
118 @Override
119 public void paint(final Graphics g) {
120 super.paint(g);
121 if (this.view != null) {
122 this.view.draw((Graphics2D) g, 0, (int) Math.ceil(this.view
123 .getAscentHeight()));
124 }
125 }
126
127 private void redo() {
128 final Graphics2D g2d = (Graphics2D) this.getGraphics();
129 if ((this.document == null) || (g2d == null)) {
130 this.view = null;
131 } else {
132 this.view = new JEuclidView(this.document, this.parameters, g2d);
133 }
134 this.repaint();
135 }
136
137
138
139
140
141
142
143 public void setDebug(final boolean debugMode) {
144 this.parameters.setParameter(Parameter.DEBUG, debugMode);
145 this.redo();
146 }
147
148
149
150
151
152 public void setDocument(final Document newDocument) {
153 this.document = newDocument;
154 this.redo();
155 }
156
157 }