View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  
19  package org.indi.objects;
20  
21  /**
22   * A single indi number
23   * 
24   * @author Dirk Hünniger
25   * 
26   */
27  public class Number extends Element {
28  
29      /**
30       * 
31       */
32      private static final long serialVersionUID = 1L;
33      /**
34       * the format string used by the client to display the number
35       */
36      private String format = "%f";
37      /**
38       * the minimum value of the number
39       */
40      private double min = 0;
41      /**
42       * the maximum value of the number
43       */
44      private double max = 0;
45      /**
46       * the step in with the gui may increase decrease the number
47       */
48      private double step = 0;
49  
50      /**
51       * class constructor
52       * 
53       * @param name
54       *                the name of the Number
55       * @param label
56       *                the label of the Number (to be used for display in the
57       *                GUI)
58       * @param format
59       *                the format string used by the client to display the number
60       * @param min
61       *                the minimum value of the number
62       * @param max
63       *                the maximum value of the number
64       * @param step
65       *                the step in with the gui may increase decrease the number
66       * @param value
67       *                the value of the number
68       */
69      public Number(String name, String label, String format, double min,
70  	    double max, double step, double value) {
71  	super(name, label, String.valueOf(value));
72  	this.format = format;
73  	this.min = min;
74  	this.max = max;
75  	this.step = step;
76      }
77  
78      /**
79       * class constructor
80       * 
81       * @param name
82       *                the name of the Number
83       * @param value
84       *                the value of the number
85       */
86      public Number(String name, String value) {
87  	super(name, null, value);
88      }
89  
90      /**
91       * return the value of the number as double
92       * 
93       * @return
94       */
95      public double getDouble() {
96  	return Double.valueOf(this.value);
97      }
98  
99      /**
100      * set the value of the number by a double
101      * 
102      * @param d
103      *                the new value to be set
104      */
105     public void setDouble(double d) {
106 	this.value = Double.toString(d);
107     }
108 
109     /**
110      * add a double to the current value
111      * 
112      * @param d
113      *                the value to be added
114      */
115     public void addDouble(double d) {
116 	this.value = Double.toString(d + getDouble());
117     }
118 
119     /**
120      * subtract a double from the current value
121      * 
122      * @param d
123      *                the value to be subtracted
124      */
125     public void subtractDouble(double d) {
126 	this.value = Double.toString(getDouble() - d);
127     }
128 
129     @Override
130     public String getStartTag(TransferType type) {
131 	switch (type) {
132 	case Def:
133 	    return "<def" + getType() + " name=\"" + this.name + "\" label=\""
134 		    + this.label + "\" format=\"" + this.format + "\" min=\""
135 		    + String.valueOf(this.min) + "\" max=\""
136 		    + String.valueOf(this.max) + "\" step=\""
137 		    + String.valueOf(this.step) + "\"> ";
138 	default:
139 	    return super.getStartTag(type);
140 	}
141     }
142 
143     @Override
144     protected String getType() {
145 	return "Number";
146     }
147 }