View Javadoc

1   package fr.ove.openmath.jome.ctrlview.bidim;
2   
3   import java.awt.*;
4   import java.awt.event.*;
5   import java.text.*;
6   import fr.ove.utils.NumberUtils;
7   import fr.ove.openmath.jome.ctrlview.bidim.StringLayout;
8   import fr.ove.openmath.jome.ctrlview.bidim.Display;
9   import fr.ove.openmath.jome.ctrlview.bidim.GraphicContext;
10  import fr.ove.openmath.jome.model.*;
11  import fr.ove.openmath.jome.model.events.ModelEvent;
12  
13  /***
14  * The display of a String.
15  *
16  * @author © 1999 DIRAT Laurent
17  * @version 2.0  04/08/1999
18  */
19  public abstract class AbstractStringDisplay extends BidimDisplay {
20      /***
21      * The string that the display represents.
22      */
23      private String string;
24          
25      /***
26      * The constructor.
27      * @param graphicContext the graphic context of the display.
28      * @param string the string to display
29      */
30      public AbstractStringDisplay(GraphicContext graphicContext, String string) {
31          super(graphicContext);
32          this.string = string;
33      }
34      
35      /***
36      * Sets a new string to the display.
37      * @param string the string to set.
38      */
39      public void setString( String string ) {
40          this.string = string;
41      }
42      
43      /***
44      * Returns the string to display.
45      * @return the string.
46      */
47      public String getString() {
48          return string;
49      }
50      
51      /***
52      * Scales the display.
53      */
54      public void scaleDisplay() {
55          //System.out.println("string : " + string +"   Level = " + getLevel());
56          setTheFont(getGraphicContext().scaleFont(getLevel()));
57          // Il faut que le layout manager recalcule les attributs du display.
58          setComputeAttributes(true);
59          // Ceci est nÈcessaire pour que ce soit le layout manager qui retourne
60          // la preferredSize du display, et donc qu'il y ait recalcul des attributs.
61          // Sinon, le container considËre qu'il a une taille valide, et donc retourne
62          // comme preferredSize, la prÈcÈdente, sans qu'il demande le recalcul au LM.
63          invalidate();
64      }    
65  
66      /***
67      * Paints the display.
68      * @param g where we paint the display.
69      */
70      public void paint(Graphics g) {
71          super.paint(g);
72          
73          Color old = g.getColor();
74          Font oldFont = g.getFont();
75          Rectangle bounds = getBounds();
76          
77          if (isSelected())
78              g.setColor(getSelectionColor());
79          else
80              g.setColor(getBackgroundColor());
81              
82          g.fillRect(0, 0, bounds.width /*- 1*/, bounds.height /*- 1*/);
83          
84          g.setColor(getForegroundColor());
85          g.setFont(getFont());
86          
87          if (weDrawBounds())
88              g.drawRect(0, 0, bounds.width - 1, bounds.height - 1);
89          
90          
91          if ( string.equals("*") )
92              g.drawString(string, 1, getHeight());
93          else if ( string.equals("-") )
94              g.drawString(string, 1, getAscent() - (int) Math.round(getDescent()*(40.0f/100.0f)));
95          else
96              g.drawString(string, 1, getAscent());
97          
98          /*
99          // on trace la baseline
100         g.setColor(Color.red);
101         g.drawLine(0, getAscent(), bounds.width, getAscent());
102         */
103         
104         g.setColor(old);
105         g.setFont(oldFont);
106     }
107     
108     /***
109     * Returns the insertion position for the moving of displays.
110     * @param mouseX the mouse position.
111     */
112     public int computeInsertionPosition(int mouseX) {
113         int insertionPosition;
114         // Si l'instance courante est un display d'opÈrateur, alors la position
115         // d'insertion est la position de l'opÈrande dont le display suit l'instance
116         // courante.
117         if (isSymbolOperatorDisplay()) {
118             Display display = (Display) getParent();
119             display = (Display) display.getComponent(getRank() + 1);  // On est s?r de pouvoir prendre rank+1
120             // puisque si on est sur une display d'opÈrateur, au pire on a un template.
121             insertionPosition = ((FormulaTreeStructure) display.getListener()).getRank();
122         }
123         else {
124             // On regarde la position de la souris.
125             // Si elle est infÈrieure ? la moitiÈ de la largeur de l'instance, alors
126             // la position d'insertion est la position de l'opÈrande dont l'instance est
127             // le display. Sinon, la position d'insertion est la position de l'opÈrande 
128             // dont l'instance l'instance est le display + 1.
129             if (mouseX <= (getWidth() / 2))
130                 insertionPosition = ((FormulaTreeStructure) getListener()).getRank();
131             else
132                 insertionPosition = ((FormulaTreeStructure) getListener()).getRank() + 1;
133                 // Pas de pb de test pour savoir si on n'est pas sur le dernier opÈrande
134                 // puisqu'on est obligÈ dans ce cas de retourner le nbre rÈel + 1 pour dire
135                 // effectivement que l'on insËre ? la derniËre position.
136         }
137         // On retourne la position d'insertion calculÈe.
138         return insertionPosition;
139     }
140     
141     public void ToString() {
142         super.ToString();
143         System.out.println(" string = " + string);
144     }
145 
146     public void whoAmI() {
147         System.out.println("I am the StringDisplay : " + string);
148         System.out.println("My level is " + getLevel());
149     }
150     
151 }