Package freegressi.tableur

Source Code of freegressi.tableur.JDialogEditColumns$GreeksAction

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    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.tableur.EditColumnsModel.ColumnsChangedEvent;
import freegressi.tableur.EditColumnsModel.EditColumnsListener;
import freegressi.tableur.EditColumnsModel.EditState;
import freegressi.utils.Utils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
*
* @author marchand
*/
public class JDialogEditColumns extends JDialog implements ActionListener,
        EditColumnsListener, DocumentListener, CaretListener{

  private JButton jbOk, jbAnnuler;
  private JColorPane colorPane;
  private boolean ok = false;
  private JLabel jlErreur;
  private int caretPosition;
  private boolean mustUpdate = true;

  private GreeksAction greeksAction = new GreeksAction(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);
  /**  */
  private UndoAction undoAction = new UndoAction("",
          new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/undo_26x26.png")),
          "Undo", 0);
  /**  */
  private RedoAction redoAction = new RedoAction("",
          new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/redo_26x26.png")),
          "Redo", 0);
  public JDialogEditColumns(JFrame parent) {
    super(parent, true);
    setTitle("Modifier les variables calculées");
    createUI();
    setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
    pack();
    setLocationRelativeTo(null);
  }

  private void createUI() {
    JToolBar toolbar = new JToolBar();
    toolbar.add(greeksAction);
    toolbar.add(functionsAction);
    toolbar.add(undoAction);
    toolbar.add(redoAction);
    add(toolbar, BorderLayout.PAGE_START);

    colorPane = new JColorPane();
    colorPane.getDocument().addDocumentListener(this);
    colorPane.setPreferredSize(new Dimension(400, 400));
    colorPane.setFont(new Font("Courrier", 0, 20));
    colorPane.setDoubleBuffered(true);
    colorPane.addCaretListener(this);
    colorPane.addKeyListener(new KeyAdapter() {

      @Override
      public void keyPressed(KeyEvent e) {
        if (e.isControlDown()){
          if (e.getKeyCode() == KeyEvent.VK_Z){
            if (undoAction.isEnabled()){
              undoAction.actionPerformed(null);
            }
          }else if (e.getKeyCode() == KeyEvent.VK_Y){
            if (redoAction.isEnabled()){
              redoAction.actionPerformed(null);
            }
          }
        }
      }

    });
    JPanel jpCenter = new JPanel(new BorderLayout());
    jlErreur = new JLabel(" ");
    jlErreur.setPreferredSize(new Dimension(400, 30));
   
    jpCenter.add(jlErreur, BorderLayout.NORTH);
    jpCenter.add(new JScrollPane(colorPane), BorderLayout.CENTER);

    add(jpCenter, BorderLayout.CENTER);
   
    jbOk = new JButton("Ok", new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/Crystal_Clear_app_clean-24.png")));
    jbOk.addActionListener(this);
    jbAnnuler = new JButton("Annuler", new javax.swing.ImageIcon(getClass().getResource("/freegressi/img/Crystal_Clear_action_button_cancel-24.png")));
    jbAnnuler.addActionListener(this);
    JPanel jpBoutons = new JPanel();
    FlowLayout layout2 = new FlowLayout();
    jpBoutons.setLayout(layout2);
    layout2.setAlignment(FlowLayout.RIGHT);
    jpBoutons.add(jbAnnuler);
    jpBoutons.add(jbOk);
    add(jpBoutons, BorderLayout.PAGE_END);
  }


  @Override
  public void columnsChanged(ColumnsChangedEvent event) {
    colorPane.setText(event.getEditString());
  }


  public boolean isOk() {
    return ok;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(jbOk)) {
      ok = true;
      EditColumnsModel.getInstance().okToChange(true, colorPane.getText());
    }else{ // cancel
      EditColumnsModel.getInstance().okToChange(false, colorPane.getText());
    }
    setVisible(false);
  }

  private void updateText(DocumentEvent e){
   
    try {
      String text;
      text = colorPane.getText();
      System.out.println("Caret : " + colorPane.getCaretPosition());
      EditState es = EditColumnsModel.getInstance().createStyledTokens(text,
              caretPosition);
      colorPane.colorize(es.getTokens());

      if (es.getErrorText().equals("")){
        jlErreur.setForeground(Color.gray);
        jlErreur.setText("Tout est Ok :)");
      }else{
        jlErreur.setForeground(Color.red);
        jlErreur.setText(es.getErrorText());
      }
      jbOk.setEnabled(es.isOkState());
      undoAction.setEnabled(es.isUndoState());
      redoAction.setEnabled(es.isRedoState());

    } catch (Exception ex) {
      System.err.println("Exception updateText" + ex);
      return;
    }

  }
  @Override
  public void insertUpdate(DocumentEvent e) {
    caretPosition += e.getLength();
    if (mustUpdate) {
      updateText(e);
    } else{
      mustUpdate = true;
    }
  }

  @Override
  public void removeUpdate(DocumentEvent e) {
    if (mustUpdate) {
      updateText(e);
    }
  }

  @Override
  public void changedUpdate(DocumentEvent e) {
   
  }

  @Override
  public void caretUpdate(CaretEvent e) {
    caretPosition = e.getDot();
  }

  /**
   * La classe qui permet d'insérer un caractère grec
   */
  class GreeksAction extends AbstractAction {

    public GreeksAction(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(colorPane);
    }
  }

  /**
   * La classe qui permet d'insérer une fonction
   */
  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(colorPane);

    }
  }

  private void updateAll(EditState es){
    mustUpdate = false;
    colorPane.setText(es.getText());
    colorPane.colorize(es.getTokens());
    jlErreur.setText(es.getErrorText());
    jbOk.setEnabled(es.isOkState());
    undoAction.setEnabled(es.isUndoState());
    redoAction.setEnabled(es.isRedoState());
    colorPane.setCaretPosition(es.getCaretPosition());
    colorPane.requestFocus();
  }

  /**
   * La classe qui permet d'annuler la dernière action
   */
  class UndoAction extends AbstractAction {

    public UndoAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
      super(text, icon);
      putValue(SHORT_DESCRIPTION, desc);
      putValue(MNEMONIC_KEY, mnemonic);
      setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      EditState es = EditColumnsModel.getInstance().undo();
      updateAll(es);
    }
  }
  /**
   * La classe qui permet de refaire la dernière action
   */
  class RedoAction extends AbstractAction {

    public RedoAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
      super(text, icon);
      putValue(SHORT_DESCRIPTION, desc);
      putValue(MNEMONIC_KEY, mnemonic);
      setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      EditState es = EditColumnsModel.getInstance().redo();
      updateAll(es);
    }

  }


}
TOP

Related Classes of freegressi.tableur.JDialogEditColumns$GreeksAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.