@Override public void actionPerformed(ActionEvent event) { String parameterXml; try { DomElement domElement = parameterSupport.toDomElement(); parameterXml = domElement.toXml(); } catch (Exception e) { Debug.trace(e); Dialogs.showError(TITLE, "Failed to convert parameters to XML." + e.getMessage()); return; } JTextArea textArea = new JTextArea(parameterXml); textArea.setEditable(false); JScrollPane textAreaScrollPane = new JScrollPane(textArea); textAreaScrollPane.setPreferredSize(new Dimension(360, 360)); showInformationDialog(getOperatorName() + " Parameters", textAreaScrollPane); }
@Override public void actionPerformed(ActionEvent event) { JFileChooser fileChooser = new JFileChooser(); final FileNameExtensionFilter parameterFileFilter = createParameterFileFilter(); fileChooser.addChoosableFileFilter(parameterFileFilter); fileChooser.setFileFilter(parameterFileFilter); fileChooser.setDialogTitle(TITLE); fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); applyCurrentDirectory(fileChooser); int response = fileChooser.showDialog(parentComponent, "Save"); if (JFileChooser.APPROVE_OPTION == response) { try { preserveCurrentDirectory(fileChooser); File selectedFile = fileChooser.getSelectedFile(); selectedFile = FileUtils.ensureExtension(selectedFile, "." + parameterFileFilter.getExtensions()[0]); DomElement domElement = parameterSupport.toDomElement(); escapeXmlElements(domElement); String xmlString = domElement.toXml(); writeToFile(xmlString, selectedFile); } catch (Exception e) { Debug.trace(e); Dialogs.showError(TITLE, "Could not load parameters.\n" + e.getMessage()); } } }
@Test public void testStoreAndLoadParameterWhenNotAllParamsAreGiven() throws IOException, ValidationException, ConversionException { final OperatorParameterSupport support = new OperatorParameterSupport(new TestOpSpi().getOperatorDescriptor()); PropertySet container = support.getPropertySet(); container.setValue("paramDouble", 0.42); final DomElement domElement = support.toDomElement(); // change container container.setValue("paramDouble", 23.67); container.setValue("paramString", "Another String"); container.setValue("paramComplex", new Complex(17)); support.fromDomElement(domElement); assertEquals(0.42, (double)support.getPropertySet().getValue("paramDouble"), 1.0e-6); assertEquals(null, support.getPropertySet().getValue("paramString")); assertEquals(null, support.getPropertySet().getValue("paramComplex")); }
@Test public void testStoreAndLoadParameter() throws IOException, ValidationException, ConversionException { final OperatorParameterSupport support = new OperatorParameterSupport(new TestOpSpi().getOperatorDescriptor()); PropertySet container = support.getPropertySet(); container.setValue("paramDouble", 0.42); container.setValue("paramString", "A String!"); container.setValue("paramComplex", new Complex(25)); final DomElement domElement = support.toDomElement(); assertEquals("parameters", domElement.getName()); assertEquals(3, domElement.getChildCount()); assertNotNull(domElement.getChild("paramDouble")); assertEquals("0.42", domElement.getChild("paramDouble").getValue()); assertNotNull(domElement.getChild("paramString")); assertEquals("A String!", domElement.getChild("paramString").getValue()); assertNotNull(domElement.getChild("paramComplex")); assertNotNull(domElement.getChild("paramComplex").getChild("complexInt")); assertEquals("25", domElement.getChild("paramComplex").getChild("complexInt").getValue()); // change container container.setValue("paramDouble", 23.67); container.setValue("paramString", "Another String"); container.setValue("paramComplex", new Complex(17)); support.fromDomElement(domElement); assertEquals(0.42, (double)support.getPropertySet().getValue("paramDouble"), 1.0e-6); assertEquals("A String!", support.getPropertySet().getValue("paramString")); assertEquals(new Complex(25), support.getPropertySet().getValue("paramComplex")); }