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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
| import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterException;
import java.io.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import EditorX.ShowImage;
import javax.swing.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
import javax.imageio.ImageIO;
///// Editeur de texte Par Armel Abdelkbir et Rachad yamin
public class Edit extends JFrame implements ActionListener, MouseListener{
JFileChooser jfc;
String name = null;
/**
*
*/
private static final long serialVersionUID = 1L;
/////////////////////////// Instanciation des objets /////////////////////////////
JTextArea text =new JTextArea();
JScrollPane scroll = new JScrollPane(text);
JMenuBar menuBar = new JMenuBar();
JPopupMenu pop = new JPopupMenu();
Border textBorder;
JLabel lign;
JMenuItem couleurFond[];
ButtonGroup groupCouleursFond;
String[] nomCouleur = {"Blanc", "Noir", "Rouge", "Vert", "Bleu", "Jaune", "Orange", "Gris", "Gris light"};
Color[] couleur = {Color.white, Color.black, new Color(150, 0, 0), new Color(0, 150, 0), new Color(0, 0, 150), new Color(225, 225, 0), new Color(175, 100, 0), new Color(100, 100, 100), Color.lightGray};
JMenu clrMenu;
JOptionPane o = new JOptionPane();
/////////Menu Fichier et ses items
JMenu fichier = new JMenu("Fichier");// Un menu fichier et ses items
JMenuItem nouveau = new JMenuItem("Nouveau", new ImageIcon("new.png"));
JMenuItem ouvrir = new JMenuItem("Ouvrir", new ImageIcon("open.png"));
JMenuItem enregistrer = new JMenuItem("Enregistrer", new ImageIcon("save.png"));
JMenuItem enregistrerss = new JMenuItem("Enregistrer Sous", new ImageIcon("saveas.png"));
JMenuItem imprimer = new JMenuItem("Imprimer", new ImageIcon("print.png"));
JMenuItem quitter = new JMenuItem("Quitter", new ImageIcon("quit.png"));
///////Menu Edition et ses items
JMenu edition = new JMenu("Edition");
JMenuItem undoo = new JMenuItem("Undo", new ImageIcon("undo.png"));
JMenuItem images = new JMenuItem("images" , new ImageIcon("image.png"));
JMenuItem redoo = new JMenuItem("Redo", new ImageIcon("redo.png"));
JMenuItem copier = new JMenuItem("copier", new ImageIcon("copier.png"));
JMenuItem couper = new JMenuItem("Couper", new ImageIcon("couper.png"));
JMenuItem coller = new JMenuItem("Coller", new ImageIcon("paste.png"));
JMenuItem supprimer = new JMenuItem("Supprimer");
JMenuItem rechercher = new JMenuItem("Rechercher", new ImageIcon("find.png"));
JMenuItem remplacer = new JMenuItem("Remplacer");
JMenuItem selectionner = new JMenuItem("Selectionner tout", new ImageIcon("select-all.png"));
////////Menu souris
JMenuItem undooo = new JMenuItem("Undo", new ImageIcon("undo.png"));
JMenuItem redooo = new JMenuItem("Redo", new ImageIcon("redo.png"));
JMenuItem copierr = new JMenuItem("copier", new ImageIcon("copier.png"));
JMenuItem couperr = new JMenuItem("Couper", new ImageIcon("couper.png"));
JMenuItem collerr = new JMenuItem("Coller", new ImageIcon("paste.png"));
JMenuItem supprimerr = new JMenuItem("Supprimer");
JMenuItem imagess =new JMenuItem("images" , new ImageIcon("image.png"));
JMenu format = new JMenu("format");//Un menu pour la format du texte
JMenuItem police = new JMenuItem("Police", new ImageIcon("bold.png"));
JMenu apropos = new JMenu("?"); // des informations
JMenuItem information = new JMenuItem("informations");
JToolBar toolBar = new JToolBar();// Un toolbar : des outils a* pour but de ne pas aller jusqu'au menu :
JButton newFile = new JButton(new ImageIcon("new.png"));
JButton openFile = new JButton(new ImageIcon("open.png"));
JButton saveFile = new JButton(new ImageIcon("save.png"));
JButton cutFile = new JButton(new ImageIcon("couper.png"));
JButton copyFile = new JButton(new ImageIcon("copier.png"));
JButton pasteFile = new JButton(new ImageIcon("paste.png"));
JButton rechFile = new JButton(new ImageIcon("find.png"));
JButton undoFile = new JButton(new ImageIcon("undo.png"));
JButton redoFile = new JButton(new ImageIcon("redo.png"));
JButton policeFile = new JButton(new ImageIcon("txt.png"));
JButton selectFile = new JButton(new ImageIcon("select-all.png"));
JButton selectimage= new JButton(new ImageIcon("image.png"));
//// Les panneaux
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//// Declaration des instances globales /////////////
String file = null;
String fileN;
boolean opened = false;
FontSelector font = new FontSelector();
UndoManager undo = new UndoManager();
/////Constructeur de la classe Edit
public Edit(){
/////L'interface de l'editeur
this.setTitle("AR-edit");
this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.getContentPane().setLayout(new BorderLayout());
textBorder = BorderFactory.createBevelBorder(0, Color.BLACK, Color.BLACK);
text.setBorder(BorderFactory.createCompoundBorder(textBorder, BorderFactory.createEmptyBorder(2, 5, 0, 0)));
////////Programme Couleur du fond
clrMenu = new JMenu("Couleur du fond");
clrMenu.setMnemonic('f');
groupCouleursFond = new ButtonGroup();
couleurFond = new JRadioButtonMenuItem[nomCouleur.length];
for (int i = 0; i < nomCouleur.length; i++) {
couleurFond[i] = new JRadioButtonMenuItem(nomCouleur[i]);
if (couleur[i] == text.getBackground())
couleurFond[i].setSelected(true);
couleurFond[i].addActionListener(new ActionListener(){@Override
public void actionPerformed(ActionEvent e){ for (int i = 0; i < nomCouleur.length; i++) {
if (couleurFond[i] == e.getSource())
text.setBackground(couleur[i]);
}}});
groupCouleursFond.add(couleurFond[i]);
clrMenu.add(couleurFond[i]);}
this.getContentPane().add(scroll, BorderLayout.CENTER);
///Menus
this.setJMenuBar(menuBar);
////////////////Fichier//////////////////////////
fichier.add(nouveau);
fichier.add(ouvrir);
fichier.add(enregistrer);
fichier.add(enregistrerss);
fichier.addSeparator();
fichier.add(imprimer);
fichier.addSeparator();
fichier.add(quitter);
////////////////Edition//////////////////////////
edition.add(undoo);
edition.add(redoo);
edition.addSeparator();
edition.add(copier);
edition.add(coller);
edition.add(couper);
edition.add(rechercher);
edition.add(remplacer);
edition.add(images);
edition.addSeparator();
edition.add(selectionner);
edition.add(clrMenu);
////////////////Format//////////////////////////
format.add(police);
////////////////Ã* propos//////////////////////////
apropos.add(information);
////////////////Ajouts des menus//////////////////////////
fichier.setMnemonic(KeyEvent.VK_F);
menuBar.add(fichier);
edition.setMnemonic(KeyEvent.VK_E);
menuBar.add(edition);
format.setMnemonic(KeyEvent.VK_T);
menuBar.add(format);
////////////////Ajouts des items de la souris//////////////////////////
pop.add(undooo);
pop.add(redooo);
pop.addSeparator();
pop.add(copierr);
pop.add(couperr);
pop.add(collerr);
pop.add(supprimerr);
pop.add(imagess);
////////////////Ajouts des items du toolbar//////////////////////////
toolBar.add(newFile);
newFile.setToolTipText("Nouveau");
newFile.setMaximumSize(new Dimension(24, 24));
newFile.setMargin(new Insets(15, 15, 15, 15));
newFile.setFocusable(false);
newFile.setPreferredSize(new Dimension(24, 24));
newFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(openFile);
openFile.setToolTipText("Ouvrir");
openFile.setMaximumSize(new Dimension(24, 24));
openFile.setMargin(new Insets(15, 15, 15, 15));
openFile.setFocusable(false);
openFile.setPreferredSize(new Dimension(24, 24));
openFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(saveFile);
saveFile.setToolTipText("Enregistrer");
saveFile.setMaximumSize(new Dimension(24, 24));
saveFile.setMargin(new Insets(15, 15, 15, 15));
saveFile.setFocusable(false);
saveFile.setPreferredSize(new Dimension(24, 24));
saveFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(cutFile);
cutFile.setToolTipText("Couper");
cutFile.setMaximumSize(new Dimension(24, 24));
cutFile.setMargin(new Insets(15, 15, 15, 15));
cutFile.setFocusable(false);
cutFile.setPreferredSize(new Dimension(24, 24));
cutFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(copyFile);
copyFile.setToolTipText("Copier");
copyFile.setMaximumSize(new Dimension(24, 24));
copyFile.setMargin(new Insets(15, 15, 15, 15));
copyFile.setFocusable(false);
copyFile.setPreferredSize(new Dimension(24, 24));
copyFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(pasteFile);
pasteFile.setToolTipText("coller");
pasteFile.setMaximumSize(new Dimension(24, 24));
pasteFile.setMargin(new Insets(15, 15, 15, 15));
pasteFile.setFocusable(false);
pasteFile.setPreferredSize(new Dimension(24, 24));
pasteFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(selectFile);
selectFile.setToolTipText("selectionner tout");
selectFile.setMaximumSize(new Dimension(24, 24));
selectFile.setMargin(new Insets(15, 15, 15, 15));
selectFile.setFocusable(false);
selectFile.setPreferredSize(new Dimension(24, 24));
selectFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(rechFile);
rechFile.setToolTipText("rechercher");
rechFile.setMaximumSize(new Dimension(24, 24));
rechFile.setMargin(new Insets(15, 15, 15, 15));
rechFile.setFocusable(false);
rechFile.setPreferredSize(new Dimension(24, 24));
rechFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(policeFile);
policeFile.setToolTipText("police");
policeFile.setMaximumSize(new Dimension(24, 24));
policeFile.setMargin(new Insets(15, 15, 15, 15));
policeFile.setFocusable(false);
policeFile.setPreferredSize(new Dimension(24, 24));
policeFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(undoFile);
undoFile.setToolTipText("undo");
undoFile.setMaximumSize(new Dimension(24, 24));
undoFile.setMargin(new Insets(15, 15, 15, 15));
undoFile.setFocusable(false);
undoFile.setPreferredSize(new Dimension(24, 24));
undoFile.setMinimumSize(new Dimension(24, 24));
toolBar.add(selectimage);
selectimage.setToolTipText("images");
selectimage.setMaximumSize(new Dimension(24, 24));
selectimage.setMargin(new Insets(15, 15, 15, 15));
selectimage.setFocusable(false);
selectimage.setPreferredSize(new Dimension(24, 24));
selectimage.setMinimumSize(new Dimension(24, 24));
toolBar.add(redoFile);
redoFile.setToolTipText("redo");
redoFile.setMaximumSize(new Dimension(24, 24));
redoFile.setMargin(new Insets(15, 15, 15, 15));
redoFile.setFocusable(false);
redoFile.setPreferredSize(new Dimension(24, 24));
redoFile.setMinimumSize(new Dimension(24, 24));
toolBar.setFloatable(false);
this.getContentPane().add(toolBar, BorderLayout.NORTH);
////////////Mnémoniques////////////////////////
nouveau.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
ouvrir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
couper.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
imprimer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
selectionner.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
enregistrer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
images.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, KeyEvent.CTRL_MASK));
////////////////Ajoutes ActionListeners////////////////////////
nouveau.addActionListener(this);
ouvrir.addActionListener(this);
enregistrer.addActionListener(this);
enregistrerss.addActionListener(this);
imprimer.addActionListener(this);
quitter.addActionListener(this);
couper.addActionListener(this);
copier.addActionListener(this);
coller.addActionListener(this);
supprimer.addActionListener(this);
rechercher.addActionListener(this);
remplacer.addActionListener(this);
images.addActionListener(this);
selectionner.addActionListener(this);
undoo.addActionListener(this);
redoo.addActionListener(this);
information.addActionListener(this);
police.addActionListener(this);
newFile.addActionListener(this);
openFile.addActionListener(this);
saveFile.addActionListener(this);
copyFile.addActionListener(this);
pasteFile.addActionListener(this);
cutFile.addActionListener(this);
policeFile.addActionListener(this);
undoFile.addActionListener(this);
redoFile.addActionListener(this);
rechFile.addActionListener(this);
selectFile.addActionListener(this);
selectimage.addActionListener(this);
text.addMouseListener(this);
toolBar.setBackground(Color.WHITE);
menuBar.setBackground(Color.WHITE);
undoMan();
////////////////
text.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent e) {
int row = 0;
try {
int length = text.getDocument().getLength();
String txt = text.getDocument().getText(0, length).replaceAll("\r", "");
while (txt.contains("\n")) {
row++;
int start = txt.indexOf("\n");
if (txt.length() - start >= 1) {
txt = txt.substring(start+1, txt.length());
}
}
lign.setText(" Ligne : "+row);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}}
);
panel1.setLayout(new BorderLayout());
lign = new JLabel();
panel1.add(lign, BorderLayout.WEST);
this.add(panel1, BorderLayout.SOUTH);
panel1.setBackground(Color.WHITE);
}
///////////Listeners
public void actionPerformed(ActionEvent e)
{ Component oComponent = (Component) e.getSource();
boolean bSelected = false;
JButton oButton = null;
String sName = oComponent.getName();
if (oComponent instanceof JButton) {
oButton = ((JButton) oComponent);}
///////Les items des menus
//////////////////////////Nouveau///////////////////////////
if(e.getSource()==nouveau ){
opened = false;
int confirm = JOptionPane.showConfirmDialog(null,
"Voulez Vous Enregistrer?",
"Nouveau",
JOptionPane.YES_NO_CANCEL_OPTION);
if( confirm == JOptionPane.YES_OPTION )
{
saveFile();
text.setText(null);
}
else
if( confirm == JOptionPane.CANCEL_OPTION )
{}
else
{
text.setText(null);
}
}
///////////////////images///////////////////////////
if (e.getSource()==images);
FileNameExtensionFilter picture = new FileNameExtensionFilter("JPEG files (*.png)", "png");
jfc.setFileFilter(picture);
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (jfc.showDialog(images, "Insérer")!=JFileChooser.APPROVE_OPTION) return;
name = jfc.getSelectedFile().getAbsolutePath();
// If no text is entered for the file name, refresh the dialog box
if (name==null) return;
{ JFrame frame = new JFrame();
JPanel panel = new ShowImage();
frame.getContentPane().add(panel);
frame.setSize(500, 400);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocation(200, 100);}
//////////////////////////Ouvrir///////////////////////////
if(e.getSource()==ouvrir ){
openFile();
}
//////////////////////////Enregistrer///////////////////////////
if(e.getSource()==enregistrer ){
saveFile();
}
//////////////////////////Enregistrer Sous///////////////////////////
if(e.getSource()==enregistrerss ){
opened = false;
saveFile();
}
//////////////////////////Imprimer///////////////////////////
if(e.getSource()==imprimer ){
try {
text.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
//////////////////////////Quitter///////////////////////////
if(e.getSource()==quitter ){
int confirm = JOptionPane.showConfirmDialog(null,
"Voulez Vous Enregistrer?",
"Quitter",
JOptionPane.YES_NO_CANCEL_OPTION);
if( confirm == JOptionPane.YES_OPTION )
{
saveFile();
dispose();
System.exit(0);
}
else
if( confirm == JOptionPane.CANCEL_OPTION )
{}
else
{
dispose();
System.exit(0);
}
}
//////////////////////////Undo///////////////////////////
if(e.getSource()==undoo ){
try {
undo.undo();
} catch(CannotUndoException cu) {
cu.printStackTrace();
}
}
//////////////////////////Redo///////////////////////////
if(e.getSource()==redoo ){
try {
undo.redo();
} catch(CannotUndoException cur) {
cur.printStackTrace();
}
}
//////////////////////////Couper///////////////////////////
if(e.getSource()==couper ){
text.cut();
}
//////////////////////////Copier///////////////////////////
if(e.getSource()==copier ){
text.copy();
}
//////////////////////////Coller///////////////////////////
if(e.getSource()==coller ){
text.paste();
}
//////////////////////////Selectionner///////////////////////////
if(e.getSource()==selectionner ){
text.selectAll();
}
//////////////////////////Supprimer///////////////////////////
if(e.getSource()==supprimer ){
text.setText("");
}
//////////////////////////Rechercher///////////////////////////
if(e.getSource()==rechercher ){
String mot = o.showInputDialog(null, "Rechercher le mot :", "Rechercher", JOptionPane.QUESTION_MESSAGE);
highligh(text , mot);
}
//////////////////////////Remplacer///////////////////////////
if(e.getSource()==remplacer ){
TextEditFrame m =new TextEditFrame();
add(m);
}
//////////////////////////Police///////////////////////////
if(e.getSource()==police ){
font.setVisible(true);
font.setLocationRelativeTo(null);
font.okButton.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent ae)
{
Font selectedFont = font.returnFont();
text.setFont(selectedFont);
font.setVisible(false);
}
});
font.cancelButton.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent ae)
{
font.setVisible(false);
}
});
}
////////Listeners toolbar
///////////////////////Nouveau///////////////////////////////////////////
if(e.getSource()==newFile ){
opened = false;
int confirm = JOptionPane.showConfirmDialog(null,
"Attention, le document actuel n'a pas été enregistré.Voulez Vous Enregistrer?",
"Nouveau",
JOptionPane.YES_NO_CANCEL_OPTION);
if( confirm == JOptionPane.YES_OPTION )
{
saveFile();
text.setText(null);
}
else
if( confirm == JOptionPane.CANCEL_OPTION )
{}
else
{
text.setText(null);
}}
////////////////////ouvrir////////////////////////
if(e.getSource()==openFile ){
openFile();
}
////////////////////Enregistrer//////////////////////////
if(e.getSource()==saveFile ){
saveFile();
}
if(e.getSource()==copyFile ){
text.copy();
}
if(e.getSource()==pasteFile ){
text.paste();
}
if(e.getSource()==cutFile ){
text.cut();
}
////////////////////undo////////////////////////
if(e.getSource()==undoFile ){
try {
undo.undo();
} catch(CannotUndoException cu) {
cu.printStackTrace();
}
}
////////////////////redo////////////////////////
if(e.getSource()==redoFile ){
try {
undo.redo();
} catch(CannotUndoException cu) {
cu.printStackTrace();
}
}
////////////////////selectionner tout////////////////////////
if(e.getSource()==selectFile ){
text.selectAll();
}
////////////////////rechercher////////////////////////
if(e.getSource()==rechFile ){
String mot = o.showInputDialog(null, "Rechercher le mot :", "Rechercher", JOptionPane.QUESTION_MESSAGE);
highligh(text , mot);
}
////////////////////Police////////////////////////
if(e.getSource()==policeFile ){
font.setVisible(true);
font.setLocationRelativeTo(null);
font.okButton.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent ae)
{
Font selectedFont = font.returnFont();
text.setFont(selectedFont);
font.setVisible(false);
}
});
font.cancelButton.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent ae)
{
font.setVisible(false);
}
});
}
}// la derniere accolades du listeners
public void itemStateChanged(ItemEvent arg0) {
}
/////////Listeners de la Souris
private void checkForPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
pop.show(e.getComponent(), e.getX(), e.getY());
}
}
public void mousePressed(MouseEvent e)
{
checkForPopup( e );
}
public void mouseReleased(MouseEvent e)
{
checkForPopup( e );
}
public void mouseClicked(MouseEvent e) {
removeHighlights(text);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
//////Fonction enregistrer/////////////////////////////
public void saveFile()
{
String line = text.getText();
if(opened==true)
{
try
{
FileWriter output = new FileWriter(file);
BufferedWriter bufout = new BufferedWriter(output);
bufout.write(line, 0, line.length());
JOptionPane.showMessageDialog(null, "Save Successful");
bufout.close();
output.close();
}catch(IOException ioe){ioe.printStackTrace();}
}
else
{
JFileChooser fc = new JFileChooser();
int result = fc.showSaveDialog(new JPanel());
if(result == JFileChooser.APPROVE_OPTION)
{
fileN = String.valueOf(fc.getSelectedFile());
try
{
FileWriter output = new FileWriter(fileN);
BufferedWriter bufout = new BufferedWriter(output);
bufout.write(line, 0, line.length());
JOptionPane.showMessageDialog(null, "Enregistrement réussi");
bufout.close();
output.close();
opened = true;
}catch(IOException ioe){ioe.printStackTrace();}
}
}
}
////////////La fonction Ouvrir
public void openFile()
{
JFileChooser dialogue = new JFileChooser(new File("."));
PrintWriter sortie;
File fichier;
String chaine="";
if (dialogue.showOpenDialog(null)==
JFileChooser.APPROVE_OPTION) {
fichier = dialogue.getSelectedFile();
try {
sortie = new PrintWriter
(new FileWriter(fichier.getPath(), true));
sortie.close();
InputStream ips=new FileInputStream(fichier);
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null){
chaine+=ligne+"\n";
}
text.setText(chaine);
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
////////Fonction Undo
private UndoManager undoMan() {
text.getDocument().addUndoableEditListener(new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
undo.addEdit(e.getEdit());
}
});
return undo;
}
///////////////////Class Remplacer
public class TextEditFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
;
private JTextField fromField = new JTextField(8);
private JTextField toField = new JTextField(8);
public TextEditFrame() {
setTitle("Remplacer");
setSize(250, 100);
setBackground(Color.WHITE);
this.setLocationRelativeTo(null);
Container contentPane = getContentPane();
JPanel panel = new JPanel();
JPanel panel1 = new JPanel();
JButton replaceButton = new JButton("Remplacer");
replaceButton.setBackground(Color.WHITE);
panel1.add(replaceButton);
replaceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String from = fromField.getText();
int start = text.getText().indexOf(from);
if (start >= 0 && from.length() > 0)
text.replaceRange(toField.getText(), start, start
+ from.length());
}
});
panel.add(fromField);
panel.add(new JLabel("Avec"));
panel.add(toField);
contentPane.add(panel1, "North");
contentPane.add(panel, "South");
this.setVisible(true);
panel1.setBackground(Color.WHITE);
panel.setBackground(Color.WHITE);
}}
////////////////Rechercher
class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter{
public MyHighlightPainter(Color color ){super(color);}}
Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.RED);
public void removeHighlights(JTextComponent text ) {
Highlighter hilite = text.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
for (int i=0;i<hilites.length;i++) {
if (hilites[i].getPainter() instanceof MyHighlightPainter ){
hilite.removeHighlight(hilites[i]);}
}
}
public void highligh(JTextComponent text ,String mot){
removeHighlights(text);
try{ Highlighter hilite= text.getHighlighter() ;
Document doc =text.getDocument() ;
String texte = doc.getText(0,doc.getLength());
int pos=0;
while((pos=texte.toUpperCase().indexOf(mot.toUpperCase(),pos))>=0){
hilite.addHighlight(pos,pos+mot.length(),myHighlightPainter);
pos+=mot.length();}
} catch(Exception e){}
}
} |
Partager