For my own project I found it rather frustrating that there seems to
be no existing control supporting ellipsis for text that won't fit
into a label with a defined width. There are some examples in the
internet but they all do not support ellipsis in the right way. The
following class will do this and I hope to receive some comments,
because I want to use it in a large web application and it is
important that it will work correctly. Since this took me only some
minutes to write, there is no license applied; you can use it as you
want. It looks a little bit complicated because it is very fast and
will work for very large texts as well:

public class LabelEllipsis extends Label{

                private final int m_MaxHeight;
                private String m_Content;

                public LabelEllipsis (
                                String InContent,
                                int InWidth,
                                int InMaxHeight){

                        m_MaxHeight = InMaxHeight;
                        m_Content = InContent;

                        this.setWidth(InWidth + "px");

                        setText(m_Content);
                }

                @Override
                protected void onLoad(){
                        setText(m_Content);
                }

                @Override
                public void setText(String InContent){
                        if(!isAttached())
                                return;

                        String OldContent = m_Content;

                        super.setText(m_Content);

                        int Height = getOffsetHeight();

                        if(Height > m_MaxHeight){
                                float Ratio = (float)m_MaxHeight / 
(float)Height - 0.1f; // < 1.0

                                m_Content = m_Content.substring(0,
                                                Math.min(m_Content.length() - 
4, (int)(m_Content.length() *
Ratio))) + "...";

                                super.setText(m_Content);

                                Height = getOffsetHeight();

                                if(Height < m_MaxHeight){
                                        // add chars until max height is reached
                                        while(Height < m_MaxHeight){
                                                m_Content = 
OldContent.substring(0, m_Content.length() - 2) +
"...";

                                                super.setText(m_Content);

                                                Height = getOffsetHeight();
                                        }

                                        // remove last char
                                        m_Content = m_Content.substring(0,
                                                        
Math.min(m_Content.length() - 4, m_Content.length() *
m_MaxHeight)) + "...";

                                        super.setText(m_Content);
                                }else if(Height > m_MaxHeight){
                                        // remove chars until max height is 
reached
                                        while(Height > m_MaxHeight){
                                                m_Content = 
m_Content.substring(0, m_Content.length() - 4) +
"...";

                                                super.setText(m_Content);

                                                Height = getOffsetHeight();
                                        }
                                }
                        }
                }
        }

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to