StateAdapter.java revision fb6a8e166f0a46d2a994a408b1679e3a67fdcb52
1fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard/*
2fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * Copyright (C) 2013 The Android Open Source Project
3fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *
4fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * Licensed under the Apache License, Version 2.0 (the "License");
5fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * you may not use this file except in compliance with the License.
6fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * You may obtain a copy of the License at
7fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *
8fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *      http://www.apache.org/licenses/LICENSE-2.0
9fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard *
10fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * Unless required by applicable law or agreed to in writing, software
11fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * distributed under the License is distributed on an "AS IS" BASIS,
12fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * See the License for the specific language governing permissions and
14fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard * limitations under the License.
15fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard */
16fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
17fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardpackage com.android.gallery3d.filtershow.state;
18fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
19fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.content.Context;
20fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.view.View;
21fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.view.ViewGroup;
22fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.widget.ArrayAdapter;
23fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport com.android.gallery3d.R;
24fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport com.android.gallery3d.filtershow.FilterShowActivity;
25fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
26fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
27fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport java.util.Vector;
28fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
29fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardpublic class StateAdapter extends ArrayAdapter<State> {
30fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
31fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private int mOrientation;
32fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private PanelTrack mListener;
33fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private String mOriginalText;
34fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private String mResultText;
35fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
36fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public StateAdapter(Context context, int textViewResourceId) {
37fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        super(context, textViewResourceId);
38fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mOriginalText = context.getString(R.string.state_panel_original);
39fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mResultText = context.getString(R.string.state_panel_result);
40fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
41fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
42fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    @Override
43fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public View getView(int position, View convertView, ViewGroup parent) {
44fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        StateView view = null;
45fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        if (convertView == null) {
46fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            convertView = new StateView(getContext());
47fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
48fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        view = (StateView) convertView;
49fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        State state = getItem(position);
50fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        view.setState(state);
51fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        view.setOrientation(mOrientation);
52fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        view.setBackgroundAlpha(1.0f);
53fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        return view;
54fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
55fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
56fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public boolean contains(State state) {
57fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        for (int i = 0; i < getCount(); i++) {
58fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            if (state == getItem(i)) {
59fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                return true;
60fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
61fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
62fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        return false;
63fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
64fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
65fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public void setOrientation(int orientation) {
66fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mOrientation = orientation;
67fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
68fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
69fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    @Override
70fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public void notifyDataSetChanged() {
71fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        if (mListener != null) {
72fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            mListener.fillContent(false);
73fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
74fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
75fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
76fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public void addAll(Vector<FilterRepresentation> filters) {
77fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        clear();
78fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        add(new State(mOriginalText));
79fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        for (FilterRepresentation filter : filters) {
80fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            State state = new State(filter.getName());
81fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            state.setFilterRepresentation(filter);
82fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            add(state);
83fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
84fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        add(new State(mResultText));
85fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        notifyDataSetChanged();
86fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
87fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
88fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    void setListener(PanelTrack listener) {
89fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mListener = listener;
90fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
91fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
92fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    @Override
93fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public void remove(State state) {
94fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        super.remove(state);
95fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        FilterRepresentation filterRepresentation = state.getFilterRepresentation();
96fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        FilterShowActivity activity = (FilterShowActivity) getContext();
97fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        activity.getPanelController().removeFilterRepresentation(filterRepresentation);
98fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
99fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard}
100