Package app.view.note

Source Code of app.view.note.TagSelection

package app.view.note;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.ytreza.tool.StringUtilities;
import org.ytreza.tool.internationalization.Tr;
import org.ytreza.view.controler.Controler;
import org.ytreza.view.field.FieldText;

import app.controler.note.Note;
import app.controler.note.NoteManager;
import app.controler.tag.Tag;
import app.database.TableFactory;
import app.tool.note.TagAndNoteManager;
import app.tool.note.TagLinkImp;

public class NoteControler extends Controler{
  private Note currentNote;

  private JFrame window;
  private JList subjectList;
  private FieldText fieldText;
  private FieldSubject fieldSubject;
  private FieldText fieldTagNote;
  private JList fieldTagsUnselected;
  private Collection<TagLinkImp> unselectedTags = new ArrayList<TagLinkImp>();
  private List<TagLinkImp> selectedTags = new ArrayList<TagLinkImp>();

  private List<Integer> listeNotesAccordingOnSelectedTags;
  private JList fieldTagsSelected;
  private TagAndNoteManager tagManager;
  private NoteManager noteManager;

  public NoteControler(String name, TableFactory tableFactory){
    super(name);
    noteManager = new NoteManager(tableFactory);
    tagManager = new TagAndNoteManager(tableFactory);
  }

  @Override
  public void start() {
    refreshNotesList();
    refreshTagList();
    createNote();
  }

  public void createNote() {
    currentNote = noteManager.create();
    refreshNotepane();
  }

  public void saveNote() {
    currentNote.setSubject(fieldSubject.getText());
    currentNote.setText(fieldText.getText());
    currentNote.setTagList(fieldTagNote.getText());
    currentNote.save();
  }


  public void selectNote(Note note) {
    if (note != null) {
      saveNote();
      currentNote = note;
      refreshNotepane();
    }
  }

  private void refreshNotepane() {
    fieldSubject.setText(currentNote.getSubject());
    fieldText.setText(currentNote.getText());
    fieldTagNote.setText(getTagListAsString());
    refreshTitle();
  }

  private void refreshTitle() {
    if (currentNote.isNew()) {
      window.setTitle("Noterex - " + Tr._("title.new.note"));
    } else {
      window.setTitle("Noterex - " + currentNote.getSubject());
    }
  }

  private void refreshTagList() {
    // TODO Faire le raffraichissement des tags
//    refreshFieldTagUnselected();
//    refreshFieldTagSelected();
  }

  private void refreshFieldTagSelected() {
    fieldTagsSelected.setModel(new ModelListTagsSelected(selectedTags));
  }

  private void refreshFieldTagUnselected() {
    fieldTagsUnselected.setModel(new ModelListTagsUnselected(getUnselectedTags()));
  }

  private Collection<TagLinkImp> getUnselectedTags() {
    if (selectedTags.isEmpty()) {
      unselectedTags = tagManager.getAllTagsNote();
    } else {
      unselectedTags = tagManager.getAllTagsNoteFrom(selectedTags);
    }
    return unselectedTags;
  }

  private void refreshNotesList() {
    List<Integer> listIds ;
    if (selectedTags.isEmpty()) {
      listIds = noteManager.getAllIds();
    } else {
      listIds = new ArrayList<Integer>();
     
//      fillNotesListAccordingOnSelectedTags(); 
    }
    getFieldSubjectList().setModel(new ListNote(listIds, noteManager));
  }

  private void fillNotesListAccordingOnSelectedTags() {
    // TODO Cr�er un it�rateur permettant de remplir la liste
//    try {
//      currentNoteIterator = new ItemIterator<ModelNote>(getNotesId(), tableNote);
//      getFieldSubjectList().setModel(new ListItem<ModelNote>(currentNoteIterator));
//    } catch (Exception e) {
//      // AUTO_TODO
//      e.printStackTrace();
//    }
  }

  private List<Integer> getNotesId(){
    if (listeNotesAccordingOnSelectedTags == null) {
      listeNotesAccordingOnSelectedTags = tagManager.getNoteAccordingOnTags(selectedTags);
    }
   
    return listeNotesAccordingOnSelectedTags;
  }


