javaquery/canvas/src/main/java/net/java/html/canvas/Style.java
author Anton Epple <toni.epple@eppleton.de>
Mon, 27 May 2013 10:18:43 +0200
branchcanvas
changeset 1150 42e29ceb8371
parent 1148 6ef06b72bb06
child 1152 2c1c6b0f5840
permissions -rw-r--r--
Grouped Style subclasses as static inner classes of Style
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@1150
    35
    static final RadialGradient createRadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@1132
    36
        return new RadialGradient(x0, y0, r0, x1, y1, r1);
toni@1132
    37
    }
toni@1141
    38
toni@1150
    39
    static final LinearGradient createLinearGradient(double x0, double y0, double x1, double y1) {
toni@1132
    40
        return new LinearGradient(x0, y0, x1, y1);
toni@1132
    41
    }
toni@1141
    42
toni@1150
    43
    static final Pattern createPattern(Image imageResource, String repeat) {
toni@1144
    44
        return new Pattern(imageResource, repeat);
toni@1132
    45
    }
toni@1141
    46
toni@1150
    47
    static final Color getWebColor(String webColor) {
toni@1148
    48
        return new Color(webColor);
toni@1148
    49
    }
toni@1148
    50
toni@1141
    51
    void cache(Object toCache) {
toni@1141
    52
        cacheHash = hashCode();
toni@1141
    53
        this.cached = toCache;
toni@1141
    54
    }
toni@1141
    55
toni@1141
    56
    private boolean isCached() {
toni@1141
    57
        return cacheHash == hashCode();
toni@1141
    58
    }
toni@1141
    59
toni@1141
    60
    Object getCached() {
toni@1141
    61
        return isCached() ? cached : null;
toni@1141
    62
    }
toni@1150
    63
toni@1150
    64
    public static final class Pattern extends Style {
toni@1150
    65
toni@1150
    66
        private Image imageResource;
toni@1150
    67
        private String repeat;
toni@1150
    68
toni@1150
    69
        Pattern(Image imageResource, String repeat) {
toni@1150
    70
            this.imageResource = imageResource;
toni@1150
    71
            this.repeat = repeat;
toni@1150
    72
        }
toni@1150
    73
toni@1150
    74
        public Image getImageResource() {
toni@1150
    75
            return imageResource;
toni@1150
    76
        }
toni@1150
    77
toni@1150
    78
        public void setImageResource(Image imageResource) {
toni@1150
    79
            this.imageResource = imageResource;
toni@1150
    80
        }
toni@1150
    81
toni@1150
    82
        public String getRepeat() {
toni@1150
    83
            return repeat;
toni@1150
    84
        }
toni@1150
    85
toni@1150
    86
        public void setRepeat(String repeat) {
toni@1150
    87
            this.repeat = repeat;
toni@1150
    88
        }
toni@1150
    89
    }
toni@1150
    90
toni@1150
    91
    public static final class Color extends Style {
toni@1150
    92
toni@1150
    93
        private String web;
toni@1150
    94
toni@1150
    95
        /**
toni@1150
    96
         * Creates an RGB color specified with an HTML or CSS attribute string.
toni@1150
    97
         *
toni@1150
    98
         * @param webColor Colordefined as
toni@1150
    99
         */
toni@1150
   100
        Color(String webColor) {
toni@1150
   101
            this.web = web;
toni@1150
   102
        }
toni@1150
   103
toni@1150
   104
        public String getAsString() {
toni@1150
   105
            return web;
toni@1150
   106
        }
toni@1150
   107
    }
toni@1150
   108
toni@1150
   109
    public static class LinearGradient extends Style {
toni@1150
   110
toni@1150
   111
        private HashMap<Double, String> stops;
toni@1150
   112
        private double x0, y0, x1, y1;
toni@1150
   113
toni@1150
   114
        LinearGradient(double x0, double y0, double x1, double y1) {
toni@1150
   115
            this.x0 = x0;
toni@1150
   116
            this.y0 = y0;
toni@1150
   117
            this.x1 = x1;
toni@1150
   118
            this.y1 = y1;
toni@1150
   119
        }
toni@1150
   120
toni@1150
   121
        void addColorStop(double position, String color) {
toni@1150
   122
            if (stops == null) {
toni@1150
   123
                stops = new HashMap<>();
toni@1150
   124
            }
toni@1150
   125
            stops.put(position, color);
toni@1150
   126
        }
toni@1150
   127
toni@1150
   128
        public HashMap<Double, String> getStops() {
toni@1150
   129
            return stops;
toni@1150
   130
        }
toni@1150
   131
toni@1150
   132
        public void setStops(HashMap<Double, String> stops) {
toni@1150
   133
            this.stops = stops;
toni@1150
   134
        }
toni@1150
   135
toni@1150
   136
        public double getX0() {
toni@1150
   137
            return x0;
toni@1150
   138
        }
toni@1150
   139
toni@1150
   140
        public void setX0(double x0) {
toni@1150
   141
            this.x0 = x0;
toni@1150
   142
        }
toni@1150
   143
toni@1150
   144
        public double getY0() {
toni@1150
   145
            return y0;
toni@1150
   146
        }
toni@1150
   147
toni@1150
   148
        public void setY0(double y0) {
toni@1150
   149
            this.y0 = y0;
toni@1150
   150
        }
toni@1150
   151
toni@1150
   152
        public double getX1() {
toni@1150
   153
            return x1;
toni@1150
   154
        }
toni@1150
   155
toni@1150
   156
        public void setX1(double x1) {
toni@1150
   157
            this.x1 = x1;
toni@1150
   158
        }
toni@1150
   159
toni@1150
   160
        public double getY1() {
toni@1150
   161
            return y1;
toni@1150
   162
        }
toni@1150
   163
toni@1150
   164
        public void setY1(double y1) {
toni@1150
   165
            this.y1 = y1;
toni@1150
   166
        }
toni@1150
   167
toni@1150
   168
        @Override
toni@1150
   169
        public int hashCode() {
toni@1150
   170
            int hash = 7;
toni@1150
   171
            hash = 29 * hash + Objects.hashCode(this.stops);
toni@1150
   172
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x0) ^ (Double.doubleToLongBits(this.x0) >>> 32));
toni@1150
   173
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y0) ^ (Double.doubleToLongBits(this.y0) >>> 32));
toni@1150
   174
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.x1) ^ (Double.doubleToLongBits(this.x1) >>> 32));
toni@1150
   175
            hash = 29 * hash + (int) (Double.doubleToLongBits(this.y1) ^ (Double.doubleToLongBits(this.y1) >>> 32));
