Example #1
0
  /**
   * Sets the Container that serves as the data source of the viewer.
   *
   * @see com.vaadin.data.Container.Viewer#setContainerDataSource(Container)
   */
  @Override
  public void setContainerDataSource(Container newDataSource) {
    if (newDataSource == null) {
      newDataSource = new HierarchicalContainer();
    }

    // Assure that the data source is ordered by making unordered
    // containers ordered by wrapping them
    if (Container.Hierarchical.class.isAssignableFrom(newDataSource.getClass())) {
      super.setContainerDataSource(newDataSource);
    } else {
      super.setContainerDataSource(new ContainerHierarchicalWrapper(newDataSource));
    }

    /*
     * Ensure previous expanded items are cleaned up if they don't exist in
     * the new container
     */
    if (expanded != null) {
      /*
       * We need to check that the expanded-field is not null since
       * setContainerDataSource() is called from the parent constructor
       * (AbstractSelect()) and at that time the expanded field is not yet
       * initialized.
       */
      cleanupExpandedItems();
    }
  }
 public void setModel(BeanItemContainer<T> model) {
   AbstractSelect presentation = getPresentation();
   if (presentation != null) {
     presentation.setContainerDataSource(model);
   }
   this.model = model;
 }
 @SuppressWarnings("unchecked")
 public T[] getSelection() {
   AbstractSelect presentation = getPresentation();
   if (presentation != null) {
     Object value = presentation.getValue();
     if (value instanceof Collection<?>) {
       Collection<T> collection = (Collection<T>) value;
       selection = collection.toArray((T[]) Array.newInstance(beanType, collection.size()));
     } else {
       selection = (T[]) Array.newInstance(beanType, 1);
       selection[0] = (T) value;
     }
   }
   return selection;
 }
Example #4
0
 @Override
 public void containerItemSetChange(com.vaadin.data.Container.ItemSetChangeEvent event) {
   super.containerItemSetChange(event);
   if (getContainerDataSource() instanceof Filterable) {
     boolean hasFilters = !((Filterable) getContainerDataSource()).getContainerFilters().isEmpty();
     if (!hasFilters) {
       /*
        * If Container is not filtered then the itemsetchange is caused
        * by either adding or removing items to the container. To
        * prevent a memory leak we should cleanup the expanded list
        * from items which was removed.
        *
        * However, there will still be a leak if the container is
        * filtered to show only a subset of the items in the tree and
        * later unfiltered items are removed from the container. In
        * that case references to the unfiltered item ids will remain
        * in the expanded list until the Tree instance is removed and
        * the list is destroyed, or the container data source is
        * replaced/updated. To force the removal of the removed items
        * the application developer needs to a) remove the container
        * filters temporarly or b) re-apply the container datasource
        * using setContainerDataSource(getContainerDataSource())
        */
       cleanupExpandedItems();
     }
   }
 }
 @SafeVarargs
 public final void setSelection(T... selection) {
   AbstractSelect presentation = getPresentation();
   if (presentation != null) {
     switch (selection.length) {
       case 0:
         presentation.setValue(null);
         break;
       case 1:
         presentation.setValue(selection[0]);
         break;
       default:
         presentation.setValue(Arrays.asList(selection));
     }
   }
   this.selection = selection;
 }
Example #6
0
  /**
   * Sets the icon for an item.
   *
   * @param itemId the id of the item to be assigned an icon.
   * @param icon the icon to use or null.
   * @param altText the alternative text for the icon
   */
  public void setItemIcon(Object itemId, Resource icon, String altText) {
    if (itemId != null) {
      super.setItemIcon(itemId, icon);

      if (icon == null) {
        itemIconAlts.remove(itemId);
      } else if (altText == null) {
        throw new IllegalArgumentException(NULL_ALT_EXCEPTION_MESSAGE);
      } else {
        itemIconAlts.put(itemId, altText);
      }
      markAsDirty();
    }
  }
Example #7
0
  /*
   * (non-Javadoc)
   *
   * @see com.vaadin.ui.AbstractSelect#changeVariables(java.lang.Object,
   * java.util.Map)
   */
  @Override
  public void changeVariables(Object source, Map<String, Object> variables) {

    if (variables.containsKey("clickedKey")) {
      String key = (String) variables.get("clickedKey");

      Object id = itemIdMapper.get(key);
      MouseEventDetails details =
          MouseEventDetails.deSerialize((String) variables.get("clickEvent"));
      Item item = getItem(id);
      if (item != null) {
        fireEvent(new ItemClickEvent(this, item, id, null, details));
      }
    }

    if (!isSelectable() && variables.containsKey("selected")) {
      // Not-selectable is a special case, AbstractSelect does not support
      // TODO could be optimized.
      variables = new HashMap<String, Object>(variables);
      variables.remove("selected");
    }

    // Collapses the nodes
    if (variables.containsKey("collapse")) {
      final String[] keys = (String[]) variables.get("collapse");
      for (int i = 0; i < keys.length; i++) {
        final Object id = itemIdMapper.get(keys[i]);
        if (id != null && isExpanded(id)) {
          expanded.remove(id);
          if (expandedItemId == id) {
            expandedItemId = null;
          }
          fireCollapseEvent(id);
        }
      }
    }

    // Expands the nodes
    if (variables.containsKey("expand")) {
      boolean sendChildTree = false;
      if (variables.containsKey("requestChildTree")) {
        sendChildTree = true;
      }
      final String[] keys = (String[]) variables.get("expand");
      for (int i = 0; i < keys.length; i++) {
        final Object id = itemIdMapper.get(keys[i]);
        if (id != null) {
          expandItem(id, sendChildTree);
        }
      }
    }

    // AbstractSelect cannot handle multiselection so we handle
    // it ourself
    if (variables.containsKey("selected")
        && isMultiSelect()
        && multiSelectMode == MultiSelectMode.DEFAULT) {
      handleSelectedItems(variables);
      variables = new HashMap<String, Object>(variables);
      variables.remove("selected");
    }

    // Selections are handled by the select component
    super.changeVariables(source, variables);

    // Actions
    if (variables.containsKey("action")) {
      final StringTokenizer st = new StringTokenizer((String) variables.get("action"), ",");
      if (st.countTokens() == 2) {
        final Object itemId = itemIdMapper.get(st.nextToken());
        final Action action = actionMapper.get(st.nextToken());
        if (action != null && (itemId == null || containsId(itemId)) && actionHandlers != null) {
          for (Handler ah : actionHandlers) {
            ah.handleAction(action, this, itemId);
          }
        }
      }
    }
  }
Example #8
0
 private void requestPartialRepaint() {
   super.markAsDirty();
   partialUpdate = true;
 }
Example #9
0
 @Override
 public void markAsDirty() {
   super.markAsDirty();
   partialUpdate = false;
 }