Java Reference
In-Depth Information
JOptionPane.showConfirmDialog(SketchFrame.this,
file.getName()+" exists. Overwrite?",
"Confirm Save As",
JOptionPane.YES _ NO _ OPTION,
JOptionPane.WARNING _ MESSAGE))
return; // No overwrite
}
saveXMLSketch(file);
}
}
}
This is very similar to code that appears in the FileAction class. The constructor only provides for
what we use - a menu item name plus a tooltip. If you want to have the option for an icon for use on a
toolbar button, you can add that in the same way as for the FileAction constructors. The
actionPerformed() method pops up a CFFileChooser dialog to enable the destination file for the
XML to be selected. The chosen file is passed to a new method that we will put together,
saveXMLSketch() , which will handle writing the XML document to the file.
We can define the XMLImportAction inner class like this:
class XMLImportAction extends AbstractAction {
public XMLImportAction(String name, String tooltip) {
super(name);
if(tooltip != null) // If there is tooltip text
putValue(SHORT _ DESCRIPTION, tooltip); // ...squirrel it away
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(DEFAULT _ DIRECTORY);
chooser.setDialogTitle("Import Sketch from XML");
chooser.setApproveButtonText("Import");
ExtensionFilter xmlFiles = new ExtensionFilter(".xml",
"XML Sketch files (*.xml)");
chooser.addChoosableFileFilter(xmlFiles); // Add the filter
chooser.setFileFilter(xmlFiles); // and select it
int result = chooser.showDialog(SketchFrame.this, null); // Show dialog
if(chooser.showDialog(SketchFrame.this, null) == chooser.APPROVE _ OPTION)
openXMLSketch(chooser.getSelectedFile());
}
}
This is more of the same but in the opposite direction as Stanley might have said. Once the name of the
file to be imported has been identified in the JFileChooser dialog, we call openXMLSketch() to
read the XML from the file and create the corresponding sketch.
Now we can go on to the slightly more difficult bits. We will start by looking at how we can write an
XML document to a file, since we can't test the process for reading a sketch as XML until we have
written some.
Search WWH ::




Custom Search