package circlebinder.common.event;

import android.content.Context;
import android.support.v4.view.ActionProvider;
import android.view.View;
import android.widget.Spinner;

import net.ichigotake.common.widget.ArrayAdapter;
import net.ichigotake.common.widget.OnItemSelectedEventListener;
import net.ichigotake.common.widget.OnItemSelectedListener;

import java.util.Collection;

import circlebinder.common.checklist.SelectorViewHolder;

public final class CircleBlockFilterActionProvider extends ActionProvider {

    private final OnItemSelectedListener<Block> onItemSelectedListener;
    private Spinner blockIconView;

    public CircleBlockFilterActionProvider(Context context, Collection<Block> blocks) {
        super(context);
        this.onItemSelectedListener = new OnItemSelectedListener<Block>();
        this.blockIconView = new Spinner(getContext());
        ArrayAdapter<Block, SelectorViewHolder> adapter = new ArrayAdapter<Block, SelectorViewHolder>(
                getContext(),
                new BlockSelectorBinder()
        );
        adapter.addAll(blocks);
        blockIconView.setAdapter(adapter);
        blockIconView.setOnItemSelectedListener(onItemSelectedListener);
    }

    public void addOnItemSelectedListener(OnItemSelectedEventListener<Block> listener) {
        onItemSelectedListener.addOnItemSelectedListener(listener);
    }

    public void setSelection(Block block) {
        blockIconView.setSelection(block.getId() > 0 ? block.getId() : 1);
    }

    @Override
    public View onCreateActionView() {
        return blockIconView;
    }

    @Override
    public boolean onPerformDefaultAction() {
        return super.onPerformDefaultAction();
    }

}

@@ -1419,72 +1419,257 @@
     
-blockIconView.setSelection(block.getId() %3E 0 ? block.getId() : 1
+final int position;%0A        if (block == null) %7B%0A            position = 0;%0A        %7D else if (block.getId() %3E 0) %7B%0A            position = block.getId();%0A        %7D else %7B%0A            position = 1;%0A        %7D%0A        blockIconView.setSelection(position
 );%0A 

fix NPE in CircleBlockFilterActionProvider