package org.llgc; import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import javax.swing.AbstractAction; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.JTextComponent; import javax.swing.text.PlainDocument; public class Swing2 extends JFrame implements ActionListener { private static final long serialVersionUID = -7065885007988227830L; private JTextComponent editor; private Swing2 () { initialize (); } private void initialize () { Container c = getContentPane (); c.setLayout (new BorderLayout ()); setTitle ("Editeur"); setDefaultCloseOperation (DISPOSE_ON_CLOSE); setJMenuBar (createMenu ()); c.add (createToolBar (), BorderLayout.NORTH); editor = createTextComponent (); c.add (new JScrollPane (editor), BorderLayout.CENTER); } private JMenuBar createMenu () { JMenuBar barre_de_menus = new JMenuBar (); JMenu menu_personnel = new JMenu ("Fichier"); JMenuItem item1 = new JMenuItem (new ActionNew ("Nouveau", new ImageIcon ("img/newdoc.png"))); JMenuItem item2 = new JMenuItem (new ActionOpen ("Ouvrir", new ImageIcon ("img/open.png"))); JMenuItem item4 = new JMenuItem (new ActionClose ("Fermer")); barre_de_menus.add (menu_personnel); menu_personnel.add (item1); menu_personnel.add (item2); menu_personnel.addSeparator (); menu_personnel.add (item4); return barre_de_menus; } private JToolBar createToolBar () { JToolBar tool = new JToolBar (); tool.add (new JButton (new ActionNew (new ImageIcon ("img/newdoc.png")))); tool.add (new JButton (new ActionOpen (new ImageIcon ("img/open.png")))); tool.addSeparator (); return tool; } private JTextComponent createTextComponent () { JTextArea txt = new JTextArea (); return txt; } public static void main (String[] args) { Swing2 frm = new Swing2 (); frm.pack (); frm.setVisible (true); } public JTextComponent getEditor () { return editor; } public class ActionNew extends AbstractAction { private static final long serialVersionUID = -9121532899728292588L; public ActionNew (String name, Icon icon) { super (name, icon); } public ActionNew (Icon icon) { super ("", icon); } public ActionNew (String name) { super (name); } @Override public void actionPerformed (ActionEvent e) { getEditor ().setDocument (new PlainDocument ()); } } private class ActionOpen extends AbstractAction { private static final long serialVersionUID = -3137082881796863964L; public ActionOpen (Icon icon) { super ("", icon); } public ActionOpen (String name, Icon icon) { super (name, icon); } @Override public void actionPerformed (ActionEvent e) { JFileChooser chooser = new JFileChooser (); chooser.setFileFilter (new FileNameExtensionFilter ("Fichier txt", "txt")); if (chooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION) { File fileName = chooser.getSelectedFile (); try (BufferedReader br = new BufferedReader ( new InputStreamReader (new FileInputStream (fileName), Charset.forName ("UTF-8")))) { char[] buffer = new char[(int) fileName.length ()]; if (br.read (buffer, 0, (int) fileName.length ()) != fileName.length ()) { throw new IOException ("Erreur lors de la lecture du fichier"); } /* * Document doc = new PlainDocument (); doc.insertString (0, new * String (buffer), null); getEditor ().setDocument (doc); */ getEditor ().setText (new String (buffer)); } catch (IOException e1) { e1.printStackTrace(); return; } } } } private class ActionClose extends AbstractAction { private static final long serialVersionUID = 1813412014719789012L; public ActionClose (String name) { super (name); } @Override public void actionPerformed (ActionEvent e) { dispose (); } } @Override public void actionPerformed (ActionEvent e) { } }