Commit 19058cf7 authored by Xavier's avatar Xavier

Context menu of missions in list and copy mission implemented

parent ea77292c
......@@ -14,4 +14,9 @@
<item>3</item>
<item>4</item>
</string-array>
<string-array name="missions_menu">
<item>Eliminar</item>
<item>Copiar</item>
<item>Editar</item>
</string-array>
</resources>
\ No newline at end of file
......@@ -115,6 +115,11 @@
<string name="dialog_delete_confirm">Eliminar</string>
<string name="dialog_delete_cancel">Cancelar</string>
<string name="dialog_copy_title">Copiar Misión</string>
<string name="dialog_copy_message">Desea copiar la misión</string>
<string name="dialog_copy_confirm">Copiar</string>
<string name="dialog_copy_cancel">Cancelar</string>
<string name="dialog_reset_title">Resetear Config.</string>
<string name="dialog_reset_message">Se dispone a resetear toda configuración y datos a los valores por defecto (Las misiones se guardarán)</string>
<string name="dialog_reset_confirm">Resetear</string>
......
......@@ -14,4 +14,9 @@
<item>3</item>
<item>4</item>
</string-array>
<string-array name="missions_menu">
<item>Enlever</item>
<item>Copier</item>
<item>Modifier</item>
</string-array>
</resources>
\ No newline at end of file
......@@ -116,6 +116,11 @@
<string name="dialog_delete_confirm">Supprimer</string>
<string name="dialog_delete_cancel">Annuler</string>
<string name="dialog_copy_title">Copier Mission</string>
<string name="dialog_copy_message">Voulez-vous copier la mission</string>
<string name="dialog_copy_confirm">Copier</string>
<string name="dialog_copy_cancel">Annuler</string>
<string name="dialog_reset_title">Réinitialiser Config.</string>
<string name="dialog_reset_message">Vous êtes en train de réinitialiser toute configuration et données avec les valeurs par défaut (Les missions sont sauvegardés)</string>
<string name="dialog_reset_confirm">Réinitialiser</string>
......
......@@ -14,4 +14,9 @@
<item>3</item>
<item>4</item>
</string-array>
<string-array name="missions_menu">
<item>Remove</item>
<item>Copy</item>
<item>Edit</item>
</string-array>
</resources>
\ No newline at end of file
......@@ -115,6 +115,11 @@
<string name="dialog_delete_confirm">Delete</string>
<string name="dialog_delete_cancel">Cancel</string>
<string name="dialog_copy_title">Copy Mission</string>
<string name="dialog_copy_message">Do you want to copy the mission</string>
<string name="dialog_copy_confirm">Copy</string>
<string name="dialog_copy_cancel">Cancel</string>
<string name="dialog_reset_title">Reset Config.</string>
<string name="dialog_reset_message">You are going to reset all configurations and data to default values (Missions will be kept)</string>
<string name="dialog_reset_confirm">Reset</string>
......
package cs.si.stavor.dialogs;
import java.io.IOException;
import cs.si.stavor.R;
import cs.si.stavor.MainActivity;
import cs.si.stavor.StavorApplication;
import cs.si.stavor.database.SerializationUtil;
import cs.si.stavor.database.MissionReaderContract.MissionEntry;
import cs.si.stavor.mission.Mission;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
/**
* Dialog to prevent unwilling mission removals
* @author Xavier Gibert
*
*/
public class CopyMissionDialogFragment extends DialogFragment {
private static final String ARG_NAME = "mission_name";
private static final String ARG_ID = "mission_id";
private static final String ARG_CLASS = "mission_class";
public static CopyMissionDialogFragment newInstance(int id, String name, Mission mis_class) {
CopyMissionDialogFragment fragment = new CopyMissionDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_NAME, name);
args.putInt(ARG_ID, id);
args.putSerializable(ARG_CLASS, mis_class);
fragment.setArguments(args);
return fragment;
}
int mission_id;
Mission mission;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
String mis_name = getArguments().getString(ARG_NAME);
int mis_id = getArguments().getInt(ARG_ID);
mission_id = mis_id;
mission = (Mission) getArguments().getSerializable(ARG_CLASS);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getString(R.string.dialog_copy_title))
.setMessage(getString(R.string.dialog_copy_message)+" "+mis_name+"?")
.setCancelable(true)
.setPositiveButton(getString(R.string.dialog_copy_confirm), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Dummy
copyMission();
}
});
builder.setNegativeButton(getString(R.string.dialog_copy_cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
/**
* Delete Mission from database
* @param mission_id
*/
private void copyMission() {
MainActivity act = ((MainActivity)getActivity());
ContentValues values = new ContentValues();
mission.name = mission.name+"_copy";
values.put(MissionEntry.COLUMN_NAME_NAME, mission.name);
values.put(MissionEntry.COLUMN_NAME_DESCRIPTION, mission.description);
try {
values.put(MissionEntry.COLUMN_NAME_CLASS, SerializationUtil.serialize(mission));
} catch (IOException e) {
e.printStackTrace();
}
// Insert the new row
((StavorApplication)act.getApplication()).loader.insert(
MissionEntry.TABLE_NAME,
null,
values);
}
}
......@@ -7,6 +7,7 @@ import cs.si.stavor.app.Parameters;
import cs.si.stavor.database.MissionReaderDbHelper;
import cs.si.stavor.database.SerializationUtil;
import cs.si.stavor.database.MissionReaderContract.MissionEntry;
import cs.si.stavor.dialogs.CopyMissionDialogFragment;
import cs.si.stavor.dialogs.DeleteMissionDialogFragment;
import cs.si.stavor.mission.Mission;
import cs.si.stavor.mission.MissionAndId;
......@@ -23,7 +24,11 @@ import android.content.Loader;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Paint;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
......@@ -166,7 +171,7 @@ public final class SimulatorFragment extends Fragment implements LoaderCallbacks
simulator.connect();
}else{
//Set mission
MissionAndId mis = getSelectedMission();
MissionAndId mis = getMission(activeMissionId);
if(mis!=null){
simulator.setSelectedMission(mis.mission, mis.id);
simulator.connect();
......@@ -225,7 +230,7 @@ public final class SimulatorFragment extends Fragment implements LoaderCallbacks
}else if (activeMissionId==0 ||activeMissionId==1 ||activeMissionId==2 ||activeMissionId==3){
Toast.makeText(getActivity().getApplicationContext(), getString(R.string.sim_local_mission_not_editable), Toast.LENGTH_LONG).show();
}else{
MissionAndId mis = getSelectedMission();
MissionAndId mis = getMission(activeMissionId);
if(mis!=null){
((MainActivity)getActivity()).showMissionEditor(mis);
}else{
......@@ -244,7 +249,7 @@ public final class SimulatorFragment extends Fragment implements LoaderCallbacks
* Returns the selected Mission from the database
* @return
*/
private MissionAndId getSelectedMission(){
private MissionAndId getMission(int id){
String[] projection = {
MissionEntry._ID,
MissionEntry.COLUMN_NAME_CLASS
......@@ -254,7 +259,7 @@ public final class SimulatorFragment extends Fragment implements LoaderCallbacks
MissionEntry.TABLE_NAME, // The table to query
projection, // The columns to return
MissionEntry._ID+" = ?", // The columns for the WHERE clause
new String[]{Integer.toString(activeMissionId)}, // The values for the WHERE clause
new String[]{Integer.toString(id)}, // The values for the WHERE clause
"", // don't group the rows
"", // don't filter by row groups
null // The sort order
......@@ -284,6 +289,17 @@ public final class SimulatorFragment extends Fragment implements LoaderCallbacks
newFragment.setCancelable(true);
newFragment.show(getFragmentManager(), "delete");
}
/**
* Shows the copy mission confirmation dialog
* @param id Mission id
* @param name Mission name
*/
public void showCopyMissionDialog(int id, String name, Mission mis) {
DialogFragment newFragment = CopyMissionDialogFragment.newInstance(id, name, mis);
newFragment.setCancelable(true);
newFragment.show(getFragmentManager(), "copy");
}
/**
* Change the text color of the missions in list that is selected
......@@ -400,4 +416,60 @@ public final class SimulatorFragment extends Fragment implements LoaderCallbacks
adapter.changeCursor(null);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId()==R.id.listView1) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
String header = ((TextView)((ListView)v).getChildAt(info.position).findViewById(R.id.textViewMission)).getText().toString();
menu.setHeaderTitle(header);
String[] menuItems = getResources().getStringArray(R.array.missions_menu);
for (int i = 0; i<menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
//String[] menuItems = getResources().getStringArray(R.array.missions_menu);
//String menuItemName = menuItems[menuItemIndex];
int listItemKey = -1;
try{
listItemKey = Integer.parseInt(((TextView)(missionsList.getChildAt(info.position).findViewById(R.id.textViewMissionId))).getText().toString());
String listItemName = ((TextView)missionsList.getChildAt(info.position).findViewById(R.id.textViewMission)).getText().toString();
if(menuItemIndex==0){
if(listItemKey==-1){
Toast.makeText(getActivity().getApplicationContext(), getString(R.string.sim_local_select_first_a_mission), Toast.LENGTH_LONG).show();
}else if (listItemKey==0 ||listItemKey==1 ||listItemKey==2 || listItemKey==3 ){
Toast.makeText(getActivity().getApplicationContext(), getString(R.string.sim_local_mission_not_removable), Toast.LENGTH_LONG).show();
}else{
showDeleteMissionDialog(listItemKey, listItemName);
}
}else if(menuItemIndex==1){
showCopyMissionDialog(listItemKey, listItemName, getMission(listItemKey).mission);
}else if(menuItemIndex==2){
if(listItemKey==-1){
Toast.makeText(getActivity().getApplicationContext(), getString(R.string.sim_local_select_first_a_mission), Toast.LENGTH_LONG).show();
}else if (listItemKey==0 ||listItemKey==1 ||listItemKey==2 ||listItemKey==3){
Toast.makeText(getActivity().getApplicationContext(), getString(R.string.sim_local_mission_not_editable), Toast.LENGTH_LONG).show();
}else{
MissionAndId mis = getMission(listItemKey);
if(mis!=null){
((MainActivity)getActivity()).showMissionEditor(mis);
}else{
Toast.makeText(getActivity().getApplicationContext(), getString(R.string.sim_local_cannot_deserialize_selected_mission), Toast.LENGTH_LONG).show();
}
}
}
}catch(Exception e){
}
return true;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment