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 java.util.*;
8   import fr.ove.openmath.mathematica.WorkBook;
9   import fr.ove.palette.swing.*;
10  
11  /***
12  *
13  */
14  public class MyMenuBar extends JMenuBar implements ChangeListener {
15      /***
16      * The WorkBook to operate with.
17      */
18      private WorkBook workBook;
19      
20      /***
21      * The "File" menu. 
22      */
23      private JMenu fileMenu;
24      
25      /***
26      * The "Edit" menu. 
27      */
28      private JMenu editMenu;
29      
30      /***
31      * The "View" menu.
32      */
33      private JMenu viewMenu;
34      
35      /***
36      * The "Window" menu.
37      */
38      private JMenu windowMenu;
39      
40      /***
41      * Indicates if we have to add the "View" and "Window" menus.
42      */
43      private boolean addMenus = true;
44      
45      /***
46      * The OpenMath viewer.
47      */
48      private OpenMathViewer omViewer;
49      
50      /***
51      * The connection configuration dialog
52      */
53      private ConnectionConfigDialog ccd;
54      
55      /***
56      * The Constructor.
57      * @param workBook the WorkBook to operate with.
58      */
59      public MyMenuBar(WorkBook workBook, OpenMathViewer omViewer, ConnectionConfigDialog ccd) {
60          this.workBook = workBook;
61          this.omViewer = omViewer;
62          this.ccd = ccd;
63          
64          createFileMenu();
65          add(fileMenu);
66          createEditMenu();
67          add(editMenu);
68          createViewMenu();
69          createWindowMenu();
70  	}
71      
72      /***
73      * Adds the "View" and "Window" menu.
74      */
75      public void addViewAndWindows() {
76          if (addMenus) {
77              add(viewMenu);
78              add(windowMenu);
79              addMenus = false;
80              validate();
81              repaint();
82          }
83      }
84      
85      /***
86      * Removes the "View" and "Window" menu.
87      */
88      public void removeViewAndWindows() {
89          if (!addMenus) {
90              remove(viewMenu);
91              remove(windowMenu);
92              addMenus = true;
93              validate();
94              repaint();
95          }
96      }
97      
98      /***
99      * Creates the "File" menu.
100     */
101     public void createFileMenu() {
102         fileMenu = new JMenu("File");
103         fileMenu.setMnemonic(KeyEvent.VK_F);
104         
105         // New item
106         JMenuItem item = new JMenuItem("New");
107         item.setMnemonic(KeyEvent.VK_N);
108         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
109         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/new.gif"));
110         item.addActionListener(
111             new ActionListener() {
112                 public void actionPerformed(ActionEvent ae) {
113                     workBook.addTab();
114                 }
115             }
116         );
117         fileMenu.add(item);
118         
119         // Open... item
120         item = new JMenuItem("Open...");
121         item.setMnemonic(KeyEvent.VK_O);
122         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
123         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/open.gif"));
124         item.addActionListener(
125             new ActionListener() {
126                 public void actionPerformed(ActionEvent ae) {
127                     workBook.open();
128                     
129                 }
130             }
131         );
132         fileMenu.add(item);
133         
134         // Save item
135         item = new JMenuItem("Save");
136         item.setMnemonic(KeyEvent.VK_S);
137         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
138         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/save.gif"));
139         item.addActionListener(
140             new ActionListener() {
141                 public void actionPerformed(ActionEvent ae) {
142                     workBook.save();
143                 }
144             }
145         );
146         fileMenu.add(item);
147         
148         // Save As... item
149         item = new JMenuItem("Save As...");
150         item.setMnemonic(KeyEvent.VK_A);
151         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/saveas.gif"));
152         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
153         item.addActionListener(
154             new ActionListener() {
155                 public void actionPerformed(ActionEvent ae) {
156                     
157                 }
158             }
159         );
160         fileMenu.add(item);
161         
162         // Close item
163         item = new JMenuItem("Close");
164         item.setMnemonic(KeyEvent.VK_C);
165         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
166         item.addActionListener(
167             new ActionListener() {
168                 public void actionPerformed(ActionEvent ae) {
169                     
170                 }
171             }
172         );
173         fileMenu.add(item);
174         
175         // A separator
176         fileMenu.addSeparator();
177         
178         // Exit item
179         item = new JMenuItem("Exit");
180         item.setMnemonic(KeyEvent.VK_X);
181         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
182         item.addActionListener(
183             new ActionListener() {
184                 public void actionPerformed(ActionEvent ae) {
185                     System.exit(0);
186                 }
187             }
188         );
189         fileMenu.add(item);
190     }
191     
192     /***
193     * Creates the "Edit" menu.
194     */
195     public void createEditMenu() {
196         editMenu = new JMenu("Edit");
197         editMenu.setMnemonic(KeyEvent.VK_E);
198         
199         // New item
200         JMenuItem item = new JMenuItem("Insert");
201         item.addActionListener(
202             new ActionListener() {
203                 public void actionPerformed(ActionEvent ae) {
204                     JInternalFrame jif = workBook.getActiveInternalFrame();
205                     OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
206                     omSheet.insertNewElement();
207                 }
208             }
209         );
210         editMenu.add(item);
211         
212         // A separator
213         fileMenu.addSeparator();
214         
215         // New item
216         item = new JMenuItem("Connection");
217         /*
218         item.setMnemonic(KeyEvent.VK_N);
219         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
220         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/new.gif"));
221         */
222         item.addActionListener(
223             new ActionListener() {
224                 public void actionPerformed(ActionEvent ae) {
225                     ccd.setLocationRelativeTo(workBook);
226                     ccd.show();
227                 }
228             }
229         );
230         editMenu.add(item);
231     }
232     
233     /***
234     * Creates the "View" menu.
235     */
236     public void createViewMenu() {
237         viewMenu = new JMenu("View");
238         viewMenu.setMnemonic(KeyEvent.VK_V);
239         
240         // Palette Editor item
241         JMenuItem item = new JMenuItem("Palette Editor");
242         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/palette.gif"));
243         item.setMnemonic(KeyEvent.VK_P);
244         item.addActionListener(
245             new ActionListener() {
246                 public void actionPerformed(ActionEvent ae) {
247                     workBook.showPalette("Palette Editor");
248                 }
249             }
250         );
251         viewMenu.add(item);
252         
253         // OpenMath item
254         item = new JMenuItem("OpenMath");
255         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/show2.gif"));
256         item.setMnemonic(KeyEvent.VK_M);
257         item.addActionListener(
258             new ActionListener() {
259                 public void actionPerformed(ActionEvent ae) {
260                     omViewer.setVisible(!omViewer.isVisible());
261                     if (omViewer.isVisible())
262                         updateOmViewer();
263                 }
264             }
265         );
266         viewMenu.add(item);
267     }
268     
269     /***
270     * Creates the "Window" menu.
271     */
272     public void createWindowMenu() {
273         windowMenu = new JMenu("Window");
274         windowMenu.setMnemonic(KeyEvent.VK_W);
275         
276         // Cascade item
277         JMenuItem item = new JMenuItem("Cascade");
278         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/cascade.gif"));
279         item.setMnemonic(KeyEvent.VK_C);
280         item.addActionListener(
281             new ActionListener() {
282                 public void actionPerformed(ActionEvent ae) {
283                     workBook.cascade();
284                 }
285             }
286         );
287         windowMenu.add(item);
288         
289         // Close All item
290         item = new JMenuItem("Close All");
291         item.setIcon(getIcon("fr/ove/openmath/mathematica/images/closeall.gif"));
292         item.setMnemonic(KeyEvent.VK_L);
293         item.addActionListener(
294             new ActionListener() {
295                 public void actionPerformed(ActionEvent ae) {
296                     workBook.closeAll();
297                 }
298             }
299         );
300         windowMenu.add(item);
301     }
302     
303     /***
304     * Updates the OpenMath viewer.
305     */
306     private void updateOmViewer() {
307         JInternalFrame jif = workBook.getActiveInternalFrame();
308         OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
309         PanelJomes panelJomes = omSheet.getCurrentElement();
310         omViewer.setOpenMathRequest(panelJomes.getOpenMathRequest());
311         omViewer.setOpenMathResult(panelJomes.getOpenMathResult());
312     }
313     
314     /***
315     * Returns the icon with the specified image filename.
316     * @param filename the image filename
317     */
318     private ImageIcon getIcon(String filename) {
319         ImageIcon icon = null;
320         
321         java.net.URL iconURL = ClassLoader.getSystemResource(filename);
322         if (iconURL != null)
323             icon = new ImageIcon(iconURL);
324             
325         return icon;
326     }
327     
328     //  ### ChangeListenerInterface implementation
329     
330     /***
331     * Invoked when the target of the listener has changed its state.
332     * @param e a ChangeEvent object 
333     */
334     public void stateChanged(ChangeEvent e) {
335         WorkBook wBook = (WorkBook) e.getSource();
336         int count = wBook.getTabCount();
337         if (count == 0)
338             removeViewAndWindows();
339         else
340             addViewAndWindows();
341     }
342 }