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 for variables and numbers.
15  *
16  * @author © 1999 DIRAT Laurent
17  * @version 2.0  04/08/1999
18  */
19  public class StringDisplay extends AbstractStringDisplay {
20      /***
21      * The constructor.
22      * @param graphicContext the graphic context of the display.
23      * @param string the string to display
24      * @param isSymbolOperatorDisplay <CODE>true</CODE> if the instance is the display of an operator.
25      * <CODE>false</CODE> otherwise.
26      */
27      public StringDisplay(GraphicContext graphicContext, String string, boolean isSymbolOperatorDisplay ) {
28          super(graphicContext, string);
29          setIsSymbolOperatorDisplay(isSymbolOperatorDisplay);
30          StringLayout layout = new StringLayout();
31          layout.initDisplay(this);
32          setLayout(layout);
33          
34          addMouseListener(
35              new MouseAdapter() {
36                  public void mouseEntered(MouseEvent e) {
37                      if (!isSymbolOperatorDisplay()) {
38                          if (getListener() instanceof VariableOrNumber) {
39                              VariableOrNumber listener = (VariableOrNumber) getListener();
40                              if (listener.isNumber() && !listener.isInteger()) {
41                                  // On a affaire ? un float
42                                  String value = listener.getValue();
43                                  
44                                  //if (!value.equals(StringDisplay.this.getString())) {
45                                  if (!value.equals(getString())) {
46                                      setString(value);
47                                      invalidate();
48                                      computeAncestorsAttributes();
49                                      Container container = StringDisplay.this;
50                                      while (container.getParent() != null)
51                                          container = container.getParent();
52                                          
53                                      container.validate();
54                                      container.repaint();
55                                  }
56                              }
57                          }
58                      }
59                  }
60                  
61                  public void mouseExited(MouseEvent e) {
62                      if (!isSymbolOperatorDisplay()) {
63                          if (getListener() instanceof VariableOrNumber) {
64                              VariableOrNumber listener = (VariableOrNumber) getListener();
65                              if (listener.isNumber() && !listener.isInteger()) {
66                                  // On a affaire ? un float
67                                  String value = listener.getValue();
68                                  String newString = NumberUtils.formatDouble(value);
69                                  
70                                  if (!newString.equals(value)) {
71                                      setString(newString);
72                                      StringDisplay.this.invalidate();
73                                      StringDisplay.this.computeAncestorsAttributes();
74                                      Container container = StringDisplay.this;
75                                      while (container.getParent() != null)
76                                          container = container.getParent();
77                                          
78                                      container.validate();
79                                      container.repaint();
80                                  }
81                              }
82                          }
83                      }
84                  }
85              }
86          );
87      }
88      
89      /***
90      * Consumes (i.e. treats) the event received from the model.
91      * @param modelEvent the event to consume.
92      */
93      public void consumeModelEvent(ModelEvent modelEvent) {
94          // En principe, c'est le seul ÈvÈnement que doit recevoir ce type de display
95          // De plus, forcÈment, le display reprÈsentÈ n'est pas le display d'un opÈrateur.
96          switch (modelEvent.getAction()) {
97              case ModelEvent.UPDATE :
98                  //System.out.println("ModelEvent.UPDATE : on update le StringDisplay");
99                  VariableOrNumber src = (VariableOrNumber) modelEvent.getSource();
100                 
101                 // On met ? jour le display.
102                 // Ca c'est pour la troncature de l'affichage d'une nombre flottant dÈpassant
103                 // 2 chiffres aprËs la virgule
104                 if (!isSymbolOperatorDisplay()) {
105                     if (src.isNumber() && !src.isInteger())
106                         // On a affaire ? un float
107                         setString(NumberUtils.formatDouble(src.getValue()));
108                     else
109                         setString(src.getValue());
110                 }
111                 else
112                     setString(src.getValue());
113 
114                 computeAncestorsAttributes();
115                 invalidate();
116         }
117     }
118 }