| 1 | package ro.ieval.unical; |
| 2 | import java.util.Calendar; |
| 3 | import java.util.GregorianCalendar; |
| 4 | |
| 5 | import android.app.ActionBar; |
| 6 | import android.app.Activity; |
| 7 | import android.app.DatePickerDialog.OnDateSetListener; |
| 8 | import android.app.Fragment; |
| 9 | import android.app.TimePickerDialog.OnTimeSetListener; |
| 10 | import android.content.Context; |
| 11 | import android.content.Intent; |
| 12 | import android.os.Bundle; |
| 13 | import android.view.Menu; |
| 14 | import android.view.MenuInflater; |
| 15 | import android.view.MenuItem; |
| 16 | import android.view.View; |
| 17 | import android.widget.*; |
| 18 | |
| 19 | public final class AddEventActivity extends Activity implements OnDateSetListener, OnTimeSetListener{ |
| 20 | private TextView dateTimeTextView; |
| 21 | private static Event tempEvent=new Event(); |
| 22 | public static final String EXTRA_EVENT = "event"; |
| 23 | private static Fragment f1,f2,f3; |
| 24 | |
| 25 | public static void editEvent(final Context c, final Event e) { |
| 26 | Intent i = new Intent(c,AddEventActivity.class); |
| 27 | i.putExtra(AddEventActivity.EXTRA_EVENT,e); |
| 28 | tempEvent=e; |
| 29 | c.startActivity(i); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | @Override |
| 34 | protected void onCreate(final Bundle savedInstanceState) { |
| 35 | super.onCreate(savedInstanceState); |
| 36 | setContentView(R.layout.add_event); |
| 37 | setTitle(R.string.add_event_title); |
| 38 | final ActionBar actionBar = getActionBar(); |
| 39 | actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 40 | final ActionBar.Tab t1 = actionBar.newTab().setText(this.getString(R.string.addevtab1)); |
| 41 | final ActionBar.Tab t2 = actionBar.newTab().setText(this.getString(R.string.addevtab2)); |
| 42 | final ActionBar.Tab t3 = actionBar.newTab().setText(this.getString(R.string.addevtab3)); |
| 43 | |
| 44 | f1 = new TrivialFragment(); |
| 45 | final Bundle b1=new Bundle(); |
| 46 | b1.putInt(TrivialFragment.ARGUMENT_LAYOUT, R.layout.add_event_basic_tab); |
| 47 | f1.setArguments(b1); |
| 48 | |
| 49 | f2 = new TrivialFragment(); |
| 50 | final Bundle b2=new Bundle(); |
| 51 | b2.putInt(TrivialFragment.ARGUMENT_LAYOUT, R.layout.add_event_details_tab); |
| 52 | f2.setArguments(b2); |
| 53 | |
| 54 | f3 = new TrivialFragment(); |
| 55 | final Bundle b3=new Bundle(); |
| 56 | b3.putInt(TrivialFragment.ARGUMENT_LAYOUT, R.layout.add_event_other_tab); |
| 57 | f3.setArguments(b3); |
| 58 | |
| 59 | t1.setTabListener(new AddEventTabListener(f1,tempEvent,0)); |
| 60 | t2.setTabListener(new AddEventTabListener(f2,tempEvent,1)); |
| 61 | t3.setTabListener(new AddEventTabListener(f3,tempEvent,2)); |
| 62 | |
| 63 | actionBar.addTab(t1); |
| 64 | actionBar.addTab(t2); |
| 65 | actionBar.addTab(t3); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean onCreateOptionsMenu(final Menu menu) { |
| 70 | final MenuInflater inflater = getMenuInflater(); |
| 71 | inflater.inflate(R.menu.add_event, menu); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public boolean onOptionsItemSelected(final MenuItem item) { |
| 77 | switch (item.getItemId()) { |
| 78 | case R.id.cancel: |
| 79 | finish(); |
| 80 | return true; |
| 81 | case R.id.save: |
| 82 | //TODO Save event |
| 83 | Toast toast = Toast.makeText(getApplicationContext(),"Event saved!",Toast.LENGTH_LONG); |
| 84 | toast.show(); |
| 85 | finish(); |
| 86 | return true; |
| 87 | default: |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public void showDateTimePickerDialog(final View v) { |
| 93 | dateTimeTextView=(TextView) v; |
| 94 | if(v.getId()==R.id.date_start || v.getId()==R.id.date_end || v.getId() == R.id.repeat_until) |
| 95 | new DatePickerFragment().show(getFragmentManager(), "datePicker"); |
| 96 | else |
| 97 | new TimePickerFragment().show(getFragmentManager(), "timePicker"); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void onTimeSet(final TimePicker picker, final int hour, final int minute) { |
| 102 | dateTimeTextView.setText(String.format("%02d:%02d",hour,minute)); |
| 103 | if(dateTimeTextView.getId() == R.id.time_start) |
| 104 | ((TextView)findViewById(R.id.time_end)).setText(String.format("%2d:%2d",hour,minute)); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onDateSet(final DatePicker picker, final int year, final int monthOfYear, final int dayOfMonth) { |
| 109 | final Calendar calendar=new GregorianCalendar(year, monthOfYear, dayOfMonth); |
| 110 | dateTimeTextView.setText(Utils.formatDate(year, monthOfYear, dayOfMonth)); |
| 111 | //calendar.add(Calendar.DAY_OF_MONTH, 1); |
| 112 | if(dateTimeTextView.getId() == R.id.date_start) |
| 113 | ((TextView)findViewById(R.id.date_end)).setText(Utils.formatDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))); |
| 114 | } |
| 115 | } |