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.filters;
18
19import android.util.JsonReader;
20import android.util.JsonWriter;
21import android.util.Log;
22
23import com.android.gallery3d.R;
24import com.android.gallery3d.filtershow.editors.EditorRotate;
25import com.android.gallery3d.filtershow.editors.ImageOnlyEditor;
26
27import java.io.IOException;
28
29public class FilterRotateRepresentation extends FilterRepresentation {
30    public static final String SERIALIZATION_NAME = "ROTATION";
31    public static final String SERIALIZATION_ROTATE_VALUE = "value";
32    private static final String TAG = FilterRotateRepresentation.class.getSimpleName();
33
34    Rotation mRotation;
35
36    public enum Rotation {
37        ZERO(0), NINETY(90), ONE_EIGHTY(180), TWO_SEVENTY(270);
38        private final int mValue;
39
40        private Rotation(int value) {
41            mValue = value;
42        }
43
44        public int value() {
45            return mValue;
46        }
47
48        public static Rotation fromValue(int value) {
49            switch (value) {
50                case 0:
51                    return ZERO;
52                case 90:
53                    return NINETY;
54                case 180:
55                    return ONE_EIGHTY;
56                case 270:
57                    return TWO_SEVENTY;
58                default:
59                    return null;
60            }
61        }
62    }
63
64    public FilterRotateRepresentation(Rotation rotation) {
65        super(SERIALIZATION_NAME);
66        setSerializationName(SERIALIZATION_NAME);
67        setShowParameterValue(false);
68        setFilterClass(FilterRotateRepresentation.class);
69        setFilterType(FilterRepresentation.TYPE_GEOMETRY);
70        setSupportsPartialRendering(true);
71        setTextId(R.string.rotate);
72        setEditorId(ImageOnlyEditor.ID);
73        setRotation(rotation);
74    }
75
76    public FilterRotateRepresentation(FilterRotateRepresentation r) {
77        this(r.getRotation());
78        setName(r.getName());
79    }
80
81    public FilterRotateRepresentation() {
82        this(getNil());
83    }
84
85    public Rotation getRotation() {
86        return mRotation;
87    }
88
89    public void rotateCW() {
90        switch(mRotation) {
91            case ZERO:
92                mRotation = Rotation.NINETY;
93                break;
94            case NINETY:
95                mRotation = Rotation.ONE_EIGHTY;
96                break;
97            case ONE_EIGHTY:
98                mRotation = Rotation.TWO_SEVENTY;
99                break;
100            case TWO_SEVENTY:
101                mRotation = Rotation.ZERO;
102                break;
103        }
104    }
105
106    public void set(FilterRotateRepresentation r) {
107        mRotation = r.mRotation;
108    }
109
110    public void setRotation(Rotation rotation) {
111        if (rotation == null) {
112            throw new IllegalArgumentException("Argument to setRotation is null");
113        }
114        mRotation = rotation;
115    }
116
117    @Override
118    public boolean allowsSingleInstanceOnly() {
119        return true;
120    }
121
122    @Override
123    public FilterRepresentation copy() {
124        return new FilterRotateRepresentation(this);
125    }
126
127    @Override
128    protected void copyAllParameters(FilterRepresentation representation) {
129        if (!(representation instanceof FilterRotateRepresentation)) {
130            throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
131        }
132        super.copyAllParameters(representation);
133        representation.useParametersFrom(this);
134    }
135
136    @Override
137    public void useParametersFrom(FilterRepresentation a) {
138        if (!(a instanceof FilterRotateRepresentation)) {
139            throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
140        }
141        setRotation(((FilterRotateRepresentation) a).getRotation());
142    }
143
144    @Override
145    public boolean isNil() {
146        return mRotation == getNil();
147    }
148
149    public static Rotation getNil() {
150        return Rotation.ZERO;
151    }
152
153    @Override
154    public void serializeRepresentation(JsonWriter writer) throws IOException {
155        writer.beginObject();
156        writer.name(SERIALIZATION_ROTATE_VALUE).value(mRotation.value());
157        writer.endObject();
158    }
159
160    @Override
161    public boolean equals(FilterRepresentation rep) {
162        if (!(rep instanceof FilterRotateRepresentation)) {
163            return false;
164        }
165        FilterRotateRepresentation rotate = (FilterRotateRepresentation) rep;
166        if (rotate.mRotation.value() != mRotation.value()) {
167            return false;
168        }
169        return true;
170    }
171
172    @Override
173    public void deSerializeRepresentation(JsonReader reader) throws IOException {
174        boolean unset = true;
175        reader.beginObject();
176        while (reader.hasNext()) {
177            String name = reader.nextName();
178            if (SERIALIZATION_ROTATE_VALUE.equals(name)) {
179                Rotation r = Rotation.fromValue(reader.nextInt());
180                if (r != null) {
181                    setRotation(r);
182                    unset = false;
183                }
184            } else {
185                reader.skipValue();
186            }
187        }
188        if (unset) {
189            Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
190        }
191        reader.endObject();
192    }
193}
194