Yaaic/app/src/main/java/org/yaaic/activity/AddCommandsActivity.java

152 lines
4.4 KiB
Java
Raw Normal View History

2010-04-25 06:17:59 -04:00
/*
Yaaic - Yet Another Android IRC Client
2013-01-21 15:32:43 -05:00
Copyright 2009-2013 Sebastian Kaspari
2010-04-25 06:17:59 -04:00
This file is part of Yaaic.
Yaaic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Yaaic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
2011-01-25 15:02:27 -05:00
*/
2010-04-25 06:17:59 -04:00
package org.yaaic.activity;
import java.util.ArrayList;
import org.yaaic.R;
import org.yaaic.model.Extra;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
2011-01-25 15:02:27 -05:00
import android.view.Window;
2010-04-25 06:17:59 -04:00
import android.widget.AdapterView;
2011-01-25 15:02:27 -05:00
import android.widget.AdapterView.OnItemClickListener;
2010-04-25 06:17:59 -04:00
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
/**
* Adding commands (to execute after connect) to a server
*
* @author Sebastian Kaspari <sebastian@yaaic.org>
*/
public class AddCommandsActivity extends Activity implements OnClickListener, OnItemClickListener
{
2010-11-18 12:52:19 -05:00
private EditText commandInput;
private ArrayAdapter<String> adapter;
private ArrayList<String> commands;
private Button okButton;
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
/**
* On create
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
setContentView(R.layout.commandadd);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
commandInput = (EditText) findViewById(R.id.command);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
adapter = new ArrayAdapter<String>(this, R.layout.commanditem);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
ListView list = (ListView) findViewById(R.id.commands);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
((Button) findViewById(R.id.add)).setOnClickListener(this);
((Button) findViewById(R.id.ok)).setOnClickListener(this);
((Button) findViewById(R.id.cancel)).setOnClickListener(this);
2010-11-18 12:52:19 -05:00
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(this);
okButton.setEnabled(false);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
commands = getIntent().getExtras().getStringArrayList(Extra.COMMANDS);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
for (String command : commands) {
adapter.add(command);
}
}
2010-04-25 06:17:59 -04:00
2010-11-18 12:52:19 -05:00
/**
* On Click
*/
2011-01-25 15:02:27 -05:00
@Override
2010-11-18 12:52:19 -05:00
public void onClick(View v)
{
switch (v.getId()) {
case R.id.add:
String command = commandInput.getText().toString().trim();
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
if (!command.startsWith("/")) {
command = "/" + command;
}
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
commands.add(command);
adapter.add(command);
commandInput.setText("/");
okButton.setEnabled(true);
break;
2011-03-14 17:15:13 -04:00
2010-11-18 12:52:19 -05:00
case R.id.cancel:
setResult(RESULT_CANCELED);
finish();
break;
2011-03-14 17:15:13 -04:00
2010-11-18 12:52:19 -05:00
case R.id.ok:
// Get list and return as result
Intent intent = new Intent();
intent.putExtra(Extra.COMMANDS, commands);
setResult(RESULT_OK, intent);
finish();
break;
}
}
2010-04-25 06:17:59 -04:00
2010-11-18 12:52:19 -05:00
/**
* On item clicked
*/
2011-01-25 15:02:27 -05:00
@Override
2010-11-18 12:52:19 -05:00
public void onItemClick(AdapterView<?> list, View item, int position, long id)
{
final String command = adapter.getItem(position);
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
String[] items = { getResources().getString(R.string.action_remove) };
2011-01-25 15:02:27 -05:00
2010-11-18 12:52:19 -05:00
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(command);
builder.setItems(items, new DialogInterface.OnClickListener() {
2011-01-25 15:02:27 -05:00
@Override
2010-11-18 12:52:19 -05:00
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: // Remove
adapter.remove(command);
commands.remove(command);
okButton.setEnabled(true);
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
2010-04-25 06:17:59 -04:00
}