Source code: org/gendiapo/editor/document/GenDiapoEditorPane.java
1 package org.gendiapo.editor.document;
2
3 import java.lang.reflect.Method;
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.awt.print.*;
7 import java.io.*;
8 import java.net.*;
9 import javax.swing.text.*;
10 import javax.swing.*;
11 import javax.swing.plaf.basic.*;
12
13 import org.gendiapo.editor.document.*;
14
15 public class GenDiapoEditorPane extends JEditorPane implements ComponentListener, Printable {
16
17 protected Element _highlight;
18 protected Object _highlightTag;
19 protected PrintView m_printView;
20
21 Rectangle rect;
22
23 public GenDiapoEditorPane() {
24 super();
25 // Hack pour eviter un bug lors de la saisie
26 setEditorKit(new GenDiapoEditorKit());
27 // Hack pour fixer UI
28 setUI(new GenDiapoEditorPaneUI());
29 // Hack pour fixer le curseur
30 addComponentListener(this);
31 //setCaret(new GenDiapoCaret());
32
33 rect = getVisibleRect();
34 }
35
36 public void modelChanged() {
37 ((GenDiapoEditorPaneUI) getUI()).modelChanged();
38 }
39
40 public void asyncModelChanged() {
41 SwingUtilities.invokeLater(new AsyncModelChanged());
42 }
43
44 private class AsyncModelChanged implements Runnable {
45
46 public AsyncModelChanged() {
47 }
48
49 public void run() {
50 modelChanged();
51 }
52 }
53
54
55 public GenDiapoEditorPane(String url) throws IOException {
56 super(url);
57 }
58
59 public GenDiapoEditorPane(String type, String text) {
60 super(type, text);
61 }
62
63 public GenDiapoEditorPane(URL initialPage) throws IOException {
64 super(initialPage);
65 }
66
67 protected EditorKit createDefaultEditorKit() {
68 return new GenDiapoEditorKit();
69 }
70
71 private Element getNodeElement(Element elem) {
72 boolean cont = true;
73 Element retval = elem;
74
75 while (cont) {
76 retval = elem;
77 AttributeSet attr = elem.getAttributes();
78 Object oType = attr.getAttribute(GenDiapoDocument.ATTRIBUT_TYPE);
79 int type = ((java.lang.Integer) oType).intValue();
80
81 // suivant
82 elem = elem.getParentElement();
83
84 // trouver
85 cont = ((type != org.w3c.dom.Node.ELEMENT_NODE) && (elem != null) );
86 }
87 return retval;
88 }
89
90 public void setHighlight(Element elem) {
91 GenDiapoHighlighter h = (GenDiapoHighlighter) getHighlighter();
92
93 if (_highlightTag != null) {
94 h.removeHighlight(_highlightTag);
95 }
96
97 _highlight = getNodeElement(elem);
98 int start = _highlight.getStartOffset();
99 int end = _highlight.getEndOffset();
100
101 try {
102 _highlightTag = h.addHighlight(start, end,
103 new GenDiapoHighlighter.NodeHighlightPainter(_highlight, getSelectionColor()));
104 } catch (BadLocationException e) {
105 }
106
107 // addHighlight a redessiner les caracteres mais pas la View complete
108 // repaint permet de s'assurer que tout est redessiner
109 repaint(getVisibleRect());
110 }
111
112 public Element getHighlight() {
113 return _highlight;
114 }
115
116 /*
117 // Ne sert rien, fixer par l'EditorKit
118 protected Caret createCaret() {
119 System.out.println("createCaret");
120 return new GenDiapoCaret();
121 }
122 */
123
124 /*
125 static final String uiClassName = "org.gendiapo.editor.document.GenDiapoEditorPaneUI";
126 static final Class uiClass = (new GenDiapoEditorPaneUI()).getClass();
127
128 public String getUIClassID() {
129 (UIManager.getDefaults()).put(uiClassName, uiClass);
130 return uiClassName;
131 }
132 */
133
134 // Patch pour la disparition du curseur
135 protected void resetCaret() {
136 //int pos = getCaretPosition();
137 setCaret(getCaret());
138 //setCaretPosition(pos);
139 }
140
141 public void componentMoved(ComponentEvent e) {
142 Rectangle n = getVisibleRect();
143 double oy1 = rect.getY();
144 double oy2 = oy1 + rect.getHeight();
145 double ny1 = n.getY();
146 double ny2 = ny1 + n.getHeight();
147
148 Rectangle newVisible;
149 if (ny1 < oy1) {
150 // Cas deplacement vers le haut
151 newVisible = new Rectangle((int)n.getX(), (int)n.getY(), (int)n.getWidth(), (int)(oy1-ny1));
152 } else {
153 // Cas deplacement vers le bas
154 newVisible = new Rectangle((int)n.getX(), (int)n.getY(), (int)n.getWidth(), (int)(ny2-oy2));
155 }
156
157 repaint(newVisible);
158 rect = n;
159 }
160
161 public void componentResized(ComponentEvent e) {
162 rect = getVisibleRect();
163 //resetCaret();
164 }
165
166 public void componentShown(ComponentEvent e) {
167 //resetCaret();
168 }
169
170 public void componentHidden(ComponentEvent e) {
171 }
172
173 // Print part
174 public int print(Graphics pg, PageFormat pageFormat, int pageIndex) throws PrinterException {
175 pg.translate((int)pageFormat.getImageableX(),
176 (int)pageFormat.getImageableY());
177 int wPage = (int)pageFormat.getImageableWidth();
178 int hPage = (int)pageFormat.getImageableHeight();
179 pg.setClip(0, 0, wPage, hPage);
180 // Only do this once per print
181 if (m_printView == null) {
182 BasicTextUI btui = (BasicTextUI)this.getUI();
183 View root = btui.getRootView(this);
184 m_printView = new PrintView(getDocument().getDefaultRootElement(), root, wPage, hPage);
185 }
186 boolean bContinue = m_printView.paintPage(pg, hPage, pageIndex);
187 System.gc();
188 if (bContinue)
189 return PAGE_EXISTS;
190 else {
191 m_printView = null;
192 return NO_SUCH_PAGE;
193 }
194 }
195
196 class PrintView extends BoxView {
197 protected int m_firstOnPage = 0;
198 protected int m_lastOnPage = 0;
199 protected int m_pageIndex = 0;
200
201 public PrintView(Element elem, View root, int w, int h) {
202 super(elem, Y_AXIS);
203 setParent(root);
204 setSize(w, h);
205 layout(w, h);
206 }
207 public boolean paintPage(Graphics g, int hPage, int pageIndex) {
208 GenDiapoHighlighter h = (GenDiapoHighlighter) getHighlighter();
209
210 if (pageIndex > m_pageIndex) {
211 m_firstOnPage = m_lastOnPage + 1;
212 if (m_firstOnPage >= getViewCount())
213 return false;
214 m_pageIndex = pageIndex;
215 }
216
217 h.setDrawsLayeredHighlights(false);
218
219 int yMin = getOffset(Y_AXIS, m_firstOnPage);
220 int yMax = yMin + hPage;
221 Rectangle rc = new Rectangle();
222 for (int k = m_firstOnPage; k < getViewCount(); k++) {
223 rc.x = getOffset(X_AXIS, k);
224 rc.y = getOffset(Y_AXIS, k);
225 rc.width = getSpan(X_AXIS, k);
226 rc.height = getSpan(Y_AXIS, k);
227 if (rc.y+rc.height > yMax)
228 break;
229 m_lastOnPage = k;
230 rc.y -= yMin;
231 paintChild(g, rc, k);
232 }
233
234 h.setDrawsLayeredHighlights(true);
235
236 return true;
237 }
238 }
239
240 }