EN · DE · RU · FR · ES

#3271: FavoritesChoicePanel.java

projectforge-wicket/src/main/java/org/projectforge/web/wicket/components/FavoritesChoicePanel.java Type: Java · Role: Panel · Source: projectforge-wicket/src/main/java/org/projectforge/web/wicket/components/FavoritesChoicePanel.java 218 lines · 147 code · 44 comments · 27 blank
Wicket Panel component for FavoritesChoice. Reusable UI fragment composed within pages or dialogs.

Code Structure

Package: org.projectforge.web.wicket.components

Classes: FavoritesChoicePanel

Extends: FormComponentPanel

Methods (7): init, getNullKey, onUpdate, createRenderer, onBeforeRender, getSelected, setSelected

Fields (11): serialVersionUID, ADD_NEW_ENTRY, selected, refresh, choice, tabIndex, cssClass, userPrefArea, clearSelectionAfterSelection, nullKey, dummy

Imports: 12 packages

Source Code (abridged)

package org.projectforge.web.wicket.components;

import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior;
import org.apache.wicket.model.PropertyModel;
import org.projectforge.business.user.UserPrefDao;
import org.projectforge.framework.persistence.user.api.UserPrefArea;
import org.projectforge.framework.persistence.user.entities.UserPrefDO;
import org.projectforge.web.WicketSupport;
import org.projectforge.web.user.UserPrefEditPage;
import org.projectforge.web.wicket.AbstractSecuredPage;

/**
 * Combo box for showing and selecting favorites quickly.
 *
 * @author Kai Reinhard (k.reinhard@micromata.de)
 */
public abstract class FavoritesChoicePanel<T, F> extends FormComponentPanel<String>
{
  private static final long serialVersionUID = 4605128072052146129L;

  protected static final String ADD_NEW_ENTRY = "ADD_NEW_ENTRY";

  protected String selected;

  private boolean refresh = false;

  private DropDownChoice<String> choice;

  private Integer tabIndex;

  private String cssClass;

  private UserPrefArea userPrefArea;

  private boolean clearSelectionAfterSelection = true;

  private String nullKey;

  @SuppressWarnings("unused")
  private String dummy;

  public FavoritesChoicePanel(final String componentId, final UserPrefArea userPrefArea)
  {
    this(componentId, userPrefArea, null, null);
  }

  public FavoritesChoicePanel(final String componentId, final UserPrefArea userPrefArea, final Integer tabIndex, final String cssClass)
  {
    super(componentId);
    this.userPrefArea = userPrefArea;
    this.tabIndex = tabIndex;
    this.cssClass = cssClass;
    setModel(new PropertyModel<String>(this, "dummy"));
  }

  @SuppressWarnings("serial")
  public DropDownChoice<String> init()
  {
    final LabelValueChoiceRenderer<String> renderer = createRenderer();
    choice = new DropDownChoice<String>("dropDownChoice", new PropertyModel<String>(this, "selected"), renderer.getValues(), renderer)
    {
      @Override
      protected String getNullKey()
      {
        if (nullKey != null) {
          return nullKey;
        }
        return super.getNullKey();
      }
    };
    choice.add(new FormComponentUpdatingBehavior()
    {
      @Override
      public void onUpdate()
      {
        String newSelection = (String) this.getFormComponent().getModelObject();
        if (StringUtils.isNotEmpty(newSelection) == true) {
          if (ADD_NEW_ENTRY.equals(newSelection) == true) {
            final Object favorite = newFavoriteInstance(getCurrentObject());
            final UserPrefEditPage page = new UserPrefEditPage(userPrefArea, favorite);
            page.setReturnToPage((AbstractSecuredPage) FavoritesChoicePanel.this.getPage());
            refresh = true;
            setResponsePage(page);
            selected = "";
            return;
          }
          // Fill fields with selected template values:
          final F favorite = getCurrentFavorite();
          if (favorite != null) {
            select(favorite);
          }
          if (FavoritesChoicePanel.this.clearSelectionAfterSelection == true) {
            selected = "";
          }
        }
      }
    });
    choice.setNullValid(true);
    if (tabIndex != null) {
      choice.add(AttributeModifier.replace("tabindex", String.valueOf(tabIndex)));
    }

    if (cssClass != null) {
      choice.add(AttributeModifier.append("class", cssClass));
    }
    add(choice);
    return choice;
  }

  private LabelValueChoiceRenderer<String> createRenderer()
  {
    final String[] entries = WicketSupport.get(UserPrefDao.class).getPrefNames(userPrefArea);
    final LabelValueChoiceRenderer<String> renderer = new LabelValueChoiceRenderer<String>();
    for (final String entry : entries) {
      renderer.addValue(entry, entry);
// ... (truncated, total 196 lines)

Git History

868d6abb7 2025 -> 2026
63081666f Source file headers: 2024-> 2025.
011000f03 Migration stuff in progress... (all tests of all packages: OK).
b095e6f7d !!!!! Big change of Transaction handling: Re-uses PfPersistenceContext as much as possible (not yet finished). Tests doesn't yet run.
4efcbd0fb Migration stuff in progress...