/*
   * Wait until the device status becomes online
   *
   * @param serialNumber device serial number
   *
   * @param instance TmL instance, if it exists
   *
   * @return true if the device became online, false otherwise
   */
  private static boolean waitForDeviceToBeOnline(String serialNumber, ISerialNumbered instance) {
    AndmoreLogger.debug("Wait device to be online: " + serialNumber);

    boolean instanceOnline = false;
    long timeoutLimit = 0;

    if (instance != null) {
      Properties prop = ((IInstance) instance).getProperties();
      String timeout = prop.getProperty(RemoteDeviceInstance.PROPERTY_TIMEOUT);
      timeoutLimit = System.currentTimeMillis() + (Integer.parseInt(timeout) * 1000);
    } else {
      timeoutLimit = System.currentTimeMillis() + (RemoteDeviceConstants.DEFAULT_TIMEOUT * 1000);
    }

    while ((instanceOnline = DDMSFacade.isDeviceOnline(serialNumber)) == false) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        AndmoreLogger.error("Wait for device to be online: thread has been interrupted");
      }

      try {
        testTimeout(timeoutLimit);
      } catch (TimeoutException e) {
        AndmoreLogger.warn("Timeout reached wile wating device to be online: " + serialNumber);
        break;
      }
    }
    return instanceOnline;
  }
  /** @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object) */
  @Override
  public boolean hasChildren(Object object) {

    boolean hasChildren = false;

    if (object instanceof EmuViewerNode) {
      EmuViewerNode nodeObject = (EmuViewerNode) object;

      // The node has children if its children collection is bigger than 0
      // in size
      hasChildren = (nodeObject.getChildren().size() > 0);

    } else {
      warn("Tried to test if an object that is not an emulation tree node has children");
    }

    return hasChildren;
  }
  /** @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object) */
  @Override
  public Object[] getChildren(Object parent) {

    Set<EmuViewerNode> childrenCollection;
    Object[] returnArray;

    if (parent instanceof EmuViewerNode) {
      // Firstly, try to retrieve the parent's children by means of the
      // appropriate method
      EmuViewerNode parentNode = (EmuViewerNode) parent;

      childrenCollection = parentNode.getChildren();

      // If the provided element is an emulator root node, it is needed to
      // test if the
      // intermediate nodes were already created (they are not created in
      // the first request).
      // If they were not created, assure that when the content framework
      // requests, the
      // intermediate nodes will be found.
      //
      // This procedure guarantees that once an emulator is started, it
      // has the intermediate
      // nodes constructed even if no emulation is being performed.
      if (parentNode instanceof EmuViewerRootNode) {
        String host = ((EmuViewerRootNode) parentNode).getEmulatorIdentifier();
        for (EmuViewerNode child : childrenCollection) {
          if (child.getChildren().size() == 0) {

            addChildrenToLeafParentNode(child, host);
          }
        }
      }

      // Creating the array of elements to be returned
      returnArray = childrenCollection.toArray(new EmuViewerNode[childrenCollection.size()]);

    } else {
      warn("Tried to get children of an object that is not an emulation tree node");
      returnArray = new Object[0];
    }

    return returnArray;
  }
  /** @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object) */
  @Override
  public Object getParent(Object element) {

    Object parent = null;

    if (element instanceof EmuViewerNode) {
      EmuViewerNode nodeElement = (EmuViewerNode) element;

      if (nodeElement instanceof EmuViewerRootNode) {
        // The IViewSite object is the parent of the whole tree
        parent = treeParent;
      } else {
        parent = nodeElement.getParent();
      }
    } else {
      warn("Tried to get parent of an object that is not an emulation tree node");
    }

    return parent;
  }