Пример #1
0
  private void emitViewBindings(StringBuilder builder, ViewInjection injection) {
    Collection<ViewBinding> viewBindings = injection.getViewBindings();
    if (viewBindings.isEmpty()) {
      return;
    }

    for (ViewBinding viewBinding : viewBindings) {
      builder.append("    target.").append(viewBinding.getName()).append(" = ");
      emitCastIfNeeded(builder, viewBinding.getType());
      builder.append("view;\n");
    }
  }
Пример #2
0
 private void emitReset(StringBuilder builder) {
   builder.append("  public static void reset(").append(targetClass).append(" target) {\n");
   if (parentInjector != null) {
     builder.append("    ").append(parentInjector).append(".reset(target);\n\n");
   }
   for (ViewInjection injection : viewIdMap.values()) {
     for (ViewBinding viewBinding : injection.getViewBindings()) {
       builder.append("    target.").append(viewBinding.getName()).append(" = null;\n");
     }
   }
   for (CollectionBinding collectionBinding : collectionBindings.keySet()) {
     builder.append("    target.").append(collectionBinding.getName()).append(" = null;\n");
   }
   builder.append("  }\n");
 }
Пример #3
0
  /**
   * Binds the given {@link ViewBinding} to the specified {@link View}.
   *
   * @param viewId A view id in a layout XML specifying the target {@link View}.
   * @param binding An {@link ViewBinding}.
   * @return The found and bound {@link View}.
   */
  @SuppressWarnings("unchecked")
  public <T extends View> T bind(final int viewId, final ViewBinding<T> binding) {
    final T view = getView(viewId);

    if (binding.canBind(view)) {
      binding.setView(view);
      mBindingsCache.put(viewId, binding);
    } else {
      throw new IllegalStateException(
          "No View with id: "
              + Integer.toString(viewId)
              + " compatible with the given ViewBinding was found");
    }
    return view;
  }
  private void parseInjectView(
      Element element,
      Map<TypeElement, ViewInjector> targetClassMap,
      Set<String> erasedTargetNames) {
    boolean hasError = false;
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

    // Verify that the target type extends from View.
    TypeMirror elementType = element.asType();
    if (elementType instanceof TypeVariable) {
      TypeVariable typeVariable = (TypeVariable) elementType;
      elementType = typeVariable.getUpperBound();
    }
    if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
      error(
          element,
          "@InjectView fields must extend from View or be an interface. (%s.%s)",
          enclosingElement.getQualifiedName(),
          element.getSimpleName());
      hasError = true;
    }

    // Verify common generated code restrictions.
    hasError |= isInaccessibleViaGeneratedCode(InjectView.class, "fields", element);
    hasError |= isBindingInWrongPackage(InjectView.class, element);

    // Check for the other field annotation.
    if (element.getAnnotation(InjectViews.class) != null) {
      error(
          element,
          "Only one of @InjectView and @InjectViews is allowed. (%s.%s)",
          enclosingElement.getQualifiedName(),
          element.getSimpleName());
      hasError = true;
    }

    if (hasError) {
      return;
    }

    // Assemble information on the injection point.
    int id = element.getAnnotation(InjectView.class).value();

    ViewInjector injector = targetClassMap.get(enclosingElement);
    if (injector != null) {
      ViewInjection viewInjection = injector.getViewInjection(id);
      if (viewInjection != null) {
        Iterator<ViewBinding> iterator = viewInjection.getViewBindings().iterator();
        if (iterator.hasNext()) {
          ViewBinding existingBinding = iterator.next();
          error(
              element,
              "Attempt to use @InjectView for an already injected ID %d on '%s'. (%s.%s)",
              id,
              existingBinding.getName(),
              enclosingElement.getQualifiedName(),
              element.getSimpleName());
          return;
        }
      }
    }

    String name = element.getSimpleName().toString();
    String type = elementType.toString();
    boolean required = element.getAnnotation(Optional.class) == null;

    ViewInjector viewInjector = getOrCreateTargetClass(targetClassMap, enclosingElement);
    ViewBinding binding = new ViewBinding(name, type, required);
    viewInjector.addView(id, binding);

    // Add the type-erased version to the valid injection targets set.
    erasedTargetNames.add(enclosingElement.toString());
  }
Пример #5
0
 public void open(Overview overview) {
   overview.open(resource);
   ViewBinding<JComponent> resourceView = overview.getView(resource);
   resourceView.select(nodeInfo);
 }