root / src / com / rackspacecloud / android / AddLoadBalancerActivity.java @ 639ae8d6
History | View | Annotate | Download (6 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import com.rackspace.cloud.loadbalancer.api.client.Algorithm; |
4 |
import com.rackspace.cloud.loadbalancer.api.client.Protocol; |
5 |
|
6 |
import android.app.Activity; |
7 |
import android.app.AlertDialog; |
8 |
import android.content.DialogInterface; |
9 |
import android.content.Intent; |
10 |
import android.os.Bundle; |
11 |
import android.view.View; |
12 |
import android.view.View.OnClickListener; |
13 |
import android.widget.AdapterView; |
14 |
import android.widget.AdapterView.OnItemSelectedListener; |
15 |
import android.widget.ArrayAdapter; |
16 |
import android.widget.Button; |
17 |
import android.widget.EditText; |
18 |
import android.widget.Spinner; |
19 |
|
20 |
public class AddLoadBalancerActivity extends Activity implements OnItemSelectedListener { |
21 |
|
22 |
//TODO Shared Virtual IP (not in API though?)
|
23 |
private static final String[] VIP_TYPES = {"Public", "ServiceNet"}; |
24 |
private static final String[] REGIONS = {"ORD", "DFW"}; |
25 |
|
26 |
private Protocol[] protocols; |
27 |
private Algorithm[] algorithms; |
28 |
private Spinner protocolSpinner;
|
29 |
private Spinner algorithmSpinner;
|
30 |
private Spinner vipSpinner;
|
31 |
private Spinner regionSpinner;
|
32 |
private EditText portText;
|
33 |
private Protocol selectedProtocol;
|
34 |
private Algorithm selectedAlgorithm;
|
35 |
private String selectedVipType; |
36 |
private String selectedRegion; |
37 |
private String selectedName; |
38 |
private String selectedPort; |
39 |
|
40 |
@Override
|
41 |
public void onCreate(Bundle savedInstanceState) { |
42 |
super.onCreate(savedInstanceState);
|
43 |
setContentView(R.layout.view_add_loadbalancer); |
44 |
restoreState(savedInstanceState); |
45 |
} |
46 |
|
47 |
private void restoreState(Bundle state) { |
48 |
portText = (EditText)findViewById(R.id.edit_port_text); |
49 |
loadProtocolSpinner(); |
50 |
loadAlgorithmSpinner(); |
51 |
loadVipSpinner(); |
52 |
loadRegionSpinner(); |
53 |
setUpButton(); |
54 |
} |
55 |
|
56 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
57 |
if (parent == protocolSpinner) {
|
58 |
selectedProtocol = protocols[position]; |
59 |
portText.setText(protocols[position].getPort()); |
60 |
} |
61 |
else if (parent == algorithmSpinner){ |
62 |
selectedAlgorithm = algorithms[position]; |
63 |
} |
64 |
else if (parent == vipSpinner){ |
65 |
selectedVipType = VIP_TYPES[position]; |
66 |
} |
67 |
else if (parent == regionSpinner){ |
68 |
selectedRegion = REGIONS[position]; |
69 |
} |
70 |
} |
71 |
|
72 |
public void onNothingSelected(AdapterView<?> parent) { |
73 |
|
74 |
} |
75 |
|
76 |
private void setUpButton(){ |
77 |
Button addNodes = (Button) findViewById(R.id.add_nodes_button); |
78 |
addNodes.setOnClickListener(new OnClickListener() {
|
79 |
|
80 |
@Override
|
81 |
public void onClick(View v) { |
82 |
selectedName = ((EditText)findViewById(R.id.edit_lb_name_text)).getText().toString(); |
83 |
selectedPort = ((EditText)findViewById(R.id.edit_port_text)).getText().toString(); |
84 |
if(!validName()){
|
85 |
showAlert("Error", "Load balancer name cannot be blank."); |
86 |
} |
87 |
else if(!validPort()){ |
88 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
89 |
} |
90 |
else{
|
91 |
Intent viewIntent = new Intent(getApplicationContext(), AddNodesActivity.class);
|
92 |
viewIntent.putExtra("name", selectedName);
|
93 |
viewIntent.putExtra("protocol", selectedProtocol);
|
94 |
viewIntent.putExtra("port", selectedPort);
|
95 |
viewIntent.putExtra("vip", selectedVipType);
|
96 |
viewIntent.putExtra("algorithm", selectedAlgorithm);
|
97 |
viewIntent.putExtra("region", selectedRegion);
|
98 |
startActivity(viewIntent); |
99 |
} |
100 |
} |
101 |
}); |
102 |
} |
103 |
|
104 |
private boolean validName(){ |
105 |
return !selectedName.equals(""); |
106 |
} |
107 |
|
108 |
private boolean validPort(){ |
109 |
return !selectedPort.equals("") && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536; |
110 |
} |
111 |
|
112 |
private void loadRegionSpinner() { |
113 |
regionSpinner = (Spinner) findViewById(R.id.edit_region_spinner); |
114 |
regionSpinner.setOnItemSelectedListener(this);
|
115 |
|
116 |
ArrayAdapter<String> regionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, REGIONS); |
117 |
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
118 |
regionSpinner.setAdapter(regionAdapter); |
119 |
} |
120 |
|
121 |
private void loadVipSpinner() { |
122 |
vipSpinner = (Spinner) findViewById(R.id.edit_vip_spinner); |
123 |
vipSpinner.setOnItemSelectedListener(this);
|
124 |
|
125 |
ArrayAdapter<String> vipAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, VIP_TYPES); |
126 |
vipAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
127 |
vipSpinner.setAdapter(vipAdapter); |
128 |
} |
129 |
|
130 |
private void loadProtocolSpinner() { |
131 |
protocolSpinner = (Spinner) findViewById(R.id.edit_protocol_spinner); |
132 |
protocolSpinner.setOnItemSelectedListener(this);
|
133 |
String protocolNames[] = new String[Protocol.getProtocols().size()]; |
134 |
protocols = new Protocol[Protocol.getProtocols().size()];
|
135 |
|
136 |
for(int i = 0; i < Protocol.getProtocols().size(); i++){ |
137 |
protocols[i] = Protocol.getProtocols().get(i); |
138 |
protocolNames[i] = Protocol.getProtocols().get(i).getName(); |
139 |
} |
140 |
|
141 |
ArrayAdapter<String> protocolAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, protocolNames); |
142 |
protocolAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
143 |
protocolSpinner.setAdapter(protocolAdapter); |
144 |
} |
145 |
|
146 |
private void loadAlgorithmSpinner() { |
147 |
algorithmSpinner = (Spinner) findViewById(R.id.edit_algorithm_spinner); |
148 |
algorithmSpinner.setOnItemSelectedListener(this);
|
149 |
String algorithmNames[] = new String[Algorithm.getAlgorithms().size()]; |
150 |
algorithms = new Algorithm[Algorithm.getAlgorithms().size()];
|
151 |
|
152 |
for(int i = 0; i < Algorithm.getAlgorithms().size(); i++){ |
153 |
algorithms[i] = Algorithm.getAlgorithms().get(i); |
154 |
algorithmNames[i] = Algorithm.getAlgorithms().get(i).getName(); |
155 |
} |
156 |
|
157 |
ArrayAdapter<String> algorithmAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, algorithmNames); |
158 |
algorithmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
159 |
algorithmSpinner.setAdapter(algorithmAdapter); |
160 |
} |
161 |
|
162 |
private void showAlert(String title, String message) { |
163 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
164 |
alert.setTitle(title); |
165 |
alert.setMessage(message); |
166 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
167 |
public void onClick(DialogInterface dialog, int which) { |
168 |
return;
|
169 |
} }); |
170 |
alert.show(); |
171 |
} |
172 |
} |