View Javadoc

1   /*
2    * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
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 org.gwtwidgets.client.svg;
18  
19  import org.gwtwidgets.client.style.Color;
20  import org.gwtwidgets.client.ext.Namespace;
21  
22  
23  public class SVGLinearGradient implements SVGGradient
24  {
25      private String id;
26      private SVGLinearGradientWidget widget;
27      private Namespace ns;
28  
29  
30  
31      protected SVGLinearGradient (Namespace ns, String id)
32      {
33          this.id = id;
34          this.ns = ns;
35          this.widget = new SVGLinearGradientWidget(ns, id);
36      }
37  
38      protected SVGLinearGradientWidget getWidget ()
39      {
40          return widget;
41      }
42  
43      public String getId ()
44      {
45          return id;
46      }
47  
48      
49      /***
50       * Set a gradient stop
51       * @param percent 1-100
52       * @param color
53       */
54      public SVGLinearGradient addStop (int percent, Color color)
55      {
56          SVGStop stop = new SVGStop(ns);
57          stop.setOffset(percent + "%");
58          stop.setStopColor(color);
59          this.getWidget().add(stop);
60          return this;
61      }
62  
63      public SVGLinearGradient addStop (int percent, double opacity, Color color)
64      {
65          SVGStop stop = new SVGStop(ns);
66          stop.setOffset(percent + "%");
67          stop.setOpacity(opacity);
68          stop.setStopColor(color);
69          this.getWidget().add(stop);
70          return this;
71      }
72  
73      public SVGLinearGradient setVector (int x1Percent, int y1Percent, int x2Percent, int y2Percent)
74      {
75          SVGWidget w = getWidget();
76          w.setAttributeNS(w.getElement(), "gradientUnits", "objectBoundingBox");
77          w.setAttributeNS(w.getElement(), "x1", x1Percent + "%");
78          w.setAttributeNS(w.getElement(), "y1", y1Percent + "%");
79          w.setAttributeNS(w.getElement(), "x2", x2Percent + "%");
80          w.setAttributeNS(w.getElement(), "y2", y2Percent + "%");
81          return this;
82      }
83  
84      
85      public class SVGLinearGradientWidget extends SVGContainerBase
86      {
87  
88          protected SVGLinearGradientWidget (Namespace ns, String id)
89          {
90              setElement(createNsElement(ns, "linearGradient"));
91              setAttributeNS(getElement(), "id", id);
92          }
93  
94      }
95  
96  }