15aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford/*
25aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Copyright (C) 2012 The Android Open Source Project
35aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *
45aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Licensed under the Apache License, Version 2.0 (the "License");
55aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * you may not use this file except in compliance with the License.
65aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * You may obtain a copy of the License at
75aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *
85aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *      http://www.apache.org/licenses/LICENSE-2.0
95aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford *
105aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Unless required by applicable law or agreed to in writing, software
115aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * distributed under the License is distributed on an "AS IS" BASIS,
125aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * See the License for the specific language governing permissions and
145aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * limitations under the License.
155aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford */
165aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
175aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordpackage com.android.gallery3d.filtershow.editors;
185aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
195aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.content.Context;
208cb64bba90333b7b54c4b270fd4448b3d856f2bcJohn Hofordimport android.content.res.Configuration;
21235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.util.AttributeSet;
221304be5806124591d1d6c5c632700ca0361b1431John Hofordimport android.util.Log;
235aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.view.LayoutInflater;
246a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport android.view.Menu;
252ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroardimport android.view.MenuItem;
265aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.view.View;
275aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport android.view.ViewGroup;
28235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.Button;
29235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.FrameLayout;
30235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.LinearLayout;
31235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.PopupMenu;
32235d2179374bec14040f20af91dc753e38ea3639John Hofordimport android.widget.SeekBar;
336a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport android.widget.SeekBar.OnSeekBarChangeListener;
345aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
356a07dfbe03c8396cc43840a1630ea480a2cff663John Hofordimport com.android.gallery3d.R;
36235d2179374bec14040f20af91dc753e38ea3639John Hofordimport com.android.gallery3d.filtershow.controller.Control;
37cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroardimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
385aa08941bda0cfbcfba522c53937dd66867f890dJohn Hofordimport com.android.gallery3d.filtershow.imageshow.ImageShow;
39cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroardimport com.android.gallery3d.filtershow.imageshow.MasterImage;
40ce9ceff5776a9b0479c30cbeb2a9388b44df1865nicolasroardimport com.android.gallery3d.filtershow.pipeline.ImagePreset;
415aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
42b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunkimport java.util.ArrayList;
43b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunkimport java.util.Collection;
44b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
455aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford/**
465aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford * Base class for Editors Must contain a mImageShow and a top level view
475aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford */
482ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroardpublic class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener {
495aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected Context mContext;
505aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected View mView;
515aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected ImageShow mImageShow;
525aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected FrameLayout mFrameLayout;
536a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    protected SeekBar mSeekBar;
54235d2179374bec14040f20af91dc753e38ea3639John Hoford    Button mEditTitle;
55f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard    protected Button mFilterTitle;
565aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected int mID;
575aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    private final String LOGTAG = "Editor";
58b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    protected boolean mChangesGeometry = false;
59cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    protected FilterRepresentation mLocalRepresentation = null;
606a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    protected byte mShowParameter = SHOW_VALUE_UNDEFINED;
61f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard    private Button mButton;
626a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public static byte SHOW_VALUE_UNDEFINED = -1;
636a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public static byte SHOW_VALUE_OFF = 0;
646a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public static byte SHOW_VALUE_INT = 1;
655aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
667638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck    public static void hackFixStrings(Menu menu) {
677638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck        int count = menu.size();
687638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck        for (int i = 0; i < count; i++) {
697638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck            MenuItem item = menu.getItem(i);
707638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck            item.setTitle(item.getTitle().toString().toUpperCase());
717638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck        }
727638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck    }
737638772c0ceb5528b12c9943bad5391658d6c78dJohn Reck
746a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
7533de212ec780eaf2bc8d86908f07da33ea8dd7f2John Hoford        return effectName.toUpperCase() + " " + parameterValue;
766a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
776a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
785aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected Editor(int id) {
795aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mID = id;
805aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
816a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
825aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public int getID() {
835aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return mID;
845aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
855aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
866a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public byte showParameterValue() {
876a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        return mShowParameter;
886a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
896a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
9091be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard    public boolean showsSeekBar() {
9191be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard        return true;
9291be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard    }
9391be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard
94f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard    public void setUpEditorUI(View actionButton, View editControl,
95f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard                              Button editTitle, Button stateButton) {
96f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        mEditTitle = editTitle;
97f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        mFilterTitle = stateButton;
98f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        mButton = editTitle;
99d33fe6fd37d8c506ab0e47139ef0f77746a275b8nicolasroard        MasterImage.getImage().resetGeometryImages(false);
100235d2179374bec14040f20af91dc753e38ea3639John Hoford        setUtilityPanelUI(actionButton, editControl);
101235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
102235d2179374bec14040f20af91dc753e38ea3639John Hoford
103be31e587c1485fb8e3d7c8b079250ffbdc26e1b9nicolasroard    public boolean showsPopupIndicator() {
1041304be5806124591d1d6c5c632700ca0361b1431John Hoford        return false;
105be31e587c1485fb8e3d7c8b079250ffbdc26e1b9nicolasroard    }
106be31e587c1485fb8e3d7c8b079250ffbdc26e1b9nicolasroard
1076a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    /**
1086a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford     * @param actionButton the would be the area for menu etc
1096a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford     * @param editControl this is the black area for sliders etc
1106a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford     */
1116a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void setUtilityPanelUI(View actionButton, View editControl) {
112235d2179374bec14040f20af91dc753e38ea3639John Hoford
113235d2179374bec14040f20af91dc753e38ea3639John Hoford        AttributeSet aset;
114235d2179374bec14040f20af91dc753e38ea3639John Hoford        Context context = editControl.getContext();
115235d2179374bec14040f20af91dc753e38ea3639John Hoford        LayoutInflater inflater =
116235d2179374bec14040f20af91dc753e38ea3639John Hoford                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
117235d2179374bec14040f20af91dc753e38ea3639John Hoford        LinearLayout lp = (LinearLayout) inflater.inflate(
118235d2179374bec14040f20af91dc753e38ea3639John Hoford                R.layout.filtershow_seekbar, (ViewGroup) editControl, true);
119235d2179374bec14040f20af91dc753e38ea3639John Hoford        mSeekBar = (SeekBar) lp.findViewById(R.id.primarySeekBar);
120235d2179374bec14040f20af91dc753e38ea3639John Hoford        mSeekBar.setOnSeekBarChangeListener(this);
12158e2f6effa634e853d521f57d284c248adc69057John Hoford        mSeekBar.setVisibility(View.GONE);
1228cb64bba90333b7b54c4b270fd4448b3d856f2bcJohn Hoford        if (context.getResources().getConfiguration().orientation
1238cb64bba90333b7b54c4b270fd4448b3d856f2bcJohn Hoford                == Configuration.ORIENTATION_PORTRAIT) {
1248cb64bba90333b7b54c4b270fd4448b3d856f2bcJohn Hoford            if (showsSeekBar()) {
1258cb64bba90333b7b54c4b270fd4448b3d856f2bcJohn Hoford               mSeekBar.setVisibility(View.VISIBLE);
1268cb64bba90333b7b54c4b270fd4448b3d856f2bcJohn Hoford            }
1278cb64bba90333b7b54c4b270fd4448b3d856f2bcJohn Hoford        }
128235d2179374bec14040f20af91dc753e38ea3639John Hoford
129f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        if (mButton != null) {
1301304be5806124591d1d6c5c632700ca0361b1431John Hoford            setMenuIcon(showsPopupIndicator());
131be31e587c1485fb8e3d7c8b079250ffbdc26e1b9nicolasroard        }
1326a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
1336a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
1346a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    @Override
1356a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
1366a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
1376a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
1386a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
1396a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void setPanel() {
1406a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
1416a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
1426a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
143f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public void createEditor(Context context, FrameLayout frameLayout) {
1445aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mContext = context;
1455aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mFrameLayout = frameLayout;
146cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        mLocalRepresentation = null;
1475aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1485aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1495aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    protected void unpack(int viewid, int layoutid) {
1505aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1515aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (mView == null) {
1525aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            mView = mFrameLayout.findViewById(viewid);
1535aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            if (mView == null) {
1545aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
1555aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                        (Context.LAYOUT_INFLATER_SERVICE);
1565aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                mView = inflater.inflate(layoutid, mFrameLayout, false);
1575aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                mFrameLayout.addView(mView, mView.getLayoutParams());
1585aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            }
1595aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1605aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mImageShow = findImageShow(mView);
1615aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1625aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1635aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    private ImageShow findImageShow(View view) {
1645aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (view instanceof ImageShow) {
1655aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            return (ImageShow) view;
1665aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1675aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (!(view instanceof ViewGroup)) {
1685aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            return null;
1695aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1705aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        ViewGroup vg = (ViewGroup) view;
1715aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        int n = vg.getChildCount();
1725aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        for (int i = 0; i < n; i++) {
1735aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            View v = vg.getChildAt(i);
1745aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            if (v instanceof ImageShow) {
1755aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                return (ImageShow) v;
1765aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            } else if (v instanceof ViewGroup) {
1775aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford                return findImageShow(v);
1785aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            }
1795aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
1805aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return null;
1815aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1825aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1835aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public View getTopLevelView() {
1845aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return mView;
1855aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1865aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1875aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public ImageShow getImageShow() {
1885aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        return mImageShow;
1895aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1905aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
1915aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void setVisibility(int visible) {
1925aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        mView.setVisibility(visible);
1935aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
1945aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
195cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    public FilterRepresentation getLocalRepresentation() {
196cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        if (mLocalRepresentation == null) {
197cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            ImagePreset preset = MasterImage.getImage().getPreset();
198cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
199cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard            mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
2005d24515190c1664f944b7950d1a75b0df0de2ff8nicolasroard            if (mShowParameter == SHOW_VALUE_UNDEFINED && filterRepresentation != null) {
2016a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford                boolean show = filterRepresentation.showParameterValue();
2026a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford                mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
2036a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford            }
2046a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
205f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        }
206cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        return mLocalRepresentation;
207cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    }
208cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard
209b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    /**
210f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford     * Call this to update the preset in MasterImage with the current representation
211f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford     * returned by getLocalRepresentation.  This causes the preview bitmap to be
212f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford     * regenerated.
213b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk     */
214cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    public void commitLocalRepresentation() {
215b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        commitLocalRepresentation(getLocalRepresentation());
216b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    }
217b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
218b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    /**
219f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford     * Call this to update the preset in MasterImage with a given representation.
220f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford     * This causes the preview bitmap to be regenerated.
221b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk     */
222b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    public void commitLocalRepresentation(FilterRepresentation rep) {
223b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        ArrayList<FilterRepresentation> filter = new ArrayList<FilterRepresentation>(1);
224b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        filter.add(rep);
225b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        commitLocalRepresentation(filter);
226b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    }
227b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
228b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    /**
229f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford     * Call this to update the preset in MasterImage with a collection of FilterRepresentations.
230f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford     * This causes the preview bitmap to be regenerated.
231b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk     */
232b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    public void commitLocalRepresentation(Collection<FilterRepresentation> reps) {
233cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        ImagePreset preset = MasterImage.getImage().getPreset();
234b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        preset.updateFilterRepresentations(reps);
235f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        if (mButton != null) {
236f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            updateText();
237f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        }
238b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        if (mChangesGeometry) {
239b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            // Regenerate both the filtered and the geometry-only bitmaps
240d33fe6fd37d8c506ab0e47139ef0f77746a275b8nicolasroard            MasterImage.getImage().resetGeometryImages(true);
241b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        }
2428b9cc5f374fa8bded7f84af6ce73123be2754cccnicolasroard        // Regenerate the filtered bitmap.
2438b9cc5f374fa8bded7f84af6ce73123be2754cccnicolasroard        MasterImage.getImage().invalidateFiltersOnly();
244b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        preset.fillImageStateAdapter(MasterImage.getImage().getState());
245b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    }
246b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
247b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    /**
248b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk     * This is called in response to a click to apply and leave the editor.
249b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk     */
250b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    public void finalApplyCalled() {
251b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        commitLocalRepresentation();
252f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard    }
253f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard
254f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard    protected void updateText() {
255f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        String s = "";
256f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        if (mLocalRepresentation != null) {
257f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            s = mContext.getString(mLocalRepresentation.getTextId());
2587112720bc6100049d24845cfbf3de69cdbd4fdebJohn Hoford        }
259f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        mButton.setText(calculateUserMessage(mContext, s, ""));
260cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard    }
261cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard
2625aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    /**
2635aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford     * called after the filter is set and the select is called
2645aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford     */
2655aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void reflectCurrentFilter() {
266cf0b2aad3bc5d80c51316ae10e1cf2c3476259aanicolasroard        mLocalRepresentation = null;
267f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        FilterRepresentation representation = getLocalRepresentation();
268f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        if (representation != null && mFilterTitle != null && representation.getTextId() != 0) {
269f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            String text = mContext.getString(representation.getTextId()).toUpperCase();
270f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            mFilterTitle.setText(text);
271f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard            updateText();
272f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard        }
2735aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
2745aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
2755aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public boolean useUtilityPanel() {
276001283a72d451999e3308f45dc365d4defc0af39John Hoford        return true;
2775aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
2785aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
2795aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    public void openUtilityPanel(LinearLayout mAccessoryViewList) {
2801304be5806124591d1d6c5c632700ca0361b1431John Hoford        setMenuIcon(showsPopupIndicator());
2815aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        if (mImageShow != null) {
2825aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford            mImageShow.openUtilityPanel(mAccessoryViewList);
2835aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford        }
2845aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford    }
2855aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford
286235d2179374bec14040f20af91dc753e38ea3639John Hoford    protected void setMenuIcon(boolean on) {
287235d2179374bec14040f20af91dc753e38ea3639John Hoford        mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
2887574509c9d3b73e77917333661e0a2fbab777129John Hoford                0, 0, on ? R.drawable.filtershow_menu_marker_rtl : 0, 0);
289235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
290f5eedf1635eba7edfa7d41fd4e991cced978c4b2nicolasroard
2916a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    protected void createMenu(int[] strId, View button) {
2926a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        PopupMenu pmenu = new PopupMenu(mContext, button);
2936a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        Menu menu = pmenu.getMenu();
2946a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        for (int i = 0; i < strId.length; i++) {
2956a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford            menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
2966a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford        }
297235d2179374bec14040f20af91dc753e38ea3639John Hoford        setMenuIcon(true);
298235d2179374bec14040f20af91dc753e38ea3639John Hoford
2996a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
3006a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
301235d2179374bec14040f20af91dc753e38ea3639John Hoford    public Control[] getControls() {
302235d2179374bec14040f20af91dc753e38ea3639John Hoford        return null;
303235d2179374bec14040f20af91dc753e38ea3639John Hoford    }
304f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
3056a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    @Override
3066a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void onStartTrackingTouch(SeekBar arg0) {
3076a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
3086a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
3096a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
3106a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    @Override
3116a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    public void onStopTrackingTouch(SeekBar arg0) {
3126a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
3136a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford    }
3146a07dfbe03c8396cc43840a1630ea480a2cff663John Hoford
3152ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard    @Override
3162ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard    public void swapLeft(MenuItem item) {
3172ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard
3182ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard    }
3192ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard
3202ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard    @Override
3212ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard    public void swapRight(MenuItem item) {
3222ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard
3232ff0e04ddcf770a782f94ddee936a1c205e1f322nicolasroard    }
3247668544ee3e8ba3b14d127b9ecd1cf4a3b2f3e11nicolasroard
3257668544ee3e8ba3b14d127b9ecd1cf4a3b2f3e11nicolasroard    public void detach() {
3269f452e09889199a28a86d9bd8f8fdaa8508ca0c1nicolasroard        if (mImageShow != null) {
3279f452e09889199a28a86d9bd8f8fdaa8508ca0c1nicolasroard            mImageShow.detach();
3289f452e09889199a28a86d9bd8f8fdaa8508ca0c1nicolasroard        }
3297668544ee3e8ba3b14d127b9ecd1cf4a3b2f3e11nicolasroard    }
3305aa08941bda0cfbcfba522c53937dd66867f890dJohn Hoford}
331