Example usage for javax.swing.text Style getAttribute

List of usage examples for javax.swing.text Style getAttribute

Introduction

In this page you can find the example usage for javax.swing.text Style getAttribute.

Prototype

public Object getAttribute(Object key);

Source Link

Document

Fetches the value of the given attribute.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();

    DefaultStyledDocument doc = (DefaultStyledDocument) textPane.getDocument();
    Enumeration e1 = doc.getStyleNames();
    while (e1.hasMoreElements()) {
        String styleName = (String) e1.nextElement();
        System.out.println(styleName);
        Style style = doc.getStyle(styleName);
        int count = style.getAttributeCount();
        System.out.println(count);

        Enumeration e = style.getAttributeNames();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            if (o instanceof String) {
                String attrName = (String) o;
                Object attrValue = style.getAttribute(attrName);
                System.out.println(attrValue);
            } else if (o == StyleConstants.NameAttribute) {
                styleName = (String) style.getAttribute(o);
                System.out.println(styleName);
            } else if (o == StyleConstants.ResolveAttribute) {
                Style parent = (Style) style.getAttribute(o);
                System.out.println(parent.getName());
            } else {
                String attrName = o.toString();
                System.out.println(attrName);
                Object attrValue = style.getAttribute(o);
                System.out.println(attrValue);
            }/*from w  w w. ja va2s.c  om*/
        }
    }
}

From source file:pl.otros.logview.gui.message.html.ExportToHtml.java

protected Map<String, String> styleToCssMap(MessageFragmentStyle mfs) {
    Style style = mfs.getStyle();
    HashMap<String, Object> attributes = new HashMap<String, Object>();
    Enumeration<?> attributeNames = style.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        Object nextElement = attributeNames.nextElement();
        attributes.put(nextElement.toString(), style.getAttribute(nextElement));
    }/*  ww  w.  jav  a 2s  . c o  m*/

    Map<String, String> cssMap = new HashMap<String, String>();

    if (attributes.get("family") != null) {
        cssMap.put("font-family", String.format("'%s'", StyleConstants.getFontFamily(style)));
    }

    if (attributes.get("size") != null) {
        cssMap.put("font-size", Integer.toString(StyleConstants.getFontSize(style)));
    }

    if (attributes.get("foreground") != null) {
        cssMap.put("color", colorToHex(StyleConstants.getForeground(style)));
    }

    if (attributes.get("background") != null) {
        cssMap.put("background-color", colorToHex(StyleConstants.getBackground(style)));
    }

    if (attributes.get("bold") != null) {
        cssMap.put("font-weight", StyleConstants.isBold(style) ? "bold" : "normal");
    }

    if (attributes.get("italic") != null) {
        cssMap.put("font-style", StyleConstants.isItalic(style) ? "italic" : "normal");
    }

    if (attributes.get("underline") != null) {
        cssMap.put("text-decoration", StyleConstants.isItalic(style) ? "underline" : "none");
    }

    return cssMap;
}

From source file:tufts.vue.RichTextBox.java

