1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2011 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.android.view.std; |
24 |
|
25 |
import org.jsampler.CC; |
26 |
import org.jsampler.Server; |
27 |
import org.jsampler.android.R; |
28 |
|
29 |
import android.app.Activity; |
30 |
import android.os.Bundle; |
31 |
import android.text.Editable; |
32 |
import android.text.TextWatcher; |
33 |
import android.widget.Button; |
34 |
import android.widget.EditText; |
35 |
|
36 |
public class AddBackendDlg extends OkCancelDialog { |
37 |
public AddBackendDlg(Activity context) { |
38 |
super(context, R.string.add_backend_title, R.layout.add_backend); |
39 |
} |
40 |
|
41 |
protected void |
42 |
onCreate(Bundle savedInstanceState) { |
43 |
super.onCreate(savedInstanceState); |
44 |
installListeners(); |
45 |
updateState(); |
46 |
} |
47 |
|
48 |
private void |
49 |
installListeners() { |
50 |
EditText et = (EditText)findViewById(R.id.add_backend_et_name); |
51 |
et.addTextChangedListener(new TextAdapter() { |
52 |
public void |
53 |
afterTextChanged(Editable s) { updateState(); } |
54 |
}); |
55 |
|
56 |
et = (EditText)findViewById(R.id.add_backend_et_addr); |
57 |
et.addTextChangedListener(new TextAdapter() { |
58 |
public void |
59 |
afterTextChanged(Editable s) { updateState(); } |
60 |
}); |
61 |
|
62 |
et = (EditText)findViewById(R.id.add_backend_et_port); |
63 |
et.addTextChangedListener(new TextAdapter() { |
64 |
public void |
65 |
afterTextChanged(Editable s) { updateState(); } |
66 |
}); |
67 |
} |
68 |
|
69 |
private void |
70 |
updateState() { |
71 |
boolean b = true; |
72 |
int name = ((EditText)findViewById(R.id.add_backend_et_name)).getText().length(); |
73 |
int addr = ((EditText)findViewById(R.id.add_backend_et_addr)).getText().length(); |
74 |
String p = ((EditText)findViewById(R.id.add_backend_et_port)).getText().toString(); |
75 |
|
76 |
if(name == 0) b = false; |
77 |
if(addr == 0) b = false; |
78 |
|
79 |
if(p.length() == 0) b = false; |
80 |
else { |
81 |
int port = Integer.parseInt(p); |
82 |
if (port < 1 || port > 0xffff) b = false; |
83 |
} |
84 |
|
85 |
((Button)findViewById(R.id.btn_ok)).setEnabled(b); |
86 |
} |
87 |
|
88 |
protected void onOk() { |
89 |
if(!((Button)findViewById(R.id.btn_ok)).isEnabled()) return; |
90 |
|
91 |
String name = ((EditText)findViewById(R.id.add_backend_et_name)).getText().toString(); |
92 |
String desc = ((EditText)findViewById(R.id.add_backend_et_desc)).getText().toString(); |
93 |
String addr = ((EditText)findViewById(R.id.add_backend_et_addr)).getText().toString(); |
94 |
String port = ((EditText)findViewById(R.id.add_backend_et_port)).getText().toString(); |
95 |
|
96 |
Server server = new Server(); |
97 |
server.setName(name); |
98 |
server.setDescription(desc); |
99 |
server.setAddress(addr); |
100 |
server.setPort(Integer.parseInt(port)); |
101 |
|
102 |
CC.getServerList().addServer(server); |
103 |
|
104 |
dismiss(); |
105 |
} |
106 |
|
107 |
protected void |
108 |
onCancel() { } |
109 |
|
110 |
private static abstract class TextAdapter implements TextWatcher { |
111 |
public void |
112 |
beforeTextChanged(CharSequence s, int start, int count, int after) { } |
113 |
|
114 |
public void |
115 |
onTextChanged(CharSequence s, int start, int before, int count) { } |
116 |
} |
117 |
} |