View Javadoc

1   package fr.ove.openmath.jome.ctrlview.bidim;
2   
3   import java.awt.*;
4   import fr.ove.openmath.jome.ctrlview.bidim.Display;
5   import fr.ove.openmath.jome.ctrlview.bidim.Displayable;
6   import fr.ove.openmath.jome.ctrlview.bidim.SymbolLayout;
7   
8   /***
9   * The display of a symbol.
10  *
11  * @author © 1999 DIRAT Laurent
12  * @version 2.0  09/07/1999
13  */
14  public class SymbolDisplay extends BidimDisplay {
15      /***
16      * the symbol to display.
17      */
18      private Displayable symbol;
19      
20      /***
21      * The constructor.
22      * @param graphicContext the graphic context of the display.
23      * @param symbol The displayable symbol of the display.
24      */
25      public SymbolDisplay(GraphicContext graphicContext, Displayable symbol) {
26          this(graphicContext);
27          this.symbol = symbol;
28          symbol.setGraphicContext(graphicContext);
29      }
30      
31      /***
32      * The constructor.<BR>
33      * Instanciate a new display, with no displayable symbol associated. The symbol has to be set
34      * by hand.
35      * @param graphicContext the graphic context of the display.
36      */
37      public SymbolDisplay(GraphicContext graphicContext) {
38          super(graphicContext);
39          SymbolLayout layout = new SymbolLayout();
40          layout.initDisplay(this);
41          setLayout(layout);
42      }
43      
44      /***
45      * Sets a new symbol to the display.
46      * @param symbol the symbol to set.
47      */
48      public void setSymbol(Displayable symbol) {
49          this.symbol = symbol;
50          symbol.setGraphicContext(getGraphicContext());
51      }
52      
53      /***
54      * Returns the symbol which is in the dispay.
55      */
56      public Displayable getSymbol() {
57          return symbol;
58      }
59  
60      /***
61      * Paints the display.
62      * @param g where we paint the display.
63      */
64      public void paint(Graphics g) {
65          super.paint(g);
66          Color old = g.getColor();
67          Rectangle bounds = getBounds();
68          
69          if (isSelected()) {
70              g.setColor(getSelectionColor());
71              g.fillRect(0, 0, bounds.width - 1, bounds.height - 1);
72          }
73          
74          g.setColor(getForegroundColor());
75          
76          if (weDrawBounds())
77              g.drawRect(0, 0, bounds.width - 1, bounds.height - 1);
78              
79          symbol.paint(g);
80          
81          g.setColor(old);
82      }
83      
84      /***
85      * Scales the display.
86      */
87      public void scaleDisplay() {
88          setTheFont(getGraphicContext().scaleFont(getLevel()));
89          // Il faut que le layout manager recalcule les attributs du display.
90          setComputeAttributes(true);
91          // Ceci est nÈcessaire pour que ce soit le layout manager qui retourne
92          // la preferredSize du display, et donc qu'il y ait recalcul des attributs.
93          // Sinon, le container considËre qu'il a une taille valide, et donc retourne
94          // comme preferredSize, la prÈcÈdente, sans qu'il demande le recalcul au LM.
95          invalidate();
96          
97          // On affecte au symbol le nouveau contexte graphique du display, en fait la rÈduction de font
98          // effectuÈe.
99          // On fait ce test ? cause de la construction. Quand on construit le display, on fait un scaleDisplay.
100         // (au niveau de Display) Or, scaleDisplay (de l'instance donc), est applelÈ avant que le symbole ne soit
101         // affectÈ dans leconstructeur.
102         if (symbol != null)
103             symbol.setGraphicContext(getGraphicContext());
104     }
105     
106     /***
107     * Sets all the attributes.
108     * @param ascent the new ascent value.
109     * @param descent the new descent value.
110     * @param shiftX the new horizontal shift value.
111     * @param shiftY the new vertical shift value.
112     */
113     public void setAttributes(int ascent, int descent, int shiftX, int shiftY) {
114         super.setAttributes(ascent, descent, shiftX, shiftY);
115         symbol.setAscent(ascent);
116         symbol.setDescent(descent);
117         symbol.setShiftX(shiftX);
118         symbol.setShiftY(shiftY);
119     }
120     
121     /***
122     * Sets the graphic context of the instance.
123     * @param graphicContext the graphic context.
124     */
125     public void setGraphicContext(GraphicContext graphicContext) {
126         super.setGraphicContext(graphicContext);
127         symbol.setGraphicContext(graphicContext);
128     }
129 }
130