/* Jaxe - Editeur XML en Java Copyright (C) 2002 Observatoire de Paris-Meudon Ce programme est un logiciel libre ; vous pouvez le redistribuer et/ou le modifier conformément aux dispositions de la Licence Publique Générale GNU, telle que publiée par la Free Software Foundation ; version 2 de la licence, ou encore (à votre choix) toute version ultérieure. Ce programme est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ; sans même la garantie implicite de COMMERCIALISATION ou D'ADAPTATION A UN OBJET PARTICULIER. Pour plus de détail, voir la Licence Publique Générale GNU . Vous devez avoir reçu un exemplaire de la Licence Publique Générale GNU en même temps que ce programme ; si ce n'est pas le cas, écrivez à la Free Software Foundation Inc., 675 Mass Ave, Cambridge, MA 02139, Etats-Unis. */ package jaxe.equations; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.BevelBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import jaxe.JaxeDocument; import jaxe.JaxeResourceBundle; import com.keypoint.PngEncoderB; public class DialogueEquation extends JDialog implements ActionListener, DocumentListener { JFrame jframe; boolean valide = false; MathComponent mathcomp; JTextArea zoneTexte; String texteEquation; JPanel epane; JaxeDocument doc; String nomImage; JTextField labelfield = null; String valeurLabel; public DialogueEquation(JaxeDocument doc, String texteEquation, String nomImage) { this(doc, texteEquation, nomImage, null, null); } public DialogueEquation(JaxeDocument doc, String texteEquation, String nomImage, String nomlabel, String valeurLabel) { super(doc.jframe, JaxeResourceBundle.getRB().getString("equation.Equation"), true); this.doc = doc; jframe = doc.jframe; this.texteEquation = texteEquation; this.nomImage = nomImage; this.valeurLabel = valeurLabel; JPanel cpane = new JPanel(new BorderLayout()); setContentPane(cpane); epane = new JPanel(new BorderLayout()); mathcomp = new MathComponent(texteEquation); epane.add(mathcomp, BorderLayout.CENTER); zoneTexte = new JTextArea(texteEquation, 2, 80); zoneTexte.setLineWrap(true); zoneTexte.setWrapStyleWord(true); zoneTexte.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); zoneTexte.getDocument().addDocumentListener(this); epane.add(zoneTexte, BorderLayout.SOUTH); cpane.add(epane, BorderLayout.CENTER); JPanel southpane = new JPanel(new GridLayout((nomlabel==null)?1:2, 1)); if (nomlabel != null) { JPanel lpane = new JPanel(new FlowLayout(FlowLayout.LEFT)); lpane.add(new JLabel(nomlabel)); labelfield = new JTextField(valeurLabel, 30); lpane.add(labelfield); southpane.add(lpane); } JPanel bpane = new JPanel(new FlowLayout(FlowLayout.RIGHT)); JButton boutonAnnuler = new JButton(JaxeResourceBundle.getRB().getString("bouton.Annuler")); boutonAnnuler.addActionListener(this); boutonAnnuler.setActionCommand("Annuler"); bpane.add(boutonAnnuler); JButton boutonOK = new JButton(JaxeResourceBundle.getRB().getString("bouton.OK")); boutonOK.addActionListener(this); boutonOK.setActionCommand("OK"); bpane.add(boutonOK); southpane.add(bpane); cpane.add(southpane, BorderLayout.SOUTH); getRootPane().setDefaultButton(boutonOK); setSize(new Dimension(500, 300)); zoneTexte.requestFocus(); addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent we) { javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { zoneTexte.requestFocus(); } } ); } }); Rectangle r = jframe.getBounds(); setLocation(r.x + r.width/4, r.y + r.height/4); } public boolean afficher() { show(); return(valide); } public String getTexte() { return(texteEquation); } public String getImage() { return(nomImage); } public String getLabel() { return(valeurLabel); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if ("OK".equals(cmd)) actionOK(); else if ("Annuler".equals(cmd)) { valide = false; setVisible(false); } } protected void actionOK() { valide = true; nomImage = creerImage(texteEquation, nomImage, doc); if (labelfield != null) valeurLabel = labelfield.getText(); setVisible(false); } protected void changementTexte() { if (zoneTexte.getText().indexOf('\n') != -1) actionOK(); else { texteEquation = zoneTexte.getText(); majAffichage(); } } public void insertUpdate(DocumentEvent e) { changementTexte(); } public void removeUpdate(DocumentEvent e) { changementTexte(); } public void changedUpdate(DocumentEvent e) { changementTexte(); } protected void majAffichage() { mathcomp.setEquationString(texteEquation); mathcomp.repaint(); } public static String creerImage(String texteEquation, String nomImage, JaxeDocument doc) { // effacement de la précédente if (nomImage != null && !"".equals(nomImage)) { File fimg; if (doc.fsave == null) fimg = new File(nomImage); else fimg = new File(doc.fsave.getParent() + File.separatorChar + nomImage); if (fimg.exists() && fimg.isFile()) fimg.delete(); } // reconstruction de l'image MathBase base = new MathBase((new StringMathBuilder(texteEquation)).getMathRootElement()); Dimension dim = new Dimension(base.getWidth(), base.getHeight()); // récupération dans une BufferedImage /*byte[] tr = new byte[256]; byte[] tg = new byte[256]; byte[] tb = new byte[256]; for (int i=0; i<256; i++) { tr[i] = (byte)(255-i); tg[i] = (byte)(255-i); tb[i] = (byte)(255-i); } IndexColorModel cm = new IndexColorModel(8, 256, tr, tg, tb); BufferedImage img2 = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_BYTE_INDEXED, cm); */ // apparemment Java 1.4.1 sur MacOS X ne permet pas l'utilisation de TYPE_BYTE_INDEXED... BufferedImage img2 = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = img2.createGraphics(); g2.setColor(Color.white); g2.fillRect(0, 0, dim.width, dim.height); g2.setColor(Color.black); base.paint(g2); // application masque /* byte[] ta = new byte[256]; for (int i=0; i<256; i++) { tr[i] = (byte)(0); tg[i] = (byte)(0); tb[i] = (byte)(0); ta[i] = (byte)(i); } cm = new IndexColorModel(8, 256, tr, tg, tb, ta); BufferedImage img3 = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_BYTE_INDEXED, cm); int[] rgbArray = new int[dim.width*dim.height]; img2.getRGB(0, 0, dim.width, dim.height, rgbArray, 0, dim.width); for (int i=0; i