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.editors;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.graphics.Color;
23import android.util.Log;
24import android.view.LayoutInflater;
25import android.view.MenuItem;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.view.ViewGroup;
29import android.widget.Button;
30import android.widget.FrameLayout;
31import android.widget.LinearLayout;
32import android.widget.PopupMenu;
33import android.widget.SeekBar;
34
35import com.android.gallery3d.R;
36import com.android.gallery3d.filtershow.FilterShowActivity;
37import com.android.gallery3d.filtershow.controller.BitmapCaller;
38import com.android.gallery3d.filtershow.controller.ColorChooser;
39import com.android.gallery3d.filtershow.controller.FilterView;
40import com.android.gallery3d.filtershow.filters.FilterColorBorderRepresentation;
41import com.android.gallery3d.filtershow.filters.FilterRepresentation;
42import com.android.gallery3d.filtershow.filters.ImageFilterColorBorder;
43import com.android.gallery3d.filtershow.imageshow.ImageShow;
44
45public class EditorColorBorder extends ParametricEditor  {
46    private static final String LOGTAG = "EditorColorBorder";
47    public static final int ID = R.id.editorColorBorder;
48
49    int[] mBasColors = {
50            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR1,
51            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR2,
52            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR3,
53            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR4,
54            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR5,
55    };
56    private EditorColorBorderTabletUI mTabletUI;
57    private String mParameterString;
58    private int mSelectedColorButton;
59
60    public EditorColorBorder() {
61        super(ID);
62    }
63
64    @Override
65    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
66        FilterColorBorderRepresentation rep = getColorBorderRep();
67        if (rep == null) {
68            return "";
69        }
70        if (mParameterString == null) {
71            mParameterString = "";
72        }
73        String paramString;
74        String val = rep.getValueString();
75        return mParameterString + val;
76    }
77
78    @Override
79    public void createEditor(Context context, FrameLayout frameLayout) {
80        mView = mImageShow = new ImageShow(context);
81        super.createEditor(context, frameLayout);
82    }
83
84    @Override
85    public void reflectCurrentFilter() {
86        super.reflectCurrentFilter();
87        FilterRepresentation rep = getLocalRepresentation();
88        if (rep != null && getLocalRepresentation() instanceof FilterColorBorderRepresentation) {
89            FilterColorBorderRepresentation cbRep =
90                    (FilterColorBorderRepresentation) getLocalRepresentation();
91            if (!ParametricEditor.useCompact(mContext)) {
92                if (mTabletUI != null) {
93                    mTabletUI.setColorBorderRepresentation(cbRep);
94                }
95
96            }
97            cbRep.setPramMode(FilterColorBorderRepresentation.PARAM_SIZE);
98            mParameterString = mContext.getString(R.string.color_border_size);
99            if (mEditControl != null) {
100                control(cbRep.getCurrentParam(), mEditControl);
101            }
102        }
103    }
104
105    @Override
106    public void openUtilityPanel(final LinearLayout accessoryViewList) {
107        Button view = (Button) accessoryViewList.findViewById(R.id.applyEffect);
108        view.setText(mContext.getString(R.string.color_border_size));
109        view.setOnClickListener(new OnClickListener() {
110
111            @Override
112            public void onClick(View arg0) {
113                showPopupMenu(accessoryViewList);
114            }
115        });
116    }
117
118    @Override
119    public boolean showsSeekBar() {
120        return false;
121    }
122
123    private void showPopupMenu(LinearLayout accessoryViewList) {
124        final Button button = (Button) accessoryViewList.findViewById(
125                R.id.applyEffect);
126        if (button == null) {
127            return;
128        }
129        final PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), button);
130        popupMenu.getMenuInflater().inflate(R.menu.filtershow_menu_color_border,
131                popupMenu.getMenu());
132        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
133
134            @Override
135            public boolean onMenuItemClick(MenuItem item) {
136                selectMenuItem(item);
137                return true;
138            }
139        });
140        popupMenu.show();
141        ((FilterShowActivity)mContext).onShowMenu(popupMenu);
142    }
143
144    protected void selectMenuItem(MenuItem item) {
145        ImageFilterColorBorder filter = (ImageFilterColorBorder) mImageShow.getCurrentFilter();
146        FilterColorBorderRepresentation rep = getColorBorderRep();
147        if (rep == null) {
148            return;
149        }
150        switch (item.getItemId()) {
151            case R.id.color_border_menu_clear:
152                clearFrame();
153                break;
154            case R.id.color_border_menu_size:
155                rep.setPramMode(FilterColorBorderRepresentation.PARAM_SIZE);
156                break;
157            case R.id.color_border_menu_corner_size:
158                rep.setPramMode(FilterColorBorderRepresentation.PARAM_RADIUS);
159                break;
160            case R.id.color_border_menu_color:
161                rep.setPramMode(FilterColorBorderRepresentation.PARAM_COLOR);
162                break;
163        }
164        if (item.getItemId() != R.id.color_border_menu_clear) {
165            mParameterString = item.getTitle().toString();
166        }
167        if (mControl instanceof ColorChooser) {
168            ColorChooser c = (ColorChooser) mControl;
169            mBasColors = c.getColorSet();
170        }
171        if (mEditControl != null) {
172            control(rep.getCurrentParam(), mEditControl);
173        }
174        if (mControl instanceof ColorChooser) {
175            ColorChooser c = (ColorChooser) mControl;
176            c.setColorSet(mBasColors);
177        }
178        updateText();
179        mControl.updateUI();
180        mView.invalidate();
181    }
182
183    public void clearFrame() {
184        commitLocalRepresentation();
185    }
186
187    @Override
188    public void setUtilityPanelUI(View actionButton, View editControl) {
189        if (ParametricEditor.useCompact(mContext)) {
190            super.setUtilityPanelUI(actionButton, editControl);
191            return;
192        }
193        mSeekBar = (SeekBar) editControl.findViewById(R.id.primarySeekBar);
194        if (mSeekBar != null) {
195            mSeekBar.setVisibility(View.GONE);
196        }
197
198        mTabletUI = new EditorColorBorderTabletUI(this, mContext, editControl);
199
200    }
201
202    FilterColorBorderRepresentation getColorBorderRep() {
203        FilterRepresentation rep = getLocalRepresentation();
204        if (rep instanceof FilterColorBorderRepresentation) {
205            return (FilterColorBorderRepresentation) rep;
206        }
207        return null;
208    }
209
210
211}
212