it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.commonnames.DefaultTaxonCommonNamesFactSheetViewController Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Mobile - Android - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile
* SCM: https://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.commonnames;
import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.RoleFactory;
import it.tidalwave.util.thread.annotation.ThreadConfined;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.spi.TaxonFactSheetViewControllerSupport;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.impl.commonnames.CommonNamesActionProvider;
import static it.tidalwave.util.thread.ThreadType.*;
/***********************************************************************************************************************
*
* @stereotype Controller
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public class DefaultTaxonCommonNamesFactSheetViewController extends TaxonFactSheetViewControllerSupport
implements TaxonCommonNamesFactSheetViewController
{
private final Provider taxonomyPreferences = Locator.createProviderFor(TaxonomyPreferences.class);
@Nonnull
private final TaxonCommonNamesFactSheetFlowController flowController;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private final RoleFactory actionProviderFactory = new RoleFactory()
{
@Nonnull
public Object createRoleFor (final @Nonnull CommonNamesPresentationModel presentationModel)
{
return new CommonNamesActionProvider(presentationModel, flowController);
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public DefaultTaxonCommonNamesFactSheetViewController (final @Nonnull TaxonCommonNamesFactSheetView view,
final @Nonnull TaxonCommonNamesFactSheetFlowController flowController)
{
super(view);
this.flowController = flowController;
roleRegister.registerRoleFactory(actionProviderFactory).forClass(CommonNamesPresentationModel.class);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@ThreadConfined(type=NOT_UI)
protected void createPresentationModels (final @Nonnull Taxon taxon)
{
final List presentationModels = new ArrayList();
for (final Locale locale : taxonomyPreferences.get().getTaxonomy().getLocales())
{
try
{
presentationModels.add(new CommonNamesPresentationModel(taxon, locale));
}
catch (NotFoundException e)
{
// ok, no name FIXME: log a warning
}
}
Collections.sort(presentationModels, new Comparator()
{
public int compare (final @Nonnull CommonNamesPresentationModel o1,
final @Nonnull CommonNamesPresentationModel o2)
{
return o1.getLanguage().compareTo(o2.getLanguage());
}
});
for (final PresentationModel presentationModel : presentationModels)
{
addPresentationModel(presentationModel);
}
}
}