View Javadoc

1   package fr.ove.openmath.jome.ctrlview.bidim;
2   
3   import java.awt.*;
4   import fr.ove.openmath.jome.ctrlview.bidim.*;
5   import fr.ove.openmath.jome.ctrlview.bidim.selection.events.SelectionEvent;
6   import fr.ove.openmath.jome.model.*;
7   
8   /***
9   * A layout manager that lays components for the variables of differentiation.<BR>
10  * We assume the display laid out contains what should be a variable for the visualisation of a differentiation,
11  * i.e. a variable (differentiation of first order) or a power of a variable (differentiation of higher order).<BR>
12  * According to the type of differentiation, either the symbol "d" is added (differentiation) or the symbol delta (the
13  * greek letter) is added (partial differentiation) to the display laid out, for correct rendering.
14  *
15  * @author © 1999 DIRAT Laurent
16  * @version 2.0 15/10/1999
17  */
18  public class VarDiffLayout extends HorizontalLayout {
19      /***
20      * <CODE>true</CODE> if we are dealing with a partial derivative. <CODEW>false</CODE> otherwise.
21      */
22      private boolean isPartial;
23      
24      /***
25      * The constructor.
26      * @param isPartial <CODE>true</CODE> if we have a partial differentiation. <CODE>false</CODE> otherwise.
27      */
28      public VarDiffLayout(boolean isPartial) {
29          this.isPartial = isPartial;
30      }
31      
32      /***
33      * According to the operator, the layout manager has to add some components (e.g. brackets, ...)
34      * or has to perform some "re-oganisation" before rendering.<BR>
35      * As soon as the layout manager is set to the display, this mehtod MUST be called with the display laid out
36      * as parameter. This method serves as well as a registering method. So all sub-classes of the instance MUST
37      * call super.initDisplay(displayToLay).
38      * @param displayToLay the display laid by the instance
39      */
40      public void initDisplay(Display displayToLay) {
41          super.initDisplay(displayToLay);
42  
43          FormulaTreeStructure fts = (FormulaTreeStructure) this.displayToLay.getListener();
44          
45          Display d;
46          // Si dÈriveÈ partielle, on ajoute un SymbolDisplay avec un delta. Sinon, un StringDisplay avec un "d"
47          if (isPartial) {
48              d = new SymbolDisplay(this.displayToLay.getGraphicContext(), new ImageSymbol("delta", this.displayToLay));
49              // Le display est le display d'un opÈrateur (on peut le considÈrer comme tel)
50              d.setIsSymbolOperatorDisplay(true);
51          }
52          else {
53              d = new StringDisplay(this.displayToLay.getGraphicContext(), "d", true);
54          }
55          
56          this.displayToLay.add(d, d, 0);
57          
58          // On met un listener au display du d/delta
59          // En fait, il n'y en a pas besoin, dans le sens o? il n'y a pas spÈcifiquement de fts qui
60          // Ècoute le comportement de ce display. NÈanmoins, il s'avËre nÈcessaire qu'il en ait
61          // un, par exemple lors de l'iconification, car c'est le display qui reÁoit la demande
62          // d'iconification qui envoie l'ÈvÈnement correspondant ? la FTS. Or si ce display n'a pas
63          // d'Ècouteur, alors pb. Par cohÈrence, l'Ècouteur du display d'opÈrateur, est le fts qui
64          // reprÈsente cette opÈration. Par contre, la fts en question, n'Ècoute pas le display
65          // d'opÈrateur.
66          d.addControlListener(fts);
67      }
68      
69      /***
70      * Checks the validity of the selection.
71      */
72      public void validateSelection() {
73          SelectionEvent selEvt = new SelectionEvent(displayToLay);
74  
75          // La validitÈ de la sÈlection est triviale.
76          // Si un des displays est sÈlectionnÈ alors on sÈlectionne tout.
77          Display d = (Display) displayToLay.getComponent(0);
78          Display v = (Display) displayToLay.getComponent(1);
79          if (d.isSelected() || v.isSelected()) {
80              // SÈlectionne le display.
81              displayToLay.select();
82              // On purge la liste des ÈlÈments sÈlectionnÈs.
83              selEvt.setAction(SelectionEvent.PURGE, null);
84              displayToLay.fireSelectionEvent(selEvt);
85              // On y ajoute os parenthËses
86              selEvt.setAction(SelectionEvent.ADD, displayToLay);
87              displayToLay.fireSelectionEvent(selEvt);
88          }
89  
90          // On a vÈrifiÈ la validitÈ de la sÈlection de la puissance. On doit maitenant
91          // la contrÙler au niveau supÈrieur, au niveau du pËre.
92          Display display = displayToLay;
93          if (display.getParent() instanceof Display) {
94              display = (Display) display.getParent();
95              //if (!(display.getListener() instanceof Formula))
96              FormulaTreeStructure fts = (FormulaTreeStructure) display.getListener();
97              if (fts.getFather() != null)
98                  ((DisplayLayout) display.getLayout()).validateSelection();
99          }
100 
101         // On met ? jour l'affichage.
102         display.repaint();
103     }
104     
105     /***
106     * Checks the validity of the deselection.
107     * @param display the display to deselect.
108     */
109     public void validateDeselection(Display display) {
110         Display father = displayToLay;
111         Display tmp;
112         SelectionEvent selEvt = new SelectionEvent(father);
113         
114         // En fait, comme quand "d/delta" ou la variable (puissance ou pas) est sÈlectionnÈ on sÈlectionne tout,
115         // quand on dÈsÈlectionne l'un ou l'autre, on dÈsÈlectionne tout.
116         if (father.isSelected()) {
117             father.deselect();
118             // On enlËve le display pËre de la liste des display sÈlectionnÈs.
119             selEvt.setAction(SelectionEvent.REMOVE, father);
120             father.fireSelectionEvent(selEvt);            
121             
122             // Comme pour la sÈlection, on contrÙle la validitÈ de la dÈsÈlection.
123             if (father.getParent() instanceof Display) {
124                 father = (Display) father.getParent();
125                 //if (!(display.getListener() instanceof Formula))
126                 FormulaTreeStructure fts = (FormulaTreeStructure) father.getListener();
127                 if (fts.getFather() != null)
128                     ((DisplayLayout) father.getLayout()).validateDeselection(displayToLay);
129             }
130             
131             // HÈ oui, on contrÙle la validitÈ de la sÈlection... dans une dÈsÈlection.
132             // Toujours le mÍme pb, est-ce que le nouvel Ètat de la sÈlection (aprËs
133             // dÈsÈlection donc) est syntaxiquement cohÈrent ?
134             validateSelection();
135             
136             // On met ? jour l'affichage.
137             father.repaint();
138         }        
139     }
140         
141     /***
142     * The display needs to be rebuilt. We do this.
143     */
144     public void rebuildDisplay() {
145         // La taille des displays est probablement diffÈrente de ceux qui Ètaient
146         // prÈcÈdemment. On demande alors le recalcul des display ancÍtres.
147         displayToLay.computeAncestorsAttributes();
148     }
149 
150 }