Source Code HTML Editor

package classes;

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.datatransfer.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.EventListener;

import java.beans.*;
import java.net.*;

public class newVersion4 extends JFrame{
   
    String[] iconFiles={"classes/new.gif","classes/open.gif","classes/save.gif","classes/cut.gif","classes/copy.gif","classes/paste.gif","classes/bold.gif","classes/underline.gif","classes/italic.gif","classes/table.gif","classes/image.gif","classes/link.gif"};
    String[] butLabels={"New","Open","Save","Cut","Copy","Paste","Bold","Underline","Italic","Table","Image","Link"};
    ImageIcon[] icons=new ImageIcon[iconFiles.length];
    JButton[] buts=new JButton[butLabels.length];
   
    public ObjectOutputStream output;
    //BufferedReader inputStream;
               
    public File filename;
    public FileWriter outputStream;
    public InputStream inputStream;               
   
    public BufferedReader input;
   
    JEditorPane pane;
    static String title;
   

    private JMenuItem items[];
    private JPopupMenu popupMenu;
    private String[] names={"Undo","Cut","Copy","Paste","Delete","Select All"};
   
    private boolean flag;//de kiem xem da copy chua
    //neu da copy thi moi duoc paste
       
    private Color TextColor;//mau cua text
    private Color BackgroundColor;
   
    private PageFormat mPageFormat;
    //private FilePageRenderer mPageRenderer;
   
    private String clip;//luu trang thai cua text phuc vu cho undo
   
    JMenu  EditMenu;
   
    //Color colorValues[]={Color.black,Color.blue,Color.red,Color.green};
    JRadioButtonMenuItem colorItems[],fonts[];
    JCheckBoxMenuItem styleItems[];
    ButtonGroup fontGroup,colorGroup;
    int style;
    int size=10;
   
    //font ho tro soan thao html
    JMenuItem styleHtmlItems[],familyHtmlItems[];
    JMenuItem sizeHtmlItems[];
    int bien;
    String sizes[]={"10","12","16","24","64"};
   
    //Integer[] kichthuoc={10,12,15,17,20,25,30,35,40,45,50};
    Integer[] kichthuoc={new Integer("10"),new Integer("12"),new Integer("15"),new Integer("17"),new Integer("20"),new Integer("25"),new Integer("30"),new Integer("40"),new Integer("50")};
    JComboBox kichthuocBox;   
   
    String[] htmls={"<B>","<U>","<I>","<FONT>","<IMG>","<A>","<TABLE>"};
    String[] html={"B>","U>","I>","FONT>","IMG>","A>","TABLE>"};
    String[] htmlEnd={"</B>","</U>","</I>","</FONT>","</IMG>","</A>","</TABLE>"};
    JPopupMenu htmlMenu;
    JMenuItem htmlItems[];
   
    private boolean isSaved=false;
   
    private static int i;//bienchay
    JToolBar toolBar;
   
    UIManager ui=new UIManager();
   
   
   
    Bar ap;//thanh tien trinh the hien trong qua trinh mo file
    //kiem tra xem qua trinh mo file da hoan tat chua
    boolean co=false;
   
   
    String[] fileList;//danh sach file trong list liet ke nhung file da mo
    int vitritrongList=0;//luu lai vi tri trong list nhung file da mo
    JList list;//list luu file da mo
    JPanel listPanel;//panel chua list
    boolean flagList;//co bao de khi nguoi dung mo lai 1 file trong list thi ten
                    //file do se khong duoc add vao list them nua
   
    JTextField[] thongtinField;
   
    JTextArea area;
    JSplitPane split;
   
