001 /*
002 * Apache License
003 * Version 2.0, January 2004
004 * http://www.apache.org/licenses/
005 *
006 * Copyright 2008 by chenillekit.org
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 */
014
015 package org.chenillekit.tapestry.core.internal;
016
017 import java.util.ArrayList;
018 import java.util.List;
019
020 import org.apache.tapestry5.OptionGroupModel;
021 import org.apache.tapestry5.OptionModel;
022 import org.apache.tapestry5.internal.OptionModelImpl;
023 import org.apache.tapestry5.ioc.services.PropertyAccess;
024 import org.apache.tapestry5.util.AbstractSelectModel;
025
026 /**
027 * @version $Id: GenericSelectionModel.java 594 2009-12-05 15:17:26Z mlusetti $
028 */
029 public class GenericSelectionModel<T> extends AbstractSelectModel
030 {
031 private String _labelField;
032 private List<T> _list;
033 private final PropertyAccess _adapter;
034
035 public GenericSelectionModel(List<T> list, String labelField, PropertyAccess adapter)
036 {
037 _labelField = labelField;
038 _list = list;
039 _adapter = adapter;
040 }
041
042 public List<OptionGroupModel> getOptionGroups()
043 {
044 return null;
045 }
046
047 public List<OptionModel> getOptions()
048 {
049 List<OptionModel> optionModelList = new ArrayList<OptionModel>();
050 for (T obj : _list)
051 {
052 if (_labelField == null)
053 optionModelList.add(new OptionModelImpl(obj + "", obj));
054 else
055 optionModelList.add(new OptionModelImpl(_adapter.get(obj, _labelField) + "", obj));
056 }
057 return optionModelList;
058 }
059 }