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 忙,
            

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

「为AI疯狂」星球上,扶墙老师正在和朋友们讨论有趣的AI话题,你要不要⼀起来呀?^-^
这里

  1. 不但有及时新鲜的AI资讯和深度探讨
  2. 还分享AI工具、产品方法和商业机会
  3. 更有体系化精品付费内容等着你,加入星球(https://t.zsxq.com/0dI3ZA0sL) 即可免费领取。(加入之后一定记得看置顶消息呀!)

知识星球二维码

存量的时代,省钱就是赚钱。
在增量的时代,省钱其实是亏钱。
避坑儿是省钱的一种形式,更是真正聪明人的选择!
弯路虽然也是路,但还是能少走就少走,背后都是高昂的试错成本。
订阅「福报」,少踩坑,少走弯路,多走一步,就是不一样的胜率!

订阅「福报Premium订阅」