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;
2000259461be82e601b58d3e970afbf0c012c5f3e7nicolasroardimport android.util.Log;
21fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.view.View;
22fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.view.ViewGroup;
23fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport android.widget.ArrayAdapter;
24fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport com.android.gallery3d.R;
25fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport com.android.gallery3d.filtershow.FilterShowActivity;
26f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroardimport com.android.gallery3d.filtershow.editors.ImageOnlyEditor;
27fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
28f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroardimport com.android.gallery3d.filtershow.imageshow.MasterImage;
29fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
30fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardimport java.util.Vector;
31fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
32fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroardpublic class StateAdapter extends ArrayAdapter<State> {
33fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
3400259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard    private static final String LOGTAG = "StateAdapter";
35fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private int mOrientation;
36fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private String mOriginalText;
37fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    private String mResultText;
38fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
39fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public StateAdapter(Context context, int textViewResourceId) {
40fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        super(context, textViewResourceId);
41fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mOriginalText = context.getString(R.string.state_panel_original);
42fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mResultText = context.getString(R.string.state_panel_result);
43fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
44fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
45fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    @Override
46fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public View getView(int position, View convertView, ViewGroup parent) {
47fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        StateView view = null;
48fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        if (convertView == null) {
49fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            convertView = new StateView(getContext());
50fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
51fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        view = (StateView) convertView;
52fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        State state = getItem(position);
53fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        view.setState(state);
54fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        view.setOrientation(mOrientation);
55f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        FilterRepresentation currentRep = MasterImage.getImage().getCurrentFilterRepresentation();
56f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        FilterRepresentation stateRep = state.getFilterRepresentation();
57f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        if (currentRep != null && stateRep != null
58f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            && currentRep.getFilterClass() == stateRep.getFilterClass()
59f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            && currentRep.getEditorId() != ImageOnlyEditor.ID) {
60f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            view.setSelected(true);
61f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        } else {
62f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            view.setSelected(false);
63f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        }
64fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        return view;
65fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
66fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
67fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public boolean contains(State state) {
68fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        for (int i = 0; i < getCount(); i++) {
69fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            if (state == getItem(i)) {
70fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard                return true;
71fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard            }
72fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
73fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        return false;
74fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
75fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
76fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public void setOrientation(int orientation) {
77fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        mOrientation = orientation;
78fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
79fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
80f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard    public void addOriginal() {
81fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        add(new State(mOriginalText));
82f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard    }
83f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard
8400259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard    public boolean same(Vector<State> states) {
8500259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        // we have the original state in addition
8600259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        if (states.size() + 1 != getCount()) {
8700259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard            return false;
88fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        }
8900259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        for (int i = 1; i < getCount(); i++) {
9000259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard            State state = getItem(i);
9100259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard            if (!state.equals(states.elementAt(i-1))) {
9200259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard                return false;
9300259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard            }
9400259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        }
9500259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        return true;
96fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
97fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
9800259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard    public void fill(Vector<State> states) {
9900259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        if (same(states)) {
10000259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard            return;
10100259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        }
10200259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        clear();
10300259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        addOriginal();
10400259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        addAll(states);
10500259461be82e601b58d3e970afbf0c012c5f3e7nicolasroard        notifyDataSetChanged();
106fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
107fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard
108fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    @Override
109fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    public void remove(State state) {
110fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        super.remove(state);
111fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        FilterRepresentation filterRepresentation = state.getFilterRepresentation();
112fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard        FilterShowActivity activity = (FilterShowActivity) getContext();
113f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        activity.removeFilterRepresentation(filterRepresentation);
114fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard    }
115fb6a8e166f0a46d2a994a408b1679e3a67fdcb52nicolasroard}
116