private void restoreState() {
   final Indexed container = grid.getContainerDataSource();
   if (container.getItemIds().isEmpty()) {
     container.removeAllItems();
     for (final UploadStatusObject statusObject :
         artifactUploadState.getUploadedFileStatusList()) {
       final Item item =
           container.addItem(
               getItemid(statusObject.getFilename(), statusObject.getSelectedSoftwareModule()));
       item.getItemProperty(REASON)
           .setValue(statusObject.getReason() != null ? statusObject.getReason() : "");
       if (statusObject.getStatus() != null) {
         item.getItemProperty(STATUS).setValue(statusObject.getStatus());
       }
       if (statusObject.getProgress() != null) {
         item.getItemProperty(PROGRESS).setValue(statusObject.getProgress());
       }
       item.getItemProperty(FILE_NAME).setValue(statusObject.getFilename());
       final SoftwareModule sw = statusObject.getSelectedSoftwareModule();
       item.getItemProperty(SPUILabelDefinitions.NAME_VERSION)
           .setValue(HawkbitCommonUtil.getFormattedNameVersion(sw.getName(), sw.getVersion()));
     }
     if (artifactUploadState.isUploadCompleted()) {
       minimizeButton.setEnabled(false);
     }
   }
 }
    public OrderChanger(final Indexed container, final Object itemId) {
      HorizontalLayout layout = new HorizontalLayout();
      layout.addComponent(up);
      up.setIcon(new ThemeResource("../runo/icons/32/arrow-up.png"));
      up.addStyleName(BaseTheme.BUTTON_LINK);
      up.addListener(
          new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
              int newIndex = container.indexOfId(itemId) - 1;
              container.removeItem(itemId);
              container.addItemAt(newIndex, itemId);
              up.setEnabled(newIndex > 0);
            }
          });
      up.setEnabled(container.indexOfId(itemId) > 0);

      layout.addComponent(down);
      down.setIcon(new ThemeResource("../runo/icons/32/arrow-down.png"));
      down.addStyleName(BaseTheme.BUTTON_LINK);
      down.addListener(
          new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
              int newIndex = container.indexOfId(itemId) + 1;
              container.removeItem(itemId);
              container.addItemAt(newIndex, itemId);
              down.setEnabled(newIndex < container.size() - 1);
            }
          });
      down.setEnabled(container.indexOfId(itemId) < container.size() - 1);

      setCompositionRoot(layout);
    }
  /**
   * Converts an event in the container to an {@link CalendarEvent}
   *
   * @param index The index of the item in the container to get the event for
   * @return
   */
  private CalendarEvent getEvent(int index) {

    // Check the event cache first
    for (CalendarEvent e : eventCache) {
      if (e instanceof ContainerCalendarEvent
          && ((ContainerCalendarEvent) e).getContainerIndex() == index) {
        return e;
      } else if (container.getIdByIndex(index) == e) {
        return e;
      }
    }

    final Object id = container.getIdByIndex(index);
    Item item = container.getItem(id);
    CalendarEvent event;
    if (id instanceof CalendarEvent) {
      /*
       * If we are using the BeanItemContainer or another container which
       * stores the objects as ids then just return the instances
       */
      event = (CalendarEvent) id;

    } else {
      /*
       * Else we use the properties to create the event
       */
      BasicEvent basicEvent = new ContainerCalendarEvent(index);

      // Set values from property values
      if (captionProperty != null && item.getItemPropertyIds().contains(captionProperty)) {
        basicEvent.setCaption(String.valueOf(item.getItemProperty(captionProperty).getValue()));
      }
      if (descriptionProperty != null && item.getItemPropertyIds().contains(descriptionProperty)) {
        basicEvent.setDescription(
            String.valueOf(item.getItemProperty(descriptionProperty).getValue()));
      }
      if (startDateProperty != null && item.getItemPropertyIds().contains(startDateProperty)) {
        basicEvent.setStart((Date) item.getItemProperty(startDateProperty).getValue());
      }
      if (endDateProperty != null && item.getItemPropertyIds().contains(endDateProperty)) {
        basicEvent.setEnd((Date) item.getItemProperty(endDateProperty).getValue());
      }
      if (styleNameProperty != null && item.getItemPropertyIds().contains(styleNameProperty)) {
        basicEvent.setStyleName(String.valueOf(item.getItemProperty(styleNameProperty).getValue()));
      }
      event = basicEvent;
    }
    return event;
  }
 /*
  * (non-Javadoc)
  *
  * @see
  * com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventResizeHandler
  * #eventResize
  * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventResize)
  */
 @Override
 public void eventResize(EventResize event) {
   CalendarEvent ce = event.getCalendarEvent();
   if (eventCache.contains(ce)) {
     int index;
     if (ce instanceof ContainerCalendarEvent) {
       index = ((ContainerCalendarEvent) ce).getContainerIndex();
     } else {
       index = container.indexOfId(ce);
     }
     ignoreContainerEvents();
     Item item = container.getItem(container.getIdByIndex(index));
     item.getItemProperty(startDateProperty).setValue(event.getNewStart());
     item.getItemProperty(endDateProperty).setValue(event.getNewEnd());
     listenToContainerEvents();
   }
 }
  /**
   * Get the first event for a date
   *
   * @param date The date to search for, NUll returns first event in container
   * @return Returns an array where the first item is the start index and the second item is the end
   *     item
   */
  private int[] getFirstAndLastEventIndex(Date start, Date end) {
    int startIndex = 0;
    int size = container.size();
    assert size >= 0;
    int endIndex = size - 1;

    if (start != null) {
      /*
       * Iterating from the start of the container, if range is in the end
       * of the container then this will be slow TODO This could be
       * improved by using some sort of divide and conquer algorithm
       */
      while (startIndex < size) {
        Object id = container.getIdByIndex(startIndex);
        Item item = container.getItem(id);
        Date d = (Date) item.getItemProperty(startDateProperty).getValue();
        if (d.compareTo(start) >= 0) {
          break;
        }
        startIndex++;
      }
    }

    if (end != null) {
      /*
       * Iterate from the start index until range ends
       */
      endIndex = startIndex;
      while (endIndex < size - 1) {
        Object id = container.getIdByIndex(endIndex);
        Item item = container.getItem(id);
        Date d = (Date) item.getItemProperty(endDateProperty).getValue();
        if (d == null) {
          // No end date present, use start date
          d = (Date) item.getItemProperty(startDateProperty).getValue();
        }
        if (d.compareTo(end) >= 0) {
          endIndex--;
          break;
        }
        endIndex++;
      }
    }

    return new int[] {startIndex, endIndex};
  }
 /*
  * (non-Javadoc)
  *
  * @see
  * com.vaadin.addon.calendar.event.CalendarEditableEventProvider#addEvent
  * (com.vaadin.addon.calendar.event.CalendarEvent)
  */
 @Override
 public void addEvent(CalendarEvent event) {
   Item item;
   try {
     item = container.addItem(event);
   } catch (UnsupportedOperationException uop) {
     // Thrown if container does not support adding items with custom
     // ids. JPAContainer for example.
     item = container.getItem(container.addItem());
   }
   if (item != null) {
     item.getItemProperty(getCaptionProperty()).setValue(event.getCaption());
     item.getItemProperty(getStartDateProperty()).setValue(event.getStart());
     item.getItemProperty(getEndDateProperty()).setValue(event.getEnd());
     item.getItemProperty(getStyleNameProperty()).setValue(event.getStyleName());
     item.getItemProperty(getDescriptionProperty()).setValue(event.getDescription());
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.vaadin.addon.calendar.event.CalendarEventProvider#getEvents(java.
   * util.Date, java.util.Date)
   */
  @Override
  public List<CalendarEvent> getEvents(Date startDate, Date endDate) {
    eventCache.clear();

    int[] rangeIndexes = getFirstAndLastEventIndex(startDate, endDate);
    for (int i = rangeIndexes[0]; i <= rangeIndexes[1] && i < container.size(); i++) {
      eventCache.add(getEvent(i));
    }
    return Collections.unmodifiableList(eventCache);
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.vaadin.addon.calendar.ui.CalendarComponentEvents.EventMoveHandler
   * #eventMove
   * (com.vaadin.addon.calendar.ui.CalendarComponentEvents.MoveEvent)
   */
  @Override
  public void eventMove(MoveEvent event) {
    CalendarEvent ce = event.getCalendarEvent();
    if (eventCache.contains(ce)) {
      int index;
      if (ce instanceof ContainerCalendarEvent) {
        index = ((ContainerCalendarEvent) ce).getContainerIndex();
      } else {
        index = container.indexOfId(ce);
      }

      long eventLength = ce.getEnd().getTime() - ce.getStart().getTime();
      Date newEnd = new Date(event.getNewStart().getTime() + eventLength);

      ignoreContainerEvents();
      Item item = container.getItem(container.getIdByIndex(index));
      item.getItemProperty(startDateProperty).setValue(event.getNewStart());
      item.getItemProperty(endDateProperty).setValue(newEnd);
      listenToContainerEvents();
    }
  }
 /*
  * (non-Javadoc)
  *
  * @see
  * com.vaadin.addon.calendar.event.CalendarEditableEventProvider#removeEvent
  * (com.vaadin.addon.calendar.event.CalendarEvent)
  */
 @Override
 public void removeEvent(CalendarEvent event) {
   container.removeItem(event);
 }