[Jsxe-cvs] SF.net SVN: jsxe: [1122] trunk/jsxe
Status: Inactive
Brought to you by:
ian_lewis
From: <ian...@us...> - 2006-08-08 20:15:03
|
Revision: 1122 Author: ian_lewis Date: 2006-08-08 13:14:54 -0700 (Tue, 08 Aug 2006) ViewCVS: http://svn.sourceforge.net/jsxe/?rev=1122&view=rev Log Message: ----------- Added undo/redo support for attributes Modified Paths: -------------- trunk/jsxe/Changelog trunk/jsxe/src/net/sourceforge/jsxe/dom/AdapterNode.java trunk/jsxe/src/net/sourceforge/jsxe/dom/XMLDocument.java Modified: trunk/jsxe/Changelog =================================================================== --- trunk/jsxe/Changelog 2006-08-08 05:55:21 UTC (rev 1121) +++ trunk/jsxe/Changelog 2006-08-08 20:14:54 UTC (rev 1122) @@ -1,3 +1,7 @@ +08/08/2006 Ian Lewis <Ian...@me...> + + * Addded undo/redo support for attributes. + 08/07/2006 Ian Lewis <Ian...@me...> * Added temporary support for undo to jsXe. Modified: trunk/jsxe/src/net/sourceforge/jsxe/dom/AdapterNode.java =================================================================== --- trunk/jsxe/src/net/sourceforge/jsxe/dom/AdapterNode.java 2006-08-08 05:55:21 UTC (rev 1121) +++ trunk/jsxe/src/net/sourceforge/jsxe/dom/AdapterNode.java 2006-08-08 20:14:54 UTC (rev 1122) @@ -411,7 +411,7 @@ */ public void setNodeValue(String str) throws DOMException { // Make sure there is a change. - if (str != null && !str.equals(m_domNode.getNodeValue())) { + if (!MiscUtilities.equals(str, m_domNode.getNodeValue())) { String oldValue = getNodeValue(); m_domNode.setNodeValue(str); fireNodeValueChanged(this, oldValue, str); @@ -540,7 +540,7 @@ } int index = index(node); m_children.remove(node); - getOwnerDocument().addUndoableEdit(new RemoveNodeChange(this, node, index)); + // getOwnerDocument().addUndoableEdit(new RemoveNodeChange(this, node, index)); } else { //Remove from previous parent AdapterNode previousParent = node.getParentNode(); @@ -617,35 +617,36 @@ Element element = (Element)m_domNode; String prefix = MiscUtilities.getNSPrefixFromQualifiedName(name); - //check if we are setting a namespace declaration - if ("xmlns".equals(prefix)) { - //if so then make sure the value is valid - if (value != null && value.equals("")) { - throw new DOMException(DOMException.NAMESPACE_ERR, "An attempt was made to create an empty namespace declaration"); + String oldValue = getAttribute(name); + + if (!MiscUtilities.equals(oldValue, value)) { + //check if we are setting a namespace declaration + if ("xmlns".equals(prefix)) { + //if so then make sure the value is valid + if (value != null && value.equals("")) { + throw new DOMException(DOMException.NAMESPACE_ERR, "An attempt was made to create an empty namespace declaration"); + } } - } - - /* - If the attribute did not have a prefix to begin with then - using setAttributeNS may add a new attribute node to the element - even though the attribute already exists. - - Also, if adding or removing a prefix we need to remove the attribute - first so that namespace errors are thrown - */ - if (prefix != null && !prefix.equals("")) { - element.setAttributeNS(lookupNamespaceURI(prefix),name,value); - } else { + /* - setAttribute doesn't throw an error if the first character is - a ":" + If the attribute did not have a prefix to begin with then + using setAttributeNS may add a new attribute node to the element + even though the attribute already exists. */ - if (name != null && !name.equals("") && name.charAt(0)==':') { - throw new DOMException(DOMException.NAMESPACE_ERR, "An attribute name cannot have a ':' as the first character"); + if (prefix != null && !prefix.equals("")) { + element.setAttributeNS(lookupNamespaceURI(prefix),name,value); + } else { + /* + setAttribute doesn't throw an error if the first character + is a ":" + */ + if (name != null && !name.equals("") && name.charAt(0)==':') { + throw new DOMException(DOMException.NAMESPACE_ERR, "An attribute name cannot have a ':' as the first character"); + } + element.setAttribute(name, value); } - element.setAttribute(name, value); + fireAttributeChanged(this, name, oldValue, value); } - fireAttributeChanged(this, name); } else { throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Only Element Nodes can have attributes"); } @@ -664,8 +665,18 @@ Element element = (Element)m_domNode; if (prefix != null && !prefix.equals("")) { + //getAttributeNS returns "" even if the attribute isn't in the + //element. + if (element.getAttributeNodeNS(lookupNamespaceURI(prefix),localName) == null) { + return null; + } return element.getAttributeNS(lookupNamespaceURI(prefix),localName); } else { + //getAttribute returns "" even if the attribute isn't in the + //element. + if (element.getAttributeNode(name) == null) { + return null; + } return element.getAttribute(name); } } else { @@ -707,43 +718,46 @@ Element element = (Element)m_domNode; String prefix = MiscUtilities.getNSPrefixFromQualifiedName(attr); String localName = MiscUtilities.getLocalNameFromQualifiedName(attr); + String oldValue = getAttribute(attr); - //Check if we are removing a namespace declaration - //This is a somewhat expensive operation, may need to - //optimize in the future - if ("xmlns".equals(prefix)) { - //check if there are nodes using the namespace - String uri = lookupNamespaceURI(localName); - //check this element's namespace - if (!uri.equals(element.getNamespaceURI())) { - //check for decendent elements with this namespace - NodeList list = element.getElementsByTagName("*"); - //check if an attribute with this NS is used - for (int i=0; i<list.getLength(); i++) { - Node ele = list.item(i); - if (uri.equals(ele.getNamespaceURI())) { - throw new DOMException(DOMException.NAMESPACE_ERR, "An attempt was made to remove a namespace declaration when nodes exist that use it"); - } - //now check the attributes - NamedNodeMap attrs = ele.getAttributes(); - for (int j=0; j<attrs.getLength(); j++) { - Node foundAttr = attrs.item(i); - if (uri.equals(foundAttr.getNamespaceURI())) { + //make sure we are actually removing an attribute. + if (oldValue != null) { + //Check if we are removing a namespace declaration + //This is a somewhat expensive operation, may need to + //optimize in the future + if ("xmlns".equals(prefix)) { + //check if there are nodes using the namespace + String uri = lookupNamespaceURI(localName); + //check this element's namespace + if (!uri.equals(element.getNamespaceURI())) { + //check for decendent elements with this namespace + NodeList list = element.getElementsByTagName("*"); + //check if an attribute with this NS is used + for (int i=0; i<list.getLength(); i++) { + Node ele = list.item(i); + if (uri.equals(ele.getNamespaceURI())) { throw new DOMException(DOMException.NAMESPACE_ERR, "An attempt was made to remove a namespace declaration when nodes exist that use it"); } + //now check the attributes + NamedNodeMap attrs = ele.getAttributes(); + for (int j=0; j<attrs.getLength(); j++) { + Node foundAttr = attrs.item(i); + if (uri.equals(foundAttr.getNamespaceURI())) { + throw new DOMException(DOMException.NAMESPACE_ERR, "An attempt was made to remove a namespace declaration when nodes exist that use it"); + } + } } + } else { + throw new DOMException(DOMException.NAMESPACE_ERR, "An attempt was made to remove a namespace declaration when nodes exist that use it"); } + } + if (prefix != null && !prefix.equals("")) { + element.removeAttributeNS(lookupNamespaceURI(prefix),localName); } else { - throw new DOMException(DOMException.NAMESPACE_ERR, "An attempt was made to remove a namespace declaration when nodes exist that use it"); + element.removeAttribute(localName); } + fireAttributeChanged(this, attr, oldValue, null); } - - if (prefix != null && !prefix.equals("")) { - element.removeAttributeNS(lookupNamespaceURI(prefix),localName); - } else { - element.removeAttribute(localName); - } - fireAttributeChanged(this, attr); } else { throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Only Element Nodes can have attributes"); } @@ -927,11 +941,9 @@ if (node != null) { int index = index(node); m_children.remove(node); - if (index != -1) { - getOwnerDocument().addUndoableEdit(new RemoveNodeChange(this, node, index)); - } else { - Log.log(Log.DEBUG, this, "node not found"); - } + // if (index != -1) { + // getOwnerDocument().addUndoableEdit(new RemoveNodeChange(this, node, index)); + // } } }//}}} @@ -981,7 +993,7 @@ //{{{ fireNodeAdded() private void fireNodeAdded(AdapterNode source, AdapterNode child, int index) { - getOwnerDocument().addUndoableEdit(new AddNodeChange(source, child, index)); + // getOwnerDocument().addUndoableEdit(new AddNodeChange(source, child, index)); ListIterator iterator = m_listeners.listIterator(); while (iterator.hasNext()) { @@ -993,7 +1005,7 @@ //{{{ fireNodeRemoved() private void fireNodeRemoved(AdapterNode source, AdapterNode child, int index) { - getOwnerDocument().addUndoableEdit(new RemoveNodeChange(source, child, index)); + // getOwnerDocument().addUndoableEdit(new RemoveNodeChange(source, child, index)); ListIterator iterator = m_listeners.listIterator(); while (iterator.hasNext()) { @@ -1041,7 +1053,9 @@ }//}}} //{{{ fireAttributeChanged() - private void fireAttributeChanged(AdapterNode source, String attr) { + private void fireAttributeChanged(AdapterNode source, String attr, String oldValue, String newValue) { + getOwnerDocument().addUndoableEdit(new AttributeChange(this, attr, oldValue, newValue)); + ListIterator iterator = m_listeners.listIterator(); while (iterator.hasNext()) { AdapterNodeListener listener = (AdapterNodeListener)iterator.next(); Modified: trunk/jsxe/src/net/sourceforge/jsxe/dom/XMLDocument.java =================================================================== --- trunk/jsxe/src/net/sourceforge/jsxe/dom/XMLDocument.java 2006-08-08 05:55:21 UTC (rev 1121) +++ trunk/jsxe/src/net/sourceforge/jsxe/dom/XMLDocument.java 2006-08-08 20:14:54 UTC (rev 1122) @@ -203,7 +203,6 @@ */ protected boolean addUndoableEdit(UndoableEdit edit) { if (!getFlag(UNDO_IN_PROGRESS)) { - Log.log(Log.DEBUG, this, edit); if (insideCompoundEdit()) { m_addedToCompoundEdits = true; return m_compoundEdit.addEdit(edit); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |