コード例 #1
0
ファイル: MyVaadinUI.java プロジェクト: koa/Marathontabelle
  private DataBinder<MarathonData> showPhaseData(final FormLayout layout, final Phase phase) {
    layout.setMargin(true);
    final BeanFieldGroup<PhaseDataCompetition> binder =
        new BeanFieldGroup<PhaseDataCompetition>(PhaseDataCompetition.class);
    final DurationFieldFactory fieldFactory = new DurationFieldFactory();
    binder.setFieldFactory(fieldFactory);

    // layout.addComponent(binder.buildAndBind("geplante Startzeit", "startTime"));
    // layout.addComponent(binder.buildAndBind("Maximale Zeit", "maxTime"));
    // layout.addComponent(binder.buildAndBind("Minimale Zeit", "minTime"));
    layout.addComponent(binder.buildAndBind("Name der Phase", "phaseName"));
    layout.addComponent(binder.buildAndBind("Länge in m", "length"));
    // layout.addComponent(binder.buildAndBind("Geschwindigkeit im m/s", "velocity"));
    // layout.addComponent(binder.buildAndBind("Tabelle", "entries"));

    final BeanItemContainer<Entry<String, PhaseDataCategory>> categoryTimesItemContainer =
        new BeanItemContainer<Map.Entry<String, PhaseDataCategory>>(Map.Entry.class);
    final Table timesTable = new Table("Zeiten");
    timesTable.setContainerDataSource(categoryTimesItemContainer);
    timesTable.setHeight("8em");
    timesTable.removeContainerProperty("key");
    timesTable.removeContainerProperty("value");
    timesTable.addGeneratedColumn(
        "Kategorie",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            @SuppressWarnings("unchecked")
            final Entry<String, PhaseDataCategory> entry =
                (Entry<String, PhaseDataCategory>) itemId;
            return new Label(entry.getKey());
          }
        });
    timesTable.addGeneratedColumn(
        "Min. Zeit",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            @SuppressWarnings("unchecked")
            final Entry<String, PhaseDataCategory> entry =
                (Entry<String, PhaseDataCategory>) itemId;
            final TextField field = fieldFactory.createField(Duration.class, TextField.class);
            final PhaseDataCategory phaseDataCategory = entry.getValue();
            final ObjectProperty<Duration> property =
                new ObjectProperty<Duration>(
                    defaultIfNull(phaseDataCategory.getMinTime(), new Duration(0)));
            field.setPropertyDataSource(property);
            property.addValueChangeListener(
                new ValueChangeListener() {

                  @Override
                  public void valueChange(final ValueChangeEvent event) {
                    phaseDataCategory.setMinTime(property.getValue());
                  }
                });
            return field;
          }
        });
    timesTable.addGeneratedColumn(
        "Max. Zeit",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            @SuppressWarnings("unchecked")
            final Entry<String, PhaseDataCategory> entry =
                (Entry<String, PhaseDataCategory>) itemId;
            final TextField field = fieldFactory.createField(Duration.class, TextField.class);
            final PhaseDataCategory phaseDataCategory = entry.getValue();
            final ObjectProperty<Duration> property =
                new ObjectProperty<Duration>(
                    defaultIfNull(phaseDataCategory.getMaxTime(), new Duration(0)));
            field.setPropertyDataSource(property);
            property.addValueChangeListener(
                new ValueChangeListener() {

                  @Override
                  public void valueChange(final ValueChangeEvent event) {
                    phaseDataCategory.setMaxTime(property.getValue());
                  }
                });
            return field;
          }
        });
    timesTable.addGeneratedColumn(
        "Geschw.",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            @SuppressWarnings("unchecked")
            final Entry<String, PhaseDataCategory> entry =
                (Entry<String, PhaseDataCategory>) itemId;
            final PhaseDataCategory phaseDataCategory = entry.getValue();
            phaseDataCategory.getVelocity();
            final TextField field = fieldFactory.createField(Double.class, TextField.class);
            final ObjectProperty<Double> property =
                new ObjectProperty<Double>(
                    defaultIfNull(phaseDataCategory.getVelocity(), Double.valueOf(0)));
            field.setPropertyDataSource(property);
            property.addValueChangeListener(
                new ValueChangeListener() {

                  @Override
                  public void valueChange(final ValueChangeEvent event) {
                    phaseDataCategory.setVelocity(property.getValue());
                  }
                });
            return field;
          }
        });

    layout.addComponent(timesTable);

    final BeanItemContainer<TimeEntry> timeEntryItemContainer =
        new BeanItemContainer<TimeEntry>(TimeEntry.class);

    final Table table = new Table("Strecke");
    table.setContainerDataSource(timeEntryItemContainer);
    table.setEditable(true);
    table.setSortEnabled(false);
    table.setNullSelectionAllowed(true);
    layout.addComponent(table);
    table.setDragMode(TableDragMode.ROW);
    table.setDropHandler(
        new DropHandler() {

          @Override
          public void drop(final DragAndDropEvent dropEvent) {
            final DataBoundTransferable t = (DataBoundTransferable) dropEvent.getTransferable();
            final TimeEntry sourceItemId = (TimeEntry) t.getItemId(); // returns our Bean

            final AbstractSelectTargetDetails dropData =
                ((AbstractSelectTargetDetails) dropEvent.getTargetDetails());
            final TimeEntry targetItemId = (TimeEntry) dropData.getItemIdOver(); // returns our Bean

            // No move if source and target are the same, or there is no target
            if (sourceItemId == targetItemId || targetItemId == null) return;

            // Let's remove the source of the drag so we can add it back where requested...
            timeEntryItemContainer.removeItem(sourceItemId);

            if (dropData.getDropLocation() == VerticalDropLocation.BOTTOM) {
              timeEntryItemContainer.addItemAfter(targetItemId, sourceItemId);
            } else {
              final Object prevItemId = timeEntryItemContainer.prevItemId(targetItemId);
              timeEntryItemContainer.addItemAfter(prevItemId, sourceItemId);
            }
          }

          @Override
          public AcceptCriterion getAcceptCriterion() {
            return new SourceIs(table);
          }
        });
    table.addGeneratedColumn(
        "",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            final Button button = new Button("Löschen");
            button.addClickListener(
                new ClickListener() {
                  @Override
                  public void buttonClick(final ClickEvent event) {
                    source.getContainerDataSource().removeItem(itemId);
                  }
                });
            return button;
          }
        });
    final TableFieldFactory tableFieldFactory =
        new TableFieldFactory() {

          @Override
          public Field<?> createField(
              final Container container,
              final Object itemId,
              final Object propertyId,
              final Component uiContext) {
            final Class<?> type = container.getType(propertyId);
            final Field field = fieldFactory.createField(type, Field.class);
            if (field instanceof AbstractTextField) {
              final AbstractTextField abstractTextField = (AbstractTextField) field;
              abstractTextField.setNullRepresentation("");
              abstractTextField.setValidationVisible(true);
            }
            return field;
          }
        };
    table.setTableFieldFactory(tableFieldFactory);

    layout.addComponent(
        new Button(
            "Neuer Steckenpunkt",
            new ClickListener() {

              @Override
              public void buttonClick(final ClickEvent event) {
                timeEntryItemContainer.addBean(new TimeEntry());
              }
            }));

    final DataBinder<MarathonData> phaseDataBinder =
        new DataBinder<MarathonData>() {

          private MarathonData data;

          @Override
          public void bindData(final MarathonData phaseData) {
            this.data = phaseData;
            final PhaseDataCompetition dataCompetition = data.getCompetitionPhases().get(phase);
            binder.setItemDataSource(dataCompetition);
            timeEntryItemContainer.removeAllItems();
            timeEntryItemContainer.addAll(dataCompetition.getEntries());
            categoryTimesItemContainer.removeAllItems();
            final Map<String, PhaseDataCategory> categoryTimes = dataCompetition.getCategoryTimes();
            for (final String category : data.getCategories()) {
              if (!categoryTimes.containsKey(category)) {
                categoryTimes.put(category, new PhaseDataCategory());
              }
              categoryTimesItemContainer.addBean(
                  new Entry<String, PhaseDataCategory>() {

                    @Override
                    public String getKey() {
                      return category;
                    }

                    @Override
                    public PhaseDataCategory getValue() {
                      return categoryTimes.get(category);
                    }

                    @Override
                    public PhaseDataCategory setValue(final PhaseDataCategory value) {
                      return categoryTimes.put(category, value);
                    }
                  });
            }
          }

          @Override
          public void commitHandler() {
            try {
              binder.commit();
              final PhaseDataCompetition dataCompetition = data.getCompetitionPhases().get(phase);
              container2Model(timeEntryItemContainer, dataCompetition);
              for (final Entry<String, PhaseDataCategory> entry :
                  categoryTimesItemContainer.getItemIds()) {
                dataCompetition.getCategoryTimes().put(entry.getKey(), entry.getValue());
              }
            } catch (final CommitException e) {
              throw new RuntimeException("Cannot commit", e);
            }
          }

          @Override
          public MarathonData getCurrentData() {
            return data;
          }
        };
    layout.addComponent(
        new Button(
            "Reset Strecke",
            new ClickListener() {

              @Override
              public void buttonClick(final ClickEvent event) {
                phaseDataBinder.commitHandler();

                final PhaseDataCompetition phaseData =
                    phaseDataBinder.getCurrentData().getCompetitionPhases().get(phase);
                phaseData.setDefaultPoints();

                model2Container(phaseData, timeEntryItemContainer);
                System.out.println(phaseData);
              }
            }));
    return phaseDataBinder;
  }
