AttributeSet aus StyledDocument

tomkruse

Erfahrenes Mitglied
Hi!

Jemand von Euch Erfahrung mit AttributeSet und StyledDocument? Ich habe nämlich das Problem, daß ich die Attribute nicht mehr aus dem Document rauskriege. Folgendes habe ich probiert:

Code:
private void printAllParagraphs() {

   boolean ready=false;
   Element p;
   int start=0;
   int end;
   StyledDocument doc=(StyledDocument)pane.getDocument();
   do {
       p=doc.getParagraphElement(start);
       end=p.getEndOffset();
       try {
           String text=doc.getText(start,end-start);
           System.out.println(start+": "+text);
           }
       catch(BadLocationException ble) {
           System.out.println("ble!");
           ready=true;
           }
       if (!ready) {
           AttributeSet as=p.getAttributes();
           printAllAttributes(as);
           int count=p.getElementCount();
           System.out.println("number of childs:"+count);
           start=end+1;
           if (count>1) {
               for (int i=0;i<count;i++) {
                    Element elem=p.getElement(i);
                    int start1=elem.getStartOffset();
                    int end1=elem.getEndOffset();
                    try {
                         String subText=doc.getText(start1,end1-start1);
                         System.out.println("("+start1+"-"+end1+") "+subText);
                         }
                     catch(BadLocationException ble) {
                         System.out.println("ble ble!");
                         }
                     as=p.getAttributes();
                     printAllAttributes(as);
                     }
               }
           }
       }
   while((p!=null)&&!ready);
   }

private void printAllAttributes(AttributeSet mas) {

   Enumeration en=mas.getAttributeNames();
   while (en.hasMoreElements()) {
          Object name=en.nextElement();
          Object attrib=mas.getAttribute(name);
          System.out.println("***"+attrib.getClass().getName());
          if (attrib.getClass().getName().indexOf("SimpleAttributeSet")!=-1) {
              Enumeration attribNames=((SimpleAttributeSet)attrib).getAttributeNames();
              while (attribNames.hasMoreElements()) {
                     Object aname=attribNames.nextElement();
                     Object value=((SimpleAttributeSet)attrib).getAttribute(aname);
                     //System.out.println(value.getClass().getName());
                     System.out.println("---"+aname.toString()+"/"+value.toString());
                     }
              }
          //System.out.println(name+": "+mas.getAttribute(name).getClass().getName());
          }
   }

Naja, jedenfalls bekomme ich dabei raus, das alle Attribute auf den Defaulteinstellungen stehen und das stimmt nicht, denn ich sehe ja den bunten Text. Was mache ich falsch?

Cu - Tom.
 
Zurück