OSDN Git Service

Refactor to use direct manipulations of FilterRepresentations.
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / editors / Editor.java
1 /*
2  * Copyright (C) 2012 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
17 package com.android.gallery3d.filtershow.editors;
18
19 import android.content.Context;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.FrameLayout;
24 import android.widget.LinearLayout;
25
26 import com.android.gallery3d.filtershow.PanelController;
27 import com.android.gallery3d.filtershow.cache.ImageLoader;
28 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
29 import com.android.gallery3d.filtershow.imageshow.ImageShow;
30 import com.android.gallery3d.filtershow.imageshow.MasterImage;
31 import com.android.gallery3d.filtershow.presets.ImagePreset;
32
33 /**
34  * Base class for Editors Must contain a mImageShow and a top level view
35  */
36 public class Editor {
37     protected Context mContext;
38     protected View mView;
39     protected ImageShow mImageShow;
40     protected FrameLayout mFrameLayout;
41     protected PanelController mPanelController;
42     protected int mID;
43     private final String LOGTAG = "Editor";
44     protected FilterRepresentation mLocalRepresentation = null;
45
46     public void setPanelController(PanelController panelController) {
47         this.mPanelController = panelController;
48     }
49
50     protected Editor(int id) {
51         mID = id;
52     }
53     public int getID() {
54         return mID;
55     }
56
57     public void createEditor(Context context,FrameLayout frameLayout) {
58         mContext = context;
59         mFrameLayout = frameLayout;
60         mLocalRepresentation = null;
61     }
62
63     protected void unpack(int viewid, int layoutid) {
64
65         if (mView == null) {
66             mView = mFrameLayout.findViewById(viewid);
67             if (mView == null) {
68                 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
69                         (Context.LAYOUT_INFLATER_SERVICE);
70                 mView = inflater.inflate(layoutid, mFrameLayout, false);
71                 mFrameLayout.addView(mView, mView.getLayoutParams());
72             }
73         }
74         mImageShow = findImageShow(mView);
75     }
76
77     private ImageShow findImageShow(View view) {
78         if (view instanceof ImageShow) {
79             return (ImageShow) view;
80         }
81         if (!(view instanceof ViewGroup)) {
82             return null;
83         }
84         ViewGroup vg = (ViewGroup) view;
85         int n = vg.getChildCount();
86         for (int i = 0; i < n; i++) {
87             View v = vg.getChildAt(i);
88             if (v instanceof ImageShow) {
89                 return (ImageShow) v;
90             } else if (v instanceof ViewGroup) {
91                 return findImageShow(v);
92             }
93         }
94         return null;
95     }
96
97     public View getTopLevelView() {
98         return mView;
99     }
100
101     public ImageShow getImageShow() {
102         return mImageShow;
103     }
104
105     public void setImageLoader(ImageLoader imageLoader) {
106         mImageShow.setImageLoader(imageLoader);
107     }
108
109     public void setVisibility(int visible) {
110         mView.setVisibility(visible);
111     }
112
113     public FilterRepresentation getLocalRepresentation() {
114         if (mLocalRepresentation == null) {
115             ImagePreset preset = MasterImage.getImage().getPreset();
116             FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
117             mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
118         }
119         return mLocalRepresentation;
120     }
121
122     public void commitLocalRepresentation() {
123         ImagePreset preset = MasterImage.getImage().getPreset();
124         preset.updateFilterRepresentation(getLocalRepresentation());
125     }
126
127     /**
128      * called after the filter is set and the select is called
129      */
130     public void reflectCurrentFilter() {
131         mLocalRepresentation = null;
132     }
133
134     public boolean useUtilityPanel() {
135         if (mImageShow != null) {
136             return mImageShow.useUtilityPanel();
137         }
138         return false;
139     }
140
141     public void openUtilityPanel(LinearLayout mAccessoryViewList) {
142         if (mImageShow != null) {
143             mImageShow.openUtilityPanel(mAccessoryViewList);
144         }
145     }
146
147 }