package presenter;
import business.ContainerArvores;
import presenter.command.RemoverArvoresCommand;
import presenter.command.AdicionarColunaCommand;
import presenter.command.Command;
import presenter.command.ImportarArvoresCommand;
import presenter.command.SalvarArvoresCommand;
import presenter.command.RemoverColunaArvoreCommand;
import facade.RFacade;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import dao.ProjetoDAO;
import util.ExcelAdapter;
import util.GUIUtils;
import view.EditorView;
import view.states.arvores.Fechado;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.util.Arrays;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.JXTableHeader;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.PatternPredicate;
import org.jdesktop.swingx.error.ErrorInfo;
import view.states.arvores.Remocao;
public final class EditorPresenter implements Observer {
private EditorView view;
private CenarioInventarioTableModel cenarioTM;
private int column;
private ProjetoDAO projetoDAO;
private ContainerArvores cenario;
private RFacade facadeR;
private Command abrirCommand;
public EditorPresenter(final EditorView pView) {
this.view = pView;
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println(e.getCause());
System.out.println(e.getMessage());
e.printStackTrace();
}
});
this.view.getGrid().getTableHeader().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
showPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
showPopup(e);
}
private void showPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
JXTableHeader source = (JXTableHeader) e.getSource();
column = source.columnAtPoint(e.getPoint());
view.getPopupMenu().show(e.getComponent(), e.getX(), e.getY());
}
}
});
createHeaderMenuItens();
projetoDAO = new ProjetoDAO();
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
this.view.getGrid().getInputMap(JTable.WHEN_FOCUSED).put(enter, "selectNextColumnCell");
new ExcelAdapter(view.getGrid());
this.view.getGrid().addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SHIFT || e.getKeyCode() == KeyEvent.VK_CONTROL) {
view.getViewState().selecionaArvore();
}
if (e.getKeyCode() == KeyEvent.VK_DELETE && view.getViewState() instanceof Remocao) {
removeArvore();
}
}
});
this.view.getGrid().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(final MouseEvent e) {
view.getViewState().selecionaArvore();
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
}
});
// view.getBtnFechar().addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(final ActionEvent e) {
// try {
//// Command fecharCommand = new FecharCommand(view, "Fechando o projeto");
// // fecharCommand.execute();
// } catch (Exception ex) {
// ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
// JXErrorPane.showDialog(view, info);
//
// }
// }
// });
//
// abrirCommand = new AbrirCommand(pView, "Abrindo o projeto...");
// view.getBtnAbrir().addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(java.awt.event.ActionEvent evt) {
// view.getViewState().abrirProjeto();
// try {
// if (view.getTxtIdProjeto().getText().isEmpty()) {
// throw new Exception("Informe um projeto");
// } else {
// abrirCommand.execute();
// }
// } catch (Exception ex) {
// ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
// JXErrorPane.showDialog(view, info);
// view.revertToPreviousState();
// }
// }
// });
view.getBtnSalvar().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
view.getViewState().salvaDados();
Command salvarCommand = new SalvarArvoresCommand(pView, "Salvando o projeto...");
salvarCommand.execute();
} catch (Exception ex) {
ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
JXErrorPane.showDialog(view, info);
}
}
});
view.getBtnNovaArvore().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
view.getViewState().novaArvore();
cenarioTM.addLinha();
GUIUtils.selectAndScroll(view.getGrid(), view.getGrid().getRowCount() - 1);
}
});
view.getBtnRemoverArvore().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeArvore();
}
});
view.getBtnCancelar().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
view.getViewState().cancela();
}
});
view.getBtnImportar().addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
Command importarArvores = new ImportarArvoresCommand(pView, "Gravando dados importados...");
importarArvores.execute();
} catch (Exception ex) {
ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
JXErrorPane.showDialog(view, info);
}
}
});
projetoDAO = new ProjetoDAO();
projetoDAO.addObserver(this);
// numberCF2 = new NumberColumnFormat();
view.setState(new Fechado(view));
HighlightPredicate predicate = new PatternPredicate("^-", 1);
ColorHighlighter highlighter = new ColorHighlighter(predicate, null, Color.RED, Color.GRAY, null);
view.getGrid().setHighlighters(highlighter);
}
public void createProjetoTM(ContainerArvores cenario) {
this.cenarioTM = new CenarioInventarioTableModel(cenario, view);
view.getGrid().setModel(cenarioTM);
}
public void createFacadeR(ContainerArvores cenario) {
facadeR = RFacade.getInstance(cenario);
}
public CenarioInventarioTableModel getCenarioTM() {
return cenarioTM;
}
public ContainerArvores getContainerArvores() {
return cenario;
}
public void removeArvore() {
if (JOptionPane.showConfirmDialog(null, "Deseja realmente remover "
+ "a(s) árvore(s): " + Arrays.toString(view.getArvoresSelecionadas().toArray())
+ " ?", "Remover árvore(s)", JOptionPane.OK_CANCEL_OPTION) == 0) {
try {
Command removerArvores = new RemoverArvoresCommand(view, "Removendo árvores...");
removerArvores.execute();
} catch (Exception e) {
System.err.println("Erro ao atualizar " + e.getMessage());
}
}
}
public void setProjeto(ContainerArvores cenario) {
this.cenario = cenario;
}
@Override
public void update(Observable o, Object arg) {
try {
abrirCommand.execute();
} catch (Exception ex) {
ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
JXErrorPane.showDialog(view, info);
}
}
public int getColumn() {
return column;
}
public RFacade getFacadeR() {
return facadeR;
}
public ProjetoDAO getProjetoDAO() {
return projetoDAO;
}
public void createHeaderMenuItens() {
JMenuItem m1 = new JMenuItem("Adicionar");
m1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Command adicionaColuna = new AdicionarColunaCommand(view, "Adicionando coluna...");
adicionaColuna.execute();
} catch (Exception ex) {
ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
JXErrorPane.showDialog(view, info);
}
}
});
JMenuItem m2 = new JMenuItem("Remover");
m2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(null, "Deseja realmente remover a coluna?", "Remover coluna", JOptionPane.OK_CANCEL_OPTION) == 0) {
try {
Command removerColuna = new RemoverColunaArvoreCommand(view, "Removendo coluna...");
removerColuna.execute();
} catch (Exception ex) {
ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
JXErrorPane.showDialog(view, info);
}
}
}
});
view.getPopupMenu().add(m1);
view.getPopupMenu().add(m2);
}
}