content: /*
   * Copyright 2009-2014 PrimeTek.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package com.github.adminfaces.showcase.bean;
  
  import com.github.adminfaces.showcase.model.Theme;
  import com.github.adminfaces.showcase.service.ThemeService;
  import org.omnifaces.cdi.ViewScoped;
  import org.primefaces.event.SelectEvent;
  import org.primefaces.event.TransferEvent;
  import org.primefaces.event.UnselectEvent;
  import org.primefaces.model.DualListModel;
  
  import javax.annotation.PostConstruct;
  import javax.faces.application.FacesMessage;
  import javax.faces.context.FacesContext;
  import javax.inject.Inject;
  import javax.inject.Named;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  
  @Named
  @ViewScoped
  public class PickListMB implements Serializable {
  
      @Inject
      private ThemeService service;
  
      private DualListModel<String> cities;
      private DualListModel<Theme> themes;
      private List<Theme> orderThemes;
  
      @PostConstruct
      public void init() {
          //Cities
          List<String> citiesSource = new ArrayList<String>();
          List<String> citiesTarget = new ArrayList<String>();
  
          citiesSource.add("San Francisco");
          citiesSource.add("London");
          citiesSource.add("Paris");
          citiesSource.add("Istanbul");
          citiesSource.add("Berlin");
          citiesSource.add("Barcelona");
          citiesSource.add("Rome");
  
          cities = new DualListModel<String>(citiesSource, citiesTarget);
  
          //Themes
          List<Theme> themesSource = service.getThemes().subList(0, 5);
          List<Theme> themesTarget = new ArrayList<Theme>();
  
          themes = new DualListModel<Theme>(themesSource, themesTarget);
          orderThemes = new ArrayList<>(themesSource);
  
      }
  
      public DualListModel<String> getCities() {
          return cities;
      }
  
      public void setCities(DualListModel<String> cities) {
          this.cities = cities;
      }
  
      public ThemeService getService() {
          return service;
      }
  
      public void setService(ThemeService service) {
          this.service = service;
      }
  
      public DualListModel<Theme> getThemes() {
          return themes;
      }
  
      public void setThemes(DualListModel<Theme> themes) {
          this.themes = themes;
      }
  
      public void onTransfer(TransferEvent event) {
          StringBuilder builder = new StringBuilder();
          for(Object item : event.getItems()) {
              builder.append(((Theme) item).getName()).append("<br />");
          }
  
          FacesMessage msg = new FacesMessage();
          msg.setSeverity(FacesMessage.SEVERITY_INFO);
          msg.setSummary("Items Transferred");
          msg.setDetail(builder.toString());
  
          FacesContext.getCurrentInstance().addMessage(null, msg);
      }
  
      public void onSelect(SelectEvent event) {
          FacesContext context = FacesContext.getCurrentInstance();
          context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Item Selected", event.getObject().toString()));
      }
  
      public void onUnselect(UnselectEvent event) {
          FacesContext context = FacesContext.getCurrentInstance();
          context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Item Unselected", event.getObject().toString()));
      }
  
      public void onReorder() {
          FacesContext context = FacesContext.getCurrentInstance();
          context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "List Reordered", null));
      }
  
      public List<Theme> getOrderThemes() {
          return orderThemes;
      }
  
      public void setOrderThemes(List<Theme> orderThemes) {
          this.orderThemes = orderThemes;
      }
  }
  