root / src / com / rackspacecloud / android / EditNodesActivity.java @ 4f9d1a69
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.LoadBalancerManager; |
7 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
8 |
import com.rackspace.cloud.loadbalancers.api.client.http.LoadBalancersException; |
9 |
import com.rackspace.cloud.servers.api.client.Server; |
10 |
|
11 |
import android.app.AlertDialog; |
12 |
import android.app.ListActivity; |
13 |
import android.app.ProgressDialog; |
14 |
import android.content.Context; |
15 |
import android.content.DialogInterface; |
16 |
import android.content.Intent; |
17 |
import android.os.AsyncTask; |
18 |
import android.os.Bundle; |
19 |
import android.util.Log; |
20 |
import android.view.LayoutInflater; |
21 |
import android.view.Menu; |
22 |
import android.view.MenuInflater; |
23 |
import android.view.MenuItem; |
24 |
import android.view.View; |
25 |
import android.view.View.OnClickListener; |
26 |
import android.view.ViewGroup; |
27 |
import android.view.WindowManager; |
28 |
import android.view.ViewGroup.LayoutParams; |
29 |
import android.widget.ArrayAdapter; |
30 |
import android.widget.Button; |
31 |
import android.widget.ListView; |
32 |
import android.widget.ProgressBar; |
33 |
import android.widget.TextView; |
34 |
|
35 |
public class EditNodesActivity extends ListActivity { |
36 |
|
37 |
private static final int EDIT_NODE_CODE = 299; |
38 |
private static final int NODE_DELETED_CODE = 389; |
39 |
private static final int ADD_MORE_NODES_CODE = 165; |
40 |
|
41 |
private ArrayList<Node> nodes; |
42 |
private LoadBalancer loadBalancer;
|
43 |
ProgressDialog pDialog; |
44 |
|
45 |
@SuppressWarnings("unchecked") |
46 |
@Override
|
47 |
public void onCreate(Bundle savedInstanceState) { |
48 |
super.onCreate(savedInstanceState);
|
49 |
setContentView(R.layout.addnodes); |
50 |
nodes = (ArrayList<Node>) this.getIntent().getExtras().get("nodes"); |
51 |
loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer"); |
52 |
displayNodes(); |
53 |
restoreState(savedInstanceState); |
54 |
} |
55 |
|
56 |
@Override
|
57 |
protected void onSaveInstanceState(Bundle outState) { |
58 |
super.onSaveInstanceState(outState);
|
59 |
outState.putSerializable("nodes", nodes);
|
60 |
} |
61 |
|
62 |
@SuppressWarnings("unchecked") |
63 |
private void restoreState(Bundle state) { |
64 |
|
65 |
if (state != null && state.containsKey("nodes")){ |
66 |
nodes = (ArrayList<Node>) state.getSerializable("nodes"); |
67 |
displayNodes(); |
68 |
} |
69 |
|
70 |
Button submitNodes = (Button) findViewById(R.id.submit_nodes_button); |
71 |
submitNodes.setOnClickListener(new OnClickListener() {
|
72 |
|
73 |
@Override
|
74 |
public void onClick(View v) { |
75 |
Intent viewIntent = new Intent(getApplicationContext(), AddMoreNodesActivity.class);
|
76 |
viewIntent.putExtra("nodes", nodes);
|
77 |
viewIntent.putExtra("loadBalancer", loadBalancer);
|
78 |
startActivityForResult(viewIntent, ADD_MORE_NODES_CODE); |
79 |
} |
80 |
}); |
81 |
} |
82 |
|
83 |
private void displayNodes(){ |
84 |
if (nodes.size() == 0) { |
85 |
displayNoNodesCell(); |
86 |
} else {
|
87 |
getListView().setDividerHeight(1); // restore divider lines |
88 |
setListAdapter(new NodeAdapter());
|
89 |
} |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public void onBackPressed(){ |
94 |
setResult(RESULT_CANCELED); |
95 |
finish(); |
96 |
} |
97 |
|
98 |
private void displayNoNodesCell() { |
99 |
String a[] = new String[1]; |
100 |
a[0] = "No Nodes"; |
101 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noserverscell, R.id.no_servers_label, a)); |
102 |
getListView().setTextFilterEnabled(true);
|
103 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
104 |
getListView().setItemsCanFocus(false);
|
105 |
} |
106 |
|
107 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
108 |
if (nodes != null && nodes.size() > 0) { |
109 |
Intent viewIntent = new Intent(this, EditNodeActivity.class); |
110 |
viewIntent.putExtra("node", nodes.get(position));
|
111 |
viewIntent.putExtra("loadBalancer", loadBalancer);
|
112 |
startActivityForResult(viewIntent, EDIT_NODE_CODE); // arbitrary number; never used again
|
113 |
} |
114 |
} |
115 |
|
116 |
protected void showDialog() { |
117 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
118 |
// // Set blur to background
|
119 |
WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes(); |
120 |
lp.dimAmount = 0.0f;
|
121 |
pDialog.getWindow().setAttributes(lp); |
122 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
123 |
pDialog.show(); |
124 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
125 |
} |
126 |
|
127 |
private void showAlert(String title, String message) { |
128 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
129 |
alert.setTitle(title); |
130 |
alert.setMessage(message); |
131 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
132 |
public void onClick(DialogInterface dialog, int which) { |
133 |
return;
|
134 |
} }); |
135 |
alert.show(); |
136 |
} |
137 |
|
138 |
// * Adapter/
|
139 |
class NodeAdapter extends ArrayAdapter<Node> { |
140 |
NodeAdapter() { |
141 |
super(EditNodesActivity.this, R.layout.displaynodecell, nodes);
|
142 |
} |
143 |
|
144 |
public View getView(int position, View convertView, ViewGroup parent) { |
145 |
|
146 |
final Node node = nodes.get(position);
|
147 |
LayoutInflater inflater = getLayoutInflater(); |
148 |
View row = inflater.inflate(R.layout.displaynodecell, parent, false); |
149 |
|
150 |
TextView ipLabel = (TextView) row.findViewById(R.id.ip_address_text); |
151 |
ipLabel.setText(node.getAddress()); |
152 |
|
153 |
TextView conditionLabel = (TextView) row.findViewById(R.id.condition_text); |
154 |
conditionLabel.setText(node.getCondition()); |
155 |
|
156 |
TextView portLabel = (TextView) row.findViewById(R.id.port_text); |
157 |
portLabel.setText(node.getPort()); |
158 |
|
159 |
/*
|
160 |
ImageView icon = (ImageView) row.findViewById(R.id.icon);
|
161 |
icon.setImageResource(server.getImage().iconResourceId());
|
162 |
*/
|
163 |
|
164 |
return(row);
|
165 |
} |
166 |
} |
167 |
|
168 |
/*
|
169 |
* if the node has the same ip as
|
170 |
* a node in the list remove it
|
171 |
*/
|
172 |
private void removeFromList(Node node){ |
173 |
for(int i = 0; i < nodes.size(); i++){ |
174 |
if(nodes.get(i).getAddress().equals(node.getAddress())){
|
175 |
nodes.remove(i); |
176 |
} |
177 |
} |
178 |
displayNodes(); |
179 |
} |
180 |
|
181 |
protected void onActivityResult(int requestCode, int resultCode, Intent data){ |
182 |
if(requestCode == EDIT_NODE_CODE && resultCode == NODE_DELETED_CODE){
|
183 |
Node node = (Node)data.getSerializableExtra("deletedNode");
|
184 |
removeFromList(node); |
185 |
} |
186 |
} |
187 |
|
188 |
} |