Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / EditNodesActivity.java @ 48601850

History | View | Annotate | Download (5.8 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.util.ArrayList;
4

    
5
import android.content.Intent;
6
import android.os.AsyncTask;
7
import android.os.Bundle;
8
import android.view.LayoutInflater;
9
import android.view.View;
10
import android.view.View.OnClickListener;
11
import android.view.ViewGroup;
12
import android.widget.ArrayAdapter;
13
import android.widget.Button;
14
import android.widget.ListView;
15
import android.widget.TextView;
16

    
17
import com.rackspace.cloud.android.R;
18
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
19
import com.rackspace.cloud.loadbalancer.api.client.Node;
20
import com.rackspace.cloud.loadbalancer.api.client.NodeManager;
21
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException;
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
                trackPageView(GoogleAnalytics.PAGE_LB_NODES);
38
                setContentView(R.layout.editnodes);
39
                nodes = (ArrayList<Node>) this.getIntent().getExtras().get("nodes");
40
                loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
41
                displayNodes();
42
                restoreState(savedInstanceState);
43
        }
44

    
45
        @Override
46
        protected void onSaveInstanceState(Bundle outState) {
47
                super.onSaveInstanceState(outState);
48
                outState.putSerializable("nodes", nodes);
49
        }
50

    
51
        @SuppressWarnings("unchecked")
52
        protected void restoreState(Bundle state) {
53
                super.restoreState(state);
54

    
55
                if (state != null && state.containsKey("nodes")){
56
                        nodes = (ArrayList<Node>) state.getSerializable("nodes");
57
                        displayNodes();
58
                }
59

    
60
                Button submitNodes = (Button) findViewById(R.id.submit_nodes_button);
61
                submitNodes.setOnClickListener(new OnClickListener() {
62

    
63
                        @Override
64
                        public void onClick(View v) {
65
                                Intent viewIntent = new Intent(getApplicationContext(), AddMoreNodesActivity.class);
66
                                viewIntent.putExtra("nodes", nodes);
67
                                viewIntent.putExtra("loadBalancer", loadBalancer);
68
                                startActivityForResult(viewIntent, EDIT_NODE_CODE);
69
                        }
70
                });
71

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

    
80
        private void displayNodes(){
81
                if (nodes.size() == 0) {
82
                        displayNoNodesCell();
83
                } else {
84
                        getListView().setDividerHeight(1); // restore divider lines
85
                        setListAdapter(new NodeAdapter());
86
                }
87
        }
88

    
89
        @Override
90
        public void onBackPressed(){
91
                setResult(RESULT_CANCELED);
92
                finish();
93
        }
94

    
95
        private void displayNoNodesCell() {
96
                String a[] = new String[1];
97
                a[0] = "No Nodes";
98
                setListAdapter(new ArrayAdapter<String>(this, R.layout.noserverscell, R.id.no_servers_label, a));
99
                getListView().setTextFilterEnabled(true);
100
                getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
101
                getListView().setItemsCanFocus(false);
102
        }
103

    
104
        protected void onListItemClick(ListView l, View v, int position, long id) {
105
                if (nodes != null && nodes.size() > 0) {
106
                        Intent viewIntent = new Intent(this, EditNodeActivity.class);
107
                        viewIntent.putExtra("node", nodes.get(position));
108
                        viewIntent.putExtra("loadBalancer", loadBalancer);
109
                        startActivityForResult(viewIntent, EDIT_NODE_CODE); // arbitrary number; never used again
110
                }
111
        }
112

    
113
        // * Adapter/
114
        class NodeAdapter extends ArrayAdapter<Node> {
115
                NodeAdapter() {
116
                        super(EditNodesActivity.this, cellType, nodes);
117
                }
118

    
119
                public View getView(int position, View convertView, ViewGroup parent) {
120

    
121
                        final Node node = nodes.get(position);
122
                        LayoutInflater inflater = getLayoutInflater();
123
                        View row = inflater.inflate(cellType, parent, false);
124

    
125
                        TextView ipLabel = (TextView) row.findViewById(R.id.ip_address_text);
126
                        ipLabel.setText(node.getAddress());
127

    
128
                        TextView conditionLabel = (TextView) row.findViewById(R.id.condition_text);
129
                        conditionLabel.setText(node.getCondition());
130

    
131
                        TextView portLabel = (TextView) row.findViewById(R.id.port_text);
132
                        portLabel.setText(node.getPort());
133

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

    
139
                        return(row);
140
                }
141
        }
142

    
143
        // tasks
144
        private class LoadNodesTask extends AsyncTask<Void, Void, ArrayList<Node>> {
145

    
146
                private LoadBalancersException exception;
147

    
148
                protected void onPreExecute() {
149
                        /*
150
                         * set to null, so if config change occurs
151
                         * it will be reloaded in onCreate()
152
                         */
153
                        showDialog();
154
                }
155

    
156
                @Override
157
                protected ArrayList<Node> doInBackground(Void... arg0) {
158
                        ArrayList<Node> result = null;
159
                        try {
160
                                result = (new NodeManager(getContext())).createList(loadBalancer);
161
                        } catch (LoadBalancersException e) {
162
                                exception = e;
163
                        }
164
                        return result;
165
                }
166

    
167
                @Override
168
                protected void onPostExecute(ArrayList<Node> result) {
169
                        hideDialog();
170
                        if (exception != null) {
171
                                showAlert("Error", exception.getMessage());
172
                        }
173
                        nodes = new ArrayList<Node>();
174
                        for(Node n : result){
175
                                nodes.add(n);
176
                        }
177
                        displayNodes();
178
                }
179
        } 
180

    
181
        /*
182
         * if the node has the same ip as
183
         * a node in the list remove it
184
         */
185
        private void removeFromList(Node node){
186
                for(int i = 0; i < nodes.size(); i++){
187
                        if(nodes.get(i).getAddress().equals(node.getAddress())){
188
                                nodes.remove(i);
189
                        }
190
                }
191
                displayNodes();
192
        }
193

    
194
        protected void onActivityResult(int requestCode, int resultCode, Intent data){
195
                //Node(s) was added so refresh the node list
196
                if(requestCode == EDIT_NODE_CODE && resultCode == RESULT_OK){
197
                        new LoadNodesTask().execute();
198
                }
199

    
200
                //Node was removed so take it off the list
201
                if(requestCode == EDIT_NODE_CODE && resultCode == NODE_DELETED_CODE){
202
                        Node node = (Node)data.getSerializableExtra("deletedNode");
203
                        removeFromList(node);
204
                }
205
        }
206

    
207
}