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);
                }
            }
        }
  • 相关阅读:
    给TextView添加超链接的四种方式
    详解ExplosionField的使用,实现View的粉碎效果
    SpannableString使用详解
    android开发之wheel控件使用详解
    使用HttpURLConnection实现在android客户端和服务器之间传递对象
    关于Fragment与Fragment、Activity通信的四种方式
    Volley完全解析
    ListView异步加载图片,完美实现图文混排
    使用DrawerLayout实现QQ5.0侧拉菜单效果
    使用DrawerLayout实现侧拉菜单
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2182975.html
Copyright © 2011-2022 走看看