Android Open Source - PrincePolo Personal Notes Fragment






From Project

Back to project page PrincePolo.

License

The source code is released under:

GNU General Public License

If you think the Android project PrincePolo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package se.agile.activities;
//w ww. j  a va2  s.  co  m
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import se.agile.princepolo.R;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class PersonalNotesFragment extends Fragment implements OnClickListener, OnItemClickListener{
  private String logTag;
  private View rootView;
  private static File noteFile;
  private Button saveNoteButton, createButton, finishButton, 
      addButton, deleteButton, mergeButton, branchButton,
      classButton, packageButton, interfaceButton,
      featureButton, taskButton, testButton;
    private ListView list,list_head;
    private ArrayList<HashMap<String, String>> mylist, mylist_title;
    private ListAdapter adapter_title, adapter;
    private HashMap<String, String> map1, map2;
    private ArrayList<String> dates = new ArrayList<String>();
    private ArrayList<String> personalNotes = new ArrayList<String>();
  private ArrayList<String> notes = new ArrayList<String>();
  private final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM d HH:mm");
  
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                Bundle savedInstanceState) {
    noteFile = new File(getActivity().getFilesDir(), "notes.txt");
    rootView = inflater.inflate(R.layout.fragment_personal_notes, container, false);
    logTag = getResources().getString(R.string.logtag_main);
        list = (ListView) rootView.findViewById(R.id.listView2);
        list_head = (ListView) rootView.findViewById(R.id.listView1);
        saveNoteButton = (Button)rootView.findViewById(R.id.saveNote_Button);
        createButton = (Button)rootView.findViewById(R.id.create_Button);
        finishButton = (Button)rootView.findViewById(R.id.finish_Button);
        addButton = (Button)rootView.findViewById(R.id.add_Button);
        deleteButton = (Button)rootView.findViewById(R.id.delete_Button);
        mergeButton = (Button)rootView.findViewById(R.id.merge_Button);
        branchButton = (Button)rootView.findViewById(R.id.branch_Button);
        classButton = (Button)rootView.findViewById(R.id.class_Button);
        packageButton = (Button)rootView.findViewById(R.id.package_Button);
        interfaceButton = (Button)rootView.findViewById(R.id.interface_Button);
        featureButton = (Button)rootView.findViewById(R.id.feature_Button);
        taskButton = (Button)rootView.findViewById(R.id.task_Button);
        testButton = (Button)rootView.findViewById(R.id.test_Button);
        createButton.setOnClickListener(this);
        finishButton.setOnClickListener(this);
        addButton.setOnClickListener(this);
        deleteButton.setOnClickListener(this);
        mergeButton.setOnClickListener(this);
        branchButton.setOnClickListener(this);
        classButton.setOnClickListener(this);
        packageButton.setOnClickListener(this);
        interfaceButton.setOnClickListener(this);
        featureButton.setOnClickListener(this);
        taskButton.setOnClickListener(this);
        testButton.setOnClickListener(this);
        saveNoteButton.setOnClickListener(this);
        list.setOnItemClickListener(this);
    try {
      updateList();
    } catch (FileNotFoundException e1) {
      e1.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }       
        return rootView;
    }
  
  private String getDate(){
    return DATE_FORMAT.format(Calendar.getInstance().getTime());
  }
  
  private void writeToFile(ArrayList<String> noteList) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(noteFile, false));
    int iter = 0;
    while (iter < noteList.size()){
      writer.write(noteList.get(iter));
      writer.newLine();
      iter++;
    }
    writer.flush();
    writer.close();
  }
  
  private void updateList() throws IOException{
    notes.clear();
      BufferedReader br = new BufferedReader(new FileReader(noteFile));
      String line;
      Boolean date = true;
      while ((line = br.readLine())!= null) {
        if (date){
          notes.add(line);
          dates.add(line);
          date = false;
        }
        else {
          notes.add(line);
          personalNotes.add(line);
          date = true;
        }
      }
      br.close();
      populateLists();
  }

  @Override 
  public void onClick(View v) {
    EditText et = (EditText)rootView.findViewById(R.id.note_textField);
    String msg = et.getText().toString();
    if (v == saveNoteButton) {
      String date = getDate();
      et.setText("");
      if((msg != null) && (msg.trim().length() > 0)){
        notes.add(date);
        notes.add(msg);
        try {
          writeToFile(notes);
          updateList();
        } catch (FileNotFoundException e) {
          fileNotFoundToast();
          e.printStackTrace();
        } catch (IOException e) {
          ioToast();
          e.printStackTrace();
        }
      }
    }
    else if ( v == createButton){
      if (msg.length() == 0){
        et.setText(msg + "Create");
      }
      else {
        et.setText(msg + " create");
      }
      et.setSelection(et.length());
    }
    else if ( v == finishButton){
      if (msg.length() == 0){
        et.setText(msg + "Finish");
      }
      else {
        et.setText(msg + " finish");
      }
      et.setSelection(et.length());
    }
    else if ( v == addButton){
      if (msg.length() == 0){
        et.setText(msg + "Add");
      }
      else {
        et.setText(msg + " add");
      }
      et.setSelection(et.length());
    }
    else if ( v == deleteButton){
      if (msg.length() == 0){
        et.setText(msg + "Delete");
      }
      else {
        et.setText(msg + " delete");
      }
      et.setSelection(et.length());
    }
    else if ( v == mergeButton){
      if (msg.length() == 0){
        et.setText(msg + "Merge");
      }
      else {
        et.setText(msg + " merge");
      }
      et.setSelection(et.length());
    }
    else if ( v == branchButton){
      if (msg.length() == 0){
        et.setText(msg + "Branch");
      }
      else {
        et.setText(msg + " branch");
      }
      et.setSelection(et.length());
    }
    else if ( v == classButton){
      if (msg.length() == 0){
        et.setText(msg + "Class");
      }
      else {
        et.setText(msg + " class");
      }
      et.setSelection(et.length());
    }
    else if ( v == packageButton){
      if (msg.length() == 0){
        et.setText(msg + "Package");
      }
      else {
        et.setText(msg + " package");
      }
      et.setSelection(et.length());
    }
    else if ( v == interfaceButton){
      if (msg.length() == 0){
        et.setText(msg + "Interface");
      }
      else {
        et.setText(msg + " interface");
      }
      et.setSelection(et.length());
    }
    else if ( v == featureButton){
      if (msg.length() == 0){
        et.setText(msg + "Feature");
      }
      else {
        et.setText(msg + " feature");
      }
      et.setSelection(et.length());
    }
    else if ( v == taskButton){
      if (msg.length() == 0){
        et.setText(msg + "Task");
      }
      else {
        et.setText(msg + " task");
      }
      et.setSelection(et.length());
    }
    else if ( v == testButton){
      if (msg.length() == 0){
        et.setText(msg + "Test");
      }
      else {
        et.setText(msg + " test");
      }
      et.setSelection(et.length());
    }
  }
  
  private void ioToast() {
    Toast IOExceptionToast = Toast.makeText(getActivity().getApplicationContext(), "Damn! IOException!", Toast.LENGTH_LONG);
    IOExceptionToast.setGravity(Gravity.CENTER, 0, 0);
    IOExceptionToast.show();
  }
  
  private void fileNotFoundToast() {
    Toast fileNotFoundToast = Toast.makeText(getActivity().getApplicationContext(), "Damn! 404 etc! File not found", Toast.LENGTH_LONG);
    fileNotFoundToast.setGravity(Gravity.CENTER, 0, 0);
    fileNotFoundToast.show();
  }
  
  public void populateLists() {
        mylist = new ArrayList<HashMap<String, String>>();
        mylist_title = new ArrayList<HashMap<String, String>>();
        map1 = new HashMap<String, String>();
      
        /**********Display the headings************/
        map1.put("date", " Date");
        map1.put("note", "Note");
        mylist_title.add(map1);

        try {
            adapter_title = new SimpleAdapter(getActivity(), mylist_title, R.layout.headrow,
                    new String[] {"date", "note"}, new int[] {
                            R.id.dateHead, R.id.noteHead});
            list_head.setAdapter(adapter_title);
        } catch (Exception e) {
           //this is bad practice but.. yeah
        }
      
        /**********Display the contents************/
        mylist.clear();
        for (int i = personalNotes.size() - 1; i >= 0; i--) {
          map2 = new HashMap<String, String>();
            map2.put("date", dates.get(i));
            map2.put("note", personalNotes.get(i));
            mylist.add(map2);
        }
        personalNotes.clear();
        dates.clear();
       
        try {
            adapter = new SimpleAdapter(getActivity(), mylist, R.layout.row,
                    new String[] {"date", "note" }, new int[] {
                            R.id.date, R.id.note});
            list.setAdapter(adapter);
        } catch (Exception e) {
          //this is bad practice but.. yeah
        }

    }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
      long id) {
    final Integer pip = (int) id;
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:

            if (notes.size() > 1){
              notes.remove((notes.size()-1) - (pip*2+1));
              notes.remove((notes.size()-1) - (pip*2));
            }
              try {
              writeToFile(notes);
              updateList();
                } catch (FileNotFoundException e1) {
                  fileNotFoundToast();
              e1.printStackTrace();
            } catch (IOException e) {
              ioToast();
              e.printStackTrace();
            }
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //Nay sir keep me contents
                break;
            }
        }
    };
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Delete this note?").setNegativeButton("No", dialogClickListener)
    .setPositiveButton("Yes", dialogClickListener).show();  
  }
}




