zoukankan      html  css  js  c++  java
  • Java Swing之StyledDocument的合并

    http://blog.fyent.net/2011/01/java_swing_merge_styleddocument/



        public void mergeDocument(DefaultStyledDocument source,
                DefaultStyledDocument dest) 
    throws BadLocationException {
            ArrayList specs 
    = new ArrayList();
            DefaultStyledDocument.ElementSpec spec 
    = new DefaultStyledDocument.ElementSpec(
                    
    new SimpleAttributeSet(),
                    DefaultStyledDocument.ElementSpec.EndTagType);
            specs.add(spec);
            fillSpecs(source.getDefaultRootElement(), specs, 
    false);
            spec 
    = new DefaultStyledDocument.ElementSpec(new SimpleAttributeSet(),
                    DefaultStyledDocument.ElementSpec.StartTagType);
            specs.add(spec);

            DefaultStyledDocument.ElementSpec[] arr 
    = new DefaultStyledDocument.ElementSpec[specs
                    .size()];
            specs.toArray(arr);
            insertSpecs(dest, dest.getLength(), arr);
        }

        
    protected void insertSpecs(DefaultStyledDocument doc, int offset,
                DefaultStyledDocument.ElementSpec[] specs) {
            
    try {
                
    // doc.insert(0, specs); method is protected so we have to
                
    // extend document or use such a hack
                Method m = DefaultStyledDocument.class.getDeclaredMethod("insert",
                        
    new Class[] { int.class,
                                DefaultStyledDocument.ElementSpec[].
    class });
                m.setAccessible(
    true);
                m.invoke(doc, 
    new Object[] { offset, specs });
            } 
    catch (Exception e) {
                e.printStackTrace();
            }
        }

        
    protected void fillSpecs(Element elem, ArrayList specs, boolean includeRoot)
                
    throws BadLocationException {
            DefaultStyledDocument.ElementSpec spec;
            
    if (elem.isLeaf()) {
                String str 
    = elem.getDocument().getText(elem.getStartOffset(),
                        elem.getEndOffset() 
    - elem.getStartOffset());
                spec 
    = new DefaultStyledDocument.ElementSpec(elem.getAttributes(),
                        DefaultStyledDocument.ElementSpec.ContentType, str
                                .toCharArray(), 
    0, str.length());
                specs.add(spec);
            } 
    else {
                
    if (includeRoot) {
                    spec 
    = new DefaultStyledDocument.ElementSpec(elem
                            .getAttributes(),
                            DefaultStyledDocument.ElementSpec.StartTagType);
                    specs.add(spec);
                }
                
    for (int i = 0; i < elem.getElementCount(); i++) {
                    fillSpecs(elem.getElement(i), specs, 
    true);
                }

                
    if (includeRoot) {
                    spec 
    = new DefaultStyledDocument.ElementSpec(elem
                            .getAttributes(),
                            DefaultStyledDocument.ElementSpec.EndTagType);
                    specs.add(spec);
                }
            }
        }
  • 相关阅读:
    [转载]instanceof和typeof区别
    【转载】DNN架构
    Delphi实现高性能的Socket通讯服务器(完成端口模型IOCP)
    record, packed record和变体记录
    Delphi操作Word的几个知识点
    WinSock学习笔记6:IOCP完成端口模型
    MyEclipse 常用设置和操作方法
    PAIP.一些流氓软件的流氓营销方法.txt
    qq安全使用指南.txt
    海量数据高性能分页
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2182975.html
Copyright © 2011-2022 走看看