RFTEditor总结续篇

FuqiangWang


2014年从msn space存档中重新恢复出来!

实际上,这些都是demo部分的代码,要使用还需雕琢,只是更好的提供相关文档。部分代码是直接摘自他人文章,在此声明,如有引用,请以尊重原作者为前提,谢谢!
接上回:
【1】文件保存以及打开等功能在demo中的代码演示,部分参考在sun提供的demo:
private void saveFileAs(ActionEvent ae)
    {
        CustomFileFilter filter = new CustomFileFilter();
        chooser.addChoosableFileFilter(filter);
        currentFile = new File(".";
        int option = chooser.showSaveDialog(this);
        if(option == JFileChooser.APPROVE_OPTION)
        {
            File file = chooser.getSelectedFile();
            FileOutputStream fos = null;
            try {
                if (file.exists())
                {
                    if (JOptionPane.showConfirmDialog(this, "要覆盖原文件嘛?","文件覆盖确认",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                    {
                        currentFile = file;
                        fos = new FileOutputStream(currentFile);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);
                        oos.writeObject(editor.getDocument());
                        oos.flush();
                        oos.close();
                    }
                }
                else
                {
                    currentFile = new File(file.getAbsolutePath());
                    fos = new FileOutputStream(currentFile);
                    ObjectOutputStream oos = new ObjectOutputStream(fos);
                    oos.writeObject(editor.getDocument());
                    oos.flush();
                    oos.close();
//                //javax.swing.text.rtf.RTFEditorKit kit =(RTFEditorKit)editor.getEditorKit();
//                editor.getDocument().putProperty( DefaultEditorKit.EndOfLineStringProperty, "\r\n" ;
//                kit.write(fos,doc,0,doc.getLength());
//                fos.flush();
//                fos.close();
                }
            }
            catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "文件保存失败!", "文件保存失败!",
                                              JOptionPane.WARNING_MESSAGE, null);
            }
..............
----------------------------
注释掉的部分是RTFEditorKit的实现,因为不能满足要求,所以注释掉了,在前面已经说过。
----------------------------
void saveButton_actionPerformed(ActionEvent e) {
        if(currentFile!=null)
        {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(currentFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(editor.getDocument());
                oos.flush();
                oos.close();
            }catch(Exception ex)
            {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this,"文件保存失败!","文件保存失败!",JOptionPane.WARNING_MESSAGE);
            }
        }
        else
        {
            saveFileAs(e);
        }
    }
-----------------------------------
void openButton_actionPerformed(ActionEvent e) {
        CustomFileFilter filter = new CustomFileFilter();
        chooser.addChoosableFileFilter(filter);
        int option = chooser.showOpenDialog(this);

        if(option == JFileChooser.APPROVE_OPTION)
        {
            File file = chooser.getSelectedFile();
            if (file != null) {
                FileInputStream fis = null;
                currentFile = new File(file.getAbsolutePath());
                try {
                    fis = new FileInputStream(currentFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    editor.setStyledDocument( (StyledDocument) ois.readObject());
                    ois.close();
                    validate();

//                     //RTFEditorKit kit = (RTFEditorKit)editor.getEditorKit();
//                     editor.getDocument().putProperty( DefaultEditorKit.EndOfLineStringProperty, "\n" ;
//
//                     doc = new DefaultStyledDocument(content);
//                     kit.read(fis,doc,0);
//                     editor.setDocument(doc);
//                     fis.close();
                }
                catch (Exception exp) {
                    exp.printStackTrace();
                    JOptionPane.showMessageDialog(this, "文件打开失败!", "文件打开失败!",
                                                  JOptionPane.WARNING_MESSAGE, null);
                }
                finally
                {
                    if(fis!=null)
                    {
                        try{fis.close();}catch(IOException ie){}
                    }
                    if(chooser.getChoosableFileFilters()!=null)
                    {
                        chooser.removeChoosableFileFilter(filter);
                    }
                }
            }
        }
        chooser.removeChoosableFileFilter(filter);
    }
以上是文件保存等操作的demo代码片断,仅供参考。
【2】搜索内容的亮显功能,使用Highlighter类实现,代码摘自别人,特此声明,非出自本人之手,惭愧,呵呵,实在没有时间,所以,直接拖过来做investigation只用。
void searchButton_actionPerformed(ActionEvent e) {
        //Search Function implementation here!
        String inputValue = JOptionPane.showInputDialog(this,"请输入欲查找的字符串!";
        if(inputValue==null)
            return;
        highlight(editor, inputValue);
    }

public void highlight(JTextComponent textComp, String pattern) {
        // First remove all old highlights
        removeHighlights(textComp);

        try {
            Highlighter hilite = textComp.getHighlighter();
            Document doc = textComp.getDocument();
            String text = doc.getText(0, doc.getLength());
            int pos = 0;

            // Search for pattern
            while ( (pos = text.indexOf(pattern, pos)) >= 0) {
                // Create highlighter using private painter and apply around pattern
                hilite.addHighlight(pos, pos + pattern.length(),
                                    myHighlightPainter);
                pos += pattern.length();
            }
        }
        catch (BadLocationException e) {
        }
    }

    public void removeHighlights(JTextComponent textComp) {
        Highlighter hilite = textComp.getHighlighter();
        Highlighter.Highlight[] hilites = hilite.getHighlights();

        for (int i = 0; i < hilites.length; i++) {
            if (hilites[i].getPainter() instanceof MyHighlightPainter) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }

    // An instance of the private subclass of the default highlight painter
    Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(
        Color.red);
    JButton colorButton = new JButton();
    TitledBorder titledBorder38;

    // A private subclass of the default highlight painter
    class MyHighlightPainter
        extends DefaultHighlighter.DefaultHighlightPainter {
        public MyHighlightPainter(Color color) {
            super(color);
        }
    }

============================
差不多了吧,部分像实现fileFilter等功能就不写了,参考sun的文档吧。
该转入后台数据持续层的调研了..............so 忙,

>>>>>> 更多阅读 <<<<<<


「福强私学」来一个?

「福强私学」, 一部沉淀了个人成长、技术与架构、组织与管理以及商业上的方法与心法的百科全书。


开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。

订阅「福报」