root / src / com / rackspacecloud / android / AddNodeActivity.java @ c4a37180
History | View | Annotate | Download (6.5 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
4 |
|
5 |
import android.content.Intent; |
6 |
import android.os.Bundle; |
7 |
import android.view.View; |
8 |
import android.view.View.OnClickListener; |
9 |
import android.view.Window; |
10 |
import android.widget.AdapterView; |
11 |
import android.widget.AdapterView.OnItemSelectedListener; |
12 |
import android.widget.ArrayAdapter; |
13 |
import android.widget.Button; |
14 |
import android.widget.Spinner; |
15 |
import android.widget.EditText; |
16 |
import android.widget.TextView; |
17 |
|
18 |
public class AddNodeActivity extends CloudActivity{ |
19 |
|
20 |
private final String[] CONDITIONS = {"Enabled", "Disabled", "Draining"}; |
21 |
private String[] ipAddresses; |
22 |
private String name; |
23 |
private String selectedPort; |
24 |
private String selectedIp; |
25 |
private String selectedWeight; |
26 |
private String selectedCondition; |
27 |
private boolean weighted; |
28 |
private Spinner conditionSpinner;
|
29 |
private Spinner ipAddressSpinner;
|
30 |
private EditText weightText;
|
31 |
private Node node;
|
32 |
|
33 |
public void onCreate(Bundle savedInstanceState) { |
34 |
super.onCreate(savedInstanceState);
|
35 |
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
36 |
setContentView(R.layout.addnode); |
37 |
ipAddresses = (String[]) this.getIntent().getExtras().get("ipAddresses"); |
38 |
name = (String) this.getIntent().getExtras().get("name"); |
39 |
weighted = (Boolean) this.getIntent().getExtras().get("weighted"); |
40 |
node = (Node) this.getIntent().getExtras().get("node"); |
41 |
restoreState(savedInstanceState); |
42 |
} |
43 |
|
44 |
protected void restoreState(Bundle state) { |
45 |
super.restoreState(state);
|
46 |
|
47 |
if (state != null){ |
48 |
if(state.containsKey("selectedPort")){ |
49 |
selectedPort = (String) state.getString("selectedPort"); |
50 |
} |
51 |
|
52 |
if(state.containsKey("selectedIp")){ |
53 |
selectedIp = (String) state.getString("selectedIp"); |
54 |
} |
55 |
|
56 |
if(state.containsKey("selectedWeight")){ |
57 |
selectedWeight = (String) state.getString("selectedWeight"); |
58 |
} |
59 |
|
60 |
if(state.containsKey("selectedCondition")){ |
61 |
selectedCondition = (String) state.getString("selectedCondition"); |
62 |
} |
63 |
} |
64 |
|
65 |
setupInputs(); |
66 |
} |
67 |
|
68 |
@Override
|
69 |
protected void onSaveInstanceState(Bundle outState) { |
70 |
super.onSaveInstanceState(outState);
|
71 |
outState.putString("selectedPort", selectedPort);
|
72 |
outState.putString("selectedIp", selectedIp);
|
73 |
outState.putString("selectedWeight", selectedWeight);
|
74 |
outState.putString("selectedCondition", selectedCondition);
|
75 |
} |
76 |
|
77 |
private void setupInputs(){ |
78 |
setupWeightedText(); |
79 |
loadConditionSpinner(); |
80 |
loadIpSpinner(); |
81 |
setUpButton(); |
82 |
restoreNode(); |
83 |
} |
84 |
|
85 |
private void setupWeightedText(){ |
86 |
((TextView)findViewById(R.id.node_name)).setText(name); |
87 |
|
88 |
weightText = (EditText) findViewById(R.id.node_weight_text); |
89 |
|
90 |
//if algorithm is not weighted then then node's weight will be null
|
91 |
if(!weighted){
|
92 |
TextView weightLabel = (TextView) findViewById(R.id.node_weight_label); |
93 |
weightLabel.setVisibility(View.GONE);
|
94 |
weightText.setVisibility(View.GONE);
|
95 |
} |
96 |
|
97 |
} |
98 |
|
99 |
private void loadConditionSpinner(){ |
100 |
conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner); |
101 |
ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS); |
102 |
conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
103 |
conditionSpinner.setAdapter(conditionAdapter); |
104 |
|
105 |
conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
|
106 |
|
107 |
@Override
|
108 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
109 |
selectedCondition = CONDITIONS[pos]; |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public void onNothingSelected(AdapterView<?> arg0) { |
114 |
|
115 |
} |
116 |
|
117 |
}); |
118 |
} |
119 |
|
120 |
private void loadIpSpinner(){ |
121 |
ipAddressSpinner = (Spinner) findViewById(R.id.node_ip_spinner); |
122 |
ArrayAdapter<String> ipAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ipAddresses); |
123 |
ipAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
124 |
ipAddressSpinner.setAdapter(ipAdapter); |
125 |
|
126 |
ipAddressSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
|
127 |
|
128 |
@Override
|
129 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
130 |
selectedIp = ipAddresses[pos]; |
131 |
} |
132 |
|
133 |
@Override
|
134 |
public void onNothingSelected(AdapterView<?> arg0) { |
135 |
|
136 |
} |
137 |
|
138 |
}); |
139 |
|
140 |
} |
141 |
|
142 |
private void setUpButton(){ |
143 |
Button submit = (Button) findViewById(R.id.add_node_button); |
144 |
submit.setOnClickListener(new OnClickListener() {
|
145 |
|
146 |
@Override
|
147 |
public void onClick(View v) { |
148 |
selectedPort = ((EditText)findViewById(R.id.node_port_text)).getText().toString(); |
149 |
selectedWeight = weightText.getText().toString(); |
150 |
if(!validPort()){
|
151 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
152 |
} else if(!(weightText.getVisibility() == View.GONE || (weightText.getVisibility() != View.GONE && validWeight(selectedWeight)))){ |
153 |
showAlert("Error", "Weight must be between 1 and 100."); |
154 |
} |
155 |
|
156 |
else{
|
157 |
Intent data = new Intent();
|
158 |
data.putExtra("nodeIp", selectedIp);
|
159 |
data.putExtra("nodePort", selectedPort);
|
160 |
data.putExtra("nodeCondition", selectedCondition);
|
161 |
data.putExtra("nodeWeight", selectedWeight);
|
162 |
setResult(RESULT_OK, data); |
163 |
finish(); |
164 |
} |
165 |
} |
166 |
}); |
167 |
} |
168 |
|
169 |
//if the node was previously selected need to
|
170 |
//restore the old values
|
171 |
private void restoreNode(){ |
172 |
if(node != null){ |
173 |
weightText.setText(node.getWeight()); |
174 |
int location = getLocation(ipAddresses, node.getAddress());
|
175 |
if(location >= 0){ |
176 |
ipAddressSpinner.setSelection(location); |
177 |
} |
178 |
location = getLocation(CONDITIONS, node.getCondition()); |
179 |
if(location >= 0){ |
180 |
conditionSpinner.setSelection(location); |
181 |
} |
182 |
((EditText) findViewById(R.id.node_port_text)).setText(node.getPort()); |
183 |
} |
184 |
} |
185 |
|
186 |
//returns the location in objects of object
|
187 |
//if it doesn't exist return -1
|
188 |
private int getLocation(Object[] objects, Object object){ |
189 |
if(objects == null || object == null){ |
190 |
return -1; |
191 |
} else {
|
192 |
for(int i = 0; i < objects.length; i++){ |
193 |
if(objects[i].toString().equalsIgnoreCase(object.toString())){
|
194 |
return i;
|
195 |
} |
196 |
} |
197 |
return -1; |
198 |
} |
199 |
} |
200 |
|
201 |
private boolean validPort(){ |
202 |
boolean result;
|
203 |
try{
|
204 |
result = !selectedPort.equals("") && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536; |
205 |
} catch (NumberFormatException e) { |
206 |
result = false;
|
207 |
} |
208 |
return result;
|
209 |
} |
210 |
private Boolean validWeight(String weight){ |
211 |
if(weight.equals("")){ |
212 |
return false; |
213 |
} |
214 |
else{
|
215 |
int w = Integer.valueOf(weight); |
216 |
return w >= 1 && w <= 100 ; |
217 |
} |
218 |
} |
219 |
|
220 |
public void onBackPressed(){ |
221 |
if(node == null){ |
222 |
setResult(RESULT_CANCELED); |
223 |
} else {
|
224 |
setResult(RESULT_OK); |
225 |
} |
226 |
finish(); |
227 |
} |
228 |
|
229 |
} |