toni@1150
   176
            return hash;
toni@1150
   177
        }
toni@1150
   178
toni@1150
   179
        @Override
toni@1150
   180
        public boolean equals(Object obj) {
toni@1150
   181
            if (obj == null) {
toni@1150
   182
                return false;
toni@1150
   183
            }
toni@1150
   184
            if (getClass() != obj.getClass()) {
toni@1150
   185
                return false;
toni@1150
   186
            }
toni@1150
   187
            final LinearGradient other = (LinearGradient) obj;
toni@1150
   188
            if (!Objects.equals(this.stops, other.stops)) {
toni@1150
   189
                return false;
toni@1150
   190
            }
toni@1150
   191
            if (Double.doubleToLongBits(this.x0) != Double.doubleToLongBits(other.x0)) {
toni@1150
   192
                return false;
toni@1150
   193
            }
toni@1150
   194
            if (Double.doubleToLongBits(this.y0) != Double.doubleToLongBits(other.y0)) {
toni@1150
   195
                return false;
toni@1150
   196
            }
toni@1150
   197
            if (Double.doubleToLongBits(this.x1) != Double.doubleToLongBits(other.x1)) {
toni@1150
   198
                return false;
toni@1150
   199
            }
toni@1150
   200
            if (Double.doubleToLongBits(this.y1) != Double.doubleToLongBits(other.y1)) {
toni@1150
   201
                return false;
toni@1150
   202
            }
toni@1150
   203
            return true;
toni@1150
   204
        }
toni@1150
   205
    }
toni@1150
   206
toni@1150
   207
    public static final class RadialGradient extends LinearGradient {
toni@1150
   208
toni@1150
   209
        private double r0, r1;
toni@1150
   210
toni@1150
   211
        RadialGradient(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@1150
   212
            super(x0, y0, x1, y1);
toni@1150
   213
            this.r0 = r0;
toni@1150
   214
            this.r1 = r1;
toni@1150
   215
        }
toni@1150
   216
toni@1150
   217
        public double getR0() {
toni@1150
   218
            return r0;
toni@1150
   219
        }
toni@1150
   220
toni@1150
   221
        public void setR0(double r0) {
toni@1150
   222
            this.r0 = r0;
toni@1150
   223
        }
toni@1150
   224
toni@1150
   225
        public double getR1() {
toni@1150
   226
            return r1;
toni@1150
   227
        }
toni@1150
   228
toni@1150
   229
        public void setR1(double r1) {
toni@1150
   230
            this.r1 = r1;
toni@1150
   231
        }
toni@1150
   232
toni@1150
   233
        @Override
toni@1150
   234
        public int hashCode() {
toni@1150
   235
            int hash = super.hashCode();
toni@1150
   236
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r0) ^ (Double.doubleToLongBits(this.r0) >>> 32));
toni@1150
   237
            hash = 17 * hash + (int) (Double.doubleToLongBits(this.r1) ^ (Double.doubleToLongBits(this.r1) >>> 32));
toni@1150
   238
toni@1150
   239
            return hash;
toni@1150
   240
        }
toni@1150
   241
toni@1150
   242
        @Override
toni@1150
   243
        public boolean equals(Object obj) {
toni@1150
   244
            if (obj == null) {
toni@1150
   245
                return false;
toni@1150
   246
            }
toni@1150
   247
            if (getClass() != obj.getClass()) {
toni@1150
   248
                return false;
toni@1150
   249
            }
toni@1150
   250
            if (!super.equals(obj)) {
toni@1150
   251
                return false;
toni@1150
   252
            }
toni@1150
   253
            final RadialGradient other = (RadialGradient) obj;
toni@1150
   254
            if (Double.doubleToLongBits(this.r0) != Double.doubleToLongBits(other.r0)) {
toni@1150
   255
                return false;
toni@1150
   256
            }
toni@1150
   257
            if (Double.doubleToLongBits(this.r1) != Double.doubleToLongBits(other.r1)) {
toni@1150
   258
                return false;
toni@1150
   259
            }
toni@1150
   260
            return true;
toni@1150
   261
        }
toni@1150
   262
    }
toni@1129
   263
}