/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package presentation;
import domain.PreferencesController;
import domain.FileControllerDomain;
import domain.UserDomainController;
import domain.TextController;
import domain.Pair;
import domain.StatisticDomainController;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
/**
*
* @author Andreu Marimon
*/
public class PresentationController {
// Constants
private static final int MAXUSER = 60000;
//Atributs vista
private MainFrame _initFrame;
//Atributs usuari
private int _id = 0;
//Atributs context
private boolean _contextNominalized = false;
private int _contextPosition = 0;
//Informacio a guardar:
private boolean _firstXinxeto = true;
private int _state = 0;
//LOGIN -> 0
//REGISTER -> 1
//EDIT -> 2
//PREFERENCES -> 3
//HELP -> 4
//STATISTICS -> 5
private static PresentationController singleton;
public static PresentationController getInstance() {
if(singleton == null) {
singleton = new PresentationController();
UserDomainController userContrl = UserDomainController.getInstance();
userContrl.initUserLog();
}
return singleton;
}
// SCREEN CONTROL
public void setMainFrame (MainFrame frame){
_initFrame = frame;
}
public void showError(String error) {
JPanel panel = new JPanel();
JLabel info = new JLabel(error);
panel.add(info);
int option = JOptionPane.showConfirmDialog(null,panel,"",JOptionPane.PLAIN_MESSAGE,JOptionPane.PLAIN_MESSAGE);
}
public void clean() {
_initFrame.clean();
}
public int getState() {
return _state;
}
public void changeScreen(JPanel newScreen) {
_initFrame.changeScreen(newScreen);
}
// USER
public int getId (){
return _id;
}
public String getUsername (){
UserDomainController uCntrl = UserDomainController.getInstance();
return uCntrl.getUsername();
}
public String getUserPassword (){
UserDomainController uCntrl = UserDomainController.getInstance();
return uCntrl.getPassword();
}
public void newUser(String name, String password) {
UserDomainController userContrl = UserDomainController.getInstance();
if (userContrl.checkAvailability(name)) {
int id = userContrl.getNextID();
if (id > MAXUSER) showError("Not enough id to identificate all users");
else userContrl.register(id, name, password);
}
else showError ("User already exists");
}
public boolean loginUser (String name, String password) {
UserDomainController userContrl = UserDomainController.getInstance();
if (_id == 0) _id = userContrl.login(name, password);
if (_id > 0) {
enter();
//initFrame.logIn();
return true;
}
if (_id == -1) {
showError ("An error happened while login, please try again");
_id = 0;
}
else showError ("The username and password you have entered do not match, please try again");
return false;
}
public void enter () {
TextController textContrl = TextController.getInstance();
textContrl.newText(_id);
goToEdit();
}
public void logOut() {
UserDomainController userContrl = UserDomainController.getInstance();
if (_id != 0) userContrl.logout();
_id = 0;
goToLogin();
}
public void exit() {
UserDomainController userContrl = UserDomainController.getInstance();
if (_id != 0) userContrl.logout();
_id = 0;
System.exit(0);
}
public void deleteUser() {
UserDomainController userContrl = UserDomainController.getInstance();
if (_id != 0) {
userContrl.logout();
userContrl.deleteUser(_id);
}
_id = 0;
goToLogin();
//initFrame.logOut();
}
public void resetModel() {
UserDomainController userContrl = UserDomainController.getInstance();
if (_id != 0) userContrl.resetModel(_id);
}
public void updateUser(String username, String password) {
UserDomainController userContrl = UserDomainController.getInstance();
userContrl.changeUsername(username);
userContrl.changePassword(password);
}
public boolean logged() {
return _id == 0;
}
public String getTextColor(){
PreferencesController pfContrl = PreferencesController.getInstance();
return pfContrl.getColor(_id);
}
public int getTextSize(){
PreferencesController pfContrl = PreferencesController.getInstance();
return pfContrl.getSize(_id);
}
public int getAssistant(){
PreferencesController pfContrl = PreferencesController.getInstance();
return pfContrl.getAssistant(_id);
}
public void setAssistant(int i){
PreferencesController pfContrl = PreferencesController.getInstance();
pfContrl.setAssistant(_id,i);
}
public void setTextColor(String color){
PreferencesController pfContrl = PreferencesController.getInstance();
pfContrl.setColor(_id, color);
}
public void setTextSize(int size){
PreferencesController pfContrl = PreferencesController.getInstance();
pfContrl.setSize(_id, size);
}
// NOMINALIZE
public ArrayList< Pair<String,Float> > nominalize(String text) {
ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
TextController tCntrl = TextController.getInstance();
tCntrl.enterText(text);
result = tCntrl.startNominalization();
return result;
}
public ArrayList< Pair<String,Float> > nextVerb() {
ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
TextController tCntrl = TextController.getInstance();
result = tCntrl.nextNominalization();
return result;
}
public ArrayList< Pair<String,Float> > previousVerb() {
ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
TextController tCntrl = TextController.getInstance();
result = tCntrl.previousNominalization();
return result;
}
public ArrayList< Pair<String,Float> > clickVerb(int pos) {
ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
TextController tCntrl = TextController.getInstance();
result = tCntrl.clickNominalization(pos);
return result;
}
public ArrayList<Integer> getPositions () {
TextController tCntrl = TextController.getInstance();
return tCntrl.getPositions();
}
public void increasePreference (int index) {
TextController tCntrl = TextController.getInstance();
tCntrl.favoriteNominalization(index);
}
//FILE
public String open() {
FileControllerDomain fCntrl = new FileControllerDomain();
JTextArea aux = new JTextArea();
try {
fCntrl.openFile(aux);
}
catch(Exception e) {
}
return aux.getText();
}
public void save(JTextArea text) {
FileControllerDomain fCntrl = new FileControllerDomain();
try {
fCntrl.saveFile(text);
}
catch(Exception e) {
}
}
//CONTEXT
public void saveContext(String text, boolean nominalized) {
TextController tCntrl = TextController.getInstance();
_contextNominalized = nominalized;
if (_contextNominalized) _contextPosition = tCntrl.getCurrentNominalizationPosition();
else tCntrl.enterTextOnly(text);
}
public boolean getContextNominalized() {
return _contextNominalized;
}
public int getContextCurrentPosition() {
return _contextPosition;
}
public int getCurrentPosition() {
TextController tCntrl = TextController.getInstance();
return tCntrl.getCurrentNominalizationPosition();
}
public String getText() {
TextController tCntrl = TextController.getInstance();
return tCntrl.getText();
}
public boolean getFirstXinxeto () {
return _firstXinxeto;
}
// GOTO
public void goToRegister() {
_state = 1;
JPanel register = new RegisterView(this);
changeScreen (register);
}
public void goToPreferences() {
_state = 3;
JPanel preferences = new PreferencesView(this);
changeScreen (preferences);
}
public void goToStatistics() {
_state = 5;
JPanel statistics = new StatisticsView(this);
changeScreen (statistics);
}
public void goToLogin () {
_state = 0;
JPanel login = new StartView (this);
changeScreen(login);
}
public void goToEdit () {
_state = 2;
JPanel edit = new EditorView (this);
_firstXinxeto = false;
changeScreen(edit);
}
public void goToHelp() {
_state = 4;
Browser b = new Browser();
}
public float getG() {
PreferencesController aux = PreferencesController.getInstance();
float g = aux.getG(_id);
return g;
}
public int getN() {
PreferencesController aux =PreferencesController.getInstance();
int n = aux.getN(_id);
return n;
}
public int getS() {
PreferencesController aux = PreferencesController.getInstance();
int s = aux.getS(_id);
return s;
}
public void setG(float g) {
PreferencesController aux = PreferencesController.getInstance();
//System.out.println("ID: "+_id+", G: "+g);
aux.setG(_id,g);
}
public void setN(int n) {
PreferencesController aux = PreferencesController.getInstance();
aux.setN(_id,n);
}
public void setS(int s) {
PreferencesController aux = PreferencesController.getInstance();
aux.setS(_id,s);
}
public int getTotalWordsUser() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getTotalWordsUser();
}
public int getMarkedWordsUser() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getMarkedWordsUser();
}
public int getNominalizedTexts() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getNominalizedTexts();
}
public int getLearntRulesUser() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getLearntRulesUser();
}
public int getLearntPairsUser() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getLearntPairsUser();
}
public int getAddedVerbs() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getAddedVerbs();
}
public double getVerbsMean() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getVerbsMean();
}
////TEXT
public int getTotalWordsText() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getTotalWordsText();
}
public int getMarkedWordsText() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getMarkedWordsText();
}
public String getMostCommonVerb() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getMostCommonVerb();
}
public int getLearntRulesText() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getLearntRulesText();
}
public int getLearntPairsText() {
StatisticDomainController sc = new StatisticDomainController();
return sc.getLearntPairsText();
}
public void addVerbToDict(String s) {
if(_id != 0) {
UserDomainController ucd = UserDomainController.getInstance();
ucd.addVerbToDict(s, _id);
}
}
}