/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (C) 2011-2012 Marchand Eric <ricoh51@free.fr>
This file is part of Freegressi.
Freegressi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Freegressi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Freegressi. If not, see <http://www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package freegressi.tableur;
import freegressi.parser.Lexer;
import freegressi.parser.Parser;
import freegressi.parser.Verificateur;
import freegressi.tableur.SpreadSheets.ActiveSheetChangedEvent;
import freegressi.tableur.SpreadSheets.SheetAddedEvent;
import freegressi.tableur.SpreadSheets.SheetDeletedEvent;
import freegressi.tableur.SpreadSheets.SheetDescriptionChangedEvent;
import freegressi.tableur.SpreadSheets.SheetsAngleChangedEvent;
import freegressi.tableur.SpreadSheets.SheetsCSChangedEvent;
import freegressi.tableur.SpreadSheets.SheetsColumnAddedEvent;
import freegressi.tableur.SpreadSheets.SheetsColumnDeletedEvent;
import freegressi.tableur.SpreadSheets.SheetsColumnModifiedEvent;
import freegressi.tableur.SpreadSheets.SheetsColumnMovedEvent;
import freegressi.tableur.SpreadSheets.SheetsColumnSortedEvent;
import freegressi.tableur.SpreadSheets.SheetsColumnsEditedEvent;
import freegressi.tableur.SpreadSheets.SheetsListener;
import freegressi.tableur.SpreadSheets.SheetsRenamedEvent;
import freegressi.utils.Utils;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
public class JPanelCalc extends javax.swing.JPanel implements SheetsListener {
private Noeud noeudExpression;
private Tableur tableur;
private int nombreCS = SpreadSheets.getInstance().getCS();
private GrecsAction grecsAction = new GrecsAction(null,
new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/insertsymbol_26x26.png")),
"Insérer un caractère grec", 0);
private FunctionsAction functionsAction = new FunctionsAction(null,
new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/insertformula_26x26.png")),
"Choisir une fonction", 0);
/** Creates new form JPanelCalc */
public JPanelCalc() {
initComponents();
jbGreek.setAction(grecsAction);
jbFunc.setAction(functionsAction);
}
private double evalueExpression(String str, int index) {
ArrayList<Noeud> liste = new ArrayList<Noeud>();
//tableur = SpreadSheets.getInstance().getActiveSheet();
if (tableur == null) {
//System.err.println("Aucun tableur");
tableur = new Tableur();
//return Double.NaN;
}
//System.out.println(str +" : "+ valeur);
//liste.clear();
try {
Lexer lex = new Lexer(new java.io.StringReader(str), liste);
lex.yylex();
} catch (Exception e) {
System.err.println("Erreur du lexer!");
}
Parser parser = new Parser(liste);
Noeud racine = parser.parser();
if (racine == null) {
//System.out.println("Résultat = null : " + str);
//return Double.NaN;
return tableur.calcule(racine, index);
} else {
if (racine.getType() == Sym.ERROR) {
System.err.println("Erreur du lexer à la colonne : " + (racine.getColonne() + 1));
return Double.NaN;
} else if (racine.getType() == Sym.PARSER_ERROR) {
System.err.println("Erreur du parser à la colonne : " + (racine.getColonne() + 1));
//racine.getRapport();
return Double.NaN;
} else {
//System.out.println("Résultat = "+racine.getResultat());
//racine.getRapport();
}
}
return tableur.calcule(racine, index);
}
@Override
public void activeSheetChanged(ActiveSheetChangedEvent event) {
tableur = event.getNewActiveSheet();
jtfsKeyReleased(null);
}
@Override
public void sheetAdded(SheetAddedEvent event) {
}
@Override
public void sheetDeleted(SheetDeletedEvent event) {
}
@Override
public void sheetRenamed(SheetsRenamedEvent event) {
}
@Override
public void columnAdded(SheetsColumnAddedEvent event) {
jtfsKeyReleased(null);
}
@Override
public void columnDeleted(SheetsColumnDeletedEvent event) {
jtfsKeyReleased(null);
}
@Override
public void angleChanged(SheetsAngleChangedEvent event) {
jtfsKeyReleased(null);
}
@Override
public void columnModified(SheetsColumnModifiedEvent event) {
jtfsKeyReleased(null);
}
@Override
public void columnMoved(SheetsColumnMovedEvent event) {
}
@Override
public void columnSorted(SheetsColumnSortedEvent event) {
jtfsKeyReleased(null);
}
@Override
public void columnsEdited(SheetsColumnsEditedEvent event) {
jtfsKeyReleased(null);
}
@Override
public void sheetDescriptionChanged(SheetDescriptionChangedEvent event) {
}
@Override
public void sheetsCSChanged(SheetsCSChangedEvent event) {
nombreCS = event.getNewCS();
jtfsKeyReleased(null);
}
/**
* La classe qui permet d'insérer un caractère grec
*/
class GrecsAction extends AbstractAction {
public GrecsAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
Utils.insertGreekCaracter(jtfs);
// Vérifier l'expression
jtfsKeyReleased(null);
}
}
/**
* La classe qui permet d'insérer un caractère grec
*/
class FunctionsAction extends AbstractAction {
public FunctionsAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
Utils.insertFunction(jtfs);
jtfsKeyReleased(null);
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jToolBar1 = new javax.swing.JToolBar();
jbGreek = new javax.swing.JButton();
jbFunc = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jtfs = new beans.JTextFieldSouligne();
jlErreur = new beans.JLabelRouge();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
jbGreek.setText("jbGreek");
jbGreek.setFocusable(false);
jbGreek.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jbGreek.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jbGreek);
jbFunc.setText("jbFunc");
jbFunc.setFocusable(false);
jbFunc.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jbFunc.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jToolBar1.add(jbFunc);
jLabel1.setText("Entrez une expression :");
jtfs.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
jtfsKeyReleased(evt);
}
});
jlErreur.setText(" ");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addContainerGap(223, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jtfs, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jlErreur, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jtfs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jlErreur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void jtfsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfsKeyReleased
String exp = jtfs.getText();
tableur = SpreadSheets.getInstance().getActiveSheet();
noeudExpression = Verificateur.parseExpression(tableur, exp, "");
if (noeudExpression == null) {
jtfs.effaceTout();
jlErreur.setText("");
return;
}
if (noeudExpression.getType() == Sym.ERROR
|| noeudExpression.getType() == Sym.PARSER_ERROR
|| noeudExpression.getType() == Sym.PARSER_COHERENCE_ERROR) {
jtfs.souligne(noeudExpression.getColonne(), noeudExpression.getColonne() + 1);
jlErreur.setText(noeudExpression.getTexteErreur());
} else {
jtfs.effaceTout();
jlErreur.setText("");
double result = evalueExpression(exp, 0);
String strResult = Utils.formatteNombre(result, nombreCS);
String str = exp + " = " + strResult;
jTextArea1.insert(str + "\n", 0);
jTextArea1.select(0, str.length());
}
}//GEN-LAST:event_jtfsKeyReleased
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JButton jbFunc;
private javax.swing.JButton jbGreek;
private beans.JLabelRouge jlErreur;
private beans.JTextFieldSouligne jtfs;
// End of variables declaration//GEN-END:variables
}