/* (non-Javadoc)
  * @see com.windowtester.runtime.IUIContext#findAll(com.windowtester.runtime.locator.IWidgetLocator)
  */
 public IWidgetLocator[] findAll(IWidgetLocator locator) {
   handleConditions();
   return (IWidgetLocator[]) locator.findAll(this);
 }
  /* (non-Javadoc)
   * @see com.windowtester.runtime.IUIContext#find(com.windowtester.runtime.locator.IWidgetLocator)
   */
  public IWidgetLocator find(IWidgetLocator locator) throws WidgetSearchException {

    /*
     * TODO: remove _findAttempts as a field and refactor to a parameter of a second
     * find helper method
     */

    handleConditions();
    IWidgetLocator[] locators = findAll(locator);

    // success condition
    if (locators.length == 1) {
      _findAttempts = 0;

      IWidgetReference ref = (IWidgetReference) locators[0];
      // test for raw/generic reference case and upgrade as needed
      Object widget = ref.getWidget();
      if (widget instanceof Widget && ref.getClass().equals(WidgetReference.class)) {
        ref = WTRuntimeManager.asReference(widget);
      }

      //			// TODO[pq]: this special case should be fixed -- rub: hyperlink refs are NOT widgets...
      //			IWidgetReference<?> ref = (IWidgetReference<?>)locators[0];
      ////			System.out.println("UIContextSWT.find(): " + ref);
      //			Object widget = ref.getWidget();
      //			if (!(widget instanceof Widget))
      //				return ref;
      //			System.out.println("+++ adapting: " + ref);
      //			HERE: thought : really only want to upgrade if it's a legacy WidgetReference instance...
      //			// TODO[pq]: this translation will not be needed when the finder is replaced
      //			return SWTWidgets.asReference((Widget)widget);

      return ref;
    }

    // update attempt number and possibly try again
    if (_findAttempts++ < SWTWidgetFinder.getMaxFinderRetries()) {
      TraceHandler.trace(
          IRuntimePluginTraceOptions.BASIC,
          "UIContextSWT failed to find widget ("
              + locator
              + ") retrying ["
              + _findAttempts
              + "/"
              + SWTWidgetFinder.getMaxFinderRetries()
              + "]");
      pause(SWTWidgetFinder.getFinderRetryInterval());
      return find(locator);
    }

    // in the error case, handle cleanup before throwing the exception
    handleCleanup();

    // be sure to reset find attempts for next find
    _findAttempts = 0;

    if (locators.length > 1) {
      StringBuffer buf = new StringBuffer(200);
      buf.append("Multiple Widgets Found:\nlooking for\n  ");
      buf.append(locator.toString());
      buf.append("\nand found:");
      for (int i = 0; i < locators.length; i++) {
        buf.append("\n  ");
        try {
          buf.append(getToStringOnUIThread(locators[i]));
        } catch (Exception e) {
          buf.append(locators[i].getClass() + " - " + e);
        }
      }
      throw new MultipleWidgetsFoundException(buf.toString());
    }
    throw new WidgetNotFoundException("Widget NOT Found:\n" + locator.toString());
  }