View Javadoc

1   package fr.ove.openmath.jome.model.processor;
2   
3   import fr.ove.openmath.jome.model.*;
4   import fr.ove.openmath.jome.model.events.*;
5   import java.util.*;
6   import java.awt.*;
7   
8   /***
9   * A processor for the width of the formula.<BR>
10  * What is called width corresponds to the number of operand of an operator. (i.e. the number
11  * of children of a node in the formula tree stucture.<BR>
12  * The processor only displays a specified amount of operand of an operator whose number of operands
13  * is greater than a specified value. The other operands are iconified.
14  *
15  * @author © 2000 DIRAT Laurent
16  * @version 1.0  21/08/2000
17  */
18  public class WidthProcessor extends ProcessorImpl implements WidthProcessorInterface {
19      /***
20      * The list of widths numbers in the formula.
21      */
22      private Vector widthList = new Vector();
23      
24      /***
25      * Check if the current list of Strahler numbers in the formula is valid or not.
26      */
27      private boolean isListValid = false;
28      
29      /***
30      * The number of children viewed.<BR>
31      * Should be a positive value. If equals to -1, then all children have to be displayed.
32      * This is the default value.
33      */
34      private int view = -1;
35              
36      /***
37      * The constructor.
38      */
39      public WidthProcessor(Formula formula) {
40          super(formula);
41      }
42      
43      /***
44      * Sets the width level form which the processing will be considering.<BR>
45      * For convenience only, in most cases this method is equivalent to the
46      * @see setLevel of the @see Processor interface.
47      * @param widthLevel the width level.
48      */
49      public void setWidthLevel(int widthLevel) {
50          setLevel(widthLevel);
51      }
52      
53      /***
54      * Returns the width level form which the processing will be considering.<BR>
55      * For convenience only, in most cases this method is equivalent to the
56      * @see getLevel of the @see Processor interface.
57      */
58      public int getWidthLevel() {
59          return getLevel();
60      }
61      
62      /***
63      * Sets the number of children viewed.
64      * @param the number of children viewed.
65      */
66      public void setView(int view) {
67          this.view = view;
68      }
69      
70      /***
71      * Returns the number of children viewed.
72      */
73      public int getView() {
74          return view;
75      }
76      
77      
78      /***
79      * Returns the list of widths of the formula.
80      */
81      public Vector getWidthList() {
82          if (!isListValid) {
83              buildWidthList(getFormula());
84              isListValid = true;
85          }
86          return widthList;
87      }
88      
89      /***
90      * Returns the biggest width of the formula.
91      */
92      public int getBiggestWidth() {
93          return ((Integer) widthList.lastElement()).intValue();
94      }
95      
96      /***
97      * Does processing.
98      */
99      public void doProcess() {
100         Formula formula = getFormula();
101         
102         if (formula != null) {
103             switch (getProcessingId()) {
104                 case COMPUTE :
105                     // La liste des Strahler n'est (a priori) plus ? jour.
106                     buildWidthList(formula);
107                     break;
108                 case SIMPLE_ICONIFICATION :
109                     // Iconifie les parties de la formule qui ont un nombre de Strahler infÈrieur
110                     // ? celui spÈcifiÈ.
111                     setUpdateDisplay(true);
112                     iconifyTheFTS(formula, getLevel());
113                     setUpdateDisplay(false);
114                     break;
115                 case RECURSIVE_ICONIFICATION :
116                     // Iconifie les parties de la formule qui ont un nombre de Strahler infÈrieur
117                     // ? celui spÈcifiÈ. Iconification rÈcursive.
118                     setUpdateDisplay(true);
119                     iconifyAllTheFTS(formula, getLevel());
120                     setUpdateDisplay(false);
121                     break;
122                 case COMPUTE_AND_ICONIFY :
123                     // La liste des Strahler n'est (a priori) plus ? jour.
124                     buildWidthList(formula);
125                     // Iconifie les parties de la formule qui ont un nombre de Strahler infÈrieur
126                     // ? celui spÈcifiÈ. Iconification rÈcursive.
127                     // Le defaut est que l'on icionifie ? une valeur correspondant ? peu prËs ? la
128                     // valeur moyenne du Strahler max calculÈ.
129                     int width = getBiggestWidth() / 2;
130                     // Maintenant on regarde dans la liste des width, le width valide immÈdiatement
131                     // supÈrieur ? la valeur correpsondant ? la moitiÈ du plus grand.
132                     int count = widthList.size();
133                     int tmp;
134                     for (int i = 0; i < count; i++ ) {
135                         tmp = ((Integer) widthList.elementAt(i)).intValue();
136                         if (tmp > width) {
137                             width = tmp;
138                             break;
139                         }
140                         
141                     }
142                     setLevel(width);
143                     view = width / 2; // On ne visualise que la moitiÈ de fils de l'opÈrateur
144                     iconifyAllTheFTS(formula, width);
145             }
146         }
147     }
148     
149     /***
150     * Iconifies the descendance of the instance which has a widht number greater
151     * than the specified.
152     * @param width the specified width.
153     */
154     private void iconifyTheFTS(FormulaTreeStructure fts, int width) {
155         // On parcourt tous les enfants de l'instance et on descend dans toute l'arboresnce
156         // jusqu'au feuilles. (DFS) ArrivÈe ? la feuille, on teste la valeur du strahler et si
157         // elle est infÈrieure ? celle spÈcifiÈe, on iconifie.
158         // On rÈitËre le process sur tous les enfants.
159         FormulaTreeStructure child = null;
160         int ftsWidth = fts.getNbChildren();
161         if ((ftsWidth > 0) && (ftsWidth >= width))
162             iconify(fts, width);
163                         
164         int count = fts.getNbChildren();
165         for (int i = 0; i < count; i++) 
166             iconifyTheFTS((FormulaTreeStructure) fts.getChild(i), width);
167     }
168     
169     /***
170     * Iconifies all the descendance of the instance which has a width greater
171     * than the specified.
172     * @param width the specified width.
173     */
174     private void iconifyAllTheFTS(FormulaTreeStructure fts, int width) {
175         // On parcourt tous les enfants de l'instance et on descend dans toute l'arboresnce
176         // jusqu'au feuilles. (DFS) ArrivÈe ? la feuille, on teste la valeur du strahler et si
177         // elle est infÈrieure ? celle spÈcifiÈe, on iconifie.
178         // On rÈitËre le process sur tous les enfants.
179         int ftsWidth = fts.getNbChildren();
180         if (ftsWidth > 0) {
181             for (Enumeration e = fts.getChildren().elements(); e.hasMoreElements(); ) 
182                 iconifyAllTheFTS((FormulaTreeStructure) e.nextElement(), width);
183                 
184             if (ftsWidth >= width)
185                 iconify(fts, width);
186         }
187     }
188     
189     /***
190     * Iconifie the instance if it has a width greater than the specified.
191     * @param width the specified width.
192     */
193     private void iconify(FormulaTreeStructure fts, int width) {
194         int count = fts.getNbChildren();
195         Icon icon = new Icon(fts);
196         // On ajoute notre instance ? iconifier dans Icon (<=> on iconfie l'instance)
197         FormulaTreeStructure child;
198         // On ajout notre icone
199         fts.addChild(icon);
200         /***
201         for (int i = view; i < count; ) {
202             child = (FormulaTreeStructure) fts.getChild(i);
203             icon.addIconified(child);
204             //fts.removeChild(child);
205             count--;
206         }*/
207         
208         for (int i = view; i < count; i++)
209             icon.addIconified((FormulaTreeStructure) fts.getChild(i));
210             
211         for (int i = view; i < count; ) {
212             fts.removeChild((FormulaTreeStructure) fts.getChild(i));
213             count--;
214         }
215         
216         if (getUpdateDisplay()) {
217             // On ajoute un display pour notre icon.
218             ModelEvent modelEvent = new ModelEvent(fts);
219             modelEvent.setAction(ModelEvent.ADD, icon);
220             fts.fireModelEvent(modelEvent);
221         }
222     }
223     
224     /***
225     * Builds the list of widths of the formula.
226     */
227     private void buildWidthList(FormulaTreeStructure fts) {
228         int width = fts.getNbChildren();
229         
230         if (width > 1)
231             addNewWidth(width);
232             
233         for (int i = 0; i < width; i++)
234             buildWidthList((FormulaTreeStructure) fts.getChild(i));
235     }
236     
237     /***
238     * Adds the specified width value in the list if necessary
239     * @param width the width value to add
240     */
241     private void addNewWidth(int width) {
242         int currentWidth;
243         
244         if (widthList.size() == 0)
245             widthList.addElement(new Integer(width));
246         else {
247             for (int i = 0; i < widthList.size(); i++) {
248                 currentWidth = ((Integer) widthList.elementAt(i)).intValue();
249                 if (width == currentWidth)
250                     return;
251                 else if (width < currentWidth) {
252                     widthList.insertElementAt(new Integer(width), i);
253                     return;
254                 }
255             }
256             widthList.addElement(new Integer(width));
257         }
258     }
259     
260     public static void main(String args[]) {
261         String exp = "a*z*e*r*r*t*(t+2+2+2+2+2+2+2+6+6+6+6+6+6)*1*6*6*6*6*6*6+2+3+4+5+6+7+8+9+10+11+12+13+14";
262         Formula formula = new Formula();
263         formula.setDoProcessing(true);
264         
265         WidthProcessor p = new WidthProcessor(formula);
266         formula.setProcessor(p);
267         
268         fr.ove.openmath.jome.ctrlview.bidim.FormulaDisplay display = new fr.ove.openmath.jome.ctrlview.bidim.FormulaDisplay();
269         formula.addModelListener(display);
270         display.addControlListener(formula);
271         
272         java.awt.Frame f = new java.awt.Frame();
273         f.setLayout(new java.awt.BorderLayout());
274         f.setBounds(50, 50, 250, 80);
275         f.add("Center", display);
276         f.setVisible(true);
277         
278         fr.ove.openmath.jome.ctrl.linear.LinearParser linearParser = new fr.ove.openmath.jome.ctrl.linear.LinearParser();
279         linearParser.addLinearParserListener(formula);
280         linearParser.parse(exp);
281         System.out.println("la formule saisie est : \t\t" + exp);
282         System.out.println("la formule construite est : \t\t" + formula.getLinear());
283     }    
284 }