View Javadoc

1   package fr.ove.openmath.mathematica;
2   
3   import javax.swing.*;
4   import java.awt.*;
5   import java.beans.*;
6   import java.util.*;
7   import java.io.*;
8   import fr.ove.openmath.mathematica.*;
9   import fr.ove.openmath.mathematica.events.*;
10  import fr.ove.utils.*;
11  
12  /***
13  * A work sheet file chooser.
14  */
15  public class WorkSheetFileChooser extends FileChooser implements VetoableChangeListener {
16      /***
17      * The constructor.
18      */
19      public WorkSheetFileChooser() {
20          this(null);
21      }
22      
23      /***
24      * The constructor.
25      */
26      public WorkSheetFileChooser(Component parent) {
27          super(parent);
28          setFileFilter(new GenericFileFilter("Work Sheet", "wks"));
29      }
30      
31      /***
32      * Indicates if we have vetoed the close operation (user pressed cancel button).
33      */
34      private boolean closedVetoed = false;
35      
36      // ####  interface VetoableChangeListener implementation
37      
38      /***
39      *
40      */
41      public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
42          String propertyName = pce.getPropertyName();
43          if (propertyName.equals(JInternalFrame.IS_CLOSED_PROPERTY)) {
44              if (!closedVetoed) {
45                  // We checked if we vetoed the closed operation.
46                  // If not, do the save process.
47                  MyJInternalFrame jif = (MyJInternalFrame) pce.getSource();
48                  if (jif.isSaveNeeded()) {
49                      
50                      int reply = JOptionPane.showConfirmDialog(getCompReference(), 
51  	    	                                                    "Do you want to save the work sheet", 
52  	    	                                                    "Save Confirmation" , 
53  	    	                                                    JOptionPane.YES_NO_CANCEL_OPTION, 
54  	    	                                                    JOptionPane.QUESTION_MESSAGE);
55  			        // If the confirmation was affirmative, handle exiting.
56  			        if (reply == JOptionPane.YES_OPTION) {
57                          File file;
58                          String title = jif.getTitle();
59                          if (title.startsWith("Untitled")) {
60                              int state = showSave();
61                              file = getSelectedFile();
62                              if ((file != null) && (state == JFileChooser.APPROVE_OPTION)) {
63                                  jif.save(file);
64                              }
65                              else {
66                                  closedVetoed = true;  // Set we veto the close operation.
67                                  throw new PropertyVetoException("Save Cancelled", pce);
68                              }
69                          }
70                          else {
71                              file = new File(title);
72                              jif.save(file);
73                          }
74                      }
75                      else if (reply == JOptionPane.CANCEL_OPTION) {
76                          closedVetoed = true;  // Set we veto the close operation.
77                          throw new PropertyVetoException("Save Cancelled", pce);
78                      }
79                  }
80              }
81              else
82                  closedVetoed = false;
83          }
84      }
85  }