DocumentReader.Interpretation interp = DocumentReader.read(file);
// If it's somehow a saved document:
if (interp != null) {
XmlDocument xml = interp.xml;
File imageFile = interp.imageFile;
ImageInfo info = interp.info;
if (imageFile == null) {
// This is a symptom of a template file.
if (file.getName().endsWith(".lzt")) {
int option = Env.getAlertDialog().showAlert(
frame,
LOCALE.get("TemplateQuestionMajor"),
LOCALE.get("TemplateQuestionMinor"),
AlertDialog.WARNING_ALERT,
LOCALE.get("TemplateImportOption"),
LOCALE.get("TemplateOpenOption"),
LOCALE.get("TemplateCancelOption")
);
if (option == 0) {
try {
InputStream in = new FileInputStream(file);
XmlDocument template = new XmlDocument(in);
TemplateKey key = TemplateKey.importKey(file);
TemplateDatabase.addTemplateDocument(
template, key, true
);
TemplateList.showDialog(frame);
}
catch (Throwable t) {
showError(
LOCALE.get(
"TemplateImportError", file.getName()
),
t, frame
);
}
return null;
}
else if (option == 2) {
return null;
}
// option == 1, let the initialization continue...
}
// LightweightDocument couldn't figure out the original image
// path, so let Document throw its MissingImageFileException:
doc = new Document(xml, null, info, cancel);
}
else {
// Check for a missing image file:
boolean hunted = false;
if (! imageFile.exists()) {
// Isolate the image file name, admitting both unix and
// windows path syntax:
String imageFileName =
imageFile.getAbsolutePath().replaceAll(
".*[/\\\\]", ""
);
// Try the DocumentDatabase:
File[] files =
DocumentDatabase.findImageFiles(imageFileName);
// Maybe check the Document file's directory directly,
// since DocumentDatabase is sometimes disabled:
if (files.length == 0) {
File docDir = file.getParentFile();
File altImageFile = new File(docDir, imageFileName);
if (altImageFile.isFile()) {
files = new File[1];
files[0] = altImageFile;
}
}
imageFile = DocumentImageSelector.chooseImageFile(
file, imageFile, files, LastOpenPath, frame
);
if (imageFile == null) {
// User cancelled.
return null;
}
hunted = true;
}
ImageInfo imageFileInfo = ImageInfo.getInstanceFor(imageFile);
ImageMetadata meta = imageFileInfo.getMetadata();
// Read the saved document:
doc = new Document(xml, meta, info, cancel);
if (hunted) {
doc.markDirty();
}
}
DocumentDatabase.addDocumentFile(file);
}
else {
// Maybe it's an image:
ImageInfo info = ImageInfo.getInstanceFor(file);
ImageMetadata meta = info.getMetadata();
ImageType type = info.getImageType();
// Maybe set up a template with default tools:
XmlDocument xml = null;
// First look for default settings in the user-defined templates:
TemplateKey template = TemplateDatabase.getDefaultTemplate(meta);
if (template != null) {
try {