Revision c4a37180 src/com/rackspacecloud/android/AddNodesActivity.java

b/src/com/rackspacecloud/android/AddNodesActivity.java
30 30
	private static final int ADD_EXTERNAL_NODE_CODE = 188;
31 31

  
32 32
	//servers are what are displayed in the ListView
33
	private ArrayList<Server> servers;
33
	//(possible and checked nodes)
34
	private ArrayList<Server> possibleNodes;
34 35
	//nodes are the nodes the user has selected
35 36
	private ArrayList<Node> nodes;
37
	//the last position in the listview that was clicked
36 38
	private int lastCheckedPos;
39
	//the location in nodes of the last node that was clicked
40
	private int positionOfNode;
37 41

  
38 42
	@SuppressWarnings("unchecked")
39 43
	@Override
40 44
	public void onCreate(Bundle savedInstanceState) {
41 45
		super.onCreate(savedInstanceState);
42 46
		nodes = (ArrayList<Node>) this.getIntent().getExtras().get("nodes");
47
		possibleNodes = (ArrayList<Server>) this.getIntent().getExtras().get("possibleNodes");
43 48
		setContentView(R.layout.addnodes);
44 49
		restoreState(savedInstanceState);
45 50
	}
......
48 53
	protected void onSaveInstanceState(Bundle outState) {
49 54
		super.onSaveInstanceState(outState);
50 55
		outState.putSerializable("nodes", nodes);
51
		outState.putSerializable("servers", servers);
56
		outState.putSerializable("possibleNodes", possibleNodes);
57
		outState.putInt("lastCheckedPos", lastCheckedPos);
58
		outState.putInt("positionOfNode", positionOfNode);
52 59
	}
53 60

  
54 61
	@SuppressWarnings("unchecked")
......
61 68
				nodes = new ArrayList<Node>();
62 69
			}
63 70
		}
71
		
72
		if (state != null && state.containsKey("lastCheckedPos")){
73
			lastCheckedPos = (Integer) state.getSerializable("lastCheckedPos");
74
		}
75
		
76
		if (state != null && state.containsKey("positionOfNode")){
77
			positionOfNode = (Integer) state.getSerializable("positionOfNode");
78
		}
