Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsItemElement.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla XForms support.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsXFormsContextContainer.h"
00040 #include "nsIXFormsSelectChild.h"
00041 #include "nsXFormsStubElement.h"
00042 #include "nsIDOMHTMLOptionElement.h"
00043 #include "nsXFormsAtoms.h"
00044 #include "nsIDOMNodeList.h"
00045 #include "nsIDOMDocument.h"
00046 #include "nsString.h"
00047 #include "nsXFormsUtils.h"
00048 #include "nsIXFormsValueElement.h"
00049 #include "nsVoidArray.h"
00050 #include "nsIDOMText.h"
00051 #include "nsIXTFBindableElementWrapper.h"
00052 #include "nsIXFormsContextControl.h"
00053 #include "nsIModelElementPrivate.h"
00054 #include "nsIXFormsItemElement.h"
00055 #include "nsIXFormsControl.h"
00056 #include "nsIDocument.h"
00057 #include "nsXFormsModelElement.h"
00058 #include "nsIXFormsCopyElement.h"
00059 #include "nsIDOMEventTarget.h"
00060 #include "nsIXFormsDelegate.h"
00061 #include "nsIXFormsAccessors.h"
00062 
00070 class nsXFormsItemElement : public nsXFormsContextContainer,
00071                             public nsIXFormsSelectChild,
00072                             public nsIXFormsItemElement
00073 {
00074 public:
00075   nsXFormsItemElement() : mDoneAddingChildren(PR_FALSE),
00076                           mIsCopyItem(PR_FALSE)
00077   {
00078   }
00079 
00080   NS_DECL_ISUPPORTS_INHERITED
00081   NS_DECL_NSIXFORMSITEMELEMENT
00082 
00083   NS_IMETHOD OnCreated(nsIXTFBindableElementWrapper *aWrapper);
00084 
00085   // nsIXTFElement overrides
00086   NS_IMETHOD ParentChanged(nsIDOMElement *aNewParent);
00087   NS_IMETHOD ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex);
00088   NS_IMETHOD ChildAppended(nsIDOMNode *aChild);
00089   NS_IMETHOD WillRemoveChild(PRUint32 aIndex);
00090   NS_IMETHOD BeginAddingChildren();
00091   NS_IMETHOD DoneAddingChildren();
00092 
00093   NS_IMETHOD Refresh();
00094 
00095   // nsIXFormsRepeatItemElement
00096   NS_IMETHOD SetIndexState(PRBool aHasIndex);
00097   NS_IMETHOD GetIndexState(PRBool *aHasIndex);
00098 
00099   // nsIXFormsSelectChild
00100   NS_DECL_NSIXFORMSSELECTCHILD
00101 
00102 
00103 private:
00104   PRBool         mDoneAddingChildren;
00105 
00106   // If true, indicates that this item contains a xf:copy element (via
00107   // xf:itemset) rather than a xf:value element
00108   PRBool         mIsCopyItem;
00109 };
00110 
00111 NS_IMPL_ISUPPORTS_INHERITED2(nsXFormsItemElement,
00112                              nsXFormsContextContainer,
00113                              nsIXFormsSelectChild,
00114                              nsIXFormsItemElement)
00115 
00116 NS_IMETHODIMP
00117 nsXFormsItemElement::OnCreated(nsIXTFBindableElementWrapper *aWrapper)
00118 {
00119   nsresult rv = nsXFormsBindableStub::OnCreated(aWrapper);
00120   NS_ENSURE_SUCCESS(rv, rv);
00121   
00122   aWrapper->SetNotificationMask(nsIXTFElement::NOTIFY_PARENT_CHANGED |
00123                                 nsIXTFElement::NOTIFY_CHILD_INSERTED |
00124                                 nsIXTFElement::NOTIFY_CHILD_APPENDED |
00125                                 nsIXTFElement::NOTIFY_WILL_REMOVE_CHILD |
00126                                 nsIXTFElement::NOTIFY_BEGIN_ADDING_CHILDREN |
00127                                 nsIXTFElement::NOTIFY_DONE_ADDING_CHILDREN);
00128 
00129   nsCOMPtr<nsIDOMElement> node;
00130   aWrapper->GetElementNode(getter_AddRefs(node));
00131 
00132   // It's ok to keep a pointer to mElement.  mElement will have an
00133   // owning reference to this object, so as long as we null out mElement in
00134   // OnDestroyed, it will always be valid.
00135 
00136   mElement = node;
00137   NS_ASSERTION(mElement, "Wrapper is not an nsIDOMElement, we'll crash soon");
00138 
00139   return NS_OK;
00140 }
00141 
00142 NS_IMETHODIMP
00143 nsXFormsItemElement::ParentChanged(nsIDOMElement *aNewParent)
00144 {
00145   if (aNewParent) {
00146     Refresh();
00147   }
00148 
00149   return NS_OK;
00150 }
00151 
00152 NS_IMETHODIMP
00153 nsXFormsItemElement::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
00154 {
00155   // If a label child was inserted, we need to clone it into our
00156   // anonymous content.
00157 
00158   if (nsXFormsUtils::IsLabelElement(aChild))
00159     Refresh();
00160 
00161   return NS_OK;
00162 }
00163 
00164 NS_IMETHODIMP
00165 nsXFormsItemElement::ChildAppended(nsIDOMNode *aChild)
00166 {
00167   // If a label child was inserted, we need to clone it into our
00168   // anonymous content.
00169 
00170   if (nsXFormsUtils::IsLabelElement(aChild))
00171     Refresh();
00172 
00173   return NS_OK;
00174 }
00175 
00176 NS_IMETHODIMP
00177 nsXFormsItemElement::WillRemoveChild(PRUint32 aIndex)
00178 {
00179   nsCOMPtr<nsIDOMNodeList> children;
00180   nsresult rv = mElement->GetChildNodes(getter_AddRefs(children));
00181   NS_ENSURE_SUCCESS(rv, rv);
00182 
00183   nsCOMPtr<nsIDOMNode> child;
00184   children->Item(aIndex, getter_AddRefs(child));
00185 
00186   if (child && nsXFormsUtils::IsLabelElement(child)) {
00187     // If a label child was removed, we need to remove the label from our
00188     // anonymous content.
00189     Refresh();
00190   }
00191 
00192   return NS_OK;
00193 }
00194 
00195 NS_IMETHODIMP
00196 nsXFormsItemElement::BeginAddingChildren()
00197 {
00198   // Suppress child notifications until we're done getting children.
00199   nsCOMPtr<nsIXTFElementWrapper> wrapper = do_QueryInterface(mElement);
00200   NS_ASSERTION(wrapper, "huh? our element must be an xtf wrapper");
00201 
00202   wrapper->SetNotificationMask(nsIXTFElement::NOTIFY_PARENT_CHANGED |
00203                                nsIXTFElement::NOTIFY_DONE_ADDING_CHILDREN);
00204 
00205   return NS_OK;
00206 }
00207 
00208 NS_IMETHODIMP
00209 nsXFormsItemElement::DoneAddingChildren()
00210 {
00211   mDoneAddingChildren = PR_TRUE;
00212 
00213   // Unsuppress notifications
00214   nsCOMPtr<nsIXTFElementWrapper> wrapper = do_QueryInterface(mElement);
00215   NS_ASSERTION(wrapper, "huh? our element must be an xtf wrapper");
00216 
00217   wrapper->SetNotificationMask(nsIXTFElement::NOTIFY_PARENT_CHANGED |
00218                                nsIXTFElement::NOTIFY_CHILD_INSERTED |
00219                                nsIXTFElement::NOTIFY_CHILD_APPENDED |
00220                                nsIXTFElement::NOTIFY_WILL_REMOVE_CHILD);
00221 
00222   // Assume that we've got something worth refreshing now.
00223   Refresh();
00224   return NS_OK;
00225 }
00226 
00227 // nsIXFormsSelectChild
00228 
00229 NS_IMETHODIMP
00230 nsXFormsItemElement::SelectItemByValue(const nsAString &aValue, nsIDOMNode **aSelected)
00231 {
00232   NS_ENSURE_ARG_POINTER(aSelected);
00233   NS_ENSURE_STATE(mElement);
00234 
00235   *aSelected = nsnull;
00236   if (mIsCopyItem) {
00237     // copy items are selected by node, not by value
00238     return NS_OK;
00239   }
00240 
00241   nsAutoString value;
00242   nsresult rv = GetValue(value);
00243 
00244   if (NS_SUCCEEDED(rv) && aValue.Equals(value)) {
00245     NS_ADDREF(*aSelected = mElement);
00246   }
00247 
00248   return rv;
00249 }
00250 
00251 NS_IMETHODIMP
00252 nsXFormsItemElement::SelectItemByNode(nsIDOMNode *aNode, nsIDOMNode **aSelected)
00253 {
00254   NS_ENSURE_ARG_POINTER(aSelected);
00255   NS_ENSURE_STATE(mElement);
00256   PRBool isCopyItem;
00257   *aSelected = nsnull;
00258 
00259   // If this item doesn't contain a copy element but instead has a value
00260   // element, then there is no sense testing further.
00261   GetIsCopyItem(&isCopyItem);
00262   if (!isCopyItem) {
00263     return NS_ERROR_FAILURE;
00264   }
00265 
00266   nsCOMPtr<nsIDOMNode> copyNode;
00267   GetCopyNode(getter_AddRefs(copyNode));
00268   NS_ENSURE_STATE(copyNode);
00269 
00270   PRUint16 nodeType;
00271   copyNode->GetNodeType(&nodeType);
00272 
00273   // copy elements are only allowed to bind to ELEMENT_NODEs per spec.  But
00274   // test first before doing all of this work.
00275   if ((nodeType == nsIDOMNode::ELEMENT_NODE) && 
00276       (nsXFormsUtils::AreNodesEqual(copyNode, aNode))) {
00277     NS_ADDREF(*aSelected = mElement);
00278   }
00279 
00280   return NS_OK;
00281 }
00282 
00283 NS_IMETHODIMP
00284 nsXFormsItemElement::GetValue(nsAString &aValue)
00285 {
00286   PRBool isCopyItem;
00287   GetIsCopyItem(&isCopyItem);
00288   if (isCopyItem) {
00289     // if this item was built by an itemset and the itemset's template used
00290     // a copy element, then there is no value element to be had.  No sense
00291     // continuing.
00292     aValue.Truncate(0);
00293     return NS_ERROR_FAILURE;
00294   }
00295 
00296   // Find our value child and get its text content.
00297   nsCOMPtr<nsIDOMNodeList> children;
00298   nsresult rv = mElement->GetChildNodes(getter_AddRefs(children));
00299   NS_ENSURE_SUCCESS(rv, rv);
00300 
00301   PRUint32 childCount;
00302   children->GetLength(&childCount);
00303 
00304   nsCOMPtr<nsIDOMNode> child;
00305   nsAutoString value;
00306 
00307   for (PRUint32 i = 0; i < childCount; ++i) {
00308     children->Item(i, getter_AddRefs(child));
00309     nsCOMPtr<nsIXFormsValueElement> valueElement = do_QueryInterface(child);
00310     if (valueElement) {
00311       return valueElement->GetValue(aValue);
00312     }
00313   }     
00314 
00315   // No value children, so our value is empty
00316   aValue.Truncate(0);
00317   return NS_OK;
00318 }
00319 
00320 NS_IMETHODIMP
00321 nsXFormsItemElement::GetCopyNode(nsIDOMNode **aNode)
00322 {
00323   NS_ENSURE_ARG_POINTER(aNode);
00324 
00325   PRBool isCopyItem;
00326   GetIsCopyItem(&isCopyItem);
00327   if (!isCopyItem) {
00328     // If this item doesn't contain a copy element but instead has a value
00329     // element, then there is no sense continuing.
00330     *aNode = nsnull;
00331     return NS_ERROR_FAILURE;
00332   }
00333 
00334   // Find the copy element contained by this item and get the copyNode from it.
00335   nsCOMPtr<nsIDOMNodeList> children;
00336   nsresult rv = mElement->GetChildNodes(getter_AddRefs(children));
00337   NS_ENSURE_SUCCESS(rv, rv);
00338 
00339   PRUint32 childCount;
00340   children->GetLength(&childCount);
00341 
00342   nsCOMPtr<nsIDOMNode> child;
00343   nsAutoString value;
00344 
00345   for (PRUint32 i = 0; i < childCount; ++i) {
00346     children->Item(i, getter_AddRefs(child));
00347     nsCOMPtr<nsIXFormsCopyElement> copyElement = do_QueryInterface(child);
00348     if (copyElement) {
00349       return copyElement->GetCopyNode(aNode);
00350     }
00351   }     
00352 
00353   // No copy element as a child.  Set return node to null and set the copyitem
00354   // boolean to false so we don't go through this unnecessary pain again.
00355   aNode = nsnull;
00356   SetIsCopyItem(PR_FALSE);
00357   return NS_OK;
00358 }
00359 
00360 NS_IMETHODIMP
00361 nsXFormsItemElement::Refresh()
00362 {
00363   if (mDoneAddingChildren) {
00364     nsCOMPtr<nsIDOMNode> parent, current;
00365     current = mElement;
00366     do {
00367       current->GetParentNode(getter_AddRefs(parent));
00368       if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select1")) ||
00369           nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select"))) {
00370         nsCOMPtr<nsIXFormsControl> select(do_QueryInterface(parent));
00371         if (select) {
00372           select->Refresh();
00373         }
00374         return NS_OK;
00375       }
00376       current = parent;
00377     } while(current);
00378   }
00379 
00380   return NS_OK;
00381 }
00382 NS_IMETHODIMP
00383 nsXFormsItemElement::SetActive(PRBool aActive)
00384 {
00386 
00387   NS_NAMED_LITERAL_STRING(active, "_moz_active");
00388 
00389   return aActive ?
00390     mElement->SetAttribute(active, NS_LITERAL_STRING("1")) :
00391     mElement->RemoveAttribute(active);
00392 }
00393 
00394 NS_IMETHODIMP
00395 nsXFormsItemElement::GetLabelText(nsAString& aValue)
00396 {
00397   NS_ENSURE_STATE(mElement);
00398   aValue.Truncate(0);
00399   
00400   nsCOMPtr<nsIDOMNodeList> children;
00401   mElement->GetChildNodes(getter_AddRefs(children));
00402   NS_ENSURE_STATE(children);
00403 
00404   PRUint32 childCount;
00405   children->GetLength(&childCount);
00406 
00407   nsCOMPtr<nsIDOMDocument> doc;
00408   mElement->GetOwnerDocument(getter_AddRefs(doc));
00409 
00410   nsCOMPtr<nsIDOMNode> child;
00411   for (PRUint32 i = 0; i < childCount; ++i) {
00412     children->Item(i, getter_AddRefs(child));
00413     if (nsXFormsUtils::IsXFormsElement(child, NS_LITERAL_STRING("label"))) {
00414       nsCOMPtr<nsIXFormsDelegate> label(do_QueryInterface(child));
00415       NS_ENSURE_STATE(label);
00416 
00417       nsCOMPtr<nsIXFormsAccessors> accessors;
00418       label->GetXFormsAccessors(getter_AddRefs(accessors));
00419       NS_ENSURE_STATE(accessors);
00420 
00421       return accessors->GetValue(aValue);
00422     }
00423   }
00424 
00425   return NS_OK;
00426 }
00427 
00428 NS_IMETHODIMP
00429 nsXFormsItemElement::LabelRefreshed()
00430 {
00431   NS_ENSURE_STATE(mElement);
00432   nsCOMPtr<nsIDOMNode> parent, current;
00433   current = mElement;
00434   do {
00435     current->GetParentNode(getter_AddRefs(parent));
00436     if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select1")) ||
00437         nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select"))) {
00438       nsCOMPtr<nsIXFormsControl> select(do_QueryInterface(parent));
00439       if (select) {
00440         select->Refresh();
00441       }
00442       return NS_OK;
00443     }
00444     current = parent;
00445   } while(current);
00446   return NS_OK;
00447 }
00448 
00449 NS_IMETHODIMP
00450 nsXFormsItemElement::GetIsCopyItem(PRBool *aIsCopyItem)
00451 {
00452   NS_ENSURE_ARG(aIsCopyItem);
00453   *aIsCopyItem = mIsCopyItem;
00454   return NS_OK;
00455 }
00456 
00457 NS_IMETHODIMP
00458 nsXFormsItemElement::SetIsCopyItem(PRBool aIsCopyItem)
00459 {
00460   mIsCopyItem = aIsCopyItem;
00461   return NS_OK;
00462 }
00463 
00464 NS_IMETHODIMP
00465 nsXFormsItemElement::CopyNodeEquals(nsIDOMNode *aNode, PRBool *aIsCopyNode)
00466 {
00467   NS_ENSURE_ARG(aNode);
00468   NS_ENSURE_ARG_POINTER(aIsCopyNode);
00469 
00470   nsCOMPtr<nsIDOMNode> selectedNode;
00471   nsresult rv = SelectItemByNode(aNode, getter_AddRefs(selectedNode));
00472   *aIsCopyNode = !!(selectedNode);
00473 
00474   return rv;
00475 }
00476 
00477 // nsIXFormsRepeatItemElement
00478 NS_IMETHODIMP
00479 nsXFormsItemElement::SetIndexState(PRBool aHasIndex)
00480 {
00481   // this function has no real meaning for an item, so don't do anything.
00482   return NS_OK;
00483 }
00484 
00485 NS_IMETHODIMP
00486 nsXFormsItemElement::GetIndexState(PRBool *aHasIndex)
00487 {
00488   // this function has no real meaning for an item, so always return false
00489   NS_ENSURE_ARG(aHasIndex);
00490   *aHasIndex = PR_FALSE;
00491   return NS_OK;
00492 }
00493 
00494 
00495 NS_HIDDEN_(nsresult)
00496 NS_NewXFormsItemElement(nsIXTFElement **aResult)
00497 {
00498   *aResult = new nsXFormsItemElement();
00499   if (!*aResult)
00500     return NS_ERROR_OUT_OF_MEMORY;
00501 
00502   NS_ADDREF(*aResult);
00503   return NS_OK;
00504 }