javaquery/canvas/src/main/java/net/java/html/canvas/Style.java
author Anton Epple <toni.epple@eppleton.de>
Mon, 27 May 2013 10:24:34 +0200
branchcanvas
changeset 1152 2c1c6b0f5840
parent 1150 42e29ceb8371
child 1154 2bdd1eba1880
permissions -rw-r--r--
removed factory methods for style. Those are in GraphicsContext now for consistency (GraphicsContext should create all graphics related Objects).
toni@1129
     1
/**
toni@1129
     2
 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
toni@1129
     3
 * <jaroslav.tulach@apidesign.org>
toni@1129
     4
 *
toni@1129
     5
 * This program is free software: you can redistribute it and/or modify it under
toni@1129
     6
 * the terms of the GNU General Public License as published by the Free Software
toni@1129
     7
 * Foundation, version 2 of the License.
toni@1129
     8
 *
toni@1129
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT
toni@1129
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
toni@1129
    11
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
toni@1129
    12
 * details.
toni@1129
    13
 *
toni@1129
    14
 * You should have received a copy of the GNU General Public License along with
toni@1129
    15
 * this program. Look for COPYING file in the top folder. If not, see
toni@1129
    16
 * http://opensource.org/licenses/GPL-2.0.
toni@1129
    17
 */
toni@1129
    18
package net.java.html.canvas;
toni@1129
    19
toni@1150
    20
import java.util.HashMap;
toni@1150
    21
import java.util.Objects;
toni@1150
    22
toni@1129
    23
/**
toni@1129
    24
 *
toni@1129
    25
 * @author antonepple
toni@1129
    26
 */
toni@1132
    27
public class Style {
toni@1132
    28
toni@1141
    29
    private Object cached;
toni@1141
    30
    private int cacheHash;
toni@1141
    31
toni@1132
    32
    Style() {
toni@1132
    33
    }
toni@1141
    34
toni@1141
    35
    void cache(Object toCache) {
toni@1141
    36
        cacheHash = hashCode();
toni@1141
    37
        this.cached = toCache;
toni@1141
    38
    }
toni@1141
    39
toni@1141
    40
    private boolean isCached() {
toni@1141
    41
        return cacheHash == hashCode();
toni@1141
    42
    }
toni@1141
    43
toni@1141
    44
    Object getCached() {
toni@1141
    45
        return isCached() ? cached : null;
toni@1141
    46
    }
toni@1150
    47
toni@1150
    48
    public static final class Pattern extends Style {
toni@1150
    49
toni@1150
    50
        private Image imageResource;
toni@1150
    51
        private String repeat;
toni@1150
    52
toni@1150
    53
        Pattern(Image imageResource, String repeat) {
toni@1150
    54
            this.imageResource = imageResource;
toni@1150
    55
            this.repeat = repeat;
toni@1150
    56
        }
toni@1150
    57
toni@1150
    58
        public Image getImageResource() {
toni@1150
    59
            return imageResource;
toni@1150
    60
        }
toni@1150
    61
toni@1150
    62
        public void setImageResource(Image imageResource) {
toni@1150
    63
            this.imageResource = imageResource;
toni@1150
    64
        }
toni@1150
    65
toni@1150
    66
        public String getRepeat() {
toni@1150
    67
            return repeat;
toni@1150
    68
        }
toni@1150
    69
toni@1150
    70
        public void setRepeat(String repeat) {
toni@1150
    71
            this.repeat = repeat;
toni@1150
    72
        }
toni@1150
    73
    }
toni@1150
    74
toni@1150
    75
    public static final class Color extends Style {
toni@1150
    76
toni@1150
    77
        private String web;
toni@1150
    78
toni@1150
    79
        /**
toni@1150
    80
         * Creates an RGB color specified with an HTML or CSS attribute string.
toni@1150
    81
         *
toni@1150
    82
         * @param webColor Colordefined as
toni@1150
    83
         */
toni@1150
    84
        Color(String webColor) {
toni@1150
    85
            this.web = web;
toni@1150
    86
        }
toni@1150
    87
toni@1150
    88
        public String getAsString() {
toni@1150
    89
            return web;
toni@1150
    90
        }
toni@1150
    91
    }
toni@1150
    92
toni@1150
    93
    public static class LinearGradient extends Style {
toni@1150
    94
toni@1150
    95
        private HashMap<Double, String> stops;
toni@1150
    96
        private double x0, y0, x1, y1;
toni@1150
    97
toni@1150
    98
        LinearGradient(double x0, double y0, double x1, double y1) {
toni@1150
    99
            this.x0 = x0;
toni@1150
   100
            this.y0 = y0;
toni@1150
   101
            this.x1 = x1;
toni@1150
   102
            this.y1 = y1;
toni@1150
   103
        }
toni@1150
   104
toni@1150
   105
        void addColorStop(double position, String color) {
toni@1150
   106
            if (stops == null) {
toni@1150
   107
                stops = new HashMap<>();
toni@1150
   108
            }
toni@1150
   109
            stops.put(position, color);
toni@1150
   110
        }
toni@1150
   111
toni@1150
   112
        public HashMap<Double, String> getStops() {
toni@1150
   113
            return stops;
toni@1150
   114
        }
toni@1150
   115
toni@1150
   116
        public void setStops(HashMap<Double, String> stops) {
toni@1150
   117
            this.stops = stops;
toni@1150
   118
        }
toni@1150
   119
toni@1150
   120
        public double getX0() {
toni@1150
   121
            return x0;
toni@1150
   122
        }
toni@1150
   123
toni@1150
   124
        public void setX0(double x0) {
toni@1150
   125
            this.x0 = x0;
toni@1150
   126
        }
toni@1150
   127
toni@1150
   128
        public double getY0() {
toni@1150
   129
            return y0;
toni@1150
   130
        }
toni@1150
   131
toni@1150
   132
        public void setY0(double y0) {
toni@1150
   133
            this.y0 = y0;
toni@1150
   134
        }
toni@1150
   135
toni@1150
   136
        public double getX1() {
toni@1150
   137
            return x1;
toni@1150
   138
        }
toni@1150
   139
toni@1150
   140
        public void setX1(double x1) {
toni@1150
   141
            this.x1 = x1;
toni@1150
   142
        }
toni@1150
   143
toni@1150
   144
        public double getY1() {
toni@1150
   145
            return y1;
toni@1150
   146
        }
toni@1150
   147
toni@1150
   148
        public void setY1(double y1) {
toni@1150
   149
            this.y1 = y1;
toni@1150
   150
        }
toni@1150
   151
toni@1150
   152
        @Override
toni@1150
   153
        public int hashCode() {
toni@1150
   154
            int hash = 7;
toni@1150
   155
            hash = 29 * hash + Objects.hashCode(this.stops);
toni@1150
   156
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
toni@1150
   157
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
toni@1150
   158
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
toni@1150
   159
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
toni@1150
   160
            return hash;
toni@1150
   161
        }
toni@1150
   162
toni@1150
   163
        @Override
toni@1150
   164
        public boolean equals(Object obj) {
toni@1150
   165
            if (obj == null) {
toni@1150
   166
                return false;
toni@1150
   167
            }
toni@1150
   168
            if (getClass() != obj.getClass()) {
toni@1150
   169
                return false;
toni@1150
   170
            }
toni@1150
   171
            final LinearGradient other = (LinearGradient) obj;
toni@1150
   172
            if (!Objects.equals(this.stops, other.stops)) {
toni@1150
   173
                return false;
toni@1150
   174
            }
toni@1150
   175
            if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
toni@1150
   176
                return false;
toni@1150
   177
            }
toni@1150
   178
            if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
toni@1150
   179
                return false;
toni@1150
   180
            }
toni@1150
   181
            if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
toni@1150
   182
                return false;
toni@1150
   183
            }
