summaryrefslogtreecommitdiff
path: root/trunk/software/host/src/org/reprap/gui/PreferencesValue.java
blob: 54543f812edf1fb1f51cb9757d500eebe6bf3224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.reprap.gui;
import javax.swing.ButtonGroup;
import javax.swing.DefaultButtonModel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.GridLayout;


public class PreferencesValue {
	
	private JTextField textfieldValue = null;
	private BooleanChoice boolchoiceValue = null;
	
	
	public class BooleanChoice extends JPanel {
		private boolean userchoice;
		private JRadioButton trueButton, falseButton;
		private ButtonGroup bgroup;
		private JTextField valueTextField;
		
		public BooleanChoice(Boolean boolvalue)
		{
			
			
			if(boolvalue == true)
				trueButton   = new JRadioButton("True"  , true);
			else
				trueButton   = new JRadioButton("True"  , false);
			
			if(boolvalue == false)
				falseButton  = new JRadioButton("False"   , true);
			else
				falseButton  = new JRadioButton("False"   , false);

			bgroup = new ButtonGroup();
			bgroup.add(trueButton);
			bgroup.add(falseButton);
			

			this.setLayout(new GridLayout(1, 2));
			this.add(trueButton);
			this.add(falseButton);
			
			this.userchoice = boolvalue;

		}
		
		public String getText()
		{
			
			if(bgroup.isSelected( (DefaultButtonModel)trueButton.getModel() ))
				this.userchoice = true;
			else
				this.userchoice = false;
			
			if(this.userchoice)
				return "true";
			else
				return "false";
		}
		
		public void setValue(boolean boolvalue)
		{
			if(boolvalue == true)
				trueButton.setSelected(true); 
			else
				trueButton.setSelected(false); 
			
			if(boolvalue == false)
				falseButton.setSelected(true);
			else
				falseButton.setSelected(false);
		}
	}

	
	
	public PreferencesValue(JTextField l)
	{
		textfieldValue = l;
	}
	
	public PreferencesValue(BooleanChoice b)
	{
		boolchoiceValue = b;
	}
	
	public String getText()
	{
		if(textfieldValue != null)
			return textfieldValue.getText();
		if(boolchoiceValue != null)
			return boolchoiceValue.getText();
		
		return null;
	}
	
	public void setText(String str)
	{
		if(textfieldValue != null)
			textfieldValue.setText(str);
		if(boolchoiceValue != null)
			boolchoiceValue.setValue(getBoolFromString(str));
	}
	
	public Component getObject()
	{
		if(textfieldValue != null)
			return textfieldValue;
		else return boolchoiceValue;
	}
	

	private boolean getBoolFromString(String strVal)
	{

		if (strVal.compareToIgnoreCase("true") == 0) return true;
		
		return false;
	}
	
	public void makeBoolean()
	{
		boolean boolvalue = getBoolFromString(this.getText());
		textfieldValue = null;
		boolchoiceValue = new BooleanChoice(boolvalue);
	}
}