* @return a writable style element
*/
protected OdfStyle getWritableStyleElementByName(String styleName, boolean isShared) {
boolean createNew = isShared;
OdfStyle styleElement = null;
OdfDefaultStyle defaultStyleElement = null;
if (styleName == null || (styleName.equals(""))) {
createNew = true;
// get from default style element
defaultStyleElement = mDocument.getDocumentStyles().getDefaultStyle(mOdfElement.getStyleFamily());
} else {
OdfOfficeAutomaticStyles styles = mOdfElement.getAutomaticStyles();
styleElement = styles.getStyle(styleName, mOdfElement.getStyleFamily());
// If not default cell style definition,
// Try to find if the style is defined in document styles
if (styleElement == null && defaultStyleElement == null) {
styleElement = mDocument.getDocumentStyles().getStyle(styleName, mOdfElement.getStyleFamily());
}
if (styleElement == null && defaultStyleElement == null) {
styleElement = mOdfElement.getDocumentStyle();
}
if (styleElement == null || styleElement.getStyleUserCount() > 1) {
createNew = true;
}
}
// if style name is null or this style are used by many users,
// should create a new one.
if (createNew) {
OdfStyle newStyle = mOdfElement.getAutomaticStyles().newStyle(mOdfElement.getStyleFamily());
if (styleElement != null) {
newStyle.setProperties(styleElement.getStylePropertiesDeep());
// copy attributes
NamedNodeMap attributes = styleElement.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attr = attributes.item(i);
if (!attr.getNodeName().equals("style:name")) {
newStyle.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
}
}// end of copying attributes
} else if (defaultStyleElement != null) {
newStyle.setProperties(defaultStyleElement.getStylePropertiesDeep());
// copy attributes
NamedNodeMap attributes = defaultStyleElement.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Node attr = attributes.item(i);
if (!attr.getNodeName().equals("style:name")) {
newStyle.setAttributeNS(attr.getNamespaceURI(), attr.getNodeName(), attr.getNodeValue());
}