From 71b6b252782bd6b5d2d8ed7380eac126ac7cf324 Mon Sep 17 00:00:00 2001 From: Derek Bankieris Date: Tue, 10 Mar 2015 15:49:24 -0500 Subject: [PATCH] Switched to using jaxb.index instead of enumerating classes in the call to JAXBContent.newInstance. Modified the makefile to copy jaxb.index files into the JAR. Removed now-unnecessary annotations from TVBean. Modified and exposed the save/open .tv file framework to subclasses so they can add additional information to .tv files. fixes #12 --- trick_source/java/makefile | 1 + .../java/src/trick/tv/TVApplication.java | 33 +++++++++++-------- trick_source/java/src/trick/tv/TVBean.java | 3 -- trick_source/java/src/trick/tv/jaxb.index | 10 ++++++ 4 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 trick_source/java/src/trick/tv/jaxb.index diff --git a/trick_source/java/makefile b/trick_source/java/makefile index 9bf165d3..aadfef33 100644 --- a/trick_source/java/makefile +++ b/trick_source/java/makefile @@ -20,6 +20,7 @@ space := $(empty) $(empty) CLASS_PATH = $(subst $(space),:,$(wildcard ${LIB_DIR}/*.jar)) RESOURCES = $(subst src/,,$(shell find src/trick -name resources)) +RESOURCES += $(subst src/,,$(shell find src/trick -name jaxb.index)) all: ${DIST_DIR}/trick.jar resources @echo "Java build successful" diff --git a/trick_source/java/src/trick/tv/TVApplication.java b/trick_source/java/src/trick/tv/TVApplication.java index 58e4d1b7..1b451b19 100644 --- a/trick_source/java/src/trick/tv/TVApplication.java +++ b/trick_source/java/src/trick/tv/TVApplication.java @@ -73,9 +73,12 @@ import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import javax.xml.namespace.QName; +import javax.xml.transform.stream.StreamSource; import joptsimple.OptionParser; import joptsimple.OptionSet; @@ -411,10 +414,7 @@ public class TVApplication extends RunTimeTrickApplication implements VariableLi // Initialize the JAXB elements. try { - JAXBContext jaxbContext = JAXBContext.newInstance(TVBean.class, - TVBoolean.class, TVByte.class, TVDouble.class, TVEnumeration.class, - TVFloat.class, TVInteger.class, TVLong.class, TVShort.class, - TVString.class); + JAXBContext jaxbContext = JAXBContext.newInstance("trick.tv"); marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); unmarshaller = jaxbContext.createUnmarshaller(); @@ -1767,13 +1767,13 @@ public class TVApplication extends RunTimeTrickApplication implements VariableLi * @param display whether or not to replace the current variable table contents with those in the file * @param set whether or not to send the values in the file to the Variable Server */ - void openFile(File file, boolean display, boolean set) { + protected void openFile(File file, boolean display, boolean set) { try { openFileAsBinary(file, display, set); } catch (Exception binaryException) { try { - restoreState((TVBean)unmarshaller.unmarshal(file), display, set); + restoreState((TVBean)unmarshaller.unmarshal(new StreamSource(file), TVBean.class).getValue(), display, set); } catch (Exception exception) { JOptionPane.showMessageDialog(getMainFrame(), exception, @@ -1856,9 +1856,7 @@ public class TVApplication extends RunTimeTrickApplication implements VariableLi * @param display whether or not to display the data * @param set whether or not to set the data's values */ - protected void restoreState(TVBean tvBean, boolean display, boolean set) - throws IOException, ClassNotFoundException, InstantiationException, - IllegalAccessException, JAXBException { + protected void restoreState(TVBean tvBean, boolean display, boolean set) { if (display) { removeAllStripCharts(); removeAllVariables(); @@ -1950,12 +1948,12 @@ public class TVApplication extends RunTimeTrickApplication implements VariableLi if (!file.exists() || (JOptionPane.showConfirmDialog(getMainFrame(),"Overwrite existing file?", "File Already Exists", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)) { try { - marshaller.marshal(new TVBean(this), file); + saveState(file); } - catch (JAXBException jaxbException) { - JOptionPane.showMessageDialog(getMainFrame(), jaxbException, + catch (Exception exception) { + JOptionPane.showMessageDialog(getMainFrame(), exception, "Failed to Save State", JOptionPane.ERROR_MESSAGE); - jaxbException.printStackTrace(System.err); + exception.printStackTrace(System.err); } } else { @@ -1964,6 +1962,15 @@ public class TVApplication extends RunTimeTrickApplication implements VariableLi } } + /** + * saves the variable table and strip charts to file + * + * @param file the target + */ + protected void saveState(File file) throws JAXBException { + marshaller.marshal(new JAXBElement(new QName("trickView"), TVBean.class, new TVBean(this)), file); + } + /** * determines how the Variable Tree is being sorted * diff --git a/trick_source/java/src/trick/tv/TVBean.java b/trick_source/java/src/trick/tv/TVBean.java index f9e51698..fe2f62ac 100644 --- a/trick_source/java/src/trick/tv/TVBean.java +++ b/trick_source/java/src/trick/tv/TVBean.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.HashSet; import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import org.jdesktop.swingx.JXFrame; @@ -23,8 +22,6 @@ import trick.common.utils.vs.Variable; import trick.sie.utils.SieEnumeration; import trick.stripchart.StripChart; -@XmlRootElement(name = "trickView") -@XmlType(name = "trickView") public class TVBean { public double cyclePeriod; diff --git a/trick_source/java/src/trick/tv/jaxb.index b/trick_source/java/src/trick/tv/jaxb.index new file mode 100644 index 00000000..f754b476 --- /dev/null +++ b/trick_source/java/src/trick/tv/jaxb.index @@ -0,0 +1,10 @@ +TVBean +TVBoolean +TVByte +TVDouble +TVEnumeration +TVFloat +TVInteger +TVLong +TVShort +TVString