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