    static int kiemtralansudung;
    public newVersion4(){
        setTitle("New-Editor");
       
        //thiet lap giao dien theo kieu cua window

        try {
           
                ui.setLookAndFeel(UIManager.
                getSystemLookAndFeelClassName());
               
            }
            catch(Exception e) {
                e.printStackTrace();
            }
       
        PrinterJob pj = PrinterJob.getPrinterJob();
        //mPageFormat = pj.defaultPage();
       
        area=new JTextArea();
       
        Container container=getContentPane();
        container.setLayout(new BorderLayout());
       
       
        pane=new JEditorPane();
        pane.setEditable(false);
        pane.setEnabled(false);
        pane.setForeground(Color.black);
        pane.setBackground(Color.white);
        pane.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,25));
        pane.setText("Chuong Trinh soan thao HTML");
       
        //container.add(pane,BorderLayout.CENTER);
        //JScrollPane scroll=new JScrollPane(pane);
        //container.add(scroll);

       
        //thiet lap giao dien cho phan soan thao gom JList,pane va phan duoi JList
        JPanel vungsoanthao=new JPanel();
       
       
        GridBagLayout layout=new GridBagLayout();
        GridBagConstraints constraints=new GridBagConstraints();
        vungsoanthao.setLayout(layout);
       
        constraints.insets=new Insets(5,2,2,2);
       
       
        //thiet lap vung JList
        listPanel=new JPanel();
        listPanel.setLayout(new BorderLayout());
       
        Icon iconList=new ImageIcon("classes/list.gif");
        JLabel labelList=new JLabel(" Danh sach file da duoc mo :      ",iconList,SwingConstants.LEFT);
        labelList.setBackground(Color.white);
        labelList.setForeground(Color.red);
        listPanel.add(labelList,BorderLayout.NORTH);
       
       
        fileList=new String[15];
        //khoi tao cho cac gia tri trong fileList
        for(int i=0;i<fileList.length;i++){
            fileList[i]=" ";
        }
       
        list=new JList(fileList);
        list.setForeground(Color.blue);
        listPanel.add(list,BorderLayout.CENTER);
        listPanel.add(new JScrollPane(list));
        list.setVisibleRowCount(15);
       
        list.addListSelectionListener(
                new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent event){
                        System.out.println("ADASA"+list.getSelectedIndex());
                           
                        if(!fileList[list.getSelectedIndex()].equals(" ")){
                           
                            flagList=true;
                            //kiem tra th chon fileList chua co gia tri thuc chi la " "
                            filename=new File(fileList[list.getSelectedIndex()]);
                                                
                   
                            //tao thread mo file
                            mofile thread1=new mofile();
       
                            //tao thread chay tien trinh
                               Thread thread2=new Thread(ap);
           
           
                              thread1.start();
                               thread2.start();
                          
                        }
                    }
                }   
            );
               
       
        //kiem tra neu nhu chua co file nao duoc mo thi khong cho thao tac tren list
        if(fileList[0]==" "){
            list.setEnabled(false);

        }
        else{
            //truong hop trong list da co file-->tuc la da co file duoc mo
            list.addListSelectionListener(
                new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent event){
                       
                        if(!fileList[list.getSelectedIndex()].equals(" ")){
                            //kiem tra th chon fileList chua co gia tri thuc chi la " "
                            System.out.println("da GOI");
                            filename=new File(fileList[list.getSelectedIndex()]);
                                                
                   
                            //tao thread mo file
                            mofile thread1=new mofile();
       
                            //tao thread chay tien trinh
                               Thread thread2=new Thread(ap);
           
           
                              thread1.start();
                               thread2.start();
                          
                        }
                    }
                }   
            );
        }
               
       
        //thiet lap cho vung phia duoi JList
        JPanel pa1=new JPanel();
        pa1.setLayout(new BorderLayout());
       
        Icon icon=new ImageIcon("classes/file.gif");
        JLabel l=new JLabel(" Thong tin File :         ",icon,SwingConstants.LEFT);
        l.setBackground(Color.white);
        l.setForeground(Color.red);
        pa1.add(l,BorderLayout.NORTH);
       
        JPanel pa2=new JPanel(new GridLayout(4,2));
        String[] tenthongtin={"Ten File","Tong so dong","Dong con tro hien hanh","Cot con tro hien hanh"};
        JLabel[] thongtinLabel=new JLabel[tenthongtin.length];
        thongtinField=new JTextField[tenthongtin.length];

       
        for(int i=0;i<tenthongtin.length;i++){
            thongtinLabel[i]=new JLabel(tenthongtin[i]);
            thongtinLabel[i].setForeground(Color.blue);
            pa2.add(thongtinLabel[i]);
            thongtinField[i]=new JTextField(10);
            pa2.add(thongtinField[i]);
            thongtinField[i].setEditable(false);
        }
       
        JPanel pa3=new JPanel();
        Icon ic=new ImageIcon("classes/html.gif");
        JLabel la=new JLabel(ic);
        pa3.add(la,BorderLayout.CENTER);
       
        JPanel pa=new JPanel(new BorderLayout());
        pa.add(pa1,BorderLayout.NORTH);
        pa.add(pa2,BorderLayout.CENTER);
        pa.add(pa3,BorderLayout.SOUTH);
       
        thongtinField[0].setText("Gioi thieu");
       

       
       
        JPanel pn=new JPanel(new BorderLayout());
        pn.add(listPanel,BorderLayout.CENTER);
        pn.add(pa,BorderLayout.NORTH);
       
        constraints.fill=GridBagConstraints.BOTH;
        constraints.gridx=0;
        constraints.gridy=0;//doi 1
        constraints.weighty=3;
        constraints.weightx=1;
       
        constraints.gridheight=3;
        constraints.gridwidth=1;
        layout.setConstraints(pn,constraints);

               
        JPanel pp=new JPanel(new BorderLayout());
        pp.add(pane,BorderLayout.CENTER);   
        JScrollPane sc=new JScrollPane(pane);
        pp.add(sc);
       
        constraints.fill=GridBagConstraints.BOTH;
        constraints.gridx=2;
        constraints.gridy=0;
        constraints.weightx=30;//chi can sua o day la thay doi ti le nhien ban dau la 10
        constraints.gridheight=3;//doi 2
        constraints.gridwidth=3;
        layout.setConstraints(pp,constraints);

        split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        split.setContinuousLayout(true);
        split.setOneTouchExpandable(true);
        split.setAutoscrolls(true);
       
       
        split.setLeftComponent(pn);
        split.setRightComponent(pp);
       
        constraints.fill=GridBagConstraints.BOTH;
        constraints.gridx=1;
        constraints.gridy=0;
        constraints.weightx=30;//chi can sua o day la thay doi ti le nhien ban dau la 10
        constraints.gridheight=3;//doi 2
        constraints.gridwidth=1;
        layout.setConstraints(split,constraints);
       
        vungsoanthao.add(split);
       
       
        container.add(vungsoanthao,BorderLayout.CENTER);
       
       
       
       
        //xu ly su kien thao tac voi html
        htmlMenu=new JPopupMenu();
        htmlItems=new JMenuItem[7];
        for(i=0;i<htmlItems.length;i++){
            htmlItems[i]=new JMenuItem(htmls[i]);
           
                   
            htmlItems[i].setForeground(Color.BLUE);
            htmlMenu.add(htmlItems[i]);
           
        }
       
        //xu ly <b>
        htmlItems[0].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.out.println(htmls[0]);
                    pane.replaceSelection(html[0]+"   "+htmlEnd[0]);//can phai bo < o dau vi da co san
                    //do do phai dung lop html khong dung htmls   
                   
                       
                }
            }   
        );
       
        //xu ly <u>
        htmlItems[1].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.out.println(htmls[1]);
                    pane.replaceSelection(html[1]+"    "+htmlEnd[1]);//can phai bo < o dau vi da co san
                    //do do phai dung lop html khong dung htmls   

                       
                }
            }   
        );
       
        //xu ly <i>
        htmlItems[2].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.out.println(htmls[2]);
                    pane.replaceSelection(html[2]+"   "+htmlEnd[2]);//can phai bo < o dau vi da co san
                    //do do phai dung lop html khong dung htmls   
                   
                       
                }
            }   
        );
       
        //xu ly <font>
        htmlItems[3].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.out.println(htmls[3]);
                    pane.replaceSelection(html[3]+"   "+htmlEnd[3]);//can phai bo < o dau vi da co san
                    //do do phai dung lop html khong dung htmls   
                   
                       
                }
            }   
        );
       
        //xu ly <img>
        htmlItems[4].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.out.println(htmls[4]);
                    pane.replaceSelection(html[4]+"   "+htmlEnd[4]);//can phai bo < o dau vi da co san
                    //do do phai dung lop html khong dung htmls   
                   
                       
                }
            }   
        );
       
        //xu ly <a>
        htmlItems[5].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.out.println(htmls[5]);
                    pane.replaceSelection(html[5]+"   "+htmlEnd[5]);//can phai bo < o dau vi da co san
                    //do do phai dung lop html khong dung htmls   
                   
                       
                }
            }   
        );
       
        //xu ly <table>
        htmlItems[6].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    System.out.println(htmls[6]);
                    pane.replaceSelection(html[6]+"   "+htmlEnd[6]);//can phai bo < o dau vi da co san
                    //do do phai dung lop html khong dung htmls   
                   
                       
                }
            }   
        );
       
        htmlMenu.setOpaque(true);
           
        pane.addKeyListener(
            new KeyListener(){
                public void keyPressed(KeyEvent event){
                    checkForTriggerEvent(event);       
                }
               
                public void keyReleased(KeyEvent event){
                    checkForTriggerEvent(event);
                }
               
                public void keyTyped(KeyEvent event){
                    if(event.getKeyChar()=='<'){
                        checkForTriggerEvent(event);
                       
                    }   
                }
               
                public void checkForTriggerEvent(KeyEvent event){
                    if(event.getKeyChar()=='<'){
                        //htmlMenu.show(kichthuocBox,0,0);
                        Caret caret = pane.getCaret();
                        Point position = caret.getMagicCaretPosition();
                        int x = 0;
                        int y = 0;
                        if (position !=null) {                           
                            x=(int)position.getX();
                            y=(int)position.getY();
                        }
                        htmlMenu.setBackground(Color.white);
                        htmlMenu.show(pane,x+10,y+15);
                       
                       

                    }
                }
            }   
        );
       
        pane.addCaretListener(
            new CaretListener(){
                public void caretUpdate(CaretEvent event){
                    Caret caret=pane.getCaret();
                    Point position=caret.getMagicCaretPosition();
                    thongtinField[2].setText("");
                    thongtinField[3].setText("");
                   
                    try{
                        //do trong qua trinh tinh toan phat hien ra la moi lan dot nhay 3 don vi
                        //nen de lay so cot phai chia cho 3
                        thongtinField[3].setText(new Double((int)pane.modelToView(event.getDot()).getX()/3).toString());
                        //trong qua trinh tinhdong nhan thay dong dau tien luon o gtri3
                        //cac dong khac thi luon + them 14 vd dong thu 2 la 17,dong thu3 la 21
                        thongtinField[2].setText(new Double((int)(pane.modelToView(event.getDot()).getY()-3)/14+1).toString());
                       
                        //pane.selectAll();
                        //pane.setCaretPosition(pane.getSelectionEnd());
                        //System.out.println(pane.getSelectionEnd());
                        //thongtinField[1].setText(new Double((int)(pane.modelToView(pane.getSelectionEnd()).getY())).toString());
                    }
                    catch(Exception e){}   
                //    thongtinField[2].setText(new String(new Integer(event.getDot()).toString()));
                //    thongtinField[3].setText(new String(new Integer(event.getMark()).toString()));
                       
                }
                   
            }   
        );
       
       
        //chua menu va toolbar
        //panel1 chua menu
        //panel1 chua bar
       
        JPanel panel1=new JPanel();
        panel1.setLayout(new BorderLayout());

        JMenuBar menu=new JMenuBar();
       
        JMenu  FileMenu=new JMenu("File");
        FileMenu.setMnemonic('F');
       
        //xu ly menu New
        FileMenu.add(new FileNewAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
        //xu ly menu Open
        FileMenu.add(new FileOpenAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));
        //xu ly menu Save
        FileMenu.add(new FileSaveAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
        FileMenu.addSeparator();
        FileMenu.add(new FilePrintAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
        FileMenu.add(new FilePageSetupAction()).setAccelerator(
        KeyStroke.getKeyStroke(KeyEvent.VK_P,
            Event.CTRL_MASK | Event.SHIFT_MASK));
        FileMenu.addSeparator();
        FileMenu.add(new FileExitAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK));   
       
           
        menu.add(FileMenu);
       
        EditMenu=new JMenu("Edit");
        EditMenu.setMnemonic('E');
       
        EditMenu.add(new EditUndoAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK));
       
        EditMenu.addSeparator();

        EditMenu.add(new EditCutAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
       
        EditMenu.add(new EditCopyAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
       
        EditMenu.add(new EditPasteAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
       
        EditMenu.add(new EditDeleteAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
       
   
        EditMenu.addSeparator();

        EditMenu.add(new EditSelectAllAction()).setAccelerator(
            KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
       
       
        menu.add(EditMenu);

        addWindowListener(new Xulyexit());
       
        EditMenu.addItemListener(
            new ItemListener(){
                public void itemStateChanged(ItemEvent event){
                    System.out.println("flag"+flag);
                    if(clip==null||clip.equals("")){
                        EditMenu.getItem(0).setEnabled(false);
                    }
                    else{
                        EditMenu.getItem(0).setEnabled(true);
                    }
                    if(flag==true){
                        //item paste
                        EditMenu.getItem(4).setEnabled(true);
                           
                    }
                    else{
                        EditMenu.getItem(4).setEnabled(false);
                   
                    }
                       
                    //kiem tra xem pane da co text nao chua
                    //neu chua thi moi nutkhong the hoat dong
                    if(pane.getText().equals("")){
                        System.out.print(1);
                       
                        EditMenu.getItem(2).setEnabled(false);//cut
                        EditMenu.getItem(3).setEnabled(false);//copy
                        EditMenu.getItem(5).setEnabled(false);//delete
                        //item(6)la separator
                        EditMenu.getItem(7).setEnabled(false);//select all
                    }else{
                        EditMenu.getItem(7).setEnabled(true);//select all
                        //can phai so sanh kieu nay de khoi bi loi                           
                        //if(!("").equals(pane.getSelectedText())){
                        if(pane.getSelectionStart()!=pane.getSelectionEnd()){
                            //chi khi nao co text duoc chon thi moi
                            //cho thao tac copy,delete
                            EditMenu.getItem(2).setEnabled(true);//cut
                            EditMenu.getItem(3).setEnabled(true);//copy
                            EditMenu.getItem(5).setEnabled(true);//delete
                        }
                        else{
                            EditMenu.getItem(2).setEnabled(false);//cut
                            EditMenu.getItem(3).setEnabled(false);//copy
                            EditMenu.getItem(5).setEnabled(false);//delete                   
                        }
                                       
                    }
                }
            }   
        );   
       
       
       
        JMenu FormatMenu=new JMenu("Format");
        FormatMenu.setMnemonic('r');
       
        //String colors[]={"Black","Blue","Red","Green"};
        JMenuItem colorMenu=new JMenuItem("Text Color");
        colorMenu.setMnemonic('C');
        colorMenu.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    TextColor=JColorChooser.showDialog(newVersion4.this,"Chon Color cho Text",TextColor);   
                    if(TextColor==null){
                        TextColor=Color.black;
                    }
                    pane.setForeground(TextColor);
                }
            }   
        );
       
       
        ItemHandler itemHandler=new ItemHandler();
        FormatMenu.add(colorMenu);
        FormatMenu.addSeparator();
       
        JMenuItem backgroundMenu=new JMenuItem("Background Color");
        FormatMenu.add(backgroundMenu);
        backgroundMenu.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    BackgroundColor=JColorChooser.showDialog(newVersion4.this,"Chon Color cho Background",BackgroundColor);
                    if(BackgroundColor==null){
                        BackgroundColor=Color.white;
                    }
                    pane.setBackground(BackgroundColor);
                }
            }
        );           
       
        FormatMenu.addSeparator();
       
       
       
        String fontNames[]={"Serif","Monospaced","SansSerif"};
       
        JMenu fontMenu=new JMenu("Font");
        fontMenu.setMnemonic('n');
       
        fonts=new JRadioButtonMenuItem[fontNames.length];
        fontGroup=new ButtonGroup();
        for(int count=0;count<fonts.length;count++){
            fonts[count]=new JRadioButtonMenuItem(fontNames[count]);
            fontMenu.add(fonts[count]);
            fontGroup.add(fonts[count]);
            fonts[count].addActionListener(itemHandler);
        }
        fonts[0].setSelected(true);
       
        fontMenu.addSeparator();
        String styleNames[]={"Bold","Italic"};
        styleItems=new JCheckBoxMenuItem[styleNames.length];
        StyleHandler styleHandler=new StyleHandler();
        for(int count=0;count<styleNames.length;count++){
            styleItems[count]=new JCheckBoxMenuItem(styleNames[count]);
            fontMenu.add(styleItems[count]);
            styleItems[count].addItemListener(styleHandler);
        }
       
   
               
        FormatMenu.add(fontMenu);
       
        menu.add(FormatMenu);
       
       
    //tao menu font ho tro soan thao html
        JMenu font=new JMenu("Font");
        font.setMnemonic('F');
       
       
        JMenu styleHtml=new JMenu("Style");
       
        String styleHtmlNames[]={"Bold","Underline","Italic"};
        styleHtmlItems=new JMenuItem[styleHtmlNames.length];
        for(int i=0;i<styleHtmlNames.length;i++){
            styleHtmlItems[i]=new JMenuItem(styleHtmlNames[i]);
            styleHtml.add(styleHtmlItems[i]);

        }
        //xu ly cac submenu trong style cua menu font
        //bold
        styleHtmlItems[0].addActionListener(
            new  ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<B>      </B>");
                }
            }
        );
        //underline
        styleHtmlItems[2].addActionListener(
            new  ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<U>      </U>");
                }
            }
        );
        //italic
        styleHtmlItems[1].addActionListener(
            new  ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<I>      </I>");
                }
            }
        );
       
       
        font.add(styleHtml);
       
        JMenu familyHtml=new JMenu("Family");
        familyHtmlItems=new JMenuItem[fontNames.length];
        for(int i=0;i<familyHtmlItems.length;i++){
            familyHtmlItems[i]=new JMenuItem(fontNames[i]);
            familyHtml.add(familyHtmlItems[i]);
        }
        //xu ly cac submenu trong family cua menu font ho tro soan thao html
        familyHtmlItems[0].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT family= \"Verdana\">  </FONT>");
                }
            }   
        );
        familyHtmlItems[1].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT family= \"Courier New\">  </FONT>");
                }
            }   
        );
        familyHtmlItems[2].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT family= \"Times New Roman\">  </FONT>");
                }
            }   
        );
       
       
        font.add(familyHtml);
       
       
        //String sizes[]={"10","12","16","24","64"};
       
        JMenu sizeHtml=new JMenu("Size");
        sizeHtmlItems=new JMenuItem[sizes.length];
        for(int i=0;i<sizes.length;i++){
            sizeHtmlItems[i]=new JMenuItem(sizes[i]);
            sizeHtml.add(sizeHtmlItems[i]);
        }
       
        sizeHtmlItems[0].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT size=\""+sizes[0]+"\">  </FONT>");
                }
            }   
        );
        sizeHtmlItems[1].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT size=\""+sizes[1]+"\">  </FONT>");
                }
            }   
        );
        sizeHtmlItems[2].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT size=\""+sizes[2]+"\">  </FONT>");
                }
            }   
        );
        sizeHtmlItems[3].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT size=\""+sizes[3]+"\">  </FONT>");
                }
            }   
        );
        sizeHtmlItems[4].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<FONT size=\""+sizes[4]+"\">  </FONT>");
                }
            }   
        );
       
        font.add(sizeHtml);
       
        menu.add(font);
       
       
       
        JMenu aboutMenu=new JMenu("About");
        aboutMenu.setMnemonic('A');
        JMenuItem about=new JMenuItem("Info");
        aboutMenu.add(about);
        menu.add(aboutMenu);
        about.addActionListener(
            new  ActionListener(){
                public void actionPerformed(ActionEvent event){
                   
                    JOptionPane.showMessageDialog(newVersion4.this,"By  Pham dinh duy phuong\nDepartment  Mathematic & Informatic\nUniversity of Natural Science\nID 0211186","Information",JOptionPane.INFORMATION_MESSAGE);
                   
                }
            }   
        );

       
        panel1.add(menu,BorderLayout.NORTH);
       
       
        toolBar=new JToolBar();
       
       
        for(int i=0;i<iconFiles.length;i++){
            icons[i]=new ImageIcon(iconFiles[i]);
            buts[i] =new JButton(icons[i]);
            buts[i].setToolTipText(butLabels[i]);
           
            if(i%3==0){
                toolBar.addSeparator();
               
            }
            toolBar.add(buts[i]);
        }
       
       
       
       
        kichthuocBox=new JComboBox(kichthuoc);
       
        toolBar.add(kichthuocBox);
       
        //xu ly su kien chung cho cac nut tren toolbar
        toolBar.addPropertyChangeListener(
            new PropertyChangeListener(){
                public void propertyChange(PropertyChangeEvent evt){
                    //kiem tra xem da copy hoac cut chua
                    //de thao tac voi nut paste
                    //chi khi nao da copy hay cut(tuc flag=true)
                    //thi moi cho paste hoat dong
                    if(flag==true){
                        buts[5].setEnabled(true);//paste
                    }
                    else{
                        buts[5].setEnabled(false);
                    }
                   
                    //kiem tra xem pane da co text nao chua
                    //neu chua thi moi nutkhong the hoat dong
                    if(pane.getText().equals("")){
                        System.out.print(1);
                        buts[4].setEnabled(false);//copy
                        buts[3].setEnabled(false);//cut
                    }else{
                        if(pane.getSelectionStart()!=pane.getSelectionEnd()){
                           
                            //chi khi nao co text duoc chon thi moi
                            //cho thao tac copy,delete
                            buts[4].setEnabled(true);//copy
                            buts[3].setEnabled(true);//cut
                            System.out.print(2);
                            System.out.println("start "+pane.getSelectionStart());
                            System.out.println("end "+pane.getSelectionEnd());
                           
                        }
                        else{
                            System.out.println(3);
                            buts[4].setEnabled(false);//copy
                            buts[3].setEnabled(false);//cut
                        }
                    }
                }   
            }   
        );
       
       
        ap=new Bar();
        //dua thanh tien trinh vao toolBar
        toolBar.add(ap);
       
       
       
        kichthuocBox.addItemListener(
            new ItemListener(){
                public void itemStateChanged(ItemEvent event){
                    if(event.getStateChange()==ItemEvent.SELECTED){
                        size=kichthuoc[kichthuocBox.getSelectedIndex()].intValue();

                        pane.setFont(new Font(pane.getFont().getName(),style,size));
                        pane.repaint();
                    }
                }
            }   
        );
       
        //thao tac voi nut New
        buts[0].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                   
                    new FileNewAction().actionPerformed(event);
                }
            }   
        );
       
       
        //thao tac voi nut Open
        buts[1].addActionListener(
            new ActionListener(){
               
                public void actionPerformed(ActionEvent event){
                    //openFile();
                    new FileOpenAction().actionPerformed(event);
                   
                }
            }   
        );
       
       
        //thao tac voi nut Save
        buts[2].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //SaveFile();
                    new FileSaveAction().actionPerformed(event);
                }
            }   
        );
       
       
        //thao tac voi nut cut
        buts[3].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                   
                    if(!("").equals(pane.getSelectedText())){
                       
                        pane.copy();
                        pane.replaceSelection("");//xoa text vua duoc chon
                        flag=true;//tuong tu nhu o copy
                        //bao cho biet co the su dung paste sau khi da cut
                    }else{
                        buts[3].setEnabled(false);
                    }
                }
            }   
        );
        //copy
        buts[4].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    if(!pane.getSelectedText().equals("")){
                        pane.copy();
                        flag=true;
                    }
                }
            }   
        );
       
        buts[5].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    clip=pane.getText();
                    if(flag){
                        pane.paste();
                       
                    }
                }
            }
        );   
        //thao tac voi nut <b>
        buts[6].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<B>     </B>");
                }
            }   
        );
       
        //thao tac voi nut <u>
        buts[7].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<U>      </U>");
                }
            }
        );   
       
        //thao tac voi nut <i>
        buts[8].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.replaceSelection("<I>      </I>");
                }
            }
        );
        //thao tac voi nut Table
        buts[9].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    new frame();
                }
            }   
        );
       
        //thao tac voi nut the image
        buts[10].addActionListener(
            new ActionListener(){
                public void actionPerformed (ActionEvent event){
                    JFileChooser filechooser=new JFileChooser();
                    int result=filechooser.showDialog(newVersion4.this,"Tim file Anh");
                      
                    if(result==JFileChooser.CANCEL_OPTION)
                           return;

                    filename=filechooser.getSelectedFile();
                                                
                    if(filename==null||("").equals(filename.getName()))
                        JOptionPane.showMessageDialog(newVersion4.this,"Ten File khong hop le" ,"Ten File khong hop le",JOptionPane.ERROR_MESSAGE);
                    else{
                        String st=filename.getPath();
                        pane.replaceSelection("<IMG SRC= \""+st+"\" >");
                    }   
                }
            }
           
        );
       
        //thao tac voi nut link
        buts[11].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    new url();
                }
            }
        );
        toolBar.setBorderPainted(true);
       
       

        panel1.add(toolBar,BorderLayout.SOUTH);
        container.add(panel1,BorderLayout.NORTH);
       
       
        popupMenu=new JPopupMenu();
        items=new JMenuItem[6];
        for(int i=0;i<items.length;i++){
            items[i]=new JMenuItem(names[i]);
            //them duong phan cach vao popupMenu
            if(i==1||i==5){
                popupMenu.addSeparator();
            }
            popupMenu.add(items[i]);
        }
       
        //xu ly undo
        items[0].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.setText(clip);
                }
            }   
        );
        //xu ly cut
        //tuong tu nhu copy
        //nhung sau khi copy se xoa text duoc chon
        items[1].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //new clipboard();//dung de luu lai trang thai truoc khi cut
                        //sau nay se co the goi undo lay lai trang thai truoc khi cut
                    clip=pane.getText();   
                    if(!("").equals(pane.getSelectedText())){
                       
                        pane.copy();
                        pane.replaceSelection("");//xoa text vua duoc chon
                        flag=true;//tuong tu nhu o copy
                        //bao cho biet co the su dung paste sau khi da cut
                    }else{
                        items[1].setEnabled(false);
                    }
                   
                }
            }   
        );
       
        //xu ly copy
        items[2].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    if(!pane.getSelectedText().equals("")){
                        pane.copy();
                        flag=true;
                    }
                }
            }   
        );
       
        //xu ly paste
        items[3].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    clip=pane.getText();
                    if(flag){
                        pane.paste();
                       
                    }
                }
            }   
        );
       
       
        //xu ly delete
        items[4].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //new clipboard();
                    clip=pane.getText();
                    if(!pane.getSelectedText().equals("")){
                        pane.replaceSelection("");
                       
                    }
                }
            }   
        );
       
        //xu ly select all
        items[5].addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    pane.selectAll();
                }
            }   
        );
       
       
        //xu ly su kien nhan chuot   
        pane.addMouseListener(
            new MouseListener(){
                public void mouseClicked(MouseEvent event){
                    checkForTriggerEvent(event);
                }
                public void mouseEntered(MouseEvent event){
                    checkForTriggerEvent(event);
                }
                public void mouseExited(MouseEvent event){
                    checkForTriggerEvent(event);
                }
                public void mousePressed(MouseEvent event){
                    checkForTriggerEvent(event);

                }
                public void mouseReleased(MouseEvent event){
                    checkForTriggerEvent(event);
                }
                private void checkForTriggerEvent(MouseEvent event){
                    System.out.println("goi su kien chuot");
                   
                    if(event.isPopupTrigger()){
                        popupMenu.show(pane,event.getX(),event.getY());
                        if(clip==null||clip.equals("")){
                            items[0].setEnabled(false);
                        }
                        else{
                            items[0].setEnabled(true);
                        }
                        //kiem tra xem da copy hoac cut chua
                        //de thao tac voi nut paste
                        //chi khi nao da copy hay cut(tuc flag=true)
                        //thi moi cho paste hoat dong
                        if(flag==true){
                            items[3].setEnabled(true);//paste
                            buts[5].setEnabled(true);
                        }
                        else{
                            items[3].setEnabled(false);
                            buts[5].setEnabled(true);
                        }
                       
                        //kiem tra xem pane da co text nao chua
                        //neu chua thi moi nutkhong the hoat dong
                        if(pane.getText().equals("")){
                            System.out.print(1);
                            items[5].setEnabled(false);//select all
                            items[4].setEnabled(false);//delete
                            items[2].setEnabled(false);//copy
                            items[1].setEnabled(false);//cut
                           
                            buts[3].setEnabled(false);
                            buts[4].setEnabled(false);
                           
                        }else{
                            items[5].setEnabled(true);//select all
                            //can phai so sanh kieu nay de khoi bi loi                           
                            //if(!("").equals(pane.getSelectedText())){
                            if(pane.getSelectionStart()!=pane.getSelectionEnd()){
                               
                                //chi khi nao co text duoc chon thi moi
                                //cho thao tac copy,delete
                                items[4].setEnabled(true);//delete
                                items[2].setEnabled(true);//copy
                                items[1].setEnabled(true);//cut
                                System.out.print(2);
                                System.out.println("start "+pane.getSelectionStart());
                                System.out.println("end "+pane.getSelectionEnd());
                               
                                buts[3].setEnabled(true);
                                buts[4].setEnabled(true);
                               
                            }
                            else{
                                System.out.println(3);
                                items[4].setEnabled(false);//delete
                                items[2].setEnabled(false);//copy
                                items[1].setEnabled(false);//cut
                               
                                buts[3].setEnabled(false);
                                buts[4].setEnabled(false);
                            }
                           
                           
                           
                        }
                       
                    }
                    //xu ly cho cac buts tren tools bar
                    //dua theo su kien mouse drag
                    //neu mouse duoc quet thi moi su ly
                    if(event.MOUSE_DRAGGED!=0){
                        if(flag==true){
                           
                            buts[5].setEnabled(true);
                        }
                        else{
                            buts[5].setEnabled(false);
                        }
                       
                        //kiem tra xem pane da co text nao chua
                        //neu chua thi moi nutkhong the hoat dong
                        if(pane.getText().equals("")){
                            System.out.print(1);
                            buts[3].setEnabled(false);
                            buts[4].setEnabled(false);
                           
                        }else{
                            //can phai so sanh kieu nay de khoi bi loi                           
                            //if(!("").equals(pane.getSelectedText())){
                            if(pane.getSelectionStart()!=pane.getSelectionEnd()){
                               
                                //chi khi nao co text duoc chon thi moi
                                //cho thao tac copy,delete
                                buts[3].setEnabled(true);
                                buts[4].setEnabled(true);
                               
                            }
                            else{
                               
                                buts[3].setEnabled(false);
                                buts[4].setEnabled(false);
                            }
                           
                        }
                    }
                }
            }
        );
       
        if(kiemtralansudung==0){
            for(int i=0;i<buts.length-1;i++){
                buts[i+1].setEnabled(false);
            }
        }
       
        setSize(800,580);
        setVisible(true);
    }
   
   
    public void showTitle() {
        setTitle(title);
    }
   
   
    public class FileNewAction extends AbstractAction{
        int choose;
        public FileNewAction(){
            super("New");
        }
        public void actionPerformed(ActionEvent event){
           
            for(int i=0;i<buts.length-1&&(i!=3||i!=4||i!=5);i++){
                buts[i+1].setEnabled(true);
            }
           
            if((pane.getText().equalsIgnoreCase("chuong trinh soan thao html"))){
                StringBuffer st=new StringBuffer();
                st.append("<HTML>\n");
                st.append("\t<HEAD>\n");
                st.append("\t\t<TITLE>      </TITLE>\n");
                st.append("\t</HEAD>\n");
                st.append("\t<BODY>\n\n\n\n\n\n\n");
                st.append("\t</BODY>\n");
                st.append("</HTML>");
                pane.setEditable(true);
                pane.setEnabled(true);
                pane.setFont(new Font("serif",Font.PLAIN,10));
                pane.setText(st.toString());
                thongtinField[0].setText("New-Editor");

                return;
            }
            else{
                if(!isSaved){
                    choose=JOptionPane.showConfirmDialog(newVersion4.this,"Ban co muon Save lai noi dung cua van ban hien hanh?","Editor",JOptionPane.YES_NO_CANCEL_OPTION);
           
                    if(choose==JOptionPane.YES_OPTION){
                        new FileSaveAction().actionPerformed(event);
                    }
                    else{
                        StringBuffer st=new StringBuffer();
                        st.append("<HTML>\n");
                        st.append("\t<HEAD>\n");
                        st.append("\t\t<TITLE>      </TITLE>\n");
                        st.append("\t</HEAD>\n");
                        st.append("\t<BODY>\n\n\n\n\n\n\n");
                        st.append("\t</BODY>\n");
                        st.append("</HTML>");
                        //pane.setText("");
                        pane.setText(st.toString());
                        title="New-Editor";
                        showTitle();
                        isSaved=false;
                    }
                }       
                else{
                    StringBuffer st=new StringBuffer();
                    st.append("<HTML>\n");
                    st.append("\t<HEAD>\n");
                    st.append("\t\t<TITLE>      </TITLE>\n");
                    st.append("\t</HEAD>\n");
                    st.append("\t<BODY>\n\n\n\n\n\n\n");
                    st.append("\t</BODY>\n");
                    st.append("</HTML>");
                        //pane.setText("");
                    pane.setText(st.toString());
                    title="New-Editor";
                    showTitle();
                    isSaved=false;
                    thongtinField[0].setText("New-Editor");
                }
            }
            thongtinField[0].setText("New-Editor");       
            thongtinField[2].setText("0");
            thongtinField[3].setText("0");
           
        }
    }
   
   
   

    public class FileOpenAction extends AbstractAction {
        private boolean cothuchien=false;
        private boolean kt=false;
        public FileOpenAction() { super("Open "); }
        public void actionPerformed(ActionEvent event) {
           
               
               JFileChooser filechooser=new JFileChooser();
               filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
               int result=filechooser.showOpenDialog(newVersion4.this);
            if(result==JFileChooser.CANCEL_OPTION)
                   return;

            filename=filechooser.getSelectedFile();
                                                    
            if(filename==null||("").equals(filename.getName()))
                JOptionPane.showMessageDialog(newVersion4.this,"Ten File khong hop le" ,"Ten File khong hop le",JOptionPane.ERROR_MESSAGE);
            else{
                thongtinField[2].setText("0");
                thongtinField[3].setText("0");
                //tao thread mo file
                mofile thread1=new mofile();
       
                //tao thread chay tien trinh
                   Thread thread2=new Thread(ap);
           
           
                  thread1.start();
                   //thread2.start();
              
               
            }
        }
    }   


       
    public class FileSaveAction extends AbstractAction{
        public FileSaveAction(){
            super("Save");
        }   
        public void actionPerformed(ActionEvent event){
           
            JFileChooser ob=new JFileChooser();
            ob.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int flag=ob.showSaveDialog(newVersion4.this);
            if(flag==JFileChooser.CANCEL_OPTION)
                return;//ko xu ly
            filename=ob.getSelectedFile();
            if(filename==null|filename.getName().equals(""))
                JOptionPane.showMessageDialog(null,"Ten File khong hop le","Ten File khong hop le",JOptionPane.ERROR_MESSAGE);
           
            //tao thread luu file   
            luufile thread1=new luufile();
           
            //tao thread chay tien trinh
            Thread thread2=new Thread(ap);
           
            thread1.start();
            thread2.start();   
           
        }
    }   
   
    public class FilePrintAction extends AbstractAction {
        public FilePrintAction() { super("Print"); }
        public void actionPerformed(ActionEvent event) {
            PrinterJob pj = PrinterJob.getPrinterJob();
           
            /**pj.setPrintable(mPageRenderer, mPageFormat);*/
            if (pj.printDialog()) {
                try { pj.print(); }
                catch (PrinterException e) {
                    System.out.println(e);
                }
            }
        }
    }
   
    public class FilePageSetupAction
        extends AbstractAction {
        public FilePageSetupAction() { super("Page setup..."); }
        public void actionPerformed(ActionEvent event) {
            PrinterJob pj = PrinterJob.getPrinterJob();
           
        }
    }
   
    public class FileExitAction    extends AbstractAction {
        int choose;
        public FileExitAction(){ super("Exit"); }
        public void actionPerformed(ActionEvent event) {
            if((pane.getText().equals(""))){
               
                System.exit(0);
            }
            else{
                choose=JOptionPane.showConfirmDialog(newVersion4.this,"Ban co muon Save lai noi dung cua van ban hien hanh?");
           
                if(choose==JOptionPane.YES_OPTION){
                    new FileSaveAction().actionPerformed(event);
                }   
                else{
                    pane.setText("");
                    title="New-Editor";
                    showTitle();
                }
            }
            System.exit(0);
        }
    }
   
   
    public void WriteObjectToFile(File fileName,Object obj){
        try{
            FileOutputStream fos=new FileOutputStream(fileName);
            ObjectOutputStream oos=new ObjectOutputStream(fos);
           
            oos.writeObject(obj);
            oos.flush();
            System.out.println(obj.toString().length());
            oos.close();

           
        }
        catch(IOException e){
            System.out.println();
        }
       
    }
   
    public class EditUndoAction extends AbstractAction{
        public EditUndoAction(){
            super("Undo");
        }
        public void actionPerformed(ActionEvent event){
            pane.setText(clip);
            //pane.paste();
        }
        public void lostOwnership(Clipboard cb,Transferable tr){}
    }
   
    public class EditCutAction extends AbstractAction{
        public EditCutAction(){
            super("Cut");
        }
        public void actionPerformed(ActionEvent event){
            //new clipboard();//dung de luu lai trang thai truoc khi cut
            //sau nay se co the goi undo lay lai trang thai truoc khi cut
            clip=pane.getText();   
            if(!("").equals(pane.getSelectedText())){
                   
                pane.copy();
                pane.replaceSelection("");//xoa text vua duoc chon
                flag=true;//tuong tu nhu o copy
                //bao cho biet co the su dung paste sau khi da cut
            }else{
                items[1].setEnabled(false);
            }
        }
    }
   
    public class EditCopyAction extends AbstractAction{
        public EditCopyAction(){
            super("Copy");
        }
        public void actionPerformed(ActionEvent event){
            if(!pane.getSelectedText().equals("")){
                pane.copy();
                flag=true;
            }
        }
    }
    public class EditPasteAction extends AbstractAction{
        public EditPasteAction(){
            super("Paste");
        }
        public void actionPerformed(ActionEvent event){
            if(flag){
                pane.paste();
            }
        }
    }
    public class EditDeleteAction extends AbstractAction{
        public EditDeleteAction(){
            super("Delete");
        }
        public void actionPerformed(ActionEvent event){
            //new clipboard();
            clip=pane.getText();
            if(!pane.getSelectedText().equals("")){
                pane.replaceSelection("");
            }
        }
       
    }
    public class EditSelectAllAction extends AbstractAction{
        public EditSelectAllAction(){
            super("Select All");
        }
        public void actionPerformed(ActionEvent event){
            pane.selectAll();
        }
    }
   

    public class clipboard  implements ClipboardOwner{
        public clipboard(){
       
            String cliptext=pane.getText();

       
            Toolkit tk=pane.getToolkit();
            Clipboard cb=tk.getSystemClipboard();

            cb.setContents(new StringSelection(cliptext),this);
       
        }
        public void lostOwnership(Clipboard cb,Transferable tr){}
    }
   
    private class ItemHandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
           
            for(int count=0;count<fonts.length;count++){
                if(event.getSource()==fonts[count]){
                    pane.setFont(new Font(fonts[count].getText(),style,size));
                    break;
                }
            }
           
                   
            repaint();
        }
    }
   
    private class StyleHandler implements ItemListener{
        public void itemStateChanged(ItemEvent event){
            style=0;
            if(styleItems[0].isSelected()){
                style+=Font.BOLD;
            }
            if(styleItems[1].isSelected()){
                style+=Font.ITALIC;
            }
            //xu ly su kien trong menu font ho tro soan thao html
            if(styleHtmlItems[0].isSelected()){
                pane.setText("s");
                //pane.replaceSelection("<B>      </B>");
            }
    /*        if(styleHtmlItems[1].isSelected()){
                pane.replaceSelection("<U>      </U>");
            }
            if(styleHtmlItems[2].isSelected()){
                pane.replaceSelection("<I>      </I>");
            }
    */       
            pane.setFont(new Font(pane.getFont().getName(),style,size));
            repaint();
        }
    }
   
    class Xulyexit extends WindowAdapter {
        //xu ly khi thoat khoi ung dung
        public void windowClosing(WindowEvent e) {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            //neu file chua save thi can thong bao cho nguoi dung
            if(!isSaved  ){
                int option=JOptionPane.showConfirmDialog(null,"Ban muon thoat ma khong Save?","Editor",
                JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
                //NO :tro ve lai editor
                if(option==JOptionPane.NO_OPTION)
                    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
                else //thoat khoi chuong trinh
                    setDefaultCloseOperation(EXIT_ON_CLOSE);
            }
        }
    }
   
    public static void main(String args[]){
        newVersion4 app=new newVersion4();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       
    }
   
   
   
    //quan ly thanh tien trinh dung thread
    class Bar extends JPanel implements Runnable{
        int count=0;
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(getToolkit().getImage("classes/warn0.gif"),10,10,this);
            if(count%5==1&&count!=0){
                g.drawImage(getToolkit().getImage("classes/warn1.gif"),10,10,this);
            }
            if(count%5==2&&count!=0){
                g.drawImage(getToolkit().getImage("classes/warn2.gif"),10,10,this);
            }
            if(count%5==3&&count!=0){
                g.drawImage(getToolkit().getImage("classes/warn3.gif"),10,10,this);
            }
            if(count%5==4&&count!=0){
                g.drawImage(getToolkit().getImage("classes/warn4.gif"),10,10,this);
            }
            if(count%5==0&&count!=0){
                g.drawImage(getToolkit().getImage("classes/warn5.gif"),10,10,this);
            }
           
        }
        public void draw(){
            repaint();
            //tang bien count de thay doi image
            count++;
            System.out.println(count);
            repaint();
        }
        public void run(){
            //luon kiem tra dieu kien xem file co mo xong chua

            while(!co){
                //file chua mo song van cho thread tien trinh chay
                draw();
                /*try{
                    Thread.sleep(10);
                }
                catch(InterruptedException e){}
                */
            }
            //neu qua trinh mo  file da xong thi tra lai thanh tien trinh nhu ban dau
            if(co){
                count=0;
                repaint();
                co=false;
            }
        }
    }
   
   
   
    //tao 1 thread trong qua trinh mo file   
    class mofile extends Thread{
        mofile(){}
   
        public void run(){
            try {
                StringBuffer buf=new StringBuffer(); 
                  
                pane.setText("");
                setTitle("Editor -"+filename.getName());
               
               
                inputStream =  new BufferedInputStream(new ProgressMonitorInputStream(
                                pane,"Dang mo file " + filename.getPath(),
                                      new FileInputStream(filename.getPath())));
                         
               
                for(int i=0;i<fileList.length;i++){
                    if(filename.getPath().equals(fileList[i])){
                        //truong hop mo file khong phai tu list
                        //nhung file nay da co trong danh sach list thi cung khong luu vao list
                        flagList=true;
                        break;
                    }
                }
               
                if(!flagList){
                    //truong hop file nay duoc mo tu trong list thi se khong can
                    //phai add them duong dan cua file vao list vi no da co roi
                    //hoac la vi pham dieu tren
               
                    //dua file vua mo nay vao list nhung file da mo
                    fileList[vitritrongList]=filename.getPath();
               
                        vitritrongList++;//phai tang vitri nay len mot don vi
               
                    list.setEnabled(true);
                       //list=new JList(fileList);
                        list.repaint();
                    listPanel.repaint();
               
                }
               
                //thiet lap gia tri cho thong tin file
                thongtinField[0].setText(filename.getName());
               
                isSaved=false;
               
                    while(inputStream.available()!=0){
                    buf.append((char)inputStream.read());
                    //Thread.sleep(1);
                   

                }
                pane.setText(buf.toString());
          
               
                //tim so dong cua text
                   System.out.println(pane.getText());
                if(!pane.getText().equals("")){
               
                    pane.selectAll();
                    //pane.setCaretPosition(pane.getSelectionEnd());
                    System.out.println("VITRI"+pane.getSelectionEnd());
                    try{
               
                        thongtinField[1].setText(new Double((int)(pane.modelToView(pane.getSelectionEnd()).getY()-3)/14).toString());
                        //System.out.println(pane.getCaret().getMagicCaretPosition().getY());
                    }
                    catch(Exception e){}
                   }   
                //nhac cho thread tien trinh biet la qua trinh mo file da xong
                co=true;
                flagList=false;//dam bao flagList mang gia tri false cho lan mo tiep theo
               
            }
            catch(FileNotFoundException ioe) {
                //truong hop khong tim thay file
                co=true;//cho dung
                JOptionPane.showMessageDialog(null, "Khong the tim thay duoc file ban yeu cau", "Loi mo File", JOptionPane.WARNING_MESSAGE );
            }
            catch(IOException ioe) {
                System.out.println(filename.getAbsoluteFile());
            }
            catch(Exception e){
                System.out.println("LOI");
            }
        }
    }
   
   
    //tao 1 thread trong qua trinh luu file
    class luufile extends Thread{
        luufile(){}
        public void run(){
            try{


                   outputStream=new FileWriter(filename.getPath());               

                setTitle("Editor -"+filename.getName());
                //co cho biet file da save
                isSaved=true;

                outputStream.write(pane.getText());

                outputStream.close();
               
                thongtinField[0].setText(filename.getName());
                //bao cho thread tien trinh biet la qua trinh luu file da hoan tat
                co=true;
            }
            catch(IOException ioe) {
                System.out.println("IOException");
            }
        }
    }
   
    //su dung de tao table
    class frame extends JFrame{
    int socot;
    int sodong;
    JTextField fieldCot,fieldDong;

       
    frame(){
        super("Table");
        Container container=getContentPane();
        container.setLayout(new BorderLayout());
       
        JPanel panel=new JPanel(new GridLayout(2,2));
        JLabel labelCot=new JLabel("    So Cot ");
        labelCot.setForeground(Color.blue);
        JLabel labelDong=new JLabel("    So Dong ");
        labelDong.setForeground(Color.blue);
        fieldCot=new JTextField();
        fieldDong=new JTextField();
        panel.add(labelCot);
        panel.add(fieldCot);
        panel.add(labelDong);
        panel.add(fieldDong);
       
        JButton but=new JButton("Tao Table");
        JButton but2=new JButton("Mac dinh");
        JPanel panel2=new JPanel();
        panel2.add(but);
        panel2.add(but2);
        container.add(panel2,BorderLayout.SOUTH);
       
        container.add(panel,BorderLayout.CENTER);
        but.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    try{
                        socot=Integer.parseInt(fieldCot.getText());
                        sodong=Integer.parseInt(fieldDong.getText());

                        StringBuffer st=new StringBuffer();
                        st.append("<TABLE> \n");
                        for(int i=0;i<sodong;i++){
                            st.append("\t<TR>\n");
                            for(int j=0;j<socot;j++){
                                st.append("\t\t<TD>       </TD>\n");
                            }
                            st.append("\t</TR>\n");
                        }
                        st.append("</TABLE>");
                        pane.replaceSelection(st.toString());
                        setVisible(false);
                       
                   
                    }
                    catch(NumberFormatException e){
                        setTitle("Gia tri nhap khong hop le");                   
                        //JOptionPane.showMessageDialog(newVersion4.this,"Gia tri nhap khong hop le","Loi nhap du lieu",JOptionPane.ERROR_MESSAGE);
                    }
                   
                   
                }
            }   
        );
       
        but2.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    StringBuffer st=new StringBuffer();
                    st.append("<TABLE> \n");
                    st.append("\t<TR>\n");
                    st.append("\t\t<TD>     </TD>\n");
                    st.append("\t</TR>\n");
                    st.append("</TABLE>");
                    pane.replaceSelection(st.toString());
                    setVisible(false);
                }
            }   
        );
        setSize(300,120);
        setVisible(true);
        setLocation(300,200);
    }
}


    public class url extends JFrame{
   
        JTextField urlField;
        JButton urlBut;
   
        url(){
       
            super("Nhap duong link ");
       
            Container container=getContentPane();
            container.setLayout(new BorderLayout());
       
            JPanel panel=new JPanel(new BorderLayout());
           
            JLabel urlLabel=new JLabel("Link ");
       
            urlField=new JTextField(100);
            urlField.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        pane.replaceSelection("<A HREF=\""+urlField.getText()+"\">    </A>");
                        setVisible(false);
                    }
                }   
            );
       
            JPanel butPanel=new JPanel(new BorderLayout());
       
            urlBut=new JButton(new ImageIcon("classes/go.gif"));
       
            urlBut.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                        pane.replaceSelection("<A HREF=\""+urlField.getText()+"\">    </A>");
                        setVisible(false);
                    }
                }
               
            );
       
       
            butPanel.add(urlBut,BorderLayout.CENTER);
       
            panel.add(urlLabel,BorderLayout.WEST);
            panel.add(urlField,BorderLayout.CENTER);
            panel.add(butPanel,BorderLayout.EAST);
       
            container.add(panel);
       
            setSize(500,60);
            setVisible(true);
            setLocation(200,200);
            setResizable(false);
       
        }   
    }
}   

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • Reddit
  • Spurl
  • StumbleUpon
  • Technorati

Leave a comment

Diharapka Memberikan Informasi Dan Feedback Yang sekiranya pantas Untuk Dibaca Ok .Kritikan Diterima Dengan Senang Hati....