toni@1150
   184
            if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
toni@1150
   185
                return false;
toni@1150
   186
            }
toni@1150
   187
            return true;
toni@1150
   188
        }
toni@1150
   189
    }
toni@1150
   190
toni@1150
   191
    public static final class RadialGradient extends LinearGradient {
toni@1150
   192
toni@1150
   193
        private double r0, r1;
toni@1150
   194
toni@1150
   195
        RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@1150
   196
            super(x0, y0, x1, y1);
toni@1150
   197
            this.r0 = r0;
toni@1150
   198
            this.r1 = r1;
toni@1150
   199
        }
toni@1150
   200
toni@1150
   201
        public double getR0() {
toni@1150
   202
            return r0;
toni@1150
   203
        }
toni@1150
   204
toni@1150
   205
        public void setR0(double r0) {
toni@1150
   206
            this.r0 = r0;
toni@1150
   207
        }
toni@1150
   208
toni@1150
   209
        public double getR1() {
toni@1150
   210
            return r1;
toni@1150
   211
        }
toni@1150
   212
toni@1150
   213
        public void setR1(double r1) {
toni@1150
   214
            this.r1 = r1;
toni@1150
   215
        }
toni@1150
   216
toni@1150
   217
        @Override
toni@1150
   218
        public int hashCode() {
toni@1150
   219
            int hash = super.hashCode();
toni@1150
   220
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
toni@1150
   221
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
toni@1150
   222
toni@1150
   223
            return hash;
toni@1150
   224
        }
toni@1150
   225
toni@1150
   226
        @Override
toni@1150
   227
        public boolean equals(Object obj) {
toni@1150
   228
            if (obj == null) {
toni@1150
   229
                return false;
toni@1150
   230
            }
toni@1150
   231
            if (getClass() != obj.getClass()) {
toni@1150
   232
                return false;
toni@1150
   233
            }
toni@1150
   234
            if (!super.equals(obj)) {
toni@1150
   235
                return false;
toni@1150
   236
            }
toni@1150
   237
            final RadialGradient other = (RadialGradient) obj;
toni@1150
   238
            if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
toni@1150
   239
                return false;
toni@1150
   240
            }
toni@1150
   241
            if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
toni@1150
   242
                return false;
toni@1150
   243
            }
toni@1150
   244
            return true;
toni@1150
   245
        }
toni@1150
   246
    }
toni@1129
   247
}