Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hartmath/HartMathPanel.java


1   /*
2    *  HartMathPanel.java
3    *  Copyright (C) 2001, Klaus Hartlage
4    *
5    *  This program is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU General Public License
7    *  as published by the Free Software Foundation; either version 2
8    *  of the License, or any later version.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package org.hartmath;
20  
21  import javax.swing.text.*;
22  import javax.swing.*;
23  import java.awt.event.*;
24  import java.awt.*;
25  import java.awt.datatransfer.*;
26  
27  import java.io.*;
28  import java.util.*;
29  
30  import com.hartmath.*;
31  import com.hartmath.lib.C;
32  import com.hartmath.expression.HObject;
33  import com.hartmath.expression.HJavaObject;
34  import com.hartmath.expression.HString;
35  import com.hartmath.expression.HSymbol;
36  import com.hartmath.expression.HFunction;
37  import com.hartmath.JSci.chemistry.PeriodicTable;
38  import com.hartmath.xfunctions.Graph3DPanel;
39  import com.hartmath.xfunctions.ParametricCurvesPanel;
40  
41  //import org.gjt.sp.jedit.textarea.JEditTextArea;
42  
43  //import com.jrefinery.chart.*;
44  //import org.hartmath.jfreechart.*;
45  
46  /**
47   *  Description of the Class
48   *
49   *@author     khartlage
50   *@created    31. Juli 2001
51   */
52  public class HartMathPanel extends JPanel {
53          private HartMathTextArea edit2;
54          private Main console;
55          private JFrame parent;
56          //  private     ActionHandler evaluationActionHandler;
57  
58          // splitted edition
59          private JTextArea inputTextArea;
60          // private Document inputDocument;
61          // GUI components
62          // private XHartMathTree xtree;
63          // gui
64  
65          private JTabbedPane textAreasPane;
66          private JSplitPane split;
67          private JTabbedPane vTabbedPane;
68          private JSplitPane splitter;
69          //  private JSplitPane textAreaSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
70  
71          private int _dividerSize;
72  
73          // private members
74  
75          private CalculatorPanel calculatorPanel;
76          // private FunctionsPanel functionsPanel;
77          private JTabbedPane outputTabbedPane;
78          private JTextPane eval_output;
79          private Document outputDocument;
80  
81          private boolean jEditPluginEnabled;
82  
83          private static String outputFont = "Serif";
84          private final static int outputFontSize = 18;
85  
86  
87  
88          /**
89           *  Constructor for the HartMathPanel object
90           *
91           *@param  args                Description of Parameter
92           *@param  parent              Description of Parameter
93           *@param  jEditPluginEnabled  Description of Parameter
94           */
95          public HartMathPanel(String[] args,
96                          JFrame parent, boolean jEditPluginEnabled) {
97                  super(new BorderLayout());
98                  this.parent = parent;
99                  this.jEditPluginEnabled = jEditPluginEnabled;
100 
101                 if (!jEditPluginEnabled) {
102                         // create the text areas pane
103                         inputTextArea = new JTextArea(4, 80);
104 
105                         vTabbedPane = new JTabbedPane();
106                         calculatorPanel = new CalculatorPanel(this);
107 
108                         vTabbedPane.add("Calculator", calculatorPanel);
109             
110                         split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
111                         split.setOneTouchExpandable(true);
112                         split.setContinuousLayout(true);
113                         split.setBorder(null);
114                         _dividerSize = split.getDividerSize();
115                         if (_dividerSize < 10) {
116                                 _dividerSize = 10;
117                         }
118                         split.setDividerSize(_dividerSize);
119 
120                         split.setTopComponent(inputTextArea);
121                         split.setBottomComponent(vTabbedPane);
122 
123                         splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
124                         _dividerSize = splitter.getDividerSize();
125                         if (_dividerSize < 10) {
126                                 _dividerSize = 10;
127                         }
128                         splitter.setDividerSize(_dividerSize);
129 
130                         splitter.setOneTouchExpandable(true);
131                         splitter.setContinuousLayout(true);
132                         splitter.setBorder(null);
133 //        }
134 //        if (! jEditPluginEnabled) {
135 //            splitter.setTopComponent(vTabbedPane);
136 //        } else {
137                         splitter.setTopComponent(split);
138                 }
139 
140                 outputTabbedPane = new JTabbedPane();
141 
142                 /*
143                  *  Number[][] data = new Double[][]{ {new Double(0.5), new Double(1.5)},
144                  *  {new Double(0.5), new Double(2.5)},
145                  *  {new Double(0.5), new Double(3.5)}};
146                  *  CategoryDataSource myDataSource = new DefaultCategoryDataSource(data);
147                  *  JFreeChart myChart = JFreeChart.createVerticalBarChart( myDataSource );
148                  *  JFreeChartPanel myChartPanel = new JFreeChartPanel( myChart );
149                  */
150                 // FreeChartPanel hmChartPanel = new FreeChartPanel(outputTabbedPane);
151 
152                 //SharpTools
153 
154                 //HartMathSharpTools spreadsheet = new HartMathSharpTools(parent);
155                 //        outputTabbedPane.addTab("SpreadSheet",
156                 //                             spreadsheet );
157 
158                 if (!jEditPluginEnabled) {
159                         splitter.setBottomComponent(outputTabbedPane);
160                         add(BorderLayout.CENTER, splitter);
161                 }
162                 else {
163                         add(BorderLayout.CENTER, outputTabbedPane);
164                 }
165                 eval_output = new JTextPane();
166 
167                 outputDocument = eval_output.getDocument();
168                 outputTabbedPane.addTab("Output",
169                                 new JScrollPane(eval_output));
170 
171                 edit2 = new HartMathTextArea(this);
172                 console = new Main(args, edit2, edit2,false);
173                 //evaluationActionHandler = new ActionHandler();
174                 if (!jEditPluginEnabled) {
175               
176                         outputTabbedPane.addTab("PeriodicTable",
177                                         new PeriodicTable(parent));
178 
179                         outputTabbedPane.addTab("Graph3D",
180                                         new Graph3DPanel("Sin(x^2*y^2)",
181                                         "x",
182                                         "y",
183                                         -2, 2, -2, 2,
184                                         console.getSession())
185                                         );
186                         outputTabbedPane.addTab("ParametricPlot",
187                                         new ParametricCurvesPanel(console.getSession())
188                                         );
189 
190                         KeyListener keyListener = new EvalKeyListener();
191                         inputTextArea.addKeyListener(keyListener);
192                 }
193         }
194 
195 
196         /**
197          *  Gets the hartMathConsole attribute of the HartMathPanel object
198          *
199          *@return    The hartMathConsole value
200          */
201         public Main getHartMathConsole() {
202                 return console;
203         }
204 
205 
206         /**
207          *  Returns current selected text area.
208          *
209          *@return    The textArea value
210          */
211 
212         public JTextArea getTextArea() {
213                 return inputTextArea;
214         }
215 
216 
217         /**
218          *  Description of the Method
219          *
220          *@param  converterFunction  Description of Parameter
221          *@param  convertResult      Description of Parameter
222          */
223         /*
224          *  public void evaluate(HFunction converterFunction) {
225          *  evaluateButton(converterFunction);
226          *  }
227          */
228         /**
229          *  Description of the Method
230          *
231          *  Description of the Method Description of the Method
232          *
233          *@param  converterFunction  Description of Parameter
234          *@param  convertResult      Description of Parameter
235          */
236         public void evaluateButton(HFunction converterFunction, boolean convertResult) {
237                 String text = inputTextArea.getText();
238                 if (text.length() == 0) {
239                         return;
240                 }
241                 evaluateText(text, converterFunction, convertResult);
242         }
243 
244 
245         /**
246          *  Evaluate the given text as a HartMath expression
247          *
248          *@param  text               Description of Parameter
249          *@param  converterFunction  Description of Parameter
250          *@param  convertResult      Description of Parameter
251          */
252         public void evaluateText(
253                         String text,
254                         HFunction converterFunction,
255                         boolean convertResult) {
256                 printInput("\n\n");
257                 //    inputTextArea.select(0, inputTextArea.getDocument().getLength());
258 
259                 if (C.initialized.equals(Boolean.FALSE)) {
260                         return;
261                 }
262 
263                 if (text == null || text.equals("")) {
264                         edit2.appendLine("Input Error: no text selected");
265                         return;
266                 }
267                 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
268 
269                 HObject result = console.objectInterpreter(text,
270                                 converterFunction,
271                                 convertResult);
272                 if (result == null) {
273                         setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
274                         return;
275                 }
276                 printHObject(result);
277                 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
278 
279         }
280 
281 
282         /**
283          *  Description of the Method
284          *
285          *@param  str  Description of Parameter
286          */
287         public void append(String str) {
288                 printInput(str);
289         }
290 
291 
292         /**
293          *  Description of the Method
294          *
295          *@param  str  Description of Parameter
296          */
297         public void appendTeX(String str) {
298                 printInput(str);
299         }
300 
301 
302         /**
303          *  Description of the Method
304          *
305          *@param  obj  Description of Parameter
306          */
307         public void printHObject(HObject obj) {
308                 Icon icon;
309                 int size = eval_output.getWidth();
310                 try {
311                         if (obj instanceof HJavaObject) {
312                                 Object object = ((HJavaObject) obj).getObject();
313                                 if (object instanceof JComponent) {
314                                         outputTabbedPane.addTab("Graph", (JComponent) object);
315                                         outputTabbedPane.setSelectedComponent((JComponent) object);
316                                 }
317                                 return;
318                         }
319                         StringBuffer strBuf = new StringBuffer(1024);
320                         Clipboard clipboard = parent.getToolkit().getSystemClipboard();
321                         if (isPrettyPrinterEnabled()) {
322                                 if ((obj instanceof HString) &&
323                                                 !(obj instanceof HSymbol)) {
324                                         obj.toStringBuffer(strBuf, console.getSession());
325                                         if (isClipBoardEnabled()) {
326                                                 clipboard.setContents(new StringSelection(strBuf.toString()), null);
327                                         }
328                                         strBuf.append("\n");
329 
330                                         printOutput(strBuf.toString());
331                                 }
332                                 else {
333                                         FormulaIcons icons = new FormulaIcons(eval_output,
334                                                         obj,
335                                                         outputFont,
336                                                         outputFontSize,
337                                                         Color.black);
338                                         obj.toStringBuffer(strBuf, console.getSession());
339                                         if (icons.isOverSized()) {
340                                                 strBuf.append("\n");
341                                                 printOutput(strBuf.toString());
342                                         }
343                                         else {
344                                                 icons.printOutput();
345                                         }
346                                         if (isClipBoardEnabled()) {
347                                                 clipboard.setContents(new StringSelection(strBuf.toString()), null);
348                                         }
349                                 }
350                         }
351                         else {
352                                 obj.toStringBuffer(strBuf, console.getSession());
353                                 if (isClipBoardEnabled()) {
354                                         clipboard.setContents(new StringSelection(strBuf.toString()),
355                                                         null);
356                                 }
357                                 strBuf.append("\n");
358 
359                                 printOutput(strBuf.toString());
360                         }
361                 }
362                 catch (Throwable e) {
363                         outputTabbedPane.setSelectedIndex(0);
364                         printError(e.getMessage());
365                         return;
366                 }
367 
368         }
369 
370 
371         /**
372          *  Description of the Method
373          *
374          *@param  text  Description of Parameter
375          */
376         public void insertInput(String text) {
377                 inputTextArea.append(text);
378                 /*
379                  *  SimpleAttributeSet style = new SimpleAttributeSet();
380                  *  style.addAttribute(StyleConstants.Foreground, Color.blue);
381                  *  try {
382                  *  inputDocument.insertString(inputDocument.getLength(), text, style);
383                  *  } catch (BadLocationException bl) {
384                  *  bl.printStackTrace();
385                  *  }
386                  *  inputTextArea.setCaretPosition(inputDocument.getLength());
387                  */
388         }
389 
390 
391         /**
392          *  Description of the Method
393          */
394         public void clearInput() {
395                 inputTextArea.setText("");
396         }
397 
398 
399         /**
400          *  Description of the Method
401          *
402          *@param  text  Description of Parameter
403          */
404         private void printInput(String text) {
405                 SimpleAttributeSet style = new SimpleAttributeSet();
406 
407                 style.addAttribute(StyleConstants.Foreground, Color.blue);
408 
409                 try {
410                         outputDocument.insertString(
411                                         outputDocument.getLength(),
412                                         text, style);
413                 }
414                 catch (BadLocationException bl) {
415                         bl.printStackTrace();
416                 }
417 
418                 eval_output.setCaretPosition(outputDocument.getLength());
419         }
420 
421 
422         /**
423          *  Description of the Method
424          *
425          *@param  text  Description of Parameter
426          */
427         private void printOutput(String text) {
428                 SimpleAttributeSet style = new SimpleAttributeSet();
429 
430                 style.addAttribute(StyleConstants.Foreground, Color.black);
431                 style.addAttribute(StyleConstants.FontFamily, outputFont);
432                 style.addAttribute(StyleConstants.FontSize, new Integer(outputFontSize));
433 
434                 try {
435                         outputDocument.insertString(
436                                         outputDocument.getLength(),
437                                         text, style);
438                 }
439                 catch (BadLocationException bl) {
440                         bl.printStackTrace();
441                 }
442 
443                 eval_output.setCaretPosition(outputDocument.getLength());
444         }
445 
446 
447         /**
448          *  Description of the Method
449          *
450          *@param  text  Description of Parameter
451          */
452         /*
453          *  private void printOutput(Icon icon) {
454          *  SimpleAttributeSet style = new SimpleAttributeSet();
455          *  StyleConstants.setIcon(style, icon);
456          *  try {
457          *  outputDocument.insertString(
458          *  outputDocument.getLength(), "\n", null);
459          *  outputDocument.insertString(
460          *  outputDocument.getLength(), " ", style);
461          *  }
462          *  catch (BadLocationException bl) {
463          *  bl.printStackTrace();
464          *  }
465          *  eval_output.setCaretPosition(outputDocument.getLength());
466          *  }
467          */
468         /**
469          *  Description of the Method
470          *
471          *  Description of the Method Description of the Method Description of the
472          *  Method Description of the Method
473          *
474          *@param  text  Description of Parameter
475          */
476         private void printError(String text) {
477                 SimpleAttributeSet style = new SimpleAttributeSet();
478 
479                 style.addAttribute(StyleConstants.Foreground, Color.red);
480 
481                 try {
482                         outputDocument.insertString(
483                                         outputDocument.getLength(),
484                                         "\n\n", null);
485                         outputDocument.insertString(
486                                         outputDocument.getLength(),
487                                         text, style);
488                 }
489                 catch (BadLocationException bl) {
490                         bl.printStackTrace();
491                 }
492 
493                 eval_output.setCaretPosition(outputDocument.getLength());
494         }
495 
496 
497         /**
498          *  Sets the prettyPrinterMode attribute of the HartMathPanel class
499          *
500          *@param  value  The new prettyPrinterMode value
501          */
502         public static void setPrettyPrinterMode(boolean value) {
503                 Main.setBooleanProperty("hartmath.usePrettyPrinter", value);
504         }
505 
506 
507         /**
508          *  Sets the clipBoard attribute of the HartMathPanel class
509          *
510          *@param  value  The new clipBoard value
511          */
512         public static void setClipBoard(boolean value) {
513                 Main.setBooleanProperty("hartmath.useClipBoard", value);
514         }
515 
516 
517         /**
518          *  Sets the font attribute of the HartMathPanel class
519          *
520          *@param  fontFamily  The new font value
521          */
522         public static void setFont(String fontFamily) {
523                 outputFont = fontFamily;
524         }
525 
526 
527         /**
528          *  Checks if the result should be printed in "pretty printer" mode
529          *
530          *@return    The prettyPrinterEnabled value
531          */
532         public static boolean isPrettyPrinterEnabled() {
533                 return Main.getBooleanProperty("hartmath.usePrettyPrinter");
534         }
535 
536 
537         /**
538          *  Checks if the last result should be copied into ClipBoard
539          *
540          *@return    The prettyPrinterEnabled value
541          */
542         public static boolean isClipBoardEnabled() {
543                 return Main.getBooleanProperty("hartmath.useClipBoard");
544         }
545 
546 
547         /**
548          *  Evaluate an expression if SHIFT+ENTER is pressed
549          *
550          *@author     khartlage
551          *@created    5. Oktober 2001
552          */
553         public class EvalKeyListener implements KeyListener {
554                 boolean shiftPressed;
555                 boolean controlPressed;
556 
557                 /**
558                  *  Constructor for the EvalKeyListener object
559                  */
560                 public EvalKeyListener() {
561                         shiftPressed = false;
562                         controlPressed = false;
563                 }
564 
565 
566                 /**
567                  *  Description of the Method
568                  *
569                  *@param  keyEvent  Description of Parameter
570                  */
571                 public void keyPressed(KeyEvent keyEvent) {
572                         int keyCode = keyEvent.getKeyCode();
573                         if (keyCode == KeyEvent.VK_SHIFT) {
574                                 shiftPressed = true;
575                         }
576                         if (keyCode == KeyEvent.VK_CONTROL) {
577                                 controlPressed = true;
578                         }
579                         if (keyCode == KeyEvent.VK_ENTER && controlPressed) {
580                                 HFunction function = calculatorPanel.getConverter();
581                                 if (function != null) {
582                                         evaluateButton(function, true);
583                                 }
584                                 else {
585                                         evaluateButton(null, false);
586                                 }
587                         }
588                         if (keyCode == KeyEvent.VK_ENTER && shiftPressed) {
589                         evaluateButton(C.N.f(), false);
590                         }
591                 }
592 
593 
594                 /**
595                  *  Description of the Method
596                  *
597                  *@param  keyEvent  Description of Parameter
598                  */
599                 public void keyReleased(KeyEvent keyEvent) {
600                         if (keyEvent.getKeyCode() == KeyEvent.VK_SHIFT) {
601                                 shiftPressed = false;
602                         }
603                         if (keyEvent.getKeyCode() == KeyEvent.VK_CONTROL) {
604                                 controlPressed = false;
605                         }
606                 }
607 
608 
609                 /**
610                  *  Description of the Method
611                  *
612                  *@param  keyEvent  Description of Parameter
613                  */
614                 public void keyTyped(KeyEvent keyEvent) {
615                 }
616         }
617 
618 }
619