Example usage for java.beans XMLEncoder writeObject

List of usage examples for java.beans XMLEncoder writeObject

Introduction

In this page you can find the example usage for java.beans XMLEncoder writeObject.

Prototype

public void writeObject(Object o) 

Source Link

Document

Write an XML representation of the specified object to the output.

Usage

From source file:com.alvermont.terraj.fracplanet.ui.AbstractTerrainViewerFrame.java

private void saveParamsItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveParamsItemActionPerformed
{//GEN-HEADEREND:event_saveParamsItemActionPerformed

    final int choice = this.xmlChooser.showSaveDialog(this);

    if (choice == JFileChooser.APPROVE_OPTION) {
        if (!this.xmlChooser.getSelectedFile().isFile() || (JOptionPane.showConfirmDialog(this,
                "This file already exists. Do you want to\n" + "overwrite it?", "Replace File?",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)) {
            try {
                final File target = FileUtils.addExtension(this.xmlChooser.getSelectedFile(), ".xml");

                final XMLEncoder enc = new XMLEncoder(new FileOutputStream(target));

                getParameters().getRenderParameters().setCameraPosition(display.getCameraPosition());
                getParameters().getCameraPositionParameters().setPositions(this.cameraPosDialog.getPositions());

                enc.setPersistenceDelegate(TerrainParameters.ObjectTypeEnum.class,
                        new EnumDelegate<TerrainParameters.ObjectTypeEnum>(
                                TerrainParameters.ObjectTypeEnum.class));

                enc.writeObject(getParameters());

                enc.close();/*from  ww  w . j  ava2 s .  c o m*/
            } catch (IOException ioe) {
                log.error("Error writing xml file", ioe);

                JOptionPane.showMessageDialog(this,
                        "Error: " + ioe.getMessage() + "\nCheck log file for full details", "Error Saving",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}

From source file:com.alvermont.terraj.planet.ui.MainFrame.java

private void saveParamsItemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_saveParamsItemActionPerformed
{//GEN-HEADEREND:event_saveParamsItemActionPerformed

    final int choice = xmlChooser.showSaveDialog(this);

    if (choice == JFileChooser.APPROVE_OPTION) {
        try {/*from w w w  .  j  a  v  a  2 s . c o m*/
            if (!xmlChooser.getFileContents().canRead() || (JOptionPane.showConfirmDialog(this,
                    "This file already exists. Do you want to\n" + "overwrite it?", "Replace File?",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)) {
                final OutputStream target = xmlChooser.getFileContents().getOutputStream(true);

                final XMLEncoder enc = new XMLEncoder(target);

                enc.writeObject(params);

                enc.close();
                target.close();
            }
        } catch (IOException ioe) {
            log.error("Error writing xml file", ioe);

            JOptionPane.showMessageDialog(this,
                    "Error: " + ioe.getMessage() + "\nCheck log file for full details", "Error Saving",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

From source file:net.sf.taverna.t2.provenance.lineageservice.EventProcessor.java

/**
 * log raw event to file system/*from www. j a  v a2s .c  om*/
 * 
 * @param content
 * @param eventType
 * @throws IOException
 */
public void saveEvent(ProvenanceItem provenanceItem, SharedVocabulary eventType) throws IOException {

    // HACK -- XMLEncoder fails on IterationEvents and there is no way to catch the exception...
    // so avoid this case
    if (eventType.equals(SharedVocabulary.ITERATION_EVENT_TYPE)) {
        return;
    }

    //      System.out.println("saveEvent: start");

    File f1 = null;

    f1 = new File(TEST_EVENTS_FOLDER);
    FileUtils.forceMkdir(f1);

    String fname = "event_" + eventCnt++ + "_" + eventType + ".xml";
    File f = new File(f1, fname);

    //      FileWriter fw = new FileWriter(f);

    XMLEncoder en = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(f)));

    en.setExceptionListener(new ExceptionListener() {
        public void exceptionThrown(Exception e) {
            logger.warn("XML encoding ERROR", e);
            return;
        }
    });

    logger.debug("saving to " + f); // save event for later inspection
    logger.debug(provenanceItem);

    en.writeObject(provenanceItem);

    logger.debug("writer ok");
    en.close();
    logger.debug("closed");

    //      fw.write(content);
    //      fw.flush();
    //      fw.close();

    //      FileWriter fw = new FileWriter(f);
    //      fw.write(content);
    //      fw.flush();
    //      fw.close();

    //      System.out.println("saved as file " + fname);

}

From source file:statechum.analysis.learning.Visualiser.java

protected void setVisualiserKeyBindings() {
    persistAction = new graphAction("saveLayout", "save the layout of the visible graph") {

        /** Serial number. */
        private static final long serialVersionUID = 1L;

        @Override/*  w  ww  .j  av  a  2 s . co m*/
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            XMLEncoder encoder = null;
            try {
                if (propName >= 0) {
                    String fileName = getLayoutFileName(graphs.get(currentGraph));
                    encoder = new XMLEncoder(new FileOutputStream(fileName));
                    Map<Integer, DoublePair> layout = ((XMLPersistingLayout) viewer.getModel().getGraphLayout())
                            .persist();
                    encoder.writeObject(layout);
                    XMLAffineTransformSerialised trV = new XMLAffineTransformSerialised();
                    trV.setFromAffineTransform(viewer.getViewTransformer().getTransform());
                    encoder.writeObject(trV);
                    XMLAffineTransformSerialised trL = new XMLAffineTransformSerialised();
                    trL.setFromAffineTransform(viewer.getLayoutTransformer().getTransform());
                    encoder.writeObject(trL);
                    ((XMLModalGraphMouse) viewer.getGraphMouse()).store(encoder);
                    encoder.writeObject(layoutOptions.get(currentGraph));
                    encoder.close();
                    encoder = null;
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            } finally {
                if (encoder != null) {
                    encoder.close();
                    encoder = null;
                }
            }
        }
    };
    keyToActionMap.put(KeyEvent.VK_F2, persistAction);
    keyToActionMap.put(KeyEvent.VK_F3,
            new graphAction("loadLayout", "loads the previously saved layout into the visible graph") {

                /** Serial number. */
                private static final long serialVersionUID = 2L;

                @Override
                public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
                    reloadLayout(false, true);
                }
            });
    keyToActionMap.put(KeyEvent.VK_F9,
            new graphAction("loadPreviousLayout", "loads the layout of the previous graph in the list") {

                /** Serial number. */
                private static final long serialVersionUID = 3L;

                @Override
                public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
                    if (currentGraph > 0) {
                        restoreLayout(false, currentGraph - 1);
                    }
                }
            });

    pickAction = new graphAction("pick", "Switches Jung into picking mode") {

        /** Serial number. */
        private static final long serialVersionUID = 7L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            ((XMLModalGraphMouse) viewer.getGraphMouse()).setMode(ModalGraphMouse.Mode.PICKING);
        }
    };
    keyToActionMap.put(KeyEvent.VK_F11, pickAction);

    transformAction = new graphAction("transform", "Switches Jung into transformation mode") {

        /** Serial number. */
        private static final long serialVersionUID = 8L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            ((XMLModalGraphMouse) viewer.getGraphMouse()).setMode(ModalGraphMouse.Mode.TRANSFORMING);
        }
    };
    keyToActionMap.put(KeyEvent.VK_F12, transformAction);

    keyToActionMap.put(KeyEvent.VK_UP, new graphAction("previous", "loads the previous graph") {

        /** Serial number. */
        private static final long serialVersionUID = 9L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            if (currentGraph > 0) {
                --currentGraph;
                reloadLayout(false, true);
            }
        }
    });
    keyToActionMap.put(KeyEvent.VK_DOWN, new graphAction("next", "loads the next graph") {

        /** Serial number. */
        private static final long serialVersionUID = 10L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            if (currentGraph < graphs.size() - 1) {
                ++currentGraph;
                reloadLayout(false, true);
            }
        }
    });

    keyToActionMap.put(KeyEvent.VK_F, new graphAction("negatives", "toggles negatives on or off") {

        /** Serial number. */
        private static final long serialVersionUID = 11L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            LayoutOptions options = layoutOptions.get(currentGraph);
            if (options != null) {
                options.showNegatives = !options.showNegatives;
                reloadLayout(false, false);
            }
        }
    });
    keyToActionMap.put(KeyEvent.VK_I, new graphAction("ignored states", "toggles ignored states on or off") {

        /** Serial number. */
        private static final long serialVersionUID = 12L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            LayoutOptions options = layoutOptions.get(currentGraph);
            if (options != null) {
                options.showIgnored = !options.showIgnored;
                reloadLayout(false, false);
            }
        }
    });
    keyToActionMap.put(KeyEvent.VK_PAGE_DOWN, new graphAction("refine", "reduces the abstraction level") {

        /** Serial number. */
        private static final long serialVersionUID = 13L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            LayoutOptions options = layoutOptions.get(currentGraph);
            if (options != null && options.componentsToPick < Integer.MAX_VALUE) {
                options.componentsToPick++;
                reloadLayout(false, false);
            }
        }
    });
    keyToActionMap.put(KeyEvent.VK_PAGE_UP, new graphAction("abstract", "increases abstraction level") {

        /** Serial number. */
        private static final long serialVersionUID = 14L;

        @Override
        public void actionPerformed(@SuppressWarnings("unused") ActionEvent e) {
            LayoutOptions options = layoutOptions.get(currentGraph);
            if (options != null && options.componentsToPick != Integer.MAX_VALUE
                    && options.componentsToPick > 0) {
                --options.componentsToPick;
                reloadLayout(false, false);
            }
        }
    });
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private void writeStatusColors(Map<HttpStatus.Type, ColorScheme> colors) {
    File appPath = getStartupApplicationPath();
    File file = new File(appPath, STATUS_COLORS_XML_FILENAME);
    try {/*from w  w w  . jav  a2 s .co m*/
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        XMLEncoder e = new XMLEncoder(bos);
        PersistenceDelegate delegate = new EnumPersistenceDelegate();
        e.setPersistenceDelegate(HttpStatus.Type.class, delegate);
        e.writeObject(colors);
        e.close();
    } catch (Throwable ex) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while writing colors!", ex);
        IOUtilities.interruptIfNecessary(ex);
    }
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private void writeLevelColors(Map<LoggingEvent.Level, ColorScheme> colors) {
    File appPath = getStartupApplicationPath();
    File file = new File(appPath, LEVEL_COLORS_XML_FILENAME);
    try {/*  ww w . j a  va  2s.  c om*/
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        XMLEncoder e = new XMLEncoder(bos);
        PersistenceDelegate delegate = new EnumPersistenceDelegate();
        e.setPersistenceDelegate(LoggingEvent.Level.class, delegate);
        e.writeObject(colors);
        e.close();
    } catch (Throwable ex) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while writing colors!", ex);
        IOUtilities.interruptIfNecessary(ex);
    }
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private boolean writeSourceLists(Map<String, Set<String>> sourceLists) {
    File appPath = getStartupApplicationPath();
    File file = new File(appPath, SOURCE_LISTS_XML_FILENAME);
    XMLEncoder e = null;
    Throwable error = null;/*w w w.  j a va2s  . c  om*/
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        e = new XMLEncoder(bos);
        e.writeObject(sourceLists);
    } catch (FileNotFoundException ex) {
        error = ex;
    } finally {
        if (e != null) {
            e.close();
        }
    }
    if (error != null) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while writing source lists!", error);
        return false;
    }
    return true;
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private boolean writeRecentFiles(List<String> recentFiles) {
    File appPath = getStartupApplicationPath();
    File file = new File(appPath, RECENT_FILES_XML_FILENAME);
    XMLEncoder e = null;
    Throwable error = null;/*w  ww  .j a  v a 2 s.  c  o  m*/
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        e = new XMLEncoder(bos);
        e.writeObject(recentFiles);
    } catch (FileNotFoundException ex) {
        error = ex;
    } finally {
        if (e != null) {
            e.close();
        }
    }
    this.recentFiles = null;
    if (error != null) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while writing recentFiles!", error);
        return false;
    }
    return true;
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private boolean writePreviousSearchStrings(List<String> searchStrings) {
    File appPath = getStartupApplicationPath();
    File file = new File(appPath, PREVIOUS_SEARCH_STRINGS_XML_FILENAME);
    XMLEncoder e = null;
    Throwable error = null;//  w  ww . ja v  a 2  s . c om
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        e = new XMLEncoder(bos);
        e.writeObject(searchStrings);
    } catch (FileNotFoundException ex) {
        error = ex;
    } finally {
        if (e != null) {
            e.close();
        }
    }
    this.previousSearchStrings = null;
    if (error != null) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while writing previous search strings!", error);
        return false;
    }
    return true;
}

From source file:de.huxhorn.lilith.swing.ApplicationPreferences.java

private boolean writeColumnLayout(File file,
        List<PersistentTableColumnModel.TableColumnLayoutInfo> layoutInfos) {
    XMLEncoder e = null;
    Throwable error = null;/* w  w  w  .j av a  2s . c  om*/
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        e = new XMLEncoder(bos);
        e.writeObject(layoutInfos);
        if (logger.isInfoEnabled())
            logger.info("Wrote layouts {} to file '{}'.", layoutInfos, file.getAbsolutePath());
    } catch (FileNotFoundException ex) {
        error = ex;
    } finally {
        if (e != null) {
            e.close();
        }
    }
    if (error != null) {
        if (logger.isWarnEnabled())
            logger.warn("Exception while writing layouts to file '" + file.getAbsolutePath() + "'!", error);
        return false;
    }
    return true;
}