001 package org.gwtbootstrap3.extras.slider.client.ui;
002
003 /*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 - 2015 GwtBootstrap3
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 * http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023 import com.google.gwt.core.client.JavaScriptObject;
024 import com.google.gwt.core.client.JsArrayNumber;
025 import com.google.gwt.core.client.JsonUtils;
026
027 /**
028 * Slider range with a min value and a max value.
029 */
030 public class Range {
031
032 private double minValue;
033 private double maxValue;
034
035 protected Range() {
036 }
037
038 /**
039 * Create a slider range with a min value and a max value.
040 *
041 * @param minValue
042 * @param maxValue
043 */
044 public Range(final double minValue, final double maxValue) {
045 this.minValue = minValue;
046 this.maxValue = maxValue;
047 }
048
049 /**
050 * Creates a slider range with a JavaScritp number array. <br>
051 * <br>
052 * This constructor is useful in JSNI calls.
053 *
054 * @param array
055 */
056 public Range(final JsArrayNumber array) {
057 this(array.get(0), array.get(1));
058 }
059
060 /**
061 * Returns the min value.
062 *
063 * @return the min value
064 */
065 public double getMinValue() {
066 return minValue;
067 }
068
069 /**
070 * Returns the max value.
071 *
072 * @return the max value
073 */
074 public double getMaxValue() {
075 return maxValue;
076 }
077
078 /**
079 * Converts the range to a JavaScript number array.
080 *
081 * @return a JavaScript number array
082 */
083 public JsArrayNumber toJsArray() {
084 JsArrayNumber array = JavaScriptObject.createArray().cast();
085 array.push(minValue);
086 array.push(maxValue);
087 return array;
088 }
089
090 /**
091 * Converts the given string to a range instance.<br>
092 * <br>
093 * Useful when using UiBinder.
094 *
095 * @param value
096 * @return
097 */
098 public static Range fromString(String value) {
099 if (value == null || value.isEmpty())
100 return null;
101 JsArrayNumber array = JsonUtils.safeEval(value);
102 return new Range(array);
103 }
104
105 @Override
106 public String toString() {
107 return new StringBuilder("[")
108 .append(getMinValue()).append(", ")
109 .append(getMaxValue())
110 .append("]").toString();
111 }
112
113 }