View Javadoc

1   /*
2   $Id: Jome.java 709 2005-03-21 16:30:05Z 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;
30  
31  import java.awt.*;
32  import java.util.Vector;
33  import java.io.*;
34  
35  import fr.ove.openmath.jome.model.*;
36  import fr.ove.openmath.jome.model.events.*;
37  import fr.ove.openmath.jome.ctrlview.events.*;
38  import fr.ove.openmath.jome.ctrlview.bidim.*;
39  import fr.ove.openmath.jome.ctrlview.bidim.selection.events.*;
40  import fr.ove.openmath.jome.ctrlview.bidim.selection.*;
41  import fr.ove.openmath.jome.ctrlview.bidim.images.ImageLoader;
42  import fr.ove.openmath.jome.ctrl.om.*;
43  import fr.ove.openmath.jome.ctrl.mml.*;
44  import fr.ove.openmath.jome.ctrl.linear.*;
45  import fr.ove.openmath.OpenMathizable;
46  
47  
48  /***
49  *
50  */
51  public class Jome extends Container implements OpenMathizable {
52      /***
53      * The formula.
54      */
55      private Formula formula;
56  
57      /***
58      * fr.ove.openmath.jome display.
59      */
60      Display jomeDisplay;
61  
62      /***
63      * The default display of the formula.
64      */
65      FormulaDisplay formulaDisplay;
66  
67      /***
68      * The graphic context.
69      */
70      private GraphicContext graphicContext = new GraphicContext(
71              new Font("Times New Roman", Font.PLAIN, 14),
72              Color.black,
73              Color.white,
74              Color.yellow);
75  
76      LinearParser linearParser = new LinearParser();
77      OMParser omParser = new OMParser();
78      MathMLParser mmlParser = new MathMLParser();
79  
80      /**********************/
81      /* Les constructeurs */
82      /**********************/
83  
84      /***
85      * The default constructor.
86      */
87      public Jome() {
88          super();
89          setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
90          super.setFont(graphicContext.getFont());
91  
92          // CrÈation de la formule (modËle)
93          formula = new Formula();
94  
95          linearParser.addLinearParserListener(formula);
96          omParser.addLinearParserListener(formula);
97          mmlParser.addLinearParserListener(formula);
98  
99          // CrÈation du gestionnaire de la sÈlection
100         SelectionManager selectionManager = new SelectionManager();
101 
102         // CrÈation du display par dÈfaut de la formule (vue + contrÙle)
103         formulaDisplay = new FormulaDisplay();
104         formulaDisplay.setGraphicContext(graphicContext);
105         formulaDisplay.addControlListener(formula);
106         formulaDisplay.setShiftX(10);
107         formulaDisplay.setShiftY(10);
108         formula.addModelListener(formulaDisplay);
109 
110         // CrÈation du display de jome.
111         // C'est Ègalement un display, qui va contenir le display (par dÈfaut)
112         // de la formule.
113         jomeDisplay = new BidimDisplay(graphicContext);
114         jomeDisplay.drawBounds();
115         OneFormulaLayout layout = new OneFormulaLayout();
116         layout.initDisplay(jomeDisplay);
117         jomeDisplay.setLayout(layout);
118         jomeDisplay.addSelectionEventListener(selectionManager);
119         // On ajoute le display (par dÈfaut) de la formule dans le display de fr.ove.openmath.jome
120         jomeDisplay.add(formulaDisplay);
121 
122         // On ajoute de display de jome dans l'instance (i.e. en JOME)
123         add(jomeDisplay);
124 
125         // On crÈÈ la formule.
126         setLinear("");
127     }
128 
129     /***********************************************/
130     /* MÈthodes nÈcessaires ? cause de l'hÈritage */
131     /***********************************************/
132 
133     /***
134     * Returns the preferred size of the fr.ove.openmath.jome.
135     */
136     public Dimension getPreferredSize() {
137         return jomeDisplay.getPreferredSize();
138  //       return new Dimension(100,100);
139     }
140 
141     /***
142     * The paint method.
143     * @param g where the formula is painted.
144     */
145     public void paint(Graphics g) {
146         Color oldColor = g.getColor();
147         Color color = getBackground();
148         if (color != null) {
149             g.setColor(color);
150             Dimension size = getSize();
151             g.fillRect(0, 0, size.width, size.height);
152             g.setColor(oldColor);
153         }
154 
155         super.paint(g);
156     }
157 
158     /**************************/
159     /* Gestion de la formule */
160     /**************************/
161 
162     /***
163     * Creates a formula from the specified maple-like expression.
164     * @param exp the specified maple-like expression.
165     */
166     public void setLinear(String exp) {
167         linearParser.parse(exp);
168         jomeDisplay.invalidate();
169         jomeDisplay.setComputeAttributes(true);
170         validate();
171         repaint();
172     }
173 
174     /***
175     * Returns the linear expression of the formula.
176     */
177     public String getLinear() {
178         return formula.createLinear("");
179     }
180 
181     /***
182     * Creates a formula from the specified OpenMath Object.
183     * @param exp the specified OpenMath Object.
184     */
185     public void setOpenMath(String exp) {
186         omParser.parseOMObject(exp);
187         jomeDisplay.invalidate();
188         jomeDisplay.setComputeAttributes(true);
189         validate();
190         repaint();
191     }
192 
193     /***
194     * Returns the OpenMath representation of the formula.
195     */
196     public String getOpenMath() {
197         return formula.getOpenMath();
198     }
199 
200     /***
201     * Creates a formula from the specified MathML Object.
202     * @param exp the specified OpenMath Object.
203     */
204     public void setMathML(String exp) {
205         mmlParser.parseMathMLObject(exp);
206         jomeDisplay.invalidate();
207         jomeDisplay.setComputeAttributes(true);
208         validate();
209         repaint();
210     }
211 
212     /***
213     * Returns the MathML representation of the formula.
214     */
215     public String getMathML() {
216         return formula.getMathML();
217     }
218     
219     public String getPMML() {
220     	return formula.getPMML();
221     }
222 
223     /****************************/
224     /* Gestion de la sÈlection */
225     /****************************/
226 
227     /***
228     * Returns the linear expression of the selection.
229     */
230     public String getLinearSelection() {
231         return formulaDisplay.getLinearSelection();
232     }
233 
234     /***
235     * Returns the OpenMath expression of the selection.
236     */
237     public String getOpenMathSelection() {
238         return formulaDisplay.getOpenMathSelection();
239     }
240 
241     /***
242     * Returns the MathML expression of the selection.
243     */
244     public String getMathMLSelection() {
245         return formulaDisplay.getMathMLSelection();
246     }
247 
248     /**********************************************************************/
249     /* DÈcalage de l'affichage de la formule ? l'intÈrieur de la fenÍtre */
250     /**********************************************************************/
251 
252     /***
253     * Sets a horizontal shift to the display of the formula.
254     * @param shiftY the vertical shift.
255     */
256     public void setShiftX(int shiftX) {
257         shiftX = (shiftX < 1) ? 1 : shiftX;
258         formulaDisplay.setShiftX(shiftX);
259         jomeDisplay.invalidate();
260         jomeDisplay.setComputeAttributes(true);
261         validate();
262         repaint();
263     }
264 
265     /***
266     * Returns the horizontal shift.
267     * @return the horizontal shift.
268     */
269     public int getShiftX() {
270         return formulaDisplay.getShiftX();
271     }
272 
273     /***
274     * Sets a vertical shift to the display of the formula.
275     * @param shiftY the vertical shift.
276     */
277     public void setShiftY(int shiftY) {
278         shiftY = (shiftY < 1) ? 1 : shiftY;
279         formulaDisplay.setShiftY(shiftY);
280         jomeDisplay.invalidate();
281         jomeDisplay.setComputeAttributes(true);
282         validate();
283         repaint();
284     }
285 
286     /***
287     * Returns the vertical shift.
288     * @return the vertical shift.
289     */
290     public int getShiftY() {
291         return formulaDisplay.getShiftY();
292     }
293 
294 
295     /***************************************/
296     /* Manipulation du contexte graphique */
297     /***************************************/
298 
299     /***
300     * Sets a new font to the context.
301     * @param font the new font.
302     */
303     public void setFont(Font font) {
304         graphicContext.setFont(font);
305         formulaDisplay.setFont(font);
306         jomeDisplay.invalidate();
307         jomeDisplay.setComputeAttributes(true);
308         validate();
309 
310         super.setFont(font);
311     }
312 
313     /***
314     * Sets a new color to the context.
315     * @param color the new color.
316     */
317     public void setForegroundColor(Color foregroundColor) {
318         graphicContext.setForegroundColor(foregroundColor);
319         repaint();
320     }
321 
322     /***
323     * Returns the foreground color used.
324     */
325     public Color getForegroundColor() {
326         return graphicContext.getForegroundColor();
327     }
328 
329     /***
330     * Sets a new background color for the display.
331     * @param background the color used as background for the display.
332     */
333     public void setBackgroundColor(Color backgroundColor) {
334         graphicContext.setBackgroundColor(backgroundColor);
335         repaint();
336     }
337 
338     /***
339     * Returns the background color used.
340     */
341     public Color getBackgroundColor() {
342         return graphicContext.getBackgroundColor();
343     }
344 
345     /***
346     * Sets a new selection color to the context.
347     * @param selectionColor the new color.
348     */
349     public void setSelectionColor(Color selectionColor) {
350         graphicContext.setSelectionColor(selectionColor);
351         repaint();
352     }
353 
354     /***
355     * Returns the selection color used.
356     */
357     public Color getSelectionColor() {
358         return graphicContext.getSelectionColor();
359     }
360 
361     /***
362     * Sets if we draw the bounds of fr.ove.openmath.jome.
363     * @param drawBounds <CODE>true</CODE> if we want the bounds to be drawn.
364     * <CODE>false</CODE> otherwise.
365     */
366     public void setDrawBounds(boolean drawBounds) {
367         if (drawBounds)
368             jomeDisplay.drawBounds();
369         else
370             jomeDisplay.dontDrawBounds();
371     }
372 
373     /***
374     * Retuns <CODE>true</CODE> if we want the bounds to be drawn.
375     * <CODE>false</CODE> otherwise.
376     */
377     public boolean getDrawBounds() {
378         return jomeDisplay.weDrawBounds();
379     }
380 
381     /***************************************************/
382     /* Iconification/DÈsiconification de la sÈlection */
383     /***************************************************/
384 
385     /***
386     * Iconifies the selection.
387     */
388     public void iconifySelection() {
389         formulaDisplay.iconify();
390     }
391 
392     /***
393     * Uniconifies the selected icons.
394     */
395     public void uniconifySelectedIcons() {
396         formulaDisplay.uniconify();
397     }
398 
399     /***
400     * Uniconifies all icons in the expression.
401     */
402     public void uniconifyAllIcons() {
403         formulaDisplay.uniconifyAll();
404     }
405 
406     /***
407     * Substitutes the selection by the specified friendly name.
408     * @param the name for the substitution
409     */
410     public void substitute(String name) {
411         if ((name != null) && !name.equals(""))
412             formulaDisplay.substitute(name);
413     }
414     
415 	public Display getDisplay(){
416 	 //System.out.println(jomeDisplay);
417 	  return jomeDisplay;
418 	}
419     
420     
421 }