View Javadoc

1   package fr.ove.openmath.jome.model;
2   
3   import java.util.*;
4   import fr.ove.openmath.jome.model.*;
5   import fr.ove.openmath.jome.model.events.*;
6   import fr.ove.openmath.jome.ctrlview.events.*;
7   import fr.ove.openmath.jome.model.evaluation.*;
8   
9   /***
10  * This is an fictive operator which replaces a real part (real in a syntactical 
11  * mathematical meaning) of the formula  by an icon.<BR>
12  *
13  * <CODE>Icon</CODE> represents a node in the formula tree structure.<BR>
14  * Its children are the iconified parts of the formula.
15  *
16  * @author © 1998 DIRAT Laurent
17  * @version 1.0  23/07/98
18  */
19  public class Icon extends FormulaTreeStructure {
20      /***
21      * The icon name.
22      */
23      private String iconName = null;    // Le nom de l'icone
24      
25      /***
26      * The list of iconified elements of the formula.
27      */
28      private Vector iconified = new Vector();
29      
30      /***
31      * The rank in the FTS of the iconified elements.
32      */
33      private Vector rankOfIconified = new Vector(); // On en a besoin si on veut lors de la dÈisconification
34      // essayer de garder la position initiale de chaque ÈlÈment iconifiÈ. En effet, ? chaque fois 
35      // que l'on supprime/ajoute un ÈlÈment de/dans la FTS, le rang des ÈlÈments est mis ? jour, et
36      // comme iconified est une liste qui contient des rÈfÈrences sur des ÈlÈments de la FTS, quand
37      // le rang des ÈlÈments de la FTS est mis ? jour, le rang des ÈlÈments de iconified est mis ?
38      // jour Ègalement. Donc on perd la "VRAI" position initiale.
39      
40      /***
41      * Sets if the icon represents a substitution or not.<BR>
42      * The default is no, this means that the iconification process is an "automatic" one,
43      * i.e. the icon represents what is iconified.<BR>
44      * The substitution is when the user want to replace a selection by a friendly term.
45      * (Ex : Let a+b be EXP, the expression a+b+c becomes EXP+c)
46      */
47      private boolean isSubstitution = false;
48      
49      /***
50      * The Constructor.<BR>
51      * @param toIconify the element (or part of the element, according to the case) to iconify
52      * in order to get the information for the icon name
53      * 
54      */
55      public Icon(FormulaTreeStructure toIconify) {
56          this(toIconify.getIconName());
57          isSubstitution = false;
58      }
59      
60      /***
61      * The Constructor.
62      * @param iconName the name of the icon
63      */
64      public Icon(String iconName) {
65          setResourceIdentifier("ICON");
66          setAsOperatorPriority(resourcesManager.getAsOperatorPriority("constantPriorities"));
67          setAsOperandPriority(resourcesManager.getAsOperandPriority("constantPriorities"));
68          isSubstitution = true;
69          this.iconName = iconName;
70      }
71      
72      /*** 
73      * Inserts the icon (node) in the formula tree structure.<BR>
74      * @param current the position in the formula tree where the operator is to be insert.
75      * @return the new current position int hte formula tree.
76      */
77      public FormulaTreeStructure insert(FormulaTreeStructure current) {
78          // La mÈthode utilisÈe pour l'insertion de l'icon est la mÍme, mais 
79          // son comportement diffËre dans l'ensemble de celui des autres fts.
80          // En effet current est la position d'insertion, puisque c'est ? sa 
81          // place, que l'instance courante va Ítre insÈrÈe.
82          // De plus la valeur de retour n'est pas utilisÈe.
83          
84          // On prend le pËre de current pou l'insertion.
85          FormulaTreeStructure father = (FormulaTreeStructure) current.getFather();
86          // On rÈcupËre le rank de current (i.e. la position d'insertion dans father)
87          int rank = current.getRank();
88          // On insËre notre icone (i.e l'instance courante)
89          father.addChild(this, rank);
90          // On parcourre toutes les fts inconifiÈes pour les enlever de father.
91          for (int i = 0; i < iconified.size(); i++)
92              father.removeChild((FormulaTreeStructure) iconified.elementAt(i));
93          
94          return null;
95      }
96      
97      /***
98      * To check is the instance is an operator.
99      * @return <CODE>true</CODE> if it is an operator. <CODE>false</CODE> otherwise.
100     */
101     public boolean isOperator() {
102         return false;
103     }
104         
105     /***
106     * The Creation of the corresponding linear expression of the formula.
107     */
108     public String createLinear(String linear) {
109         // Si le nombre d'ÈlÈments iconifiÈs est 1, alors on concatËne la forme linÈaire de cet ÈlÈment
110         // Sinon, c'est qu'on a iconifiÈ plusieurs opÈrandes d'une opÈration. On doit concatÈner la
111         // forme linÈaire de cette sous-expression.
112         
113         int nbIconified = iconified.size();
114         if (nbIconified == 1) 
115             linear = getIconified(0).createLinear(linear);
116         else {
117             FormulaTreeStructure father = (FormulaTreeStructure) getIconified(0).getFather();
118             // On duplique le father pour avoir la mÈthode createLinear
119             father = (FormulaTreeStructure) father.duplicate();
120             // On ajoute ? father les ÈlÈments iconifiÈs
121             for (int i = 0; i < nbIconified; i++)
122                 father.addChild(getIconified(i));
123             // On rÈcupËre la sous-expression
124             linear = father.createLinear(linear);
125         }
126         
127         return linear;
128     }
129     
130     /***
131     * Sets if the instance represents a substitution or not.
132     * @param isSubstitution <CODE>true</CODE> if the instance is a substitution.
133     * <CODE>false</CODE> otherwise.
134     */
135     public void setIsSubstitution(boolean isSubstitution) {
136         this.isSubstitution = isSubstitution;
137     }
138     
139     /***
140     * Returns <CODE>true</CODE> if the instance is a substitution.
141     * <CODE>false</CODE> otherwise.
142     */
143     public boolean isSubstitution() {
144         return isSubstitution;
145     }
146     
147     // On a redÈfini une liste d'opÈrateur fils (les opÈrateurs iconifiÈs) malgrË le fait que 
148     // l'on Ètende FormulaTreeStructure pour Èviter que l'insertion ne change le rang d'un 
149     // opÈrateur parmi les fils de son pËre pour que, lors de la dÈsiconification, il se retrouve
150     // ? la mÍme place. De plus, le fait de rajouter des fils comme si Icon Ètait un opÈrateur
151     // entraÓnerai la poursuite de la recherche dans l'arbre alors que l'on ne
152     // souhaite pas, cela poserait des problËmes lors de l'affichage.
153     
154     /*
155     * Adds into the list of the fts to iconify, a fts ... to iconify.
156     * @param fts a formula tree structure to iconify.
157     */
158     public void addIconified(FormulaTreeStructure fts) {
159         iconified.addElement(fts);
160         // On conserve le rang dans la FTS
161         rankOfIconified.addElement(new Integer(fts.getRank()));
162     }
163     
164     /***
165     * Returns the number of iconified operators.
166     * @return the number of iconified operators.
167     */
168     public int getNbIconified() {
169         return iconified.size();
170     }
171     
172     /***
173     * Returns the formula tree structure at the specified rank in the list of the fts to iconify.
174     * @param rank a rank into the list of fts to iconify.
175     * @return the fts at the specified rank.
176     */
177     public FormulaTreeStructure getIconified(int rank) {
178         return (FormulaTreeStructure) iconified.elementAt(rank);
179     }
180     
181     /***
182     * Returns the rank in the formula tree structure of the iconified element at the specified
183     * rank in the list of iconified elments, before iconification.
184     * @param rank a rank into the list of fts to iconify.
185     * @return the rank in the FTS of the fts at the specified rank.
186     */
187     public int getRankOfIconified(int rank) {
188         return ((Integer) rankOfIconified.elementAt(rank)).intValue();
189     }
190     
191     /***
192     * Evaluates the instance.
193     */
194     public String evaluate() {
195         // A voir ce qu'on fait pour plus tard
196         return "";
197     }
198 
199     
200     // *****************************************
201     // ImplÈmentation de l'interface Iconifiable
202     
203     /***
204     * @return <CODE>true</CODE> if the instance is an icon. <CODE>false</CODE> otherwise.
205     */
206     public boolean isIcon() {
207         return true;
208     }
209     
210     /***
211     * Associates an icon name to the instance.
212     * @param iconName the name of the icon
213     */
214     public void setIconName(String iconName) {
215         this.iconName = iconName;
216     }
217     
218     /***
219     * Returns the name of the icon associated to the instance.
220     *
221     * @returns The name of the icon, or <CODE>null</CODE> if there is no name
222     * associated.
223     */
224     public String getIconName() {
225         if (!isSubstitution) {
226             // Si le nombre d'ÈlÈments iconifiÈs est 1, alors le nom de l'icone est le nom
227             // de l'ÈlÈment ? iconifier.
228             // Sinon, c'est qu'on a ? iconifier plusieurs opÈrandes d'une opÈration, donc le nom
229             // de l'icone correspond ? celui de cette opÈration, donc au nom du pËre de l'instance.
230             if (iconified.size() == 1) 
231                 iconName = getIconified(0).getIconName();
232             else
233                 iconName = ((FormulaTreeStructure) getFather()).getIconName();
234         }
235         
236         return iconName;
237     }
238     
239     /***
240     * Returns <CODE>true</CODE> if the instance is iconifiable.
241     * <CODE>false</CODE> otherwise.
242     */
243     public boolean isIconifiable() {
244         return false;  // On n'iconifie pas une icone !!!
245     }
246     
247     /***
248     * Iconifies the instance.
249     */
250     public void iconify() {
251         // On n'iconifie pas une icone
252     }
253     
254     /***
255     * Uniconifies the instance.
256     */
257     public void uniconify() {
258         FormulaTreeStructure father = (FormulaTreeStructure) getFather();
259         // On enlËve notre icon (i.e. l'instance).
260         father.removeChild(this);
261         // On parcourre la liste des fts iconifiÈes pour les rÈinsÈrer ? leur position
262         // avant iconification. (heuu... ce sera leur ancienne position si aucune modif.
263         // de father n'a ÈtÈ faite, of course.)
264         int fatherNbChildren = father.getNbChildren();
265         int countIconified = iconified.size();
266         FormulaTreeStructure iconifiedFts = null;
267         int rankOfIconifiedFts;
268         for (int i = 0; i < countIconified; i++) {
269             iconifiedFts = (FormulaTreeStructure) iconified.elementAt(i);
270             rankOfIconifiedFts = getRankOfIconified(i);
271             if (rankOfIconifiedFts > fatherNbChildren)
272                 father.addChild(iconifiedFts);
273             else
274                 father.addChild(iconifiedFts, rankOfIconifiedFts);
275                 
276             // On a ajoutÈ une fts anciennement iconifiÈe, on incrÈmente donc le nbre
277             // d'enfant du pËre
278             fatherNbChildren++;
279         }
280     }
281     
282     // *** Fin de l'interface Iconifiable ***
283     // **************************************
284 }