001: package com.xoetrope.swing;
002:
003: import com.xoetrope.carousel.build.BuildProperties;
004: import java.awt.Color;
005: import java.awt.Cursor;
006: import java.awt.Font;
007: import java.awt.Graphics;
008: import java.awt.Graphics2D;
009: import java.awt.Insets;
010: import java.awt.RenderingHints;
011: import java.awt.event.MouseEvent;
012: import java.awt.event.MouseListener;
013: import javax.swing.JButton;
014: import javax.swing.JLabel;
015: import javax.swing.SwingConstants;
016: import javax.swing.border.EmptyBorder;
017: import javax.swing.plaf.basic.BasicButtonUI;
018: import net.xoetrope.debug.DebugLogger;
019:
020: import net.xoetrope.xui.XAttributedComponent;
021: import net.xoetrope.xui.XProject;
022: import net.xoetrope.xui.XProjectManager;
023: import net.xoetrope.xui.XTextHolder;
024: import net.xoetrope.xui.helper.XTranslator;
025: import net.xoetrope.xui.style.XStyle;
026: import net.xoetrope.xui.style.XStyleComponent;
027: import net.xoetrope.xui.style.XStyleManager;
028:
029: /**
030: * A hyperlink component, showing an underline when under the cursor and
031: * enabling button like actions on mouse click
032: *
033: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
034: * the GNU Public License (GPL), please see license.txt for more details. If
035: * you make commercial use of this software you must purchase a commercial
036: * license from Xoetrope.</p>
037: * <p> $Revision: 1.2 $</p>
038: */
039: public class XHyperLabel extends JButton implements XTextHolder,
040: MouseListener, XAttributedComponent, XStyleComponent {
041: protected String origText;
042: protected XStyle style, selectedStyle;
043: protected String alignment = "left";
044: protected boolean selected;
045: protected XTranslator translator;
046: private XProject currentProject;
047:
048: protected XStyleManager styleMgr;
049:
050: protected boolean antiAlias;
051:
052: protected boolean forceMouseOver = false;
053:
054: /**
055: * Create a new hyperlink label
056: */
057: public XHyperLabel() {
058: currentProject = XProjectManager.getCurrentProject();
059: styleMgr = currentProject.getStyleManager();
060: translator = currentProject.getTranslator();
061:
062: addMouseListener(this );
063: //setModel( new ButtonModel());
064: setOpaque(false);
065:
066: setHorizontalAlignment(SwingConstants.LEFT);
067: setUI(new BasicButtonUI());
068: setBorder(new EmptyBorder(0, 0, 0, 0));
069:
070: setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
071: }
072:
073: public Insets getInsets() {
074: return new Insets(0, 0, 0, 0);
075: }
076:
077: /**
078: * Set the component style
079: * @param name the new style name
080: */
081: public void setStyle(String name) {
082: style = styleMgr.getStyle(name);
083: setFont(styleMgr.getFont(style));
084: }
085:
086: /**
087: * Set the component style
088: * @param name the new style name
089: */
090: public void setStyle(XStyle s) {
091: style = s;
092: setText(origText);
093: }
094:
095: /**
096: * Set the component to render it's text as though in a rollover state, with
097: * the text underlined.
098: * @param force the new flag state, true to set the component to the
099: * rollover/underlined state
100: */
101: public void setForceMouseOver(boolean force) {
102: forceMouseOver = force;
103: }
104:
105: /**
106: * Set the style of a selected hyper link
107: * @param name the selected style name
108: */
109: public void setSelectedStyle(String name) {
110: selectedStyle = currentProject.getStyleManager().getStyle(name);
111: setText(origText);
112: }
113:
114: /**
115: * Set the font for the label
116: * @param f the new font
117: */
118: public void setFont(Font f) {
119: super .setFont(f);
120: setText(origText);
121: }
122:
123: /**
124: * Set the selected state of the component, as the component is slected it is
125: * drawn in the selected style
126: * @param state true to select this link
127: */
128: public void setSelected(boolean state) {
129: selected = state;
130: setText(origText);
131: }
132:
133: /**
134: * Get the selection state of the component
135: * @return true if the link is seldted
136: */
137: public boolean getSelected() {
138: return selected;
139: }
140:
141: /**
142: * Get the formatted (html) text for the label
143: * @return the label's html
144: */
145: protected String getFormattedText() {
146: if (origText != null) {
147: String content = "";
148: XStyle currentStyle = selected ? selectedStyle : style;
149: if (currentStyle == null)
150: currentStyle = style;
151: if (currentStyle != null) {
152: if (currentStyle.getStyleAsInt(XStyle.FONT_WEIGHT) == 1)
153: content += "<b>" + origText + "</b>";
154: else
155: content = origText;
156:
157: String text = "<font";
158: text += " color=#" + getHexColor();
159: text += " style=\"font-size: "
160: + currentStyle.getStyleAsInt(XStyle.FONT_SIZE)
161: + "\"";
162: text += " face="
163: + styleMgr.getFont(currentStyle).getFontName()
164: + ">" + content;
165: text += "</font>";
166:
167: if (forceMouseOver)
168: text = "<u>" + text + "</u>";
169:
170: return text;
171: } else if (BuildProperties.DEBUG)
172: DebugLogger
173: .logError("No style specified for the XHyperLabel instance, the component will not render");
174:
175: return "";
176: } else
177: return "";
178: }
179:
180: public void paintComponent(Graphics g) {
181: Graphics2D g2d = (Graphics2D) g;
182: Object oldHint = g2d
183: .getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
184: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
185: antiAlias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
186: : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
187: super .paintComponent(g);
188: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
189: oldHint);
190: }
191:
192: private String getHexColor() {
193: Color c = style.getStyleAsColor(XStyle.COLOR_FORE);
194: String ret = "";
195: ret = formatHexStr(c.getRed());
196: ret += formatHexStr(c.getGreen());
197: ret += formatHexStr(c.getBlue());
198: return ret;
199: }
200:
201: private String formatHexStr(int input) {
202: return ((input < 16) ? "0" : "") + Integer.toHexString(input);
203: }
204:
205: /**
206: * Set the raw (un-translated) text of the label
207: * @param content The translation key or the text to display if no translation is to take place
208: */
209: public void setContent(String content) {
210: setText(content);
211: }
212:
213: /**
214: * Set the text alignment
215: * @param align left, right, center
216: */
217: public void setAlignment(String align) {
218: alignment = align.toLowerCase();
219: if (alignment.equals("center"))
220: super .setHorizontalAlignment(JLabel.CENTER);
221: else if (alignment.equals("left"))
222: super .setHorizontalAlignment(JLabel.LEFT);
223: else if (alignment.equals("right"))
224: super .setHorizontalAlignment(JLabel.RIGHT);
225: }
226:
227: /**
228: * Set the component text
229: * @param text the component text
230: */
231: public void setText(String text) {
232: if (translator != null)
233: origText = translator.translate(text);
234: else
235: origText = text;
236: super .setText("<html>" + getFormattedText() + "</html>");
237: }
238:
239: /**
240: * Set one or more attributes of the component.
241: * <ul><li>alignment - set the text alignment</li></ul>
242: * @param attribName the name of the attribute
243: * @param attribValue the value of the attribute
244: * @return 0 for success, non zero otherwise
245: */
246: public int setAttribute(String attribName, Object attribValue) {
247: if (attribName.equalsIgnoreCase("alignment"))
248: setAlignment((String) attribValue);
249: else if (attribName.equalsIgnoreCase("tooltip"))
250: setToolTipText((String) attribValue);
251: else if (attribName.equals("antialias"))
252: antiAlias = attribValue.equals("true");
253:
254: return 0;
255: }
256:
257: /**
258: * Get the content of the label
259: * @return the original text (un-translated)
260: */
261: public String getContent() {
262: return origText;
263: }
264:
265: /**
266: * The cursor has just entered the component's area
267: * @param me the current mouse event
268: */
269: public void mouseEntered(MouseEvent me) {
270: super .setText("<html><u>" + getFormattedText() + "</html></u>");
271: }
272:
273: /**
274: * The cursor has just exited the component's area
275: * @param me the current mouse event
276: */
277: public void mouseExited(MouseEvent me) {
278: super .setText("<html>" + getFormattedText() + "</html>");
279: }
280:
281: /**
282: * The user has clicked on the component
283: * @param me the current mouse event
284: */
285: public void mouseClicked(MouseEvent me) {
286: }
287:
288: /**
289: * The mouse has been released while the mouse is over the component
290: * @param me the current mouse event
291: */
292: public void mouseReleased(MouseEvent me) {
293: mouseExited(me);
294: }
295:
296: /**
297: * The mouse has been pressed while the mouse is over the component
298: * @param me the current mouse event
299: */
300: public void mousePressed(MouseEvent me) {
301: }
302: }
|