View Javadoc

1   package fr.ove.openmath.mathematica;
2   
3   import javax.swing.*;
4   import javax.swing.event.*;
5   import java.awt.*;
6   import java.awt.event.*;
7   import javax.swing.text.*;
8   import fr.ove.openmath.mathematica.*;
9   import fr.ove.palette.swing.*;
10  
11  /***
12  * The Command part.<BR>
13  * Allows the user to edit, send calculations, etc.
14  */
15  public class PanelCommand extends JPanel implements ChangeListener {
16      /***
17      * The entry for the formula
18      */
19      private LinearEntry entry;
20      
21      /***
22      * Flag to indiquate if we have to update the entry value.
23      */
24      private boolean updateEntry = true; // The default is true. This flag is necessary because
25      // when we are typing in the linear form of the formula, once a modification is realized,
26      // we set the linear value typed to the current instance in the worksheet which send a event to
27      // notify a change. The instance receives the event and tries to update the entry value.
28      // However, at this point (worksheet event treatment), we are still in the document listener
29      // which has locked the document (entry) value. This causes an exception. So we set the value
30      // to false to forbide entry's value change.
31      
32      /***
33      * The current element to edit.
34      */
35      private OMWorkSheet worksheet;
36      
37      /***
38      * The OpenMath viewer.
39      */
40      private OpenMathViewer omViewer;
41      
42      /***
43      * The Constructor.
44      */
45      public PanelCommand(OpenMathViewer viewer, JPaletteEditor palette) {
46          setLayout(new BorderLayout());
47          omViewer = viewer;
48          entry = new LinearEntry();
49          palette.addPaletteListener(entry);
50          entry.addActionListener(
51              new ActionListener() {
52                  public void actionPerformed(ActionEvent ae) {
53                      worksheet.runCurrentRequest();
54                      worksheet.addNewElement();
55                      entry.setText("");
56              		// pour le moment
57              		worksheet.validate();
58                  }
59              }
60          );
61          
62          Document document = entry.getDocument();
63          document.addDocumentListener(
64              new DocumentListener() {
65                  public void changedUpdate(DocumentEvent e) {
66                  }
67                  
68                  public void insertUpdate(DocumentEvent e) {
69                      Document doc = e.getDocument();
70                      try {
71                          updateEntry = false;
72                          String linear = doc.getText(0, doc.getLength());
73                          worksheet.getCurrentElement().setLinearRequest(linear);
74                          worksheet.validate();
75                          omViewer.setOpenMathRequest(worksheet.getCurrentElement().getOpenMathRequest());
76                          updateEntry = true;
77                      }
78                      catch (BadLocationException ble) {
79                          ble.printStackTrace();
80                      }
81                  }
82                  
83                  public void removeUpdate(DocumentEvent e) {
84                      Document doc = e.getDocument();
85                      try {
86                          updateEntry = false;
87                          String linear = doc.getText(0, doc.getLength());
88                          worksheet.getCurrentElement().setLinearRequest(linear);
89                          worksheet.validate();
90                          omViewer.setOpenMathRequest(worksheet.getCurrentElement().getOpenMathRequest());
91                          updateEntry = true;
92                      }
93                      catch (BadLocationException ble) {
94                          ble.printStackTrace();
95                      }
96                  }
97              }
98          );
99          
100         add(BorderLayout.CENTER, entry);
101         
102         setVisible(false);
103     }
104     
105     /***
106     * Clear the entry.
107     */
108     public void clear() {
109         entry.setText("");
110     }
111     
112     //  ### ChangeListenerInterface implementation
113     
114     /***
115     * Invoked when the target of the listener has changed its state.
116     * @param e a ChangeEvent object 
117     */
118     public void stateChanged(ChangeEvent e) {
119         WorkBook wBook = (WorkBook) e.getSource();
120         int count = wBook.getTabCount();
121         if (count == 0) {
122             setVisible(false);
123             worksheet = null;
124         }
125         else {
126             setVisible(true);
127             JInternalFrame jif = wBook.getActiveInternalFrame();
128             
129             Container contentPane = jif.getContentPane();
130             if (contentPane instanceof OMWorkSheet) {
131                 worksheet = (OMWorkSheet) jif.getContentPane();
132                 String linearRequest = worksheet.getCurrentElement().getLinearRequest();
133                 if (updateEntry)
134                     entry.setText(linearRequest);
135             }
136         }
137     }
138 }