Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / AddNodeActivity.java @ b722cab3

History | View | Annotate | Download (4.6 kB)

1
package com.rackspacecloud.android;
2

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

    
17
public class AddNodeActivity extends CloudActivity{
18

    
19
        private final String[] CONDITIONS = {"Enabled", "Disabled", "Draining"};
20
        private String[] ipAddresses;
21
        private String name;
22
        private String selectedPort;
23
        private String selectedIp;
24
        private String selectedWeight;
25
        private boolean weighted;
26
        private String selectedCondition;
27
        private Spinner conditionSpinner;
28
        private Spinner ipAddressSpinner;
29
        private EditText weightText;
30
        
31
        public void onCreate(Bundle savedInstanceState) {
32
                super.onCreate(savedInstanceState);
33
                requestWindowFeature(Window.FEATURE_NO_TITLE);
34
                setContentView(R.layout.addnode);
35
                ipAddresses = (String[]) this.getIntent().getExtras().get("ipAddresses");
36
                name = (String) this.getIntent().getExtras().get("name");
37
                weighted = (Boolean) this.getIntent().getExtras().get("weighted");
38
                restoreState(savedInstanceState);
39
        } 
40
        
41
        protected void restoreState(Bundle state) {
42
                super.restoreState(state);
43
                setupInputs();
44
        }
45
        
46
        private void setupInputs(){
47
                
48
                ((TextView)findViewById(R.id.node_name)).setText(name);
49
                
50
                weightText = (EditText) findViewById(R.id.node_weight_text);
51
                
52
                //if algorithm is not weighted then then node's weight will be null
53
                if(!weighted){
54
                        TextView weightLabel = (TextView) findViewById(R.id.node_weight_label);
55
                        weightLabel.setVisibility(View.GONE);
56
                        weightText.setVisibility(View.GONE);
57
                }
58
                
59
                loadConditionSpinner();
60
                loadIpSpinner();
61
                setUpButton();
62
                
63
        }
64
        
65
        private void setUpButton(){
66
                Button submit = (Button) findViewById(R.id.add_node_button);
67
                submit.setOnClickListener(new OnClickListener() {
68
                        
69
                        @Override
70
                        public void onClick(View v) {
71
                                selectedPort = ((EditText)findViewById(R.id.node_port_text)).getText().toString();
72
                                selectedWeight = weightText.getText().toString();
73
                                if(!validPort()){
74
                                        showAlert("Error", "Must have a protocol port number that is between 1 and 65535.");
75
                                } else if(!(weightText.getVisibility() == View.GONE || (weightText.getVisibility() != View.GONE && validWeight(selectedWeight)))){
76
                                        showAlert("Error", "Weight must be between 1 and 100.");
77
                                }
78
                                
79
                                else{
80
                                        Intent data = new Intent();
81
                                        data.putExtra("nodeIp", selectedIp);
82
                                        data.putExtra("nodePort", selectedPort);
83
                                        data.putExtra("nodeCondition", selectedCondition);
84
                                        Log.d("info", "saving the weight as " + selectedWeight);
85
                                        data.putExtra("nodeWeight", selectedWeight);
86
                                        setResult(RESULT_OK, data);
87
                                        finish();
88
                                }
89
                                
90
                        }
91
                });
92
        }
93
        
94
        public void onBackPressed(){
95
                setResult(RESULT_CANCELED);
96
                finish();
97
        }
98
        
99
        private void loadIpSpinner(){
100
                ipAddressSpinner = (Spinner) findViewById(R.id.node_ip_spinner);
101
                ArrayAdapter<String> ipAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ipAddresses);
102
                ipAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
103
                ipAddressSpinner.setAdapter(ipAdapter);
104
                
105
                ipAddressSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
106

    
107
                        @Override
108
                        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
109
                                selectedIp = ipAddresses[pos];        
110
                        }
111

    
112
                        @Override
113
                        public void onNothingSelected(AdapterView<?> arg0) {
114
                                                        
115
                        }
116
                        
117
                });
118
        }
119
        
120
        private void loadConditionSpinner(){
121
                conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner);
122
                
123
                ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS);
124
                conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
125
                conditionSpinner.setAdapter(conditionAdapter);
126
                
127
                conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
128

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

    
134
                        @Override
135
                        public void onNothingSelected(AdapterView<?> arg0) {
136
                                                        
137
                        }
138
                        
139
                });
140
        }
141
        
142
        private boolean validPort(){
143
                return !selectedPort.equals("") && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536;
144
        }
145
        
146
        private Boolean validWeight(String weight){
147
                if(weight.equals("")){
148
                        return false;
149
                }
150
                else{
151
                        int w = Integer.valueOf(weight);
152
                        return w >= 1 && w <= 100 ;
153
                }
154
        }
155

    
156
}