1 /*
2  * Created on 10 juil. 2003
3  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * This program 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
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16  *
17  */
18 package org.gudy.azureus2.ui.swt.config;
19 
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.swt.widgets.Listener;
25 import org.eclipse.swt.widgets.Text;
26 import org.gudy.azureus2.core3.config.*;
27 import org.gudy.azureus2.ui.swt.Utils;
28 
29 public class FloatParameter {
30 
31   Text inputField;
32   float fMinValue = 0;
33   float fMaxValue = -1;
34   float fDefaultValue;
35   int iDigitsAfterDecimal = 1;
36   String sParamName;
37   boolean allowZero = false;
38 
FloatParameter(Composite composite, final String name)39   public FloatParameter(Composite composite, final String name) {
40     fDefaultValue = COConfigurationManager.getFloatParameter(name);
41     initialize(composite,name);
42   }
43 
FloatParameter(Composite composite, final String name, float minValue, float maxValue, boolean allowZero, int digitsAfterDecimal)44   public FloatParameter(Composite composite, final String name,
45                         float minValue, float maxValue, boolean allowZero,
46                         int digitsAfterDecimal) {
47     fDefaultValue = COConfigurationManager.getFloatParameter(name);
48     initialize(composite,name);
49     fMinValue = minValue;
50     fMaxValue = maxValue;
51     this.allowZero = allowZero;
52     iDigitsAfterDecimal = digitsAfterDecimal;
53   }
54 
55 
initialize(Composite composite, final String name)56   public void initialize(Composite composite, final String name) {
57     sParamName = name;
58 
59     inputField = new Text(composite, SWT.BORDER | SWT.RIGHT);
60     float value = COConfigurationManager.getFloatParameter(name);
61     inputField.setText(String.valueOf(value));
62     inputField.addListener(SWT.Verify, new Listener() {
63       public void handleEvent(Event e) {
64         String text = e.text;
65         char[] chars = new char[text.length()];
66         text.getChars(0, chars.length, chars, 0);
67         for (int i = 0; i < chars.length; i++) {
68           if ( !((chars[i] >= '0' && chars[i] <= '9') || chars[i] == '.') ) {
69             e.doit = false;
70             return;
71           }
72         }
73       }
74     });
75 
76     inputField.addListener(SWT.Modify, new Listener() {
77       public void handleEvent(Event event) {
78         try {
79           float val = Float.parseFloat(inputField.getText());
80           if (val < fMinValue) {
81             if (!(allowZero && val == 0)) {
82             	val = fMinValue;
83             }
84           }
85           if (val > fMaxValue) {
86             if (fMaxValue > -1) {
87               val = fMaxValue;
88             }
89           }
90           COConfigurationManager.setParameter(name, val);
91         }
92         catch (Exception e) {}
93       }
94     });
95 
96     inputField.addListener(SWT.FocusOut, new Listener() {
97       public void handleEvent(Event event) {
98         try {
99           float val = Float.parseFloat(inputField.getText());
100           if (val < fMinValue) {
101             if (!(allowZero && val == 0)) {
102               inputField.setText(String.valueOf(fMinValue));
103               COConfigurationManager.setParameter(name, fMinValue);
104             }
105           }
106           if (val > fMaxValue) {
107             if (fMaxValue > -1) {
108             	inputField.setText(String.valueOf(fMaxValue));
109             	COConfigurationManager.setParameter(name, fMaxValue);
110             }
111           }
112         }
113         catch (Exception e) {}
114       }
115     });
116   }
117 
118 
setLayoutData(Object layoutData)119   public void setLayoutData(Object layoutData) {
120   	Utils.adjustPXForDPI(layoutData);
121     inputField.setLayoutData(layoutData);
122   }
123 
124   public Control
getControl()125   getControl()
126   {
127   	return( inputField );
128   }
129 }
130