package teamnotepad;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import teamnotepad.dialogwindows.*;
import teamnotepad.entities.Note;
import teamnotepad.entities.SerializedNote;
import teamnotepad.entities.NotesSuite;
import teamnotepad.entities.SerializedNotesSuite;
import teamnotepad.entities.User;
/**
*
* @author User
*/
public final class TeamNotepadController {
private final TeamNotepadModel model;
private final TeamNotepadView view;
private User currentUser = null;
private Note currentNote = null;
TeamNotepadController(TeamNotepadModel model, TeamNotepadView view) {
this.model = model;
this.view = view;
}
public void Init(){
view.setCurrentUserName("No user logged in");
if(currentUser != null){
handleAuthenticatedUser();
}else{
handleNewUser();
}
}
private void handleAuthenticatedUser(){
if(currentUser != null){
setCurrentUserName();
initEventListeners();
refreshNotesTree();
}
}
private void handleNewUser(){
currentUser = model.getCurrentUserByAuthFile();
handleAuthenticatedUser();
if(currentUser == null){
UserAuthenticationDialog dialog = new UserAuthenticationDialog(this);
dialog.setVisible(true);
}
}
public boolean loginUser(String login, String password, boolean rememberUser) {
currentUser = model.loginUser(login, password, rememberUser);
handleAuthenticatedUser();
return currentUser != null;
}
public boolean registerUser(String login, String fullName, String password) {
currentUser = model.registerUser(login, fullName, password);
handleAuthenticatedUser();
return currentUser != null;
}
public void shareNotebook(int port){
try {
model.shareNotesSuite(currentUser, port);
} catch (IOException ex) {
Logger.getLogger(TeamNotepadController.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(TeamNotepadController.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void openRemoteNotebook(String address, int port){
try {
List<SerializedNotesSuite> notesSuites = model.getRemoteNotesSuites(address, port);
view.mainFrame.feedNotesTreeWithRemoteNotes(notesSuites, "Remote notes");
} catch (IOException ex) {
Logger.getLogger(TeamNotepadController.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(TeamNotepadController.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void setCurrentUserName(){
if(currentUser != null)
this.view.setCurrentUserName("Logged in: " + currentUser.getName()+ " (" + currentUser.getLogin() + ")");
}
private void initEventListeners(){
view.mainFrame.addNotesSuiteListener(new AddNotesSuiteListener());
view.mainFrame.addTreeNodeClickedListener(new ClickedTreeNodeListener());
view.mainFrame.addSaveNoteListener(new SaveNoteButtonClickedListener());
view.mainFrame.addOpenRemoteNotebookMenuItemListener(new OpenRemoteNotebookClickedListener(this));
view.mainFrame.addShareNotebookMenuItemListener(new ShareNotebookClickedListener(this));
}
private void refreshNotesTree(){
try{
view.reloadNotesTree(model.getNotesSuites(currentUser), currentUser);
}catch(Exception ex){
System.out.println("EXCEPTION: " + ex.getMessage());
ex.printStackTrace();
}
}
private void openNote(Note note){
currentNote = note;
view.showNoteDetails(note);
}
private void openSerializedNote(SerializedNote serializedNote){
view.showSrializedNoteDetails(serializedNote);
}
class AddNotesSuiteListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
try{
String name = view.GetNewNotesSuiteName();
model.createNotesSuite(currentUser, name);
view.ClearNewNotesSuiteName();
refreshNotesTree();
}catch(Exception ex){
System.err.println("Exception: " + ex.getMessage());
view.showErrorMessage(ex.getMessage());
}
}
}
class ClickedTreeNodeListener implements MouseListener{
// http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#select
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 3){
view.handleRightClickOnNotesTree(e.getX(), e.getY());
}else if(e.getButton() == 1 && e.getClickCount() == 2){
Object nodeUserObject = view.getNotebookTreeLastSelectedNode();
if(nodeUserObject.getClass() == Note.class){
Note note = (Note)nodeUserObject;
openNote(note);
}else if(nodeUserObject.getClass() == SerializedNote.class){
SerializedNote serializedNote = (SerializedNote) nodeUserObject;
openSerializedNote(serializedNote);
}
}
}
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
}
class SaveNoteButtonClickedListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String newNoteTitle = view.getNoteTitle();
String newNoteContent = view.getNoteContent();
currentNote.setTitle(newNoteTitle);
currentNote.setContent(newNoteContent);
currentNote.setAuthor(currentUser);
boolean saved;
if(currentNote.getId() > 0){
saved = model.updateNote(currentNote);
}else{
saved = model.createNote(currentNote);
}
if(saved){
view.showMessageDialog("Note saved", "Note saved.");
refreshNotesTree();
}
}
}
class OpenRemoteNotebookClickedListener implements ActionListener{
OpenRemoteNotebookDialog dialog;
OpenRemoteNotebookClickedListener(TeamNotepadController controller){
super();
dialog = new OpenRemoteNotebookDialog(controller);
}
@Override
public void actionPerformed(ActionEvent e){
dialog.setVisible(true);
}
}
class ShareNotebookClickedListener implements ActionListener{
ShareNotebookDialog dialog;
ShareNotebookClickedListener(TeamNotepadController controller){
super();
dialog = new ShareNotebookDialog(controller);
}
@Override
public void actionPerformed(ActionEvent e){
dialog.setVisible(true);
}
}
}