public Dimension getPreferredSize() {
    Dimension s = null;// w w  w  .  j ava2s.  co  m
    //   Dimension s = super.getPreferredSize();

    Dimension minS = getMinimumSize();

    //System.out.println(javax.swing.SwingUtilities.getLocalBounds(this));
    //if (TestDebug||DEBUG.TEXT) out("getPrefer", s);

    // System.out.println("GetPrefSize : " +  mBounds.width + " "+ s.width);
    //    System.out.println("Required Lines : " + s.width/mBounds.width);
    Caret c = this.getCaret();
    Point position = c.getMagicCaretPosition();
    //    if (position != null)
    //       System.out.println("magic caret : " + position.getX());

    //HTMLDocument builds a hierarchical Element structure where each Element
    //represents a structural block of HTML, and not just a line of text. so I'm not sure
    //how to figure out what line you're on.

    Dimension min = new Dimension();
    final Dimension text = getMinimumSize();
    min.width = text.width;

    int EdgePadY = 0; // Was 3 in VUE 1.5
    int LabelPadLeft = 8; // Was 6 in VUE 1.5; fixed

    //System.out.println("Text.height : " + text.height);
    // *** set icon Y position in all cases to a centered vertical
    // position, but never such that baseline is below bottom of
    // first icon -- this is tricky tho, as first icon can move
    // down a bit to be centered with the label!

    min.width += LabelPadLeft;

    min.width = Math.max(min.width, minS.width);
    /*
    System.out.println("Min.Width =" + min.width);
    System.out.println("MinS.Width =" + minS.width);
    System.out.println("S.Width =" + s.width);
    System.out.println("MBounds.Width = " + mBounds.width);
    System.out.println("GetSize.Width = " + getSize().width);
    System.out.println("Bounds : " + this.getBounds().width);
    System.out.println("LWC width : " + lwc.width);
    System.out.println("LWC bounds : " + lwc.getBounds().width);
    System.out.println("LWC layoutbounds : " + lwc.getLayoutBounds().width);
    System.out.println("LWC min size : " + lwc.getMinimumSize().width);
    System.out.println("Get super width : " + super.getWidth());
    System.out.println("Selection Eend : " + this.getSelectionEnd());
    System.out.println("Selection Start : " +       this.getSelectionStart());
    System.out.println("Bounds Box Width : " +this.getBoxBounds().getBounds().width);      
    System.out.println("Visible Rect : " +this.getVisibleRect().width);*/
    //**      Rectangle p2 = null;
    //if (this.getGraphics() != null)
    //{
    //   System.out.println("GRAPHICS : " + this.getGraphics().toString());
    //   if (this.getGraphics().getClipBounds() != null)
    //   System.out.println("GRAPHICS : " + this.getGraphics().getClipBounds().width);
    //   this.getPreferredScrollableViewportSize().width
    //   
    //   

    View ui = getUI().getRootView(this);
    ui = ui.createFragment(this.getSelectionStart(), this.getSelectionEnd());
    //System.out.println("min span: " + ui.getMinimumSpan(View.X_AXIS));

    //**        int start = getSelectionStart();
    //**        int end = getSelectionEnd();
    //**       float f = ui.getPreferredSpan(View.X_AXIS);
    //System.out.println("Preferred span : " + f);

    //**       f = ui.getPreferredSpan(View.Y_AXIS);
    //System.out.println("Preferred span y : " + f);
    //**       f = ui.getPreferredSpan(View.X_AXIS);
    //System.out.println("{referred span X : " + f);
    //ui.breakView(View.X_AXIS, offset, pos, len)
    //((HTMLEditorKit)this.getEditorKit()).getViewFactory().create(this.getDocument().getDefaultRootElement()).
    //  System.out.println("BREAK WEIGHT : " + ui.getBreakWeight(View.X_AXIS, this.getCaretPosition(), this.getSelectionEnd()));
    //  System.out.println("RESIZE WEIGHT : " + ui.getResizeWeight(View.X_AXIS));
    //  System.out.println("RESIZE WEIGHTY : " + ui.getResizeWeight(View.Y_AXIS));
    //**       float align = ui.getAlignment(View.X_AXIS);
    //  System.out.println("align span : " + align);

    //**      try {
    //**         p2 = this.modelToView(this.getSelectionEnd());
    //**         
    //**      } catch (BadLocationException e) {         
    //**         //Nothing we can do really.
    //**      }
    //**      //if (p2 != null)
    //**         //System.out.println("Selection end rect : " + p2.x);
    //**      
    if (mBounds.width > 0) {
        //**           if (p2 != null && p2.x > mBounds.width)
        //**              mBounds.width = p2.x;

        //int height = 48;
        /*if (getFont() != null &&this.getFontMetrics(getFont())!=null)
        {
        System.out.println("A:"+this.getFontMetrics(getFont()).getMaxAscent());
        System.out.println("B:"+this.getFontMetrics(getFont()).getMaxDescent());
        }*/

        int height = super.getPreferredSize().height;//s.height;//Math.max(s.height, 48);

        Style style = ((HTMLDocument) getDocument()).getStyleSheet().getStyle("body");
        Object a = style.getAttribute(javax.swing.text.html.CSS.Attribute.FONT_SIZE);

        if (a != null) {
            if (DEBUG.TEXT)
                out("got style");
            int diff = 0;
            Integer i = new Integer(a.toString());
            diff = i.intValue();
            //minS.height = minS.height - diff;
            if (VUE.getActiveViewer() != null) {
                java.awt.Container parent = getParent();
                double zoom = 1.0;
                if (parent != null)
                    zoom = ((MapViewer) parent).getZoomFactor();
                else {
                    //parent is null during duplicate.
                    zoom = VUE.getActiveViewer().getZoomFactor();
                }
                zoom *= lwc.getMapScale();

                diff *= zoom;
            }
            height = height - diff;
        }

        if (position != null) {

            float minSpan = ui.getMinimumSpan(View.X_AXIS);
            float mins2 = Math.max(minSpan, (float) position.getX());
            s = new Dimension((int) (mBounds.width > mins2 ? mBounds.width : mins2), (int) height);
        } else {
            float minSpan = ui.getMinimumSpan(View.X_AXIS);
            s = new Dimension((int) (mBounds.width > minSpan ? mBounds.width : minSpan), (int) height);
        }
        //float widthRatio = ((float)s.width /(float)lwc.width);
        //   float heightRatio =((float)s.height/(float)lwc.height);
        /*    if ((widthRatio > 0) && (heightRatio > 0) && (widthRatio != heightRatio))
              {
                
        s.height = (int)(lwc.height * widthRatio);
              //   System.out.print("NEW SHEIGHT : " + s.height);
              //   prevDim = s;
        //            
              }
            System.out.println("width ratio : " + widthRatio);
              System.out.println("height ratio : " + heightRatio);
           */
        /*if (this.getGraphics() != null)
         System.out.println("FONT HEIGHT : " +this.getGraphics().getFontMetrics().getMaxAscent());
         System.out.println("===================================");
         System.out.println("super pref height : " + s.height);
         System.out.println("Min Vertical Span : " +ui.getMinimumSpan(View.Y_AXIS));
         System.out.println("MBounds height : " + mBounds.height);
         System.out.println("super pref height : " + s.height);
         System.out.println("Min Vertical Span : " +ui.getMinimumSpan(View.Y_AXIS));
         System.out.println("MBounds height : " + mBounds.height);
         System.out.println("Mins height : " + min.height);
         System.out.println("Bounds Y : " + this.getBounds().y);
         System.out.println("Bounds height :" +this.getBounds().height);
         System.out.println("Bounds2D Y : " +this.getBoxBounds().getY());
         System.out.println("Bounds2D height : " +this.getBoxBounds().getHeight());
         if (this.getCaret().getMagicCaretPosition() != null)
         System.out.println("Cursor : " + this.getCaret().getMagicCaretPosition().toString());
         //this.getCaret().getMagicCaretPosition().
         System.out.println("===================================");*/
        /*if (position != null)
        {
           System.out.println("Y Pos : " + position.getY());
        try {
            Rectangle r = modelToView(this.getCaretPosition());
            System.out.println("model pos : " + r.y);
            System.out.println("Model height : " + r.height);
         } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
        }*/

    }
    // s.width = (int)(s.width * VUE.getActiveViewer().getZoomFactor());
    //   s.height = (int)(s.height * VUE.getActiveViewer().getZoomFactor());
    //   s.height=(int) this.getBoxBounds().getHeight();

    if (this.getBoxBounds() != null && s.height < this.getBoxBounds().getHeight()) {
        s.height = (int) this.getBoxBounds().getHeight();

    }

    return s;
}

