1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.filtershow.state;
18
19import android.content.Context;
20import android.util.Log;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.ArrayAdapter;
24import com.android.gallery3d.R;
25import com.android.gallery3d.filtershow.FilterShowActivity;
26import com.android.gallery3d.filtershow.editors.ImageOnlyEditor;
27import com.android.gallery3d.filtershow.filters.FilterRepresentation;
28import com.android.gallery3d.filtershow.imageshow.MasterImage;
29
30import java.util.Vector;
31
32public class StateAdapter extends ArrayAdapter<State> {
33
34    private static final String LOGTAG = "StateAdapter";
35    private int mOrientation;
36    private String mOriginalText;
37    private String mResultText;
38
39    public StateAdapter(Context context, int textViewResourceId) {
40        super(context, textViewResourceId);
41        mOriginalText = context.getString(R.string.state_panel_original);
42        mResultText = context.getString(R.string.state_panel_result);
43    }
44
45    @Override
46    public View getView(int position, View convertView, ViewGroup parent) {
47        StateView view = null;
48        if (convertView == null) {
49            convertView = new StateView(getContext());
50        }
51        view = (StateView) convertView;
52        State state = getItem(position);
53        view.setState(state);
54        view.setOrientation(mOrientation);
55        FilterRepresentation currentRep = MasterImage.getImage().getCurrentFilterRepresentation();
56        FilterRepresentation stateRep = state.getFilterRepresentation();
57        if (currentRep != null && stateRep != null
58            && currentRep.getFilterClass() == stateRep.getFilterClass()
59            && currentRep.getEditorId() != ImageOnlyEditor.ID) {
60            view.setSelected(true);
61        } else {
62            view.setSelected(false);
63        }
64        return view;
65    }
66
67    public boolean contains(State state) {
68        for (int i = 0; i < getCount(); i++) {
69            if (state == getItem(i)) {
70                return true;
71            }
72        }
73        return false;
74    }
75
76    public void setOrientation(int orientation) {
77        mOrientation = orientation;
78    }
79
80    public void addOriginal() {
81        add(new State(mOriginalText));
82    }
83
84    public boolean same(Vector<State> states) {
85        // we have the original state in addition
86        if (states.size() + 1 != getCount()) {
87            return false;
88        }
89        for (int i = 1; i < getCount(); i++) {
90            State state = getItem(i);
91            if (!state.equals(states.elementAt(i-1))) {
92                return false;
93            }
94        }
95        return true;
96    }
97
98    public void fill(Vector<State> states) {
99        if (same(states)) {
100            return;
101        }
102        clear();
103        addOriginal();
104        addAll(states);
105        notifyDataSetChanged();
106    }
107
108    @Override
109    public void remove(State state) {
110        super.remove(state);
111        FilterRepresentation filterRepresentation = state.getFilterRepresentation();
112        FilterShowActivity activity = (FilterShowActivity) getContext();
113        activity.removeFilterRepresentation(filterRepresentation);
114    }
115}
116