64 79

  
65
		if (state != null && state.containsKey("servers")) {
66
			servers = (ArrayList<Server>) state.getSerializable("servers");
67
			if (servers.size() == 0) {
80
		if (state != null && state.containsKey("possibleNodes")) {
81
			possibleNodes = (ArrayList<Server>) state.getSerializable("possibleNodes");
82
			if (possibleNodes.size() == 0) {
68 83
				displayNoServersCell();
69 84
			} else {
70 85
				getListView().setDividerHeight(1); // restore divider lines
......
81 96
			public void onClick(View v) {
82 97
				Intent viewIntent = new Intent();
83 98
				viewIntent.putExtra("nodes", nodes);
99
				viewIntent.putExtra("possibleNodes", possibleNodes);
84 100
				setResult(RESULT_OK, viewIntent);
101
				printTheNodes();
85 102
				finish();
86 103
			}
87 104
		});
......
91 108

  
92 109
			@Override
93 110
			public void onClick(View v) {
111
				positionOfNode = -1;
94 112
				Intent viewIntent = new Intent(getContext(), AddExternalNodeActivity.class);
95 113
				//when first creating a load balancer
96 114
				//weighting nodes in not an option
......
107 125
	}
108 126

  
109 127
	protected void onListItemClick(ListView l, View v, int position, long id) {
110
		if (servers != null && servers.size() > 0) {
111
			LinearLayout linear = (LinearLayout) findViewById(R.id.nodes_linear_layout); 
112
			ListView serversList = (ListView) linear.findViewById(android.R.id.list);
113
			View row = serversList.getChildAt(position);
114
			CheckBox checkBox = (CheckBox)row.findViewById(R.id.add_node_checkbox);
115
			if(!checkBox.isChecked()){
116
				//if the checkbox was not previously checked, treat the listItemClick
117
				//the same as checking the checkbox
118
				checkBox.setChecked(!checkBox.isChecked());
119
			} else {
120
				//if the checkbox was already checked when the listItemClick occurred,
121
				//then treat it like an edit
122

  
123
				Server server = servers.get(position);
124

  
125
				//Need to put all the ip's of the server into one
126
				//list so they can all be displayed in one spinner
127
				String[] ipAddresses = getAllIpsOfServer(server);
128

  
129
				Node node = getNodeFromServer(server);
130

  
131
				lastCheckedPos = position;
132
				Intent viewIntent = new Intent(getContext(), AddNodeActivity.class);
133
				viewIntent.putExtra("ipAddresses", ipAddresses);
134
				viewIntent.putExtra("name", server.getName());
135
				if(node != null){
136
					viewIntent.putExtra("node", node);
128
		if (possibleNodes != null && possibleNodes.size() > 0) {
129
			if(!possibleNodes.get(position).getName().equals("External Node")){
130
				LinearLayout linear = (LinearLayout) findViewById(R.id.nodes_linear_layout); 
131
				ListView serversList = (ListView) linear.findViewById(android.R.id.list);
132
				View row = serversList.getChildAt(position);
133
				CheckBox checkBox = (CheckBox)row.findViewById(R.id.add_node_checkbox);
134
				if(!checkBox.isChecked()){
135
					//if the checkbox was not previously checked, treat the listItemClick
136
					//the same as checking the checkbox
137
					checkBox.setChecked(!checkBox.isChecked());
138
				} else {
139
					//if the checkbox was already checked when the listItemClick occurred,
140
					//then treat it like an edit
141

  
142
					Server server = possibleNodes.get(position);
143

  
144
					//Need to put all the ip's of the server into one
145
					//list so they can all be displayed in one spinner
146
					String[] ipAddresses = getAllIpsOfServer(server);
147

  
148
					Node node = getNodeFromServer(server);
149
					
150
					positionOfNode = findNodePosition(node);
151
					lastCheckedPos = position;
152
					
153
					Intent viewIntent = new Intent(getContext(), AddNodeActivity.class);
154
					viewIntent.putExtra("ipAddresses", ipAddresses);
155
					viewIntent.putExtra("name", server.getName());
156
					if(node != null){
157
						viewIntent.putExtra("node", node);
158
					}
159
					//weighted is false, because on initial node add
160
					//weight is not option
161
					viewIntent.putExtra("weighted", false);
162
					startActivityForResult(viewIntent, ADD_NODE_CODE);
137 163
				}
138
				//weighted is false, because on initial node add
139
				//weight is not option
140
				viewIntent.putExtra("weighted", false);
141
				startActivityForResult(viewIntent, ADD_NODE_CODE);
164
			} else {
165
			//When clicked on an external node
166
			Server server = possibleNodes.get(position);
167
			Node node = getNodeFromServer(server);
168
			positionOfNode = findNodePosition(node);
169
			lastCheckedPos = position;
170
			Intent viewIntent = new Intent(getContext(), AddExternalNodeActivity.class);
171
			if(node != null){
172
				viewIntent.putExtra("node", node);
173
			}
174
			//weighted is false, because on initial node add
175
			//weight is not option
176
			viewIntent.putExtra("weighted", false);
177
			startActivityForResult(viewIntent, ADD_EXTERNAL_NODE_CODE);
142 178
			}
143

  
144 179
		}
145 180
	}
181
	
182
	//return the location of node in nodes
183
	//if it is no in there then -1
184
	private int findNodePosition(Node node){
185
		for(int i = 0; i < nodes.size(); i++){
186
			String address = node.getAddress();
187
			if(address.equals(nodes.get(i).getAddress())){
188
				return i;
189
			}
190
		}
191
		return -1;
192
	}
146 193

  
147 194
	private void displayNoServersCell() {
148 195
		String a[] = new String[1];
......
158 205
			servers = new ArrayList<Server>();
159 206
		}
160 207
		String[] serverNames = new String[servers.size()];
161
		this.servers = new ArrayList<Server>();
208
		this.possibleNodes = new ArrayList<Server>();
162 209

  
163 210
		if (servers != null) {
164 211
			for (int i = 0; i < servers.size(); i++) {
165 212
				Server server = servers.get(i);
166
				this.servers.add(i, server);
213
				this.possibleNodes.add(i, server);
167 214
				serverNames[i] = server.getName();
168 215
			}
169 216
		}
......
183 230
	// * Adapter/
184 231
	class ServerAdapter extends ArrayAdapter<Server> {
185 232
		ServerAdapter() {
186
			super(AddNodesActivity.this, R.layout.listservernodecell, servers);
233
			super(AddNodesActivity.this, R.layout.listservernodecell, possibleNodes);
187 234
		}
188 235

  
189 236
		public View getView(int position, View convertView, ViewGroup parent) {
190 237

  
191
			final Server server = servers.get(position);
238
			final Server server = possibleNodes.get(position);
192 239
			LayoutInflater inflater = getLayoutInflater();
193 240
			View row = inflater.inflate(R.layout.listservernodecell, parent, false);
194 241

  
......
202 249
				sublabel.setText(server.getFlavor().getName() + " - " + server.getImage().getName());
203 250
			}
204 251

  
205

  
206 252
			//Need to put all the ip's of the server into one
207 253
			//list so they can all be displayed in one spinner
208 254
			final String[] ipAddresses = getAllIpsOfServer(server);
......
219 265
				@Override
220 266
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
221 267
					if(isChecked){
268
						//if node is being check it won't be in nodes positionOfNode is -1
269
						positionOfNode = -1;
222 270
						lastCheckedPos = pos;
223 271
						Intent viewIntent = new Intent(getContext(), AddNodeActivity.class);
224 272
						viewIntent.putExtra("ipAddresses", ipAddresses);
......
231 279
					else{
232 280
						removeNodeFromList(server);
233 281
						if(server.getName().equals("External Node")){
234
							servers.remove(server);
235
							setServerList(servers);
282
							possibleNodes.remove(server);
283
							setServerList(possibleNodes);
236 284
						}
237 285
					}
238 286
				}
......
269 317
	}
270 318

  
271 319
	private String getNameFromIp(String address){
272
		for(Server s: servers){
320
		for(Server s: possibleNodes){
273 321
			if(serverHasIp(s, address)){
274 322
				return s.getName();
275 323
			}
......
280 328
	//returns true if address is an address of
281 329
	//one of the users cloud servers
282 330
	private boolean isCloudServerIp(String address){
283
		for(Server s : servers){
331
		for(Server s : possibleNodes){
284 332
			if(serverHasIp(s, address)){
285 333
				return true;
286 334
			}
......
380 428
		}
381 429
	}
382 430

  
383
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
431
	//removes a node with ip of address from nodes
432
	//if one doesnt exists doesn nothing
433
	/*private void removeNodeWithIp(String address){
434
		for(int i = 0; i < nodes.size(); i++){
435
			Node n = nodes.get(i);
436
			if(n.getAddress().equals(address)){
437
				nodes.remove(i);
438
				break;
439
			}
440
		}
441
	}*/
442

  
443
	protected void onActivityResult(int requestCode, int resultCode, Intent data){	
384 444
		int pos = lastCheckedPos;
385 445
		if(requestCode == ADD_NODE_CODE && resultCode == RESULT_OK){
386 446
			//data will be null is user back out on edit
......
388 448
			//if new node added data won't be null
389 449
			//so create the new node and add it to the list
390 450
			if(data != null){
451
				//will remove the node if it's already in the list 
452
				//so we can update it
453
				//removeNodeWithIp(data.getStringExtra("nodeIp"));
454
				if(positionOfNode >= 0){
455
					nodes.remove(positionOfNode);
456
				}
457
				
391 458
				Node node = new Node();
392 459
				node.setAddress(data.getStringExtra("nodeIp"));
393 460
				node.setCondition(data.getStringExtra("nodeCondition"));
394
				node.setName(servers.get(pos).getName());
461
				node.setName(possibleNodes.get(pos).getName());
395 462
				node.setPort(data.getStringExtra("nodePort"));
396 463
				node.setWeight(data.getStringExtra("nodeWeight"));
397 464
				nodes.add(node);
......
401 468
			//uncheck the node at lastCheckedPos
402 469
			LinearLayout linear = (LinearLayout) findViewById(R.id.nodes_linear_layout); 
403 470
			ListView serversList = (ListView) linear.findViewById(android.R.id.list);
471
			Log.d("info", "number of items in serverList " + serversList.getChildCount());
472
			Log.d("info", "lastCheckedPos is " + Integer.toString(pos));
404 473
			View row = serversList.getChildAt(pos);
474
			Log.d("info", ("is row null? " + Boolean.toString(row == null)));
405 475
			CheckBox checkBox = (CheckBox)row.findViewById(R.id.add_node_checkbox);
406 476
			checkBox.setChecked(false);
407 477
		}
......
419 489
			 * so they can select it from there
420 490
			 */	
421 491
			if(!isCloudServerIp(node.getAddress())){
492
				
493
				if(positionOfNode >= 0){
494
					nodes.remove(positionOfNode);
495
				}
496
				
422 497
				nodes.add(node);
423 498
				//Add it to server list so it display in the listview
424 499
				Server server = new Server();
425 500
				server.setName("External Node");
426 501
				String[] ip = {data.getStringExtra("nodeIp")};
427 502
				server.setPrivateIpAddresses(ip);
428
				servers.add(server);
429
				setServerList(servers);
503
				possibleNodes.add(server);
504
				setServerList(possibleNodes);
430 505
			} else {
431 506
				showAlert("Error", "This IP belongs to a cloud server: \"" + getNameFromIp(node.getAddress()) 
432 507
						+ "\", please select it from the list.");
433 508
			}
434 509
		}
435
		Log.d("info", "number of nodes is " + nodes.size());
510
		printTheNodes();
436 511
	}
437 512

  
513
	private void printTheNodes(){
514
		for(Node n: nodes){
515
			Log.d("info", "address: " + n.getAddress() + " Port: " + n.getPort() + " Cond: " + n.getCondition());
516
		}
517
		Log.d("info", " SPACE ");
518
	}
438 519

  
439 520
}

Also available in: Unified diff