root / src / com / rackspacecloud / android / AddNodeActivity.java @ 96a8e632
History | View | Annotate | Download (3.9 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import android.app.Activity; |
4 |
import android.app.AlertDialog; |
5 |
import android.content.Context; |
6 |
import android.content.DialogInterface; |
7 |
import android.content.Intent; |
8 |
import android.os.Bundle; |
9 |
import android.view.View; |
10 |
import android.view.View.OnClickListener; |
11 |
import android.view.Window; |
12 |
import android.widget.AdapterView; |
13 |
import android.widget.AdapterView.OnItemSelectedListener; |
14 |
import android.widget.ArrayAdapter; |
15 |
import android.widget.Button; |
16 |
import android.widget.Spinner; |
17 |
import android.widget.EditText; |
18 |
|
19 |
public class AddNodeActivity extends Activity{ |
20 |
|
21 |
private static final int NODE_ADDED_RESULT_CODE = 111; |
22 |
private static final int CANCEL_RESULT_CODE = 112; |
23 |
private final String[] CONDITIONS = {"Enabled", "Disabled", "Draining"}; |
24 |
private String[] ipAddresses; |
25 |
private Spinner conditionSpinner;
|
26 |
private Spinner ipAddressSpinner;
|
27 |
private String selectedPort; |
28 |
private String selectedIp; |
29 |
private String selectedCondition; |
30 |
|
31 |
public void onCreate(Bundle savedInstanceState) { |
32 |
super.onCreate(savedInstanceState);
|
33 |
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
34 |
setContentView(R.layout.addnode); |
35 |
ipAddresses = (String[]) this.getIntent().getExtras().get("ipAddresses"); |
36 |
restoreState(savedInstanceState); |
37 |
loadConditionSpinner(); |
38 |
loadIpSpinner(); |
39 |
setUpButton(); |
40 |
} |
41 |
|
42 |
private void restoreState(Bundle state) { |
43 |
|
44 |
} |
45 |
|
46 |
private void setUpButton(){ |
47 |
Button submit = (Button) findViewById(R.id.add_node_button); |
48 |
submit.setOnClickListener(new OnClickListener() {
|
49 |
|
50 |
@Override
|
51 |
public void onClick(View v) { |
52 |
selectedPort = ((EditText)findViewById(R.id.node_port_text)).getText().toString(); |
53 |
if(!validPort()){
|
54 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
55 |
} |
56 |
else{
|
57 |
Intent data = new Intent();
|
58 |
data.putExtra("nodeIp", selectedIp);
|
59 |
data.putExtra("nodePort", selectedPort);
|
60 |
data.putExtra("nodeCondition", selectedCondition);
|
61 |
setResult(NODE_ADDED_RESULT_CODE, data); |
62 |
finish(); |
63 |
} |
64 |
|
65 |
} |
66 |
}); |
67 |
} |
68 |
|
69 |
public void onBackPressed(){ |
70 |
setResult(CANCEL_RESULT_CODE); |
71 |
finish(); |
72 |
} |
73 |
|
74 |
private void loadIpSpinner(){ |
75 |
ipAddressSpinner = (Spinner) findViewById(R.id.node_ip_spinner); |
76 |
ArrayAdapter<String> ipAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ipAddresses); |
77 |
ipAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
78 |
ipAddressSpinner.setAdapter(ipAdapter); |
79 |
|
80 |
ipAddressSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
|
81 |
|
82 |
@Override
|
83 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
84 |
selectedIp = ipAddresses[pos]; |
85 |
} |
86 |
|
87 |
@Override
|
88 |
public void onNothingSelected(AdapterView<?> arg0) { |
89 |
|
90 |
} |
91 |
|
92 |
}); |
93 |
} |
94 |
|
95 |
private void loadConditionSpinner(){ |
96 |
conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner); |
97 |
|
98 |
ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS); |
99 |
conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
100 |
conditionSpinner.setAdapter(conditionAdapter); |
101 |
|
102 |
conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
|
103 |
|
104 |
@Override
|
105 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
106 |
selectedCondition = CONDITIONS[pos]; |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public void onNothingSelected(AdapterView<?> arg0) { |
111 |
|
112 |
} |
113 |
|
114 |
}); |
115 |
} |
116 |
|
117 |
private boolean validPort(){ |
118 |
return !selectedPort.equals("") && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536; |
119 |
} |
120 |
|
121 |
private void showAlert(String title, String message) { |
122 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
123 |
alert.setTitle(title); |
124 |
alert.setMessage(message); |
125 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
126 |
public void onClick(DialogInterface dialog, int which) { |
127 |
return;
|
128 |
} }); |
129 |
alert.show(); |
130 |
} |
131 |
|
132 |
} |