1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;
/**
* Titre : N0tepad
* Description : éditeur de texte
* Copyright : Copyright n0strat0un(c) 2002
* Société : wesh wesh la nôtre !!
* @author : n0stra & t0un
* @version 1.1
*/public class N0tepad extends JPanel
{
static JFrame frame;
static JTextArea jta;
static JFileChooser fc;
static File file;
JMenuBar jmb;
JMenu jm1;
JMenu jm2;
JMenu jm3;
JMenu jm4;
static JMenuItem jmi5;
JMenuItem jmi1;
JMenuItem jmi2;
JMenuItem jmi3;
JMenuItem jmi4;
JMenuItem jmi6;
JMenuItem jmi7;
JMenuItem sjm1;
JMenuItem sjm2;
JCheckBoxMenuItem jmi8;
JMenuItem jmi9;
JMenuItem jmi10;
JMenuItem jmi11;
JMenuItem jmi12;
//JCheckBoxMenuItem jmi13;
String aRecherché;
static String nomFenetre = "Sans Titre - N0tepad 1.1";
final static String docHTML = new String("<html>\n<head>\n<title> </title>\n<meta name=\"author\" content=\" \">\n<meta name=\"description\" content=\" \">\n<meta name=\"language\" content=\"FR\">\n<meta name=\"copyright\" content=\" \">\n<meta http-equiv=\"reply-to\" content=\" \">\n</head>\n\n<body background=\" \" bgcolor=\"black\" link=\"gray\" vlink=\"gray\" alink=\"darkgray\" text=\"darkgray\">\n\n\n</body>\n</html>");
Icon nouvo = new ImageIcon("icons/new.gif");
Icon open = new ImageIcon("icons/open.gif");
Icon save = new ImageIcon("icons/save.gif");
static Icon quit = new ImageIcon("icons/fee.gif");
static boolean sauvé = false;
//static JCheckBoxMenuItem[] tab_fich_recents = new JCheckBoxMenuItem[5];
//static int index_tab = 0;
/**
*
* constructeur de n0tepad
*
*/ public N0tepad()
{
//création de la barre de menus
jmb = new JMenuBar();
jm1 = new JMenu("\u2D30 \u2D31");
jm3 = new JMenu("\u2D31");
jm4 = new JMenu("\u2D31");
jm2 = new JMenu("?");
jmi1 = new JMenu("\u2D31");
sjm1 = new JMenuItem("\u2D31", nouvo);
sjm2 = new JMenuItem("\u2D31", nouvo);
jmi2 = new JMenuItem("\u2D31..", open);
jmi3 = new JMenuItem("\u2D30", save);
jmi4 = new JMenuItem("\u2D31 \u2D30..", save);
jmi5 = new JMenuItem("\u2D31");
jmi6 = new JMenuItem("\u2D31..");
jmi7 = new JMenuItem("\u2D31");
jmi10 = new JMenuItem("\u2D31");
jmi11 = new JMenuItem("\u2D31");
jmi12 = new JMenuItem("\u2D31");
jmi8 = new JCheckBoxMenuItem("\u2D31 \u2D31");
jmi9 = new JMenuItem("\u2D31..");
jmi1.add(sjm1);
jmi1.add(sjm2);
jmb.add(jm1);
jmb.add(jm3);
jmb.add(jm4);
jmb.add(jm2);
jm1.add(jmi1);
jm1.add(jmi2);
jm1.add(jmi3);
jm1.add(jmi4);
jm1.addSeparator();
jm1.add(jmi5);
//jm1.addSeparator();
//jm1.add(jmi13);
jm2.add(jmi6);
jm3.add(jmi10);
jm3.add(jmi11);
jm3.add(jmi12);
jm3.addSeparator();
jm3.add(jmi7);
jm3.addSeparator();
jm3.add(jmi8);
jm4.add(jmi9);
//initialisation de la boite d'ouverture
fc = new JFileChooser();
this.setLayout(new BorderLayout(5, 5));
//création de la zone d'affichage de texte
jta = new JTextArea(30, 60);
jta.setEditable(true);
//création du panneau "ajustable"
JScrollPane jsp = new JScrollPane(jta);
jsp.setBorder(BorderFactory.createLineBorder(Color.darkGray, 3));
//ajout d'auditeurs aux boutons
/**Sélectionner tout**/
jmi7.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.selectAll();
}
});
/**retour à la ligne auto**/
jmi8.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (jmi8.getState() == true) jta.setLineWrap(true);
else jta.setLineWrap(false);
}
});
/**nouveau document texte**/
sjm1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.selectAll();
jta.replaceSelection("");
file = new File("sanstitre.txt");
String nomFrame = nomFenetre;
frame.setTitle(nomFrame);
frame.repaint();
sauvé = false;
}
});
/**nouveau document HTML**/
sjm2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.selectAll();
jta.replaceSelection(docHTML);
file = new File("sanstitre.html");
String nomFrame = nomFenetre;
frame.setTitle(nomFrame);
frame.repaint();
sauvé = false;
}
});
/**ouverture**/
jmi2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int choix = fc.showOpenDialog(frame); //affichage d'une fenêtre "d'ouverture"
if(choix == fc.APPROVE_OPTION)
{
file = fc.getSelectedFile(); //récupère le fichier sélectionné
ouverture(file);
String nomFrame = file.getName()+" - N0tepad 1.1";
frame.setTitle(nomFrame);
/*if(index_tab == 5)
{
index_tab = 0;
tab_fich_recents[index_tab] = new JCheckBoxMenuItem(file.getName());
jmi13 = tab_fich_recents[index_tab];
/**fichiers recents**/ /*jmi13.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jmi13.isSelected())
{
File fichier = new File(jmi13.getName());
ouverture(fichier);
}
}
});
index_tab += 1;
jm1.add(jmi13);
}
else
{
tab_fich_recents[index_tab] = new JCheckBoxMenuItem(file.getName());
jmi13 = tab_fich_recents[index_tab];
/**fichiers recents**/ /*jmi13.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jmi13.getState() == true)
{
File fichier = new File(jmi13.getText());
ouverture(fichier);
}
}
});
index_tab += 1;
jm1.add(jmi13);
}*/ }
frame.repaint();
}
});
/**copier**/
jmi10.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.copy();
frame.repaint();
}
});
/**couper**/
jmi11.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.cut();
frame.repaint();
}
});
/**coller**/
jmi12.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jta.paste();
frame.repaint();
}
});
/**sauvegarder**/
jmi3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (file == null)
{
int choix = fc.showSaveDialog(frame); //affichage d'une fenêtre "de fermeture"
if(choix == fc.APPROVE_OPTION)
{
File fichier = fc.getSelectedFile(); //récupère le fichier sélectionné
sauvegarde(fichier);
}
}
else sauvegarde(file);
frame.repaint();
}
});
/**sauvegarder sous**/
jmi4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int choix = fc.showSaveDialog(frame); //affichage d'une fenêtre "de fermeture"
if(choix == fc.APPROVE_OPTION)
{
File fichier = fc.getSelectedFile(); //récupère le fichier sélectionné
sauvegarde(fichier);
}
frame.repaint();
}
});
/**à propos**/
jmi6.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "N0tepad 1.1\nEditeur de texte en Java\n\nversion 1.1", "A propos de N0tepad 1.1", -1);
}
});
/**rechercher**/
jmi9.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file != null)
{
Recherche recherche = new Recherche(file);
}
}
});
//mise en place des panneaux intermédiaires
this.add(jsp, BorderLayout.CENTER);
this.add(jmb, BorderLayout.NORTH);
}
/**
*
* méthode qui met en place les marges
*
* @return insets la valeur des marges(h, b, g, d)
*/ public Insets getInsets()
{
return new Insets(5, 5, 5, 5);
}
/**
*
* méthode d'affichage du fichier à ouvrir
*
* @param f fichier à ouvrir
*
*/ public void ouverture(File f)
{
//efface la zone de texte
jta.selectAll();
jta.replaceSelection("");
try
{
//tampon de lecture
BufferedReader bfreader = new BufferedReader(new FileReader(f));
//lecture d'une ligne
String s = bfreader.readLine();
while(s != null)
{
//affichage de la ligne lue
jta.append(s+"\n");
//lire une nouvelle ligne
s = bfreader.readLine();
}
//fermeture du tampon
bfreader.close();
sauvé = false;
}
catch(Exception e)
{
System.err.println("ba ya blême");
}
}
/**
*
* méthode d'écriture du fichier
*
* @param f fichier à écrire
*
*/ public static void sauvegarde(File f)
{
try
{
//tampon d'écriture
BufferedWriter bfwriter = new BufferedWriter(new FileWriter(f));
/* écriture du texte (jta.getText()) à partir du rang 0
jusqu'à (jta.getText().length()), longueur du texte à copier */ bfwriter.write(jta.getText(), 0, jta.getText().length());
//fermeture du tampon d'écriture
bfwriter.close();
sauvé = true;
}
catch(Exception e)
{
System.err.println("ba ya blême");
}
}
/**
*
* méthode principale
*
*/ public static void main(String[] args)
{
//création de la fenêtre d'affichage du n0tepad
frame = new JFrame(nomFenetre);
frame.setResizable(true);
//création du n0tepad
N0tepad essai = new N0tepad();
//ajout du n0tepad à la fenêtre
frame.getContentPane().add(essai);
//ajout d'un auditeur de fermeture de la fenêtre
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if ((file != null) && (!sauvé))
{
int saveOrNot = JOptionPane.showConfirmDialog(new JFrame(), "Quitter sans sauvegarder?", "Quitter", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, quit);
if(saveOrNot == JOptionPane.YES_OPTION) System.exit(0);
if(saveOrNot == JOptionPane.NO_OPTION)
{
int choix = fc.showSaveDialog(frame); //affichage d'une fenêtre "de fermeture"
if(choix == fc.APPROVE_OPTION)
{
File fichier = fc.getSelectedFile(); //récupère le fichier sélectionné
sauvegarde(fichier);
}
frame.repaint();
System.exit(0);
}
if(saveOrNot == JOptionPane.CANCEL_OPTION) ;
}
else System.exit(0);
}
});
/**quitter**/
jmi5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if ((file != null) && (!sauvé))
{
int saveOrNot = JOptionPane.showConfirmDialog(new JFrame(), "Quitter sans sauvegarder?", "Quitter", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, quit);
if(saveOrNot == JOptionPane.YES_OPTION) System.exit(0);
if(saveOrNot == JOptionPane.NO_OPTION)
{
int choix = fc.showSaveDialog(frame); //affichage d'une fenêtre "de fermeture"
if(choix == fc.APPROVE_OPTION)
{
File fichier = fc.getSelectedFile(); //récupère le fichier sélectionné
sauvegarde(fichier);
}
frame.repaint();
System.exit(0);
}
if(saveOrNot == JOptionPane.CANCEL_OPTION) ;
}
else System.exit(0);
}
});
//affichage de la fenêtre et donc du n0tepad
frame.pack();
frame.setVisible(true);
}
} |
Partager