NewActionActivity.java :  » UnTagged » peno3-2010-cwa1 » be » kuleuven » VTKfakbarCWA1 » view » sales » Action » Android Open Source

Android Open Source » UnTagged » peno3 2010 cwa1 
peno3 2010 cwa1 » be » kuleuven » VTKfakbarCWA1 » view » sales » Action » NewActionActivity.java
package be.kuleuven.VTKfakbarCWA1.view.sales.Action;

import java.util.ArrayList;
import java.util.Calendar;

import be.kuleuven.VTKfakbarCWA1.R;
import be.kuleuven.VTKfakbarCWA1.controller.sales.ActionController;
import be.kuleuven.VTKfakbarCWA1.model.sales.Action;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

/**
 * This activity controls the output that is seen by the user.  Every interaction from that uses a new action goes by this class.
 * @author Niels
 *
 */
public class NewActionActivity extends Activity{
  // instantiate all the buttons
  private Button mPickTimeStop;
  private Button mPickDateStart;
  private Button mPickDateStop;
  private Button mPickTimeStart;
  // initiate all the parameters for an action
  private int mHourStart;
  private int mMinuteStart;
    private int mYearStart;
  private int mMonthStart;
  private int mDayStart;
  private int mHourStop;
  private int mMinuteStop;
    private int mYearStop;
  private int mMonthStop;
  private int mDayStop;
  private EditText mEditTextActionName;
  private String mActionName;
  private ActionController mActionController;
  private Intent i;
  
    
    /**
     * This is an onclicklistener: it listens to what button the user clicks and knows what to do than.
     */
  private OnClickListener mNewActionClickListener = new OnClickListener(){
      public void onClick(View v) {
        mActionName = mEditTextActionName.getText().toString();
        if (v.getId() == R.id.actionOk){
          Action thisAction = mActionController.getAction(mActionName);
          if(thisAction != null){
            finish();
          }
        }
        if(v.getId()==R.id.addRulesToAction){
          if(mActionName.equals("")){
            CharSequence text = "Je moet eerste een actie hebben aangemaakt";
            Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
            toast.show();
          }
          else{
            AlertDialog.Builder adb=new AlertDialog.Builder(NewActionActivity.this);
              adb.setTitle("Actie toevoegen");
              adb.setMessage("alle gegevens voor de actie zijn ingevuld?");
              
              adb.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                // when the user clicks ok on the dialog, the activity removes the action
                public void onClick(DialogInterface dialog, int which) {
                  mActionController.addAction(mActionName, mYearStart, mMonthStart, mDayStart, mHourStart, mMinuteStart, mYearStop, mMonthStop, mDayStop, mHourStop, mMinuteStop);
                  i = new Intent(NewActionActivity.this, AddRulesToAction.class);
                  i.putExtra("newAction",mActionName);
                  startActivity(i);
                }
              });
              adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();}
              });
              adb.setIcon(R.drawable.icon);
              adb.show();
          }
        }
      }
  };
  // these static variables are used by the time and date picker in order to know which of them to use.
  static final int DATE_STARTDATE_ID = 0;
  static final int DATE_STOPDATE_ID = 1;
  static final int TIME_STARTTIME_ID = 2;
  static final int TIME_STOPTIME_ID = 3;
  // here we make a date picker for the begin date
  private DatePickerDialog.OnDateSetListener mDateSetListenerStart =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYearStart = year;
                mMonthStart = monthOfYear;
                mDayStart = dayOfMonth;
            }
        };
     // here we make a date picker for the stop date
     private DatePickerDialog.OnDateSetListener mDateSetListenerStop =
          new DatePickerDialog.OnDateSetListener() {

              public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                  mYearStop = year;
                  mMonthStop = monthOfYear;
                  mDayStop = dayOfMonth;
              }
          };
    // here we create a time picker for the start time
  private TimePickerDialog.OnTimeSetListener mTimeSetListenerStart =
      new TimePickerDialog.OnTimeSetListener() {
          public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
              mHourStart = hourOfDay;
              mMinuteStart = minute;
          }
      };
  // here we create a time picker for the stop time
  private TimePickerDialog.OnTimeSetListener mTimeSetListenerStop =
     new TimePickerDialog.OnTimeSetListener() {
          public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
              mHourStop = hourOfDay;
              mMinuteStop = minute;
          }
        };
    /**
     * In this method we initialize everything that is used by the activity.  It is the method that is first called when created.
     */
  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.action);
    // initialize all the buttons with their own listeners
    Button buttonOk = (Button)findViewById(R.id.actionOk);
      buttonOk.setOnClickListener(mNewActionClickListener);
      Button buttonAddRules = (Button)findViewById(R.id.addRulesToAction);
      buttonAddRules.setOnClickListener(mNewActionClickListener);
      Button buttonCancel = (Button)findViewById(R.id.actionCancel);
      buttonCancel.setOnClickListener(mNewActionClickListener);
    mActionController = ActionController.getSingletonActionController();
    mEditTextActionName = (EditText)findViewById(R.id.actionName);
      mPickDateStart = (Button) findViewById(R.id.myDatePickerStart);
      mPickDateStop = (Button) findViewById(R.id.myDatePickerStop);
      mPickTimeStart = (Button) findViewById(R.id.pickTimeStart);
      mPickTimeStop = (Button) findViewById(R.id.pickTimeStop);
      mPickDateStart.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              showDialog(DATE_STARTDATE_ID);
          }
      });
      mPickDateStop.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              showDialog(DATE_STOPDATE_ID);
          }
      });
        mPickTimeStart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_STARTTIME_ID);
            }
        });
        mPickTimeStop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_STOPTIME_ID);
            }
        });
        // get the current date
      final Calendar date = Calendar.getInstance();
      mYearStart = date.get(Calendar.YEAR);
      mMonthStart = date.get(Calendar.MONTH);
      mDayStart = date.get(Calendar.DAY_OF_MONTH);
        // get the current time
        final Calendar time = Calendar.getInstance();
        mHourStart = time.get(Calendar.HOUR_OF_DAY);
        mMinuteStart = time.get(Calendar.MINUTE);
  }
  /**
   * This method creates all the dialogs for the time picker so a user can adjust the time or date.  The initial time or date is the current time and date.
   */
  @Override
  protected Dialog onCreateDialog(int id) {
     switch (id) {
     case DATE_STARTDATE_ID:
       return new DatePickerDialog(this,
                   mDateSetListenerStart,
                   mYearStart, mMonthStart, mDayStart);
     case DATE_STOPDATE_ID:
       return new DatePickerDialog(this,
                   mDateSetListenerStop,
                   mYearStart, mMonthStart, mDayStart);
     case TIME_STARTTIME_ID:
       return new TimePickerDialog(this,
                  mTimeSetListenerStart, mHourStart, mMinuteStart, false);
     case TIME_STOPTIME_ID:
       return new TimePickerDialog(this,
                  mTimeSetListenerStop, mHourStart, mMinuteStart, false);
     }
     return null;
  }
  
    
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.