ContactSimpleAdapterBuilderAndCommandInvoker.java :  » UnTagged » excilys » com » excilys » ymca » view » Android Open Source

Android Open Source » UnTagged » excilys 
excilys » com » excilys » ymca » view » ContactSimpleAdapterBuilderAndCommandInvoker.java
/*
 * Copyright 2009 eBusinessInformation
 * 
 * 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.excilys.ymca.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import roboguice.inject.ContextScoped;
import roboguice.inject.InjectResource;
import android.content.Context;
import android.util.Log;
import android.widget.SimpleAdapter;

import com.excilys.ymca.R;
import com.excilys.ymca.command.Command;
import com.google.inject.Inject;

/**
 * Cette classe a en charge la construction de la liste de maps qui sera fournie
 *  un SimpleAdapter. Notez qu' chaque lment de la liste est associe une
 * commande (implmentation de Command)  effectuer en cas de slection de
 * l'lment correspondant. Une fois la mthode build() appele sur une instance
 * de {@link #ContactSimpleAdapterBuilderAndCommandInvoker(Context)}, il n'est
 * plus possible d'ajouter d'actions.
 * 
 * @author Pierre-Yves Ricau ( py.ricau+ymca @ gmail.com )
 * 
 */
@ContextScoped
public class ContactSimpleAdapterBuilderAndCommandInvoker {

  /**
   * Marqueur pour les logs.
   */
  private static final String        TAG        = ContactSimpleAdapterBuilderAndCommandInvoker.class.getSimpleName();

  private final static String        TITLE_KEY    = "title";
  private final static String        CONTENT_KEY    = "content";

  /**
   * Liste permettant de construire le SingleAdapter.
   */
  private ArrayList<Map<String, String>>  adapterData    = new ArrayList<Map<String, String>>();

  /**
   * Liste de commandes  excutes. L'ordre des commandes est le mme que les
   * lments de adapterData.
   */
  private ArrayList<Command>        contactCommands  = new ArrayList<Command>();

  /**
   * Marqueur permettant d'empcher l'ajout d'actions une fois que l'adapter a
   * t construit.
   */
  private boolean              built      = false;

  @Inject
  private Context              context;

  @InjectResource(R.string.no_data)
  private String              noData;

  /**
   * Ajoute une action dans le builder. Le titre et le contenu seront deux
   * lignes diffrentes affiches dans l'lment de la liste. La commande est
   * l'action  effectuer lorsque l'lment sera slectionn dans la liste.
   * 
   * @param title
   * @param content
   * @param command
   */
  public void addAction(String title, String content, Command command) {

    /*
     * On empche l'ajout d'actions une fois la liste construite.
     */
    if (built) {
      throw new UnsupportedOperationException("List already built");
    }

    if (content == null) {
      content = noData;
    }

    HashMap<String, String> actionData = new HashMap<String, String>();
    actionData.put(TITLE_KEY, title);
    actionData.put(CONTENT_KEY, content);
    adapterData.add(actionData);
    contactCommands.add(command);
  }

  /**
   * Construit le SimpleAdapter  partir des diffrentes actions ajoutes. Une
   * fois la mthode {@link #build()} appele, il n'est plus possible
   * d'appeler la mthode {@link #addAction(String, String, Command)}.
   * 
   * @return
   */
  public SimpleAdapter build() {
    built = true;

    String[] from = new String[] { TITLE_KEY, CONTENT_KEY };

    int[] to = new int[] { R.id.item_title, R.id.item_content };

    /*
     * On envoie au SimpleAdapter les donnes associes  la liste,
     * l'identifiant du layout  utiliser pour construire chaque lment de
     * la liste, un tableau contenant les cls des hashmap contenant les
     * donnes, et pour finir un tableau contenant les identifiants de vues
     * associs  ces donnes.
     */
    return new SimpleAdapter(context, adapterData, R.layout.contact_list_item, from, to);
  }

  /**
   * Excute la Command correspondant  la position de l'action correspondante
   * dans l'adapter.
   * 
   * @param position
   */
  public void execute(int position) {
    if (position >= 0 && position < contactCommands.size()) {
      Command command = contactCommands.get(position);
      if (command != null) {
        command.execute();
      }
    } else {
      Log.e(TAG, "No command found for position " + position);
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.