/* 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; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GraphicsEnvironment; 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.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.Element; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; import javax.swing.text.StyledDocument; /** * Fenêtre de source XML */ public class SourceFrame extends JFrame implements ActionListener { private JaxeDocument jdoc; private JFrame jframe; private StyledDocument srcdoc; private JTextPane textPane; private Style styleElement; private Style styleNomAttribut; private Style styleValeurAttribut; private Style styleTexte; private Style styleEntite; public SourceFrame(JaxeDocument jdoc, JFrame jframe) { this.jframe = jframe; newdoc(jdoc); } public void newdoc(JaxeDocument jdoc) { this.jdoc = jdoc; Rectangle fr = jframe.getBounds(); setLocation(fr.x + fr.width/2 + 50, fr.y + fr.height/2 + 50); setSize(new Dimension(620, 460)); affichage(); miseAJour(); } protected void affichage() { textPane = new JTextPane(); // disabled horizontal scrolling doesn't work with JEditorPane textPane.setEditable(false); //editorPane.setFont(new Font("Monospaced", Font.PLAIN, 12)); // setFont doesn't work for JTextPane JScrollPane paneScrollPane = new JScrollPane(textPane); paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); JPanel boutonsP = new JPanel(); boutonsP.setLayout(new FlowLayout()); JButton boutonMAJ = new JButton(JaxeResourceBundle.getRB().getString("source.MiseAJour")); boutonMAJ.addActionListener(this); boutonsP.add(boutonMAJ); JPanel contentPane = new JPanel(new BorderLayout()); contentPane.add(paneScrollPane, BorderLayout.CENTER); contentPane.add(boutonsP, BorderLayout.NORTH); setContentPane(contentPane); addWindowListener( new WindowAdapter() { public void windowActivated(WindowEvent e) { textPane.requestFocus(); // gnaaaargh ah ah ! je l'ai eu mon focus ! 8¬> } }); } public void miseAJour() { if (jdoc.fsave == null) { JOptionPane.showMessageDialog(jframe, JaxeResourceBundle.getRB().getString("source.SauverAvant"), JaxeResourceBundle.getRB().getString("erreur.Erreur"), JOptionPane.ERROR_MESSAGE); return; } try { BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(jdoc.fsave), jdoc.encodage)); textPane.read(in, null); } catch (IOException ex) { System.err.println("IOException: " + ex.getMessage()); return; } srcdoc = (StyledDocument)textPane.getDocument(); // Monaco font looks much better than the default Courier on MacOS X with Java 1.4.1 String[] fontnames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); boolean trouv = false; for (int i=0; i'); if (ps != -1) { srcdoc.setCharacterAttributes(debl+pl, ps+1, styleElement, false); int attpos = debl+pl+1; String subatt = sub.substring(1, ps); int pesp = subatt.indexOf(' '); while (pesp != -1) { subatt = subatt.substring(pesp+1); attpos += pesp+1; int peg = subatt.indexOf('='); if (peg != -1) { srcdoc.setCharacterAttributes(attpos, peg, styleNomAttribut, false); attpos += peg+1; subatt = subatt.substring(peg+1); int pq1 = subatt.indexOf('"'); if (pq1 != -1) { int pq2 = subatt.substring(pq1+1).indexOf('"'); if (pq2 != -1) pq2 += pq1+1; if (pq2 != -1) { int pesp2 = subatt.substring(pq2+1).indexOf(' '); if (pesp2 != -1) pesp2 += pq2+1; int finatt; if (pesp2 == -1) finatt = subatt.length(); else finatt = pesp2; srcdoc.setCharacterAttributes(attpos, pq2-pq1+1, styleValeurAttribut, false); pesp = pesp2; } else pesp = -1; } else pesp = -1; } else pesp = -1; } pl += ps + 1; } else pl++; sub = sl.substring(pl); } int pent1 = sl.indexOf('&'); while (pent1 != -1) { int pent2 = sl.substring(pent1+1).indexOf(';'); if (pent2 != -1) pent2 += pent1+1; if (pent2 != -1) { srcdoc.setCharacterAttributes(debl+pent1, pent2-pent1+1, styleEntite, false); pent1 = sl.substring(pent2+1).indexOf('&'); if (pent1 != -1) pent1 += pent2+1; } else pent1 = -1; } } catch (BadLocationException ex) { System.err.println("BadLocationException: " + ex.getMessage()); } } public void colorier() { Element root = srcdoc.getDefaultRootElement(); for (int i=0; i 0) ligne--; else ligne = 0; int pos = srcdoc.getDefaultRootElement().getElement(ligne).getStartOffset(); // bidouille pour afficher la position en haut de la fenêtre try { textPane.scrollRectToVisible(textPane.modelToView(srcdoc.getLength())); textPane.scrollRectToVisible(textPane.modelToView(pos)); } catch (BadLocationException ex) { } } /** * Sélectionne la ligne indiquée (la première ligne a le numéro 1) */ public void selectLigne(int ligne) { if (ligne > 0) ligne--; else ligne = 0; Element ligneel = srcdoc.getDefaultRootElement().getElement(ligne); try { textPane.scrollRectToVisible(textPane.modelToView(ligneel.getStartOffset())); } catch (BadLocationException ex) { } textPane.setCaretPosition(ligneel.getStartOffset()); if (ligneel.getEndOffset() <= srcdoc.getLength()) textPane.moveCaretPosition(ligneel.getEndOffset()); else textPane.moveCaretPosition(srcdoc.getLength()); } }