View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package org.xnap.gui.component;
20  
21  import org.apache.log4j.Logger;
22  
23  import org.xnap.gui.util.*;
24  
25  import javax.swing.*;
26  import java.awt.event.*;
27  import java.awt.*;
28  
29  /***
30   *
31   * @todo handle icons too.
32   */
33  public class SqueezedTextLabel extends JLabel implements ComponentListener
34  {
35  	//--- Constant(s) ---
36  
37  	//--- Data field(s) ---
38  
39      private static Logger logger = Logger.getLogger(SqueezedTextLabel.class);
40  
41  	private String text = null;
42  	
43  	//--- Constructor(s) ---
44  
45      public SqueezedTextLabel()
46  	{
47  		addComponentListener(this);
48  	}
49  
50  	public SqueezedTextLabel(String text)
51  	{
52  		this();
53  		setText(text);
54  	}
55  
56      //--- Method(s) ---
57  
58  	private void squeezeText()
59  	{
60  		if (text == null) {
61  			super.setText(null);
62  			return;
63  		}
64  
65  		Font font = getFont();
66  		if (font == null) {
67  			return;
68  		}
69  
70  		FontMetrics fm = getFontMetrics(font);
71  		int textwidth = fm.stringWidth(text);
72  		int width = getWidth();
73  
74  		setPreferredSize(new Dimension(textwidth, getPreferredSize().height));
75  
76  		if (textwidth > width) {
77  			super.setText(getSqueezedText(fm, width));
78  			setToolTipText(GUIHelper.label(text));
79  		}
80  		else {
81  			super.setText(text);
82  			setToolTipText(null);
83  		}
84  	}
85  
86  	//  public Dimension getPreferredSize()
87  //  	{
88  //  		String txt = getText();
89  //  		super.setText(text);
90  //  		Dimension size = super.getPreferredSize();
91  //  		super.setText(txt);
92  //  		return size;
93  //  	}
94  
95  
96  	private String getSqueezedText(FontMetrics fm, int width)
97  	{
98  		int dots = fm.stringWidth("...") + fm.getMaxAdvance();
99  		char[] array = text.toCharArray();
100 		int length = 0;
101 
102 		// this could be done more efficiently, maybe binary search
103 		// or based on an approximisation
104 		while (length < array.length 
105 			   && fm.charsWidth(array, 0, length) + dots < width) {
106 			length++;
107 		}
108 		return new String(array, 0, length) + "...";
109 	}
110 									   
111 	public void setText(String t)
112 	{
113 		text = t;
114 		squeezeText();
115 	}
116 
117 	/***
118 	 * Returns the original unsqueezed text.
119 	 */
120 	public String getUnsqueezedText()
121 	{
122 		return text;
123 	}
124 
125 	public void componentHidden(ComponentEvent e)
126     {
127     }
128 
129     public void componentMoved(ComponentEvent e)
130     {
131     }
132 
133     public void componentResized(ComponentEvent e)
134     {
135 		squeezeText();
136     }
137 
138 
139     public void componentShown(ComponentEvent e)
140 	{
141     }
142 }