Java Source Code List

se.agile.activities.AboutFragment.java
se.agile.activities.BranchFragment.java
se.agile.activities.BranchesFragment.java
se.agile.activities.CardFragment.java
se.agile.activities.CollaboratorsFragment.java
se.agile.activities.CommitFragment.java
se.agile.activities.ConflictFragment.java
se.agile.activities.ConnectToGitHubFragment.java
se.agile.activities.LoginActivity.java
se.agile.activities.MainActivity.java
se.agile.activities.NotificationFragmentSwitcher.java
se.agile.activities.NotificationsFragment.java
se.agile.activities.PersonalNotesFragment.java
se.agile.activities.PokerFragment.java
se.agile.activities.RepositoryOverviewFragment.java
se.agile.activities.SelectRepositoryFragment.java
se.agile.activities.SelectWorkingFilesActivity.java
se.agile.activities.SelectWorkingFilesFragment.java
se.agile.activities.SelectWorkingFilesOverviewFragment.java
se.agile.activities.SettingsFragment.java
se.agile.activities.SignOutFragment.java
se.agile.asynctasks.RequestAccessToken.java
se.agile.asynctasks.RequestAllCommitsForBranch.java
se.agile.asynctasks.RequestAllFilesForBranch.java
se.agile.asynctasks.RequestBranch.java
se.agile.asynctasks.RequestBranches.java
se.agile.asynctasks.RequestFiles.java
se.agile.asynctasks.RequestFullCommit.java
se.agile.asynctasks.RequestListenerAdapter.java
se.agile.asynctasks.RequestListener.java
se.agile.asynctasks.RequestRepositories.java
se.agile.asynctasks.RequestShortCommit.java
se.agile.asynctasks.RequestTask.java
se.agile.asynctasks.RequestUser.java
se.agile.controller.MainActivityController.java
se.agile.controller.settingsFragmentController.java
se.agile.githubdata.Branch.java
se.agile.githubdata.Commit.java
se.agile.githubdata.Directory.java
se.agile.githubdata.File.java
se.agile.githubdata.Folder.java
se.agile.githubdata.GitHubDataInterface.java
se.agile.githubdata.Repository.java
se.agile.githubdata.User.java
se.agile.model.BranchNotification.java
se.agile.model.BranchSelectionModel.java
se.agile.model.CommitNotification.java
se.agile.model.ConflictNotification.java
se.agile.model.CreateNotificationActivity.java
se.agile.model.DirectoryListArrayAdapter.java
se.agile.model.InteractiveArrayAdapter.java
se.agile.model.JSONParser.java
se.agile.model.NotificationDialog.java
se.agile.model.NotificationHandler.java
se.agile.model.NotificationListArrayAdapter.java
se.agile.model.NotificationListener.java
se.agile.model.Notification.java
se.agile.model.PreferenceListener.java
se.agile.model.Preferences.java
se.agile.model.TemporaryStorage.java
se.agile.model.Tuple.java
se.agile.model.WorkingFileListArrayAdapter.java
se.agile.navigator.NavDrawerItem.java
se.agile.navigator.NavDrawerListAdapter.java