コード例 #2
0
ファイル: MyVaadinUI.java プロジェクト: koa/Marathontabelle
  private DataBinder<MarathonData> showOverviewData(final FormLayout layout) {
    layout.setMargin(true);
    final BeanFieldGroup<MarathonData> binder =
        new BeanFieldGroup<MarathonData>(MarathonData.class);
    layout.addComponent(binder.buildAndBind("Veranstaltung", "marathonName"));
    final BeanItemContainer<String> categoryListContainer =
        new BeanItemContainer<String>(String.class);

    final ListSelect categoryListSelect = new ListSelect("Kategorieen", categoryListContainer);
    categoryListSelect.setNewItemsAllowed(true);
    categoryListSelect.setNullSelectionAllowed(false);
    categoryListSelect.setMultiSelect(false);
    layout.addComponent(categoryListSelect);
    final Button removeItemButton = new Button("Kategorie löschen");
    layout.addComponent(removeItemButton);
    removeItemButton.addClickListener(
        new ClickListener() {

          @Override
          public void buttonClick(final ClickEvent event) {
            categoryListContainer.removeItem(categoryListSelect.getValue());
          }
        });
    // final ListSelect categoryList = new ListSelect("Kategorieen");
    // layout.addComponent(categoryList);

    final BeanItemContainer<DriverData> driverContainer =
        new BeanItemContainer<DriverData>(DriverData.class);

    final Table table = new Table("Fahrer");
    table.setContainerDataSource(driverContainer);
    table.removeContainerProperty("smallSheets");
    table.removeContainerProperty("startTimes");
    table.removeContainerProperty("category");
    table.addGeneratedColumn(
        "Kategorie",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            final ComboBox comboBox = new ComboBox();
            comboBox.setContainerDataSource(categoryListContainer);
            final DriverData driverData = (DriverData) itemId;
            final String category =
                driverData.getCategory() == null ? "" : driverData.getCategory();

            final ObjectProperty<String> property = new ObjectProperty<String>(category);
            comboBox.setPropertyDataSource(property);
            property.addValueChangeListener(
                new ValueChangeListener() {

                  @Override
                  public void valueChange(final ValueChangeEvent event) {
                    driverData.setCategory(property.getValue());
                  }
                });
            return comboBox;
          }
        });
    table.addGeneratedColumn(
        "Generieren",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            final DriverData driverData = (DriverData) itemId;
            final Button button = new Button("PDF");
            button.addClickListener(
                new ClickListener() {
                  @Override
                  public void buttonClick(final ClickEvent event) {
                    // showPdfHandler.showPdf(driverData.getName());
                  }
                });
            return button;
          }
        });
    table.addGeneratedColumn("Phase A Zettel", createPhaseCheckbox(Phase.A));
    table.addGeneratedColumn("Phase D Zettel", createPhaseCheckbox(Phase.D));
    table.addGeneratedColumn("Phase E Zettel", createPhaseCheckbox(Phase.E));
    final DurationFieldFactory fieldFactory = new DurationFieldFactory();
    table.addGeneratedColumn("Phase A Start", createPhaseStartInput(fieldFactory, Phase.A));
    table.addGeneratedColumn("Phase D Start", createPhaseStartInput(fieldFactory, Phase.D));
    table.addGeneratedColumn("Phase E Start", createPhaseStartInput(fieldFactory, Phase.E));
    table.addGeneratedColumn(
        "Fahrer Löschen",
        new ColumnGenerator() {

          @Override
          public Object generateCell(
              final Table source, final Object itemId, final Object columnId) {
            final Button button = new Button("-");
            button.addClickListener(
                new ClickListener() {

                  @Override
                  public void buttonClick(final ClickEvent event) {
                    driverContainer.removeItem(itemId);
                  }
                });
            return button;
          }
        });

    table.setEditable(true);
    table.setSortEnabled(false);
    // table.setNullSelectionAllowed(true);
    layout.addComponent(table);
    final Button addDriverButton = new Button("Neuer Fahrer");
    addDriverButton.addClickListener(
        new ClickListener() {

          @Override
          public void buttonClick(final ClickEvent event) {
            final DriverData driver = new DriverData();
            driver.setName("Fahrer - " + driverContainer.size());
            driverContainer.addBean(driver);
          }
        });
    layout.addComponent(addDriverButton);

    return new DataBinder<MarathonData>() {

      private MarathonData data;

      @Override
      public void bindData(final MarathonData data) {
        this.data = data;
        binder.setItemDataSource(data);
        categoryListContainer.removeAllItems();
        for (final String category : data.getCategories()) {
          categoryListContainer.addBean(category);
        }
        driverContainer.removeAllItems();
        for (final DriverData driver : data.getDrivers().values()) {
          driverContainer.addBean(driver);
        }
      }

      @Override
      public void commitHandler() {
        try {
          binder.commit();
          final List<String> categories = data.getCategories();
          categories.clear();
          categories.addAll(categoryListContainer.getItemIds());
          final Map<String, DriverData> drivers = data.getDrivers();
          drivers.clear();
          for (final DriverData driver : driverContainer.getItemIds()) {
            drivers.put(driver.getName(), driver);
          }
        } catch (final CommitException e) {
          throw new RuntimeException("Cannot commit", e);
        }
      }

      @Override
      public MarathonData getCurrentData() {
        return data;
      }
    };
  }