  private JList getFieldSubjectList() {
    if (subjectList == null) {
      subjectList = new JList();
    }
    return subjectList;
  }

  private String getTagListAsString() {
//    List<ModelTag> list = getTagList();
    List<String> listLabel = new ArrayList<String>();
//    for (ModelTag tag : list) {
//      listLabel.add(tag.getFullLabel());
//    }
    return StringUtilities.Join(listLabel, ";");
  }

  private List<Tag> getTagList() {
    return null;
    // TODO Faire la r�cup�ration d'une liste de tag valide
//    return currentNote.getTagList();
  }

  @Override
  public void setup() {
    // AUTO_TODO Auto-generated method stub
  }

  public void setWindow (JFrame window) {
    this.window = window;
  }

  public void setFieldText(FieldText fieldText) {
    this.fieldText = fieldText;
  }

  public void setFieldSubject(FieldSubject fieldSubject) {
    this.fieldSubject = fieldSubject;
  }

  public void setFieldTagNote(FieldText fieldTagNote) {
    this.fieldTagNote = fieldTagNote;
  }

  public void setFieldSubjectList(JList subjectList) {
    this.subjectList = subjectList;
    this.subjectList.addListSelectionListener(new NoteSelection(this, subjectList));
  }

  public void setFieldTagUnselected(JList fieldTagsUnselected) {
    this.fieldTagsUnselected = fieldTagsUnselected;
    this.fieldTagsUnselected.addListSelectionListener(new TagSelection(this, fieldTagsUnselected));
    this.fieldTagsUnselected.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    this.fieldTagsUnselected.setLayoutOrientation(JList.VERTICAL_WRAP);
    this.fieldTagsUnselected.setVisibleRowCount(-1);
  }

  public void setFieldTagSelected(JList fieldSelectedTag) {
    this.fieldTagsSelected = fieldSelectedTag;
    this.fieldTagsSelected .setLayoutOrientation(JList.VERTICAL_WRAP);
    this.fieldTagsSelected .setVisibleRowCount(-1);
  }

  public Note getNote() {
    return currentNote;
  }

  @Override
  public void clickOnCopy() {
    saveNote();
    copyNote();
    refreshNotepane();
  }

  private void copyNote() {
    currentNote = currentNote.copy();
  }

  @Override
  public void clickOnDelete() {
    currentNote.delete();
    refreshTagList();
    refreshNotesList();
    refreshNotepane();
  }

  @Override
  public void clickOnNew() {
    saveNote();
    createNote();
  }

  @Override
  public void clickOnSave() {
    saveNote();
  }

  public void onLostFocusOnSubject() {
    saveNote();
  }

  public void onLostFocusOnText() {
    saveNote();
  }

  public void onLostFocusOnTagNote() {
    saveNote();
  }

  public void selectTag(TagLinkImp tag) {
    if (tag != null && ! selectedTags.contains(tag)) {
      System.out.println("coucou");
      selectedTags.add(tag);
      listeNotesAccordingOnSelectedTags = null;
      refreshTagList();
      refreshNotesList();
    }
  }

  public void unselectTag(Tag tag) {
    if (selectedTags.contains(tag)) {
      selectedTags.remove(tag);
      listeNotesAccordingOnSelectedTags = null;
      refreshTagList();
      refreshNotesList();
    }
  }


  public void selectAllTag() {
    if (selectedTags.size() > 0 ) {
      selectedTags.clear();
      refreshTagList();
      refreshNotesList();
    }
  }
}

class NoteSelection implements ListSelectionListener {
  private NoteControler controler;
  private JList list;

  public NoteSelection(NoteControler controler, JList list) {
    this.controler = controler;
    this.list = list;
  }

  @Override
  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting() == false) {
      controler.selectNote((Note) list.getSelectedValue());
    }
  }
}

class TagSelection implements ListSelectionListener {
  private NoteControler controler;
  private JList list;

  public TagSelection(NoteControler controler, JList list) {
    this.controler = controler;
    this.list = list;
  }

  @Override
  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting() == false) {
      if (list.getSelectedIndex() == 0) {
        controler.selectAllTag();
      } else {
        controler.selectTag((TagLinkImp) list.getSelectedValue());
      }
    }
  }
}
TOP

Related Classes of app.view.note.TagSelection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
y>