Changeset 3132

Show
Ignore:
Timestamp:
03/02/10 14:16:10 (5 months ago)
Author:
martijn
Message:

Make visualization writing more robust,
by reporting an error when it fails,
and by writing to a tempory file, then renaming that file

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugins/visualization/org/pathvisio/visualization/gui/VisualizationDialog.java

    r2979 r3132  
    2020import java.awt.Frame; 
    2121 
     22import javax.swing.JOptionPane; 
    2223import javax.swing.JScrollPane; 
    2324 
     25import org.pathvisio.debug.Logger; 
    2426import org.pathvisio.gui.swing.dialogs.OkCancelDialog; 
    2527import org.pathvisio.visualization.Visualization; 
     
    6264 
    6365        protected void okPressed() { 
    64                 visMgr.saveXML(); //Save the settings 
     66                try 
     67                { 
     68                        visMgr.saveXML(); //Save the settings 
     69                } 
     70                catch (Exception ex) 
     71                { 
     72                        Logger.log.error ("Couldn't save visualization", ex); 
     73                        JOptionPane.showMessageDialog(this,"Couldn't write modifications to disk.\n" + ex.getMessage() +  
     74                                        "\nSee error log for details.",   
     75                                        "Couldn't save visualization", JOptionPane.ERROR_MESSAGE); 
     76                } 
    6577                super.okPressed(); 
    6678        } 
  • trunk/src/swing/org/pathvisio/visualization/VisualizationManager.java

    r3120 r3132  
    279279        public static final String ROOT_XML_ELEMENT = "expression-data-visualizations"; 
    280280 
    281         public InputStream getXmlInput() 
     281        private InputStream getXmlInput() 
    282282        { 
    283283                File xmlFile = new File(gexManager.getCurrentGex().getDbName() + ".xml"); 
     
    293293        } 
    294294 
    295         public  OutputStream getXmlOutput() { 
    296                 try { 
    297                         File f = new File(gexManager.getCurrentGex().getDbName() + ".xml"); 
    298                         Logger.log.trace("Visualization settings will be saved to: " + f); 
    299                         OutputStream out = new FileOutputStream(f); 
    300                         return out; 
    301                 } catch(Exception e) { 
    302                         Logger.log.error("Unable to create visualization settings file", e); 
    303                         return null; 
    304                 } 
    305         } 
    306  
    307         public  void saveXML() { 
     295        public void saveXML() throws IOException 
     296        { 
    308297                if(!gexManager.isConnected()) return; 
    309298 
    310                 OutputStream out = getXmlOutput(); 
     299                // write to a temporary file, rename to final file after write was successful. 
     300                File finalFile = new File(gexManager.getCurrentGex().getDbName() + ".xml"); 
     301                File tempFile = File.createTempFile(finalFile.getName(), ".tmp", finalFile.getParentFile()); 
     302                OutputStream out = new FileOutputStream(tempFile); 
    311303 
    312304                Logger.log.trace("Saving visualizations and color sets to xml: " + out); 
     
    318310 
    319311                Element vis = new Element(XML_ELEMENT); 
    320  
    321312                for(Visualization v : getVisualizations()) { 
    322313                        vis.addContent(v.toXML()); 
     
    325316 
    326317                XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); 
    327  
    328                 try { 
    329                         xmlOut.output(xmlDoc, out); 
    330                         out.close(); 
    331                 } catch(IOException e) { 
    332                         Logger.log.error("Unable to save visualization settings", e); 
    333                 } 
     318                xmlOut.output(xmlDoc, out); 
     319                out.close(); 
     320 
     321                if (finalFile.exists()) finalFile.delete(); 
     322                if (!tempFile.renameTo(finalFile)) throw new IOException ("Couldn't rename temporary file"); 
    334323        } 
    335324 
     
    369358                        doc = parser.build(in); 
    370359                        in.close(); 
    371  
    372360                        root = doc.getRootElement(); 
    373361                } catch(Exception e) { 
     
    375363                        root = new Element(ROOT_XML_ELEMENT); 
    376364                        doc.setRootElement(root); 
    377  
    378365                } 
    379366                return doc;