View Javadoc

1   package fr.ove.openmath.mathematica;
2   
3   import javax.swing.*;
4   import javax.swing.event.*;
5   import java.awt.event.*;
6   import fr.ove.palette.swing.*;
7   
8   /***
9   * A Toolbar under the menu bar.
10  */
11  public class ToolBar extends JToolBar implements ChangeListener {
12      /***
13      * The WorkBook to operate with.
14      */
15      private WorkBook workBook;
16      
17      /***
18      * The save button.
19      */
20      private JButton buttonSave;
21      
22      /***
23      * The clear button.
24      */
25      private JButton buttonClear;
26      
27      /***
28      * The delete button.
29      */
30      private JButton buttonTrash;
31      
32      /***
33      * The purge button.
34      */
35      private JButton buttonPurge;
36      
37      /***
38      * The palette button.
39      */
40      private JButton buttonPalette;
41      
42      /***
43      * The OpenMath button.
44      */
45      private JButton buttonOM;
46      
47      /***
48      * The run button.
49      */
50      private JButton buttonRun;
51      
52      /***
53      * The run button.
54      */
55      private JButton buttonRunAll;
56      
57      /***
58      * The OpenMath viewer.
59      */
60      private OpenMathViewer omViewer;
61      
62      /***
63      * The palette editor.
64      */
65      private JPaletteEditor palette;
66      
67      /***
68      * The panel command for typing in the linear form of the formula
69      */
70      private PanelCommand panelCommand;
71      
72      public ToolBar(WorkBook wBook, OpenMathViewer viewer, JPaletteEditor jpalette, PanelCommand panelComm) {
73          workBook = wBook;
74          omViewer = viewer;
75          palette = jpalette;
76          panelCommand = panelComm;
77          
78          JButton button = new JButton(getIcon("fr/ove/openmath/mathematica/images/new.gif"));
79  		button.setDefaultCapable(false);
80  		button.setToolTipText("Create a new worksheet");
81  		button.addActionListener(
82              new ActionListener() {
83                  public void actionPerformed(ActionEvent ae) {
84                      workBook.addTab();
85                  }
86              }
87          );
88  		add(button);
89  		
90  		button = new JButton(getIcon("fr/ove/openmath/mathematica/images/open.gif"));
91  		button.setDefaultCapable(false);
92  		button.setToolTipText("Open an existing worksheet");
93  		button.addActionListener(
94              new ActionListener() {
95                  public void actionPerformed(ActionEvent ae) {
96  		            workBook.open();
97                  }
98              }
99          );
100 		add(button);
101 		
102 		buttonSave = new JButton(getIcon("fr/ove/openmath/mathematica/images/save.gif"));
103 		buttonSave.setDefaultCapable(false);
104 		buttonSave.setToolTipText("Save the active worksheet");
105 		buttonSave.setEnabled(false);
106 		buttonSave.addActionListener(
107             new ActionListener() {
108                 public void actionPerformed(ActionEvent ae) {
109                     workBook.save();
110                 }
111             }
112         );
113 		add(buttonSave);
114 		
115 		add(new JToolBar.Separator());
116 		
117 		buttonClear = new JButton(getIcon("fr/ove/openmath/mathematica/images/clear.gif"));
118 		buttonClear.setDefaultCapable(false);
119 		buttonClear.setToolTipText("Clear the current request");
120 		buttonClear.setEnabled(false);
121 		buttonClear.addActionListener(
122             new ActionListener() {
123                 public void actionPerformed(ActionEvent ae) {
124                     panelCommand.clear();
125                 }
126             }
127         );
128 		add(buttonClear);
129 		
130 		buttonTrash = new JButton(getIcon("fr/ove/openmath/mathematica/images/trash.gif"));
131 		buttonTrash.setDefaultCapable(false);
132 		buttonTrash.setToolTipText("Remove the current request");
133 		buttonTrash.setEnabled(false);
134 		buttonTrash.addActionListener(
135             new ActionListener() {
136                 public void actionPerformed(ActionEvent ae) {
137                     JInternalFrame jif = workBook.getActiveInternalFrame();
138                     OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
139                     omSheet./*initialize*/removeCurrent();
140                 }
141             }
142         );
143 		add(buttonTrash);
144 		
145 		buttonPurge = new JButton(getIcon("fr/ove/openmath/mathematica/images/basket.gif"));
146 		buttonPurge.setDefaultCapable(false);
147 		buttonPurge.setToolTipText("Clear the work sheet");
148 		buttonPurge.setEnabled(false);
149 		buttonPurge.addActionListener(
150             new ActionListener() {
151                 public void actionPerformed(ActionEvent ae) {
152                     JInternalFrame jif = workBook.getActiveInternalFrame();
153                     OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
154                     omSheet.initialize();
155                 }
156             }
157         );
158 		add(buttonPurge);
159 		
160 		add(new JToolBar.Separator());
161 		
162 		buttonPalette = new JButton(getIcon("fr/ove/openmath/mathematica/images/palette.gif"));
163 		buttonPalette.setDefaultCapable(false);
164 		buttonPalette.setToolTipText("Open the palette editor");
165 		buttonPalette.setEnabled(false);
166 		buttonPalette.addActionListener(
167             new ActionListener() {
168                 public void actionPerformed(ActionEvent ae) {
169                     workBook.showPalette("Palette Editor");
170                 }
171             }
172         );
173 		add(buttonPalette);
174 		
175 		buttonOM = new JButton(getIcon("fr/ove/openmath/mathematica/images/show2.gif"));
176 		buttonOM.setDefaultCapable(false);
177 		buttonOM.setToolTipText("View the OpenMath objects");
178 		buttonOM.setEnabled(false);
179 		buttonOM.addActionListener(
180             new ActionListener() {
181                 public void actionPerformed(ActionEvent ae) {
182                     omViewer.setVisible(!omViewer.isVisible());
183                     if (omViewer.isVisible())
184                         updateOmViewer();
185                 }
186             }
187         );
188 		add(buttonOM);
189 		
190 		add(new JToolBar.Separator());
191 		
192 		buttonRun = new JButton(getIcon("fr/ove/openmath/mathematica/images/run.gif"));
193 		buttonRun.setDefaultCapable(false);
194 		buttonRun.setToolTipText("Run the current calculation");
195 		buttonRun.setEnabled(false);
196 		buttonRun.addActionListener(
197             new ActionListener() {
198                 public void actionPerformed(ActionEvent ae) {
199                     JInternalFrame jif = workBook.getActiveInternalFrame();
200                     OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
201                     omSheet.runCurrentRequest();
202                 }
203             }
204         );
205 		add(buttonRun);
206 		
207 		buttonRunAll = new JButton(getIcon("fr/ove/openmath/mathematica/images/runall2.gif"));
208 		buttonRunAll.setDefaultCapable(false);
209 		buttonRunAll.setToolTipText("Run the work sheet");
210 		buttonRunAll.setEnabled(false);
211 		buttonRunAll.addActionListener(
212             new ActionListener() {
213                 public void actionPerformed(ActionEvent ae) {
214                     JInternalFrame jif = workBook.getActiveInternalFrame();
215                     OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
216                     omSheet.runWorkSheet();
217                 }
218             }
219         );
220 		add(buttonRunAll);
221     }
222     
223     /***
224     * Returns the icon with the specified image filename.
225     * @param filename the image filename
226     */
227     private ImageIcon getIcon(String filename) {
228         ImageIcon icon = null;
229         
230         java.net.URL iconURL = ClassLoader.getSystemResource(filename);
231         if (iconURL != null)
232             icon = new ImageIcon(iconURL);
233             
234         return icon;
235     }
236     
237     /***
238     * Updates the OpenMath viewer.
239     */
240     private void updateOmViewer() {
241         JInternalFrame jif = workBook.getActiveInternalFrame();
242         OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
243         PanelJomes panelJomes = omSheet.getCurrentElement();
244         omViewer.setOpenMathRequest(panelJomes.getOpenMathRequest());
245         omViewer.setOpenMathResult(panelJomes.getOpenMathResult());
246     }
247     
248     
249     //  ### ChangeListenerInterface implementation
250     
251     /***
252     * Invoked when the target of the listener has changed its state.
253     * @param e a ChangeEvent object 
254     */
255     public void stateChanged(ChangeEvent e) {
256         if (workBook.getTabCount() == 0) {
257             buttonSave.setEnabled(false);
258     		buttonClear.setEnabled(false);
259     		buttonTrash.setEnabled(false);
260     		buttonPurge.setEnabled(false);
261     		buttonPalette.setEnabled(false);
262             buttonOM.setEnabled(false);
263             omViewer.setVisible(false);
264             buttonRun.setEnabled(false);
265             buttonRunAll.setEnabled(false);
266         }
267         else {
268             if (!buttonSave.isEnabled()) {
269                 // If one is enabled, others are also. So...
270                 buttonSave.setEnabled(true);
271     		    buttonClear.setEnabled(true);
272     		    buttonTrash.setEnabled(true);
273     		    buttonPurge.setEnabled(true);
274     		    buttonPalette.setEnabled(true);
275                 buttonOM.setEnabled(true);
276                 buttonRun.setEnabled(true);
277                 buttonRunAll.setEnabled(true);
278             }
279             
280             if (omViewer.isVisible())
281                 updateOmViewer();
282                 
283         }
284     }
285 }