View Javadoc

1   package fr.ove.openmath.jome.ctrlview.bidim;
2   
3   import java.awt.*;
4   import java.awt.image.ImageObserver;
5   import fr.ove.openmath.jome.ctrlview.bidim.*;
6   import fr.ove.openmath.jome.ctrlview.bidim.images.ImageLoader;
7   
8   /***
9   * The Displayable implementation of an image symbol.
10  *
11  * @author © 2000 DIRAT Laurent
12  * @version 2.0  05/01/2000
13  */
14  public class ImageSymbol extends DisplayableImpl {
15      /***
16      * The image identifier
17      */
18      private String imgId;
19      
20      /***
21      * The initial image.
22      * The reason of this field is we need to create an instance of the image
23      * which will have a size proportional with the characters of the formula. Because
24      * all the characters don't have the same size (exponents for example), we
25      * need to keep a reference of the initial size of the icon, to compute
26      * correctly its real size for displaying.
27      */
28      private Image symbolInit;
29      private Image symbol;
30          
31      /***
32      * Object to be notified as more of the image is converted. 
33      */
34      private ImageObserver observer;
35      
36      private Insets insets = new Insets(0, 0, 0, 0);
37          
38      /***
39      * The constructor.
40      * @param imgId the image identifier of the symbol.
41      * @param observer the object to be notified as more of the image is converted.
42      */
43      public ImageSymbol(String imgId, ImageObserver observer) {
44          symbolInit = ImageLoader.getImage(imgId);
45          this.observer = observer;
46          this.imgId = imgId;
47      }
48      
49      /***
50      * Sets the graphic context in which the object is.
51      * @param graphicContext the object graphic context.
52      * Actually, this methods gives information to the object, notably when its preferred
53      * size is required. If the context has changed, the displayable object must be informed.
54      * However, in the most part of the time, the whole graphic context won't be necessary
55      * stored, but only specific and usefull data.
56      */
57      public void setGraphicContext(GraphicContext graphicContext) {
58          super.setGraphicContext(graphicContext);
59          FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(graphicContext.getFont());
60          
61          float heightFactor = ImageLoader.getHeightFactor(imgId);
62          int fontHeight = fm.getHeight();
63          
64          insets.top = Math.round(ImageLoader.getTopInset(imgId)*fontHeight);
65          insets.bottom = Math.round(ImageLoader.getBottomInset(imgId)*fontHeight);
66          insets.left = Math.round(ImageLoader.getLeftInset(imgId)*fontHeight);
67          insets.right = Math.round(ImageLoader.getRightInset(imgId)*fontHeight);
68          
69          setHeight((int) Math.round(fontHeight*heightFactor + insets.top + insets.bottom));
70          
71          float baseline = ImageLoader.getBaseline(imgId);
72          int ascent;
73          if (baseline == -1) {
74              // C'est la baseline de la police courante.
75              // Par contre, dans le cas d'un Èventuel facteur de taille, l'ascent s'en trouve aussi
76              // modifiÈ
77              ascent = (int) Math.round(fm.getAscent()*heightFactor);
78          }
79          else
80              ascent = (int) Math.round(fontHeight*heightFactor*baseline);
81              
82          setAscent(ascent + insets.top);
83          setDescent(getHeight() - ascent);
84      }
85  
86      /***
87      * The paint method of the object to display.
88      * @param g the drawing area of the symbol.
89      */
90      public void paint(Graphics g) {
91          g.drawImage(symbol, insets.left, insets.top, observer);
92      }
93      
94      /***
95      * Returns the preferred size of the display.
96      */
97      public Dimension getPreferredSize() {
98  		MediaTracker tracker = new MediaTracker((Component) observer);
99          // Le -1 signifie que la largeur sera proportionnelle ? la hauteur.
100         symbol = symbolInit.getScaledInstance(-1, getHeight()- insets.top - insets.bottom, Image.SCALE_SMOOTH);
101         tracker.addImage(symbol, 0);
102         // On attend que toutes les images soient chargÈes.
103 		try {
104 		    tracker.waitForAll();
105 		}
106 		catch (InterruptedException e) {
107 		    System.out.print(e.toString());
108 		}
109 		        
110         tracker.removeImage(symbol);
111         return new Dimension(symbol.getWidth(observer) + insets.left + insets.right, getHeight());
112     }
113     
114     /***
115     * Returns the image of the symbol.
116     */
117     public Image getSymbol() {
118         return symbol;
119     }
120 }