Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / AddExternalNodeActivity.java @ 7dbfc514

History | View | Annotate | Download (5.3 kB)

1
package com.rackspace.cloud.android;
2

    
3
import java.util.regex.Matcher;
4
import java.util.regex.Pattern;
5

    
6
import android.content.Intent;
7
import android.os.Bundle;
8
import android.util.Log;
9
import android.view.View;
10
import android.view.View.OnClickListener;
11
import android.view.Window;
12
import android.widget.AdapterView;
13
import android.widget.AdapterView.OnItemSelectedListener;
14
import android.widget.ArrayAdapter;
15
import android.widget.Button;
16
import android.widget.EditText;
17
import android.widget.Spinner;
18
import android.widget.TextView;
19

    
20
import com.rackspace.cloud.android.R;
21
import com.rackspace.cloud.loadbalancer.api.client.Node;
22

    
23
public class AddExternalNodeActivity extends CloudActivity {
24

    
25
        private final String[] CONDITIONS = {"Enabled", "Disabled", "Draining"};
26
        private String selectedPort;
27
        private String selectedIp;
28
        private String selectedWeight;
29
        private boolean weighted;
30
        private String selectedCondition;
31
        private Spinner conditionSpinner;
32
        private EditText ipAddress;
33
        private EditText weightText;
34
        private Node node;
35
        
36
        public void onCreate(Bundle savedInstanceState) {
37
                super.onCreate(savedInstanceState);
38
                requestWindowFeature(Window.FEATURE_NO_TITLE);
39
                setContentView(R.layout.addexternalnode);
40
                weighted = (Boolean) this.getIntent().getExtras().get("weighted");
41
                node = (Node) this.getIntent().getExtras().get("node");
42
                selectedPort = (String) this.getIntent().getExtras().get("loadBalancerPort");
43
                restoreState(savedInstanceState);
44
        } 
45

    
46
        protected void restoreState(Bundle state) {
47
                super.restoreState(state);
48
                setupInputs();
49
        }
50

    
51
        private void setupInputs(){
52
                
53
                ipAddress = (EditText) findViewById(R.id.ip_address);
54
                
55
                weightText = (EditText) findViewById(R.id.node_weight_text);
56

    
57
                //if algorithm is not weighted then then node's weight will be null
58
                if(!weighted){
59
                        TextView weightLabel = (TextView) findViewById(R.id.node_weight_label);
60
                        weightLabel.setVisibility(View.GONE);
61
                        weightText.setVisibility(View.GONE);
62
                }
63

    
64
                loadConditionSpinner();
65
                setUpButton();
66

    
67
                if(node != null){
68
                        ipAddress.setText(node.getAddress());
69
                        weightText.setText(node.getWeight());
70
                        ((EditText)findViewById(R.id.node_port_text)).setText(node.getPort());
71
                        conditionSpinner.setSelection(getLocation(CONDITIONS, node.getCondition()));
72
                } else {
73
                        Log.d("info", "node was null");
74
                        ((EditText)findViewById(R.id.node_port_text)).setText(selectedPort);
75
                }
76
        }
77
        
78
        private int getLocation(Object[] objects, Object object){
79
                for(int i = 0; i < objects.length; i++){
80
                        if(object.equals(objects[i])){
81
                                return i;
82
                        }
83
                }
84
                return 0;
85
        }
86

    
87
        private void setUpButton(){
88
                Button submit = (Button) findViewById(R.id.add_node_button);
89
                submit.setOnClickListener(new OnClickListener() {
90

    
91
                        @Override
92
                        public void onClick(View v) {
93
                                selectedIp = ipAddress.getText().toString().trim();
94
                                selectedPort = ((EditText)findViewById(R.id.node_port_text)).getText().toString().trim();
95
                                selectedWeight = weightText.getText().toString().trim();
96
                                if(!validPort(selectedPort)){
97
                                        showAlert("Error", "Must have a protocol port number that is between 1 and 65535.");
98
                                } else if(!(weightText.getVisibility() == View.GONE || (weightText.getVisibility() != View.GONE && validWeight(selectedWeight)))){
99
                                        showAlert("Error", "Weight must be between 1 and 100.");
100
                                } else if(selectedIp.equals("")){
101
                                        //TODO use regex to validate the ip for IPV4 and IPV6
102
                                        showAlert("Error", "Enter an IP Address");
103
                                } else if(!validIp(selectedIp)) {
104
                                        showAlert("Error", "Enter a valid IP Address");
105
                                } else {
106
                                        Intent data = new Intent();
107
                                        data.putExtra("nodeIp", selectedIp);
108
                                        data.putExtra("nodePort", selectedPort);
109
                                        data.putExtra("nodeCondition", selectedCondition);
110
                                        data.putExtra("nodeWeight", selectedWeight);
111
                                        setResult(RESULT_OK, data);
112
                                        finish();
113
                                }
114

    
115
                        }
116
                });
117
        }
118

    
119
        private void loadConditionSpinner(){
120
                conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner);
121

    
122
                ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS);
123
                conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
124
                conditionSpinner.setAdapter(conditionAdapter);
125

    
126
                conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
127

    
128
                        @Override
129
                        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
130
                                selectedCondition = CONDITIONS[pos];        
131
                        }
132

    
133
                        @Override
134
                        public void onNothingSelected(AdapterView<?> arg0) {
135

    
136
                        }
137

    
138
                });
139
        }
140
        
141
        //basic ip validation just checks that the string
142
        //is only composed of letters, numbers, ., :
143
        private static boolean validIp(String ip){
144
                //Enter regex
145
                if(ip != null){
146
                        Pattern pattern = Pattern.compile("[a-zA-Z0-9.:]+");
147
                        Matcher match = pattern.matcher(ip);
148
                        return match.matches();
149
                } else {
150
                        return false;
151
                }
152
        }
153

    
154
        private boolean validPort(String port){
155
                boolean result;
156
                try{
157
                        result = !port.equals("") && Integer.valueOf(port) > 0 && Integer.valueOf(port) < 65536;
158
                } catch (NumberFormatException e) {
159
                    result = false;
160
            }
161
            return result;
162
        }
163

    
164
        private Boolean validWeight(String weight){
165
                if(weight.equals("")){
166
                        return false;
167
                }
168
                else{
169
                        int w;
170
                        try{
171
                                w = Integer.valueOf(weight);
172
                        } catch (NumberFormatException e){
173
                                return false;
174
                        }
175
                        return w >= 1 && w <= 100 ;
176
                }
177
        }
178

    
179
        public void onBackPressed(){
180
                setResult(RESULT_CANCELED);
181
                finish();
182
        }
183

    
184
}
185