View Javadoc

1   /*
2    * Copyright 2007 - 2009 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: MathView.java,v 2986a8eeaebc 2009/09/24 12:53:08 max $ */
18  
19  package net.sourceforge.jeuclid.swt;
20  
21  import net.sourceforge.jeuclid.MutableLayoutContext;
22  import net.sourceforge.jeuclid.context.LayoutContextImpl;
23  import net.sourceforge.jeuclid.elements.generic.DocumentElement;
24  
25  import org.eclipse.swt.events.DisposeEvent;
26  import org.eclipse.swt.events.DisposeListener;
27  import org.eclipse.swt.events.PaintEvent;
28  import org.eclipse.swt.events.PaintListener;
29  import org.eclipse.swt.graphics.Color;
30  import org.eclipse.swt.graphics.Device;
31  import org.eclipse.swt.graphics.GC;
32  import org.eclipse.swt.graphics.Image;
33  import org.eclipse.swt.graphics.ImageData;
34  import org.eclipse.swt.widgets.Canvas;
35  import org.eclipse.swt.widgets.Composite;
36  import org.w3c.dom.Node;
37  
38  /**
39   * Contains a display component for SWT.
40   * <p>
41   * TODO: This is a very basic implementation.
42   * 
43   * @version $Revision: 2986a8eeaebc $
44   */
45  public final class MathView extends Canvas {
46  
47      // /**
48      // * Logger for this class
49      // */
50      // private static final Log LOGGER = LogFactory.getLog(MathView.class);
51  
52      private final MathRenderer mathRenderer = MathRenderer.getInstance();
53  
54      private Node document;
55  
56      private ImageData renderedFormula;
57  
58      private MutableLayoutContext layoutContext = new LayoutContextImpl(
59              LayoutContextImpl.getDefaultLayoutContext());
60  
61      /**
62       * Create a new MathView Widget.
63       * 
64       * @param parent
65       *            Parent component
66       * @param style
67       *            SWT style attributes.
68       */
69      public MathView(final Composite parent, final int style) {
70          super(parent, style);
71          this.setDocument(new DocumentElement());
72          this.addDisposeListener(new DisposeListener() {
73              public void widgetDisposed(final DisposeEvent e) {
74                  MathView.this.widgetDisposed(e);
75              }
76          });
77          this.addPaintListener(new PaintListener() {
78              public void paintControl(final PaintEvent e) {
79                  MathView.this.paintControl(e);
80              }
81          });
82      }
83  
84      private void paintControl(final PaintEvent e) {
85          final GC gc = e.gc;
86          final Device device = gc.getDevice();
87          final Color c = new Color(device, 255, 255, 255);
88          gc.setBackground(c);
89          gc.fillRectangle(e.x, e.y, e.width, e.height);
90          c.dispose();
91          if (this.renderedFormula != null) {
92              final Image im = new Image(device, this.renderedFormula);
93              gc.drawImage(im, 0, 0);
94              im.dispose();
95          }
96      }
97  
98      private void widgetDisposed(final DisposeEvent e) {
99          this.document = null;
100         this.renderedFormula = null;
101     }
102 
103     private void recreate() {
104         this.renderedFormula = this.mathRenderer.render(this.document,
105                 this.layoutContext);
106     }
107 
108     /**
109      * @param doc
110      *            the document to set
111      */
112     public void setDocument(final Node doc) {
113         final Node oldValue = this.document;
114         this.document = doc;
115         if (doc != oldValue) {
116             this.recreate();
117             this.redraw();
118         }
119     }
120 
121     /**
122      * @return the document
123      */
124     public Node getDocument() {
125         return this.document;
126     }
127 
128 }