From source file:tufts.vue.RichTextBox.java

public Dimension getMinimumSize() {
    Dimension s = super.getMinimumSize();

    Style style = ((HTMLDocument) getDocument()).getStyleSheet().getStyle("body");
    Object a = style.getAttribute(javax.swing.text.html.CSS.Attribute.FONT_SIZE);

    if (a != null) {
        if (DEBUG.TEXT)
            out("got style");
        int diff = 0;
        Integer i = new Integer(a.toString());
        diff = i.intValue();//from  www  . ja v  a2 s  . co m
        if (VUE.getActiveViewer() != null) {
            java.awt.Container parent = getParent();
            double zoom = 1.0;

            // System.out.println("SHEIGHT : " + s.height);
            if (parent != null) {
                zoom = ((MapViewer) parent).getZoomFactor();
                zoom *= lwc.getMapScale();
                diff *= zoom;

                //  System.out.println("if ::: " + diff);
            } else {
                if (VUE.getActiveViewer() != null)
                    diff *= VUE.getActiveViewer().getZoomFactor();

                diff *= lwc.getMapScale();
                //  System.out.println("else ::: " + diff);

            }

        }

        /*if (a !=null)
        {   if (DEBUG.TEXT)
              out("got style");
           int diff =0;
           Integer i = new Integer(a.toString());
           diff = i.intValue();
           if (VUE.getActiveViewer()!=null)
              diff *= VUE.getActiveViewer().getZoomFactor();
                
                      
           s.height = s.height - diff;
        }*/

        s.height = s.height - diff;
    }

    if (TestDebug || DEBUG.TEXT)
        out("getMinimumSize", s);
    return s;
}