/** * @return Returns the filename hint for an attribute set. The first take is the attribute sets * resource associated with the algorithm. If this fails, we try to name the file after the * algorithm itself. */ static IPath getDefaultHint(String componentId, String prefix) { final ProcessingComponentDescriptor component = WorkbenchCorePlugin.getDefault().getComponent(componentId); String nameHint = component.getAttributeSetsResource(); if (StringUtils.isBlank(nameHint)) { // Try a fallback. nameHint = FileDialogs.sanitizeFileName(prefix + componentId + "-attributes.xml"); } return new Path(nameHint); }
/** Open attributes from an XML file. May return an empty value set, but never null. */ static AttributeValueSets openAttributes() { final IPath pathHint = FileDialogs.recallPath(REMEMBER_DIRECTORY); final IPath readLocation = FileDialogs.openReadXML(pathHint); if (readLocation != null) { FileDialogs.rememberDirectory(REMEMBER_DIRECTORY, readLocation); try { final Persister persister = new Persister(); final AttributeValueSets avs = persister.read(AttributeValueSets.class, readLocation.toFile()); return avs; } catch (Exception e) { Utils.showError( new Status( IStatus.ERROR, WorkbenchCorePlugin.PLUGIN_ID, "Failed to read attributes from: " + readLocation.toOSString(), e)); } } return new AttributeValueSets(); }
/** Save attributes to an XML file. */ static void saveAttributes(IPath filenameHint, AttributeValueSets attributes) { final IPath pathHint = filenameHint.isAbsolute() ? filenameHint : FileDialogs.recallPath(REMEMBER_DIRECTORY).append(filenameHint); final Path saveLocation = FileDialogs.openSaveXML(pathHint); if (saveLocation != null) { try { final Persister persister = new Persister(new Format(2)); persister.write(attributes, saveLocation.toFile()); } catch (Exception e) { Utils.showError( new Status( IStatus.ERROR, WorkbenchCorePlugin.PLUGIN_ID, "An error occurred while saving attributes.", e)); } FileDialogs.rememberDirectory(REMEMBER_DIRECTORY, saveLocation); } }