View Javadoc

1   /*
2   $Id: Bracket.java 710 2005-03-21 16:34:22Z guest $
3   */
4   
5   
6   /*
7   Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  For further information on the GNU Lesser General Public License,
24  see: http://www.gnu.org/copyleft/lesser.html
25  For further information on this library, contact: mainline@essi.fr
26  */
27  
28  
29  package fr.ove.openmath.jome.model;
30  
31  import java.util.*;
32  import fr.ove.openmath.jome.model.FormulaTreeStructure;
33  import fr.ove.openmath.jome.model.events.ModelEvent;
34  import fr.ove.openmath.jome.model.evaluation.*;
35  
36  /***
37  * The operator bracket.
38  *
39  * @author © 1999 DIRAT Laurent
40  * @version 2.0  25/06/1999
41  */
42  public class Bracket extends UnaryOperator {
43      private boolean isVisible = true;
44      
45      /***
46      * The Constructor.
47      */
48      public Bracket() {
49          setResourceIdentifier("OPEN_PAREN" /*"BRACKET"*/);
50          setValue("(");
51          setAsOperatorPriority(resourcesManager.getAsOperatorPriority("bracketPriorities"));
52          setAsOperandPriority(resourcesManager.getAsOperandPriority("bracketPriorities"));
53      }
54      
55      /***
56      * Returns the father of the node which, from the current instance, have the specified
57      * priority.
58      *
59      * @param priority the specified priority.
60      * @return the desired father.
61      */
62      public FormulaTreeStructure goTo(int priority) {
63          FormulaTreeStructure current = this;
64  
65          // Si le test est vrai, c'est qu'on est dÈj? sur un tel noeud.
66          // Cela veut dire que l'on est en train de fermer des parenthËses successives
67          // Ex: ((2+3))
68          // Ce qui c'est passÈ: on a eu un goto avec la premiËre ). On se retrouve alors
69          // sur le noeud reprÈsentant les secondes parenthËses, c'est ? dire l'instance courante.
70          // Donc l'appel de ce goto a pour but de fermer les parenthËses reprÈsentÈes par l'instance
71          // courante. On retourne donc simplement le pËre. Sinon, on remonte.
72          if (current.getAsOperatorPriority() == priority)
73              return (FormulaTreeStructure) current.getFather();
74          else
75              current = (FormulaTreeStructure) current.getFather();
76  
77          while ((current.getAsOperatorPriority() != priority) &&
78                  (((FormulaTreeStructure)current.getFather()).getAsOperatorPriority() != 0))
79              current = (FormulaTreeStructure) current.getFather();
80  
81          return current;
82      }
83  
84      /***
85      * Inserts the operator instance in the formula tree, from the current insertion position.
86      * (checks the priorities and goes up in the tree if necessary).
87      *
88      * @param ope the current insertion position.
89      * @return the new insertion position.
90      */
91      public FormulaTreeStructure insert(FormulaTreeStructure current) {
92          // On recherche la position d'insertion de notre instance.
93          current = findLocation(current);
94  
95          if (current.getFather() == null) {
96              // On commence la saisie de la formule, on ajoute l'instance courante telle
97              // qu'elle dansla formule.
98              current.addChild(this);
99  
100         }
101         else { // 2 possibilitÈs s'offrent ? nous !!!
102             if ((current.getAsOperatorPriority() == resourcesManager.getAsOperatorPriority("constantPriorities")) &&
103                 current.isTemplate()) {
104                 // Le cas classique (on va mÍme dire normal !!!)
105                 // La position d'insertion est un template.
106                 // On doit remplacer le template par l'instance courante (nos parenthËses)
107                 int rank = current.getRank();
108                 FormulaTreeStructure father = (FormulaTreeStructure) current.getFather();
109                 father.addChild(this, rank);
110                 father.removeChild(current);
111 
112                 // On regarde quel est le type de father. Si c'est une Fraction, les parenthËses
113                 // ne sont pas visibles.
114                 if (father.getAsOperatorPriority() ==  resourcesManager.getAsOperatorPriority("dividePriorities"))
115                     isVisible = false;
116             }
117             else {
118                 // Le cas pas classique
119                 // La position d'insertion n'est pas un template.
120                 // On crÈÈ une multiplication implicite.
121                 current = (new HorizentalGroup()).insert(current);
122                 // On insËre nos parenthËses
123                 current = insert(current);
124    //            FormulaTreeStructure formula = (FormulaTreeStructure)current.getFather();
125      //          formula.addChild(current);
126                 // On retourne le dernier point d'insertion.
127                 return current;
128             }
129         }
130 
131         // On ajoute un template ? nos parenthËses.
132         VariableOrNumber template = new VariableOrNumber();
133         addChild(template);
134 
135         // On retourne le dernier point d'insertion.
136         return template;
137 
138     }
139     
140     /***
141     * The Creation of the corresponding linear expression of the formula.
142     */
143     public String createLinear(String linear) {
144         linear += "(" + ((FormulaTreeStructure) getChild(0)).createLinear("") + ")";
145 
146         return linear;
147     }
148 
149     /***
150     * Evaluates the instance.
151     */
152     public String evaluate() {
153         return "(" + ((FormulaTreeStructure) getChild(0)).evaluate() + ")";
154     }
155 
156     /***
157     * Returns <CODE>true</CODE> if the brackets have to be displayed.
158     * <CODE>false</CODE> otherwise.
159     */
160     public boolean isVisible() {
161         return isVisible;
162     }
163 
164     /***
165     * Sets if the brackets have to be displayed or not.
166     * @param isVisible <CODE>true</CODE> if we want it. <CODE>false</CODE> otherwise.
167     */
168     public void setIsVisible(boolean isVisible) {
169         this.isVisible = isVisible;
170     }
171     
172     /***
173     * Returns the name of the icon associated to the instance.<BR>
174     * The icon name is the name of the ressource identifier where "_Ico" added to the end.
175     * @returns The name of the icon, or <CODE>null</CODE> if there is no name
176     * associated.
177     */
178     public String getIconName() {
179         // On surcharge cette mÈthode pour que l'icÙne de la parenthËse soit en fait l'icÙne
180         // de ce qu'elle contient.
181         return ((FormulaTreeStructure) getChild(0)).getIconName();
182     }
183 }