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