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   import fr.ove.utils.FontInfo;
8   
9   /***
10  * Layout manager that lays the display of an operator whose symbol is superposed to its operand.
11  *
12  * @author © 1999 DIRAT Laurent
13  * @version 2.0 15/12/1999
14  */
15  public abstract class SuperposedOperatorLayout extends VerticalCenteredLayout implements OperatorDisplayCreator {
16      /***
17      * Computes the size of the display according to its children size (if any),
18      * and its different attributes.
19      * @return the size of the display.
20      */
21      public Dimension computeAttributes() {
22          // C'est strictement la mÍme chose, sauf que VerticalCenteredLayout place l'ascent comme Ètant la moitiÈ
23          // de la hauteur du display. Or dans le cas qui nous occupe, l'ascent du display doit ce trouver sur celui
24          // de l'opÈrande
25          Dimension dim = super.computeAttributes();
26      
27          displayToLay.setAscent(((Display) displayToLay.getComponent(0)).getShiftY() +
28                    ((Display) displayToLay.getComponent(0)).getHeight() + 
29                    ((Display) displayToLay.getComponent(1)).getShiftY() +
30                    ((Display) displayToLay.getComponent(1)).getAscent());
31                    
32          displayToLay.setDescent(displayToLay.getHeight() - displayToLay.getAscent());
33      
34          return dim;
35      }
36      
37      /***
38      * According to the operator, the layout manager has to add some components (e.g. brackets, ...)
39      * or has to perform some "re-oganisation" before rendering.<BR>
40      * As soon as the layout manager is set to the display, this mehtod MUST be called with the display laid out
41      * as parameter. This method serves as well as a registering method. So all sub-classes of the instance MUST
42      * call super.initDisplay(displayToLay).
43      * @param displayToLay the display laid by the instance
44      */
45      public void initDisplay(Display displayToLay) {
46          super.initDisplay(displayToLay);
47          
48          // On rÈcupËre le fts associÈ (listener) au display.
49          FormulaTreeStructure fts = (FormulaTreeStructure) displayToLay.getListener();
50          
51          Display displayOperator = createOperatorDisplay();
52          
53          // On met un listener ? l'opÈrateur.
54          // En fait, il n'y en a pas besoin, dans le sens o? il n'y a pas spÈcifiquement de fts qui
55          // Ècoute le comportement de ce display. NÈanmoins, il s'avËre nÈcessaire qu'il en ait
56          // un, par exemple lors de l'iconification, car c'est le display qui reÁoit la demande
57          // d'iconification qui envoie l'ÈvÈnement correspondant ? la FTS. Or si ce display n'a pas
58          // d'Ècouteur, alors pb. Par cohÈrence, l'Ècouteur du display d'opÈrateur, est le fts qui
59          // reprÈsente cette opÈration. Par contre, la fts en question, n'Ècoute pas le display
60          // d'opÈrateur.
61          displayOperator.addControlListener(fts);
62          
63          // On ajoute le display d'opÈrateur en premiËre position
64          // A priori, display ne contient rien, donc un add(displayOperator) suffit.
65          this.displayToLay.add(displayOperator);
66          
67          this.displayToLay.computeAncestorsAttributes();
68      }
69      
70      /***
71      * Checks the validity of the selection.
72      */
73      public void validateSelection() {
74          SelectionEvent selEvt = new SelectionEvent(displayToLay);
75  
76          // La validitÈ de la sÈlection est triviale.
77          // Si l'opÈrateur est slÈectionnÈ, alors on sÈlectionne tout.
78          Display displayOperator = (Display) displayToLay.getComponent(0);
79          if (displayOperator.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              FormulaTreeStructure fts = (FormulaTreeStructure) display.getListener();
96              if (fts.getFather() != null)
97                  ((DisplayLayout) display.getLayout()).validateSelection();
98          }
99  
100         // On met ? jour l'affichage.
101         display.repaint();
102     }
103     
104     /***
105     * Checks the validity of the deselection.
106     * @param display the display to deselect.
107     */
108     public void validateDeselection(Display display) {
109         Display father = displayToLay;
110         SelectionEvent selEvt = new SelectionEvent(father);
111         
112         // Si l'opÈrateur est sÈlectionnÈ, alors il faut le dÈselectionner.
113         if (father.isSelected()) {
114             father.setNotSelected();
115             // On enlËve le display pËre de la liste des display sÈlectionnÈs.
116             selEvt.setAction(SelectionEvent.REMOVE, father);
117             father.fireSelectionEvent(selEvt);
118             
119             Display displayOperator = (Display) displayToLay.getComponent(0);
120             Display argument = (Display) displayToLay.getComponent(1);
121             
122             if (display == displayOperator) {
123                 displayOperator.setNotSelected();
124                 selEvt.setAction(SelectionEvent.ADD, argument);
125                 father.fireSelectionEvent(selEvt);
126             }
127             else {
128                 displayOperator.setNotSelected();
129                 argument.deselect();
130             }
131 
132             // Comme pour la sÈlection, on contrÙle la validitÈ de la dÈsÈlection.
133             if (father.getParent() instanceof Display) {
134                 father = (Display) father.getParent();
135                 FormulaTreeStructure fts = (FormulaTreeStructure) father.getListener();
136                 if (fts.getFather() != null)
137                     ((DisplayLayout) father.getLayout()).validateDeselection(displayToLay);
138             }
139             
140             // HÈ oui, on contrÙle la validitÈ de la sÈlection... dans une dÈsÈlection.
141             // Toujours le mÍme pb, est-ce que le nouvel Ètat de la sÈlection (aprËs
142             // dÈsÈlection donc) est syntaxiquement cohÈrent ?
143             validateSelection();
144             
145             // On met ? jour l'affichage.
146             father.repaint();
147         }        
148 
149     }
150     
151     /***
152     * The display needs to be rebuilt. We do this.
153     */
154     public void rebuildDisplay() {
155         // La taille des displays est probablement diffÈrente de ceux qui Ètaient
156         // prÈcÈdemment. On demande alors le recalcul des display ancÍtres.
157         displayToLay.computeAncestorsAttributes();
158     }
159 }