View Javadoc

1   package fr.ove.openmath.mathematica;
2   
3   import javax.swing.*;
4   import javax.swing.event.*;
5   import java.io.*;
6   import java.awt.*;
7   import java.util.*;
8   import java.beans.PropertyVetoException;
9   
10  public class MyJInternalFrame extends JInternalFrame implements Savable {
11      /***
12      * The desktop which will contain the instance
13      */
14      Desktop desktop;
15      
16      /***
17      * The Constructor.
18      * @param desktop the desktop which will contain the instance.
19      */
20      public MyJInternalFrame(Desktop desktop) {
21          super();
22          this.desktop = desktop;
23      }
24          
25      /***
26      * The Constructor.
27      * @param title the title of the instance.
28      * @param desktop the desktop which will contain the instance.
29      */
30      public MyJInternalFrame(String title, Desktop desktop) {
31          super(title);
32          this.desktop = desktop;
33      }
34      
35      /***
36      * Specifies if the instance has its maximum size (i.e. occupies all the desktop
37      * space).
38      * @param max <CODE>true</CODE> if we want the instance to be maximum. <CODE>false</CODE>
39      * otherwise.
40      */
41      public void setMaximum(boolean max) throws PropertyVetoException {
42          if (isMaximum() != max) {
43              super.setMaximum(max);
44              desktop.setMaximize(max);
45              desktop.setAllMaximum();
46          }
47          else
48              return;
49              
50          // Sets the current instance as selected.
51          // Its it which had received
52          try {
53              this.setSelected(true);
54          } catch (PropertyVetoException pve) {
55              pve.printStackTrace();
56          }
57      }
58              
59      // ### Savable interface implementation
60          
61      /***
62      * Saves the instance.
63      * @returns the result of the save action. Should be <CODE>null</CODE> if nothing
64      * is expected after the save operation. The saved object otherwise.
65      * The instance is saved as the concatenation of all the OpenMath object request contained.
66      */
67      public Object save() {
68          StringBuffer buffer = new StringBuffer();
69          buffer.append(((Savable) getContentPane()).save());
70              
71          try {
72              FileWriter fw = new FileWriter(getTitle() + ".wks");
73              fw.flush();
74              fw.write(buffer.toString(), 0, buffer.length());
75              fw.close();
76          }
77          catch (IOException ioe) {
78              System.out.println("Impossible to create the file : " + getTitle() + ".wks");
79          }
80              
81                  
82          return null;
83      }
84      
85      /***
86      * Saves the instance into the specified file.
87      * @returns the result of the save action. Should be <CODE>null</CODE> if nothing
88      * is expected after the save operation. The saved object otherwise.
89      */
90      public Object save(File file) {
91          StringBuffer buffer = new StringBuffer();
92          buffer.append(((Savable) getContentPane()).save());
93              
94          try {
95              FileWriter fw = new FileWriter(file);
96              fw.flush();
97              fw.write(buffer.toString(), 0, buffer.length());
98              fw.close();
99          }
100         catch (IOException ioe) {
101             System.out.println("Impossible to write into the file : " + file.getPath() );
102         }
103                 
104         return null;
105     }
106     /***
107     * Saves the instance with the specified name.
108     * @param name the specified name.
109     * @returns the result of the save action. Should be <CODE>null</CODE> if nothing
110     * is expected after the save operation. The saved object otherwise.
111     */
112     public Object saveAs(String name) {
113         return null;
114     }
115         
116     /***
117     * Checks if the instance need to be saved.
118     * @return <CODE>true</CODE> if the instance need to be saved. <CODE>false</CODE> otherwise.
119     */
120     public boolean isSaveNeeded() {
121         // The instance need be saved if its content need be saved.
122         return ((Savable) getContentPane()).isSaveNeeded();
123     }
124         
125     /***
126     * Sets if the instance need to be saved.
127     * @param needed <CODE>true</CODE> if the instance need to be saved. <CODE>false</CODE> otherwise.
128     */
129     public void setSaveNeeded(boolean needed) {
130         // If the instance need be saved, then its content need be saved.
131         ((Savable) getContentPane()).setSaveNeeded(needed);
132     }
133 }