/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package presenter;
import expressao.IParser;
import expressao.ParserEquacao;
import expressao.ParserExpressao;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import view.ExpressaoView;
import view.helpers.WindowClose;
/**
*
* @author Helder
*/
public final class ExpressaoPresenter implements Observer {
private ExpressaoView view;
private String exp;
private IParser p;
private void incItemExp(int item) {
String it = null;
String Model, NewModel;
int pos;
int incPos = 0;
switch (item) {
case 0: // Variavel
it = (String) view.getListVariaveis().getSelectedValue();
incPos = it.length();
break;
case 1: // Coeficiente
it = (String) view.getListCoeficientes().getSelectedValue();
incPos = it.length();
break;
case 2: // Operador
it = (String) view.getListOperadores().getSelectedValue();
incPos = it.length();
break;
case 3: // Funcao
it = (String) view.getListFuncoes().getSelectedValue();
it = it.concat("()");
incPos = it.length() - 1;
break;
}
if (it != null) {
Model = view.getTextFieldExp().getText();
pos = view.getTextFieldExp().getCaretPosition();
NewModel = Model.substring(0, pos);
NewModel = NewModel.concat(it);
if (pos <= Model.length()) {
NewModel = NewModel.concat(Model.substring(pos, Model.length()));
}
view.getTextFieldExp().setText(NewModel);
view.getTextFieldExp().setCaretPosition(pos + incPos);
}
}
public ExpressaoPresenter(List<String> var, boolean colCoeficientes, String e) {
int i;
DefaultListModel m = new DefaultListModel();
DefaultListModel m1 = new DefaultListModel();
ListModel m2;
exp = e;
this.view = new ExpressaoView(null);
this.view.setLocationRelativeTo(null);
view.getTextFieldExp().setText(exp);
if (colCoeficientes) {
view.getListCoeficientes().setEnabled(true);
p = new ParserEquacao(var);
} else {
view.getListCoeficientes().setEnabled(false);
m2 = view.getListOperadores().getModel();
for (i = 0; i < m2.getSize(); i++) {
m1.addElement(m2.getElementAt(i));
}
m1.removeElement("=");
view.getListOperadores().setModel(m1);
p = new ParserExpressao(var);
}
for (i = 0; i < var.size(); i++) {
m.addElement(var.get(i));
}
view.getListVariaveis().setModel(m);
view.getBtnConfirmar().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
String erro;
try {
p.expressaoValida(view.getTextFieldExp().getText());
exp = view.getTextFieldExp().getText();
view.setVisible(false);
view.dispose();
} catch (Exception e) {
System.out.println("erro na analise da expressao");
JOptionPane.showMessageDialog(view, e);
}
}
});
view.getBtnCancelar().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
exp = "";
view.setVisible(false);
view.dispose();
}
});
view.getListVariaveis().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
incItemExp(0);
}
});
view.getListOperadores().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
incItemExp(2);
}
});
view.getListCoeficientes().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
incItemExp(1);
}
});
view.getListFuncoes().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
incItemExp(3);
}
});
view.getTextFieldExp().addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
int i;
String exp;
String newExp = "";
exp = view.getTextFieldExp().getText();
i = 0;
while (i < exp.length()) {
if (exp.charAt(i) != 'ß') {
newExp = newExp.concat(exp.substring(i, i + 1).toUpperCase());
} else {
newExp = newExp.concat(exp.substring(i, i + 1));
}
i++;
}
view.getTextFieldExp().setText(newExp);
}
});
view.addWindowListener(new WindowClose(view));
view.setAlwaysOnTop(true);
view.setVisible(true);
}
public String getExpressao() {
return exp;
}
@Override
public void update(Observable o, Object arg) {
throw new UnsupportedOperationException("Not supported yet.");
}
}