root / src / com / rackspacecloud / android / EditNodesActivity.java @ 232548ba
History | View | Annotate | Download (5.8 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
6 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
7 |
import com.rackspace.cloud.loadbalancer.api.client.NodeManager; |
8 |
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException; |
9 |
|
10 |
import android.content.Intent; |
11 |
import android.os.AsyncTask; |
12 |
import android.os.Bundle; |
13 |
import android.util.Log; |
14 |
import android.view.LayoutInflater; |
15 |
import android.view.View; |
16 |
import android.view.View.OnClickListener; |
17 |
import android.view.ViewGroup; |
18 |
import android.widget.ArrayAdapter; |
19 |
import android.widget.Button; |
20 |
import android.widget.ListView; |
21 |
import android.widget.TextView; |
22 |
|
23 |
public class EditNodesActivity extends CloudListActivity { |
24 |
|
25 |
private static final int EDIT_NODE_CODE = 299; |
26 |
private static final int NODE_DELETED_CODE = 389; |
27 |
//private static final int ADD_MORE_NODES_CODE = 165;
|
28 |
|
29 |
private ArrayList<Node> nodes; |
30 |
private LoadBalancer loadBalancer;
|
31 |
private int cellType; |
32 |
|
33 |
@SuppressWarnings("unchecked") |
34 |
@Override
|
35 |
public void onCreate(Bundle savedInstanceState) { |
36 |
super.onCreate(savedInstanceState);
|
37 |
setContentView(R.layout.addnodes); |
38 |
nodes = (ArrayList<Node>) this.getIntent().getExtras().get("nodes"); |
39 |
loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer"); |
40 |
displayNodes(); |
41 |
restoreState(savedInstanceState); |
42 |
} |
43 |
|
44 |
@Override
|
45 |
protected void onSaveInstanceState(Bundle outState) { |
46 |
super.onSaveInstanceState(outState);
|
47 |
outState.putSerializable("nodes", nodes);
|
48 |
} |
49 |
|
50 |
@SuppressWarnings("unchecked") |
51 |
protected void restoreState(Bundle state) { |
52 |
super.restoreState(state);
|
53 |
|
54 |
if (state != null && state.containsKey("nodes")){ |
55 |
nodes = (ArrayList<Node>) state.getSerializable("nodes"); |
56 |
displayNodes(); |
57 |
} |
58 |
|
59 |
Button submitNodes = (Button) findViewById(R.id.submit_nodes_button); |
60 |
submitNodes.setOnClickListener(new OnClickListener() {
|
61 |
|
62 |
@Override
|
63 |
public void onClick(View v) { |
64 |
Intent viewIntent = new Intent(getApplicationContext(), AddMoreNodesActivity.class);
|
65 |
viewIntent.putExtra("nodes", nodes);
|
66 |
viewIntent.putExtra("loadBalancer", loadBalancer);
|
67 |
startActivityForResult(viewIntent, EDIT_NODE_CODE); |
68 |
} |
69 |
}); |
70 |
|
71 |
if(loadBalancer.getAlgorithm().contains("WEIGHTED")){ |
72 |
cellType = R.layout.displayweightednodecell; |
73 |
} |
74 |
else{
|
75 |
cellType = R.layout.displaynodecell; |
76 |
} |
77 |
} |
78 |
|
79 |
private void displayNodes(){ |
80 |
if (nodes.size() == 0) { |
81 |
displayNoNodesCell(); |
82 |
} else {
|
83 |
getListView().setDividerHeight(1); // restore divider lines |
84 |
setListAdapter(new NodeAdapter());
|
85 |
} |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public void onBackPressed(){ |
90 |
setResult(RESULT_CANCELED); |
91 |
finish(); |
92 |
} |
93 |
|
94 |
private void displayNoNodesCell() { |
95 |
String a[] = new String[1]; |
96 |
a[0] = "No Nodes"; |
97 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noserverscell, R.id.no_servers_label, a)); |
98 |
getListView().setTextFilterEnabled(true);
|
99 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
100 |
getListView().setItemsCanFocus(false);
|
101 |
} |
102 |
|
103 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
104 |
if (nodes != null && nodes.size() > 0) { |
105 |
Intent viewIntent = new Intent(this, EditNodeActivity.class); |
106 |
viewIntent.putExtra("node", nodes.get(position));
|
107 |
viewIntent.putExtra("loadBalancer", loadBalancer);
|
108 |
startActivityForResult(viewIntent, EDIT_NODE_CODE); // arbitrary number; never used again
|
109 |
} |
110 |
} |
111 |
|
112 |
// * Adapter/
|
113 |
class NodeAdapter extends ArrayAdapter<Node> { |
114 |
NodeAdapter() { |
115 |
super(EditNodesActivity.this, cellType, nodes);
|
116 |
} |
117 |
|
118 |
public View getView(int position, View convertView, ViewGroup parent) { |
119 |
|
120 |
final Node node = nodes.get(position);
|
121 |
LayoutInflater inflater = getLayoutInflater(); |
122 |
View row = inflater.inflate(cellType, parent, false); |
123 |
|
124 |
TextView ipLabel = (TextView) row.findViewById(R.id.ip_address_text); |
125 |
ipLabel.setText(node.getAddress()); |
126 |
|
127 |
TextView conditionLabel = (TextView) row.findViewById(R.id.condition_text); |
128 |
conditionLabel.setText(node.getCondition()); |
129 |
|
130 |
TextView portLabel = (TextView) row.findViewById(R.id.port_text); |
131 |
portLabel.setText(node.getPort()); |
132 |
|
133 |
if(cellType == R.layout.displayweightednodecell){
|
134 |
TextView weightLabel = (TextView) row.findViewById(R.id.weight_text); |
135 |
weightLabel.setText(node.getWeight()); |
136 |
} |
137 |
|
138 |
return(row);
|
139 |
} |
140 |
} |
141 |
|
142 |
// tasks
|
143 |
private class LoadNodesTask extends AsyncTask<Void, Void, ArrayList<Node>> { |
144 |
|
145 |
private LoadBalancersException exception;
|
146 |
|
147 |
protected void onPreExecute() { |
148 |
/*
|
149 |
* set to null, so if config change occurs
|
150 |
* it will be reloaded in onCreate()
|
151 |
*/
|
152 |
showDialog(); |
153 |
} |
154 |
|
155 |
@Override
|
156 |
protected ArrayList<Node> doInBackground(Void... arg0) { |
157 |
ArrayList<Node> result = null; |
158 |
try {
|
159 |
result = (new NodeManager(getContext())).createList(loadBalancer);
|
160 |
} catch (LoadBalancersException e) {
|
161 |
exception = e; |
162 |
} |
163 |
return result;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
protected void onPostExecute(ArrayList<Node> result) { |
168 |
hideDialog(); |
169 |
if (exception != null) { |
170 |
showAlert("Error", exception.getMessage());
|
171 |
} |
172 |
nodes = new ArrayList<Node>(); |
173 |
for(Node n : result){
|
174 |
nodes.add(n); |
175 |
} |
176 |
displayNodes(); |
177 |
} |
178 |
} |
179 |
|
180 |
/*
|
181 |
* if the node has the same ip as
|
182 |
* a node in the list remove it
|
183 |
*/
|
184 |
private void removeFromList(Node node){ |
185 |
for(int i = 0; i < nodes.size(); i++){ |
186 |
if(nodes.get(i).getAddress().equals(node.getAddress())){
|
187 |
nodes.remove(i); |
188 |
} |
189 |
} |
190 |
displayNodes(); |
191 |
} |
192 |
|
193 |
protected void onActivityResult(int requestCode, int resultCode, Intent data){ |
194 |
//Node(s) was added so refresh the node list
|
195 |
if(requestCode == EDIT_NODE_CODE && resultCode == RESULT_OK){
|
196 |
new LoadNodesTask().execute();
|
197 |
} |
198 |
|
199 |
//Node was removed so take it off the list
|
200 |
if(requestCode == EDIT_NODE_CODE && resultCode == NODE_DELETED_CODE){
|
201 |
Node node = (Node)data.getSerializableExtra("deletedNode");
|
202 |
removeFromList(node); |
203 |
} |
204 |
} |
205 |
|
206 |
} |