Revision b722cab3 src/com/rackspacecloud/android/EditNodesActivity.java

b/src/com/rackspacecloud/android/EditNodesActivity.java
3 3
import java.util.ArrayList;
4 4

  
5 5
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
6
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
7 6
import com.rackspace.cloud.loadbalancer.api.client.Node;
7
import com.rackspace.cloud.loadbalancer.api.client.NodeManager;
8 8
import com.rackspace.cloud.loadbalancers.api.client.http.LoadBalancersException;
9
import com.rackspace.cloud.servers.api.client.Server;
10 9

  
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 10
import android.content.Intent;
17 11
import android.os.AsyncTask;
18 12
import android.os.Bundle;
19 13
import android.util.Log;
20 14
import android.view.LayoutInflater;
21
import android.view.Menu;
22
import android.view.MenuInflater;
23
import android.view.MenuItem;
24 15
import android.view.View;
25 16
import android.view.View.OnClickListener;
26 17
import android.view.ViewGroup;
27
import android.view.WindowManager;
28
import android.view.ViewGroup.LayoutParams;
29 18
import android.widget.ArrayAdapter;
30 19
import android.widget.Button;
31 20
import android.widget.ListView;
32
import android.widget.ProgressBar;
33 21
import android.widget.TextView;
34 22

  
35
public class EditNodesActivity extends ListActivity {
23
public class EditNodesActivity extends CloudListActivity {
36 24

  
37 25
	private static final int EDIT_NODE_CODE = 299;
38 26
	private static final int NODE_DELETED_CODE = 389;
39
	private static final int ADD_MORE_NODES_CODE = 165;
27
	//private static final int ADD_MORE_NODES_CODE = 165;
40 28

  
41 29
	private ArrayList<Node> nodes;
42 30
	private LoadBalancer loadBalancer;
43
	ProgressDialog pDialog;
44
	
31
	private int cellType;
32

  
45 33
	@SuppressWarnings("unchecked")
46 34
	@Override
47 35
	public void onCreate(Bundle savedInstanceState) {
......
60 48
	}
61 49

  
62 50
	@SuppressWarnings("unchecked")
63
	private void restoreState(Bundle state) {
64
		
51
	protected void restoreState(Bundle state) {
52
		super.restoreState(state);
53

  
65 54
		if (state != null && state.containsKey("nodes")){
66 55
			nodes = (ArrayList<Node>) state.getSerializable("nodes");
67 56
			displayNodes();
......
75 64
				Intent viewIntent = new Intent(getApplicationContext(), AddMoreNodesActivity.class);
76 65
				viewIntent.putExtra("nodes", nodes);
77 66
				viewIntent.putExtra("loadBalancer", loadBalancer);
78
				startActivityForResult(viewIntent, ADD_MORE_NODES_CODE);
67
				startActivityForResult(viewIntent, EDIT_NODE_CODE);
79 68
			}
80 69
		});
70

  
71
		if(loadBalancer.getAlgorithm().contains("WEIGHTED")){
72
			cellType = R.layout.displayweightednodecell;
73
		}
74
		else{
75
			cellType = R.layout.displaynodecell;
76
		}
81 77
	}
82
	
78

  
83 79
	private void displayNodes(){
84 80
		if (nodes.size() == 0) {
85 81
			displayNoNodesCell();
......
103 99
		getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
104 100
		getListView().setItemsCanFocus(false);
105 101
	}
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 102

  
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();
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
		}
136 110
	}
137 111

  
138 112
	// * Adapter/
139 113
	class NodeAdapter extends ArrayAdapter<Node> {
140 114
		NodeAdapter() {
141
			super(EditNodesActivity.this, R.layout.displaynodecell, nodes);
115
			super(EditNodesActivity.this, cellType, nodes);
142 116
		}
143 117

  
144 118
		public View getView(int position, View convertView, ViewGroup parent) {
145 119

  
146 120
			final Node node = nodes.get(position);
147 121
			LayoutInflater inflater = getLayoutInflater();
148
			View row = inflater.inflate(R.layout.displaynodecell, parent, false);
122
			View row = inflater.inflate(cellType, parent, false);
149 123

  
150 124
			TextView ipLabel = (TextView) row.findViewById(R.id.ip_address_text);
151 125
			ipLabel.setText(node.getAddress());
152 126

  
153 127
			TextView conditionLabel = (TextView) row.findViewById(R.id.condition_text);
154 128
			conditionLabel.setText(node.getCondition());
155
			
129

  
156 130
			TextView portLabel = (TextView) row.findViewById(R.id.port_text);
157 131
			portLabel.setText(node.getPort());
158
			
159
			/*
160
			ImageView icon = (ImageView) row.findViewById(R.id.icon);
161
			icon.setImageResource(server.getImage().iconResourceId());
162
			*/
163
			
132

  
133
			if(cellType == R.layout.displayweightednodecell){
134
				TextView weightLabel = (TextView) row.findViewById(R.id.weight_text);
135
				weightLabel.setText(node.getWeight());
136
			}
137

  
164 138
			return(row);
165 139
		}
166 140
	}
167
	
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

  
168 180
	/*
169 181
	 * if the node has the same ip as
170 182
	 * a node in the list remove it
......
179 191
	}
180 192

  
181 193
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
182
		 if(requestCode == EDIT_NODE_CODE && resultCode == NODE_DELETED_CODE){
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){
183 201
			Node node = (Node)data.getSerializableExtra("deletedNode");
184 202
			removeFromList(node);
185
		 }
203
		}
186 204
	}
187 205

  
188 206
}

Also available in: Unified diff