001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package com.sun.svg.component;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.svg.SVGAnimationElement;
036: import org.w3c.dom.svg.SVGElement;
037: import org.w3c.dom.svg.SVGLocatableElement;
038: import org.w3c.dom.svg.SVGMatrix;
039: import org.w3c.dom.svg.SVGRect;
040: import org.w3c.dom.svg.SVGSVGElement;
041:
042: /**
043: * The convention for a scroll bar is that the background element has the 'bkg' suffix
044: * (e.g., "myScrollBar.bkg") and the thumb element has the suffix 'thumb'
045: * (e.g., "myScrollBar.thumb").
046: */
047: public class SVGHorizontalScrollBar {
048: static final String BACKGROUND_SUFFIX = "bkg";
049: static final String THUMB_SUFFIX = "thumb";
050:
051: /**
052: * The scroll bar thumb
053: */
054: protected SVGLocatableElement thumb;
055:
056: /**
057: * The thumb's initial transform.
058: */
059: protected SVGMatrix thumbTxf;
060:
061: /**
062: * The min position of the thumb along the x-axis
063: */
064: protected float minTranslate;
065:
066: /**
067: * The max position of the thumb along the x-axis
068: */
069: protected float maxTranslate;
070:
071: /**
072: * The document's root svg element.
073: */
074: protected SVGSVGElement svg;
075:
076: /**
077: * The scroll bar's id prefix, i.e., the prefix used for all of the
078: * scrollbar component ids.
079: */
080: protected String idPrefix;
081:
082: /**
083: * The current scrollbar position.
084: */
085: protected float pos;
086:
087: /**
088: * @param idPrefix the scrollBar id prefix.
089: */
090: public SVGHorizontalScrollBar(final String idPrefix) {
091: if (idPrefix == null || "".equals(idPrefix)) {
092: throw new IllegalArgumentException(
093: "SVGHorizontalScrollBar "
094: + "idPrefix should not be null or empty string");
095: }
096:
097: this .idPrefix = idPrefix;
098: }
099:
100: /**
101: * Hooks this scroll bar with a new skin.
102: *
103: * @param doc - the new skin content.
104: */
105: public void hookSkin(final Document doc) {
106: svg = (SVGSVGElement) doc.getDocumentElement();
107: SVGLocatableElement bkg = (SVGLocatableElement) doc
108: .getElementById(idPrefix + "." + BACKGROUND_SUFFIX);
109:
110: if (bkg == null) {
111: throw new IllegalArgumentException(
112: "SVGHorizontalScrollBar : element with id "
113: + idPrefix + "." + BACKGROUND_SUFFIX
114: + " does not exist in skin");
115: }
116:
117: thumb = (SVGLocatableElement) doc.getElementById(idPrefix + "."
118: + THUMB_SUFFIX);
119: if (thumb == null) {
120: throw new IllegalArgumentException(
121: "SVGHorizontalScrollBar : element with id "
122: + idPrefix + "." + THUMB_SUFFIX
123: + " does not exist in skin");
124: }
125:
126: SVGRect bkgBBox = bkg.getBBox();
127: SVGRect thumbBBox = thumb.getBBox();
128:
129: thumbTxf = thumb.getMatrixTrait("transform");
130:
131: minTranslate = -thumbBBox.getX() + bkgBBox.getX();
132: maxTranslate = -thumbBBox.getX() + bkgBBox.getX()
133: + bkgBBox.getWidth() - thumbBBox.getWidth();
134:
135: setThumbPosition(pos);
136:
137: }
138:
139: /**
140: * @param pos the desired thumb position in the [0, 1] interval. If the value is out of range, it
141: * is clipped to the valid range.
142: */
143: public void setThumbPosition(float pos) {
144: if (pos < 0) {
145: pos = 0;
146: } else if (pos > 1) {
147: pos = 1;
148: }
149:
150: float xTranslate = minTranslate + pos
151: * (maxTranslate - minTranslate);
152: SVGMatrix txf = svg.createSVGMatrixComponents(1, 0, 0, 1, 0, 0);
153: txf.mMultiply(thumbTxf);
154: txf.mTranslate(xTranslate, 0);
155: thumb.setMatrixTrait("transform", txf);
156: this.pos = pos;
157: }
158:
159: }
|