KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > AttributedLabel


1 package text;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 import rero.config.*;
8
9 public class AttributedLabel extends JComponent implements MouseListener
10 {
11    protected AttributedString left;
12
13    public Dimension getPreferredSize()
14    {
15       return new Dimension(left.getAttributedText().getWidth(), TextSource.fontMetrics.getHeight() + 2);
16    }
17
18    public void setText(AttributedString string)
19    {
20       left = string;
21    }
22
23    public void setText(String JavaDoc text)
24    {
25       left = AttributedString.CreateAttributedString(text);
26       left.assignWidths();
27    }
28
29    public AttributedLabel()
30    {
31       setText("");
32       addMouseListener(this);
33    }
34
35    public AttributedLabel(String JavaDoc text)
36    {
37       setText(text);
38       addMouseListener(this);
39    }
40
41    public void paint (Graphics g)
42    {
43       TextSource.initGraphics(g);
44
45       int width = super.getWidth();
46       int height = super.getHeight();
47
48       int baseline = height - 4; // gives us a 5 pixel buffer -- was 4
49
// between the textbox and the textarea
50

51       TextSource.drawText(g, left.getAttributedText(), TextSource.UNIVERSAL_TWEAK, baseline);
52    }
53
54    public void mousePressed(MouseEvent ev) { }
55    public void mouseEntered(MouseEvent ev) { }
56    public void mouseExited(MouseEvent ev) { }
57    public void mouseReleased(MouseEvent ev) { }
58
59    public void mouseClicked(MouseEvent ev)
60    {
61       if (ev.isShiftDown())
62       {
63          int width;
64          AttributedText iter;
65
66          width = TextSource.UNIVERSAL_TWEAK;
67          iter = left.getAttributedText();
68
69          while (iter != null && (iter.width + width) < ev.getX())
70          {
71             width = width + iter.width;
72             iter = iter.next;
73          }
74
75          if (iter != null)
76          {
77             int index;
78
79             if (ev.isControlDown() && iter.backIndex > -1)
80             {
81                index = iter.backIndex;
82             }
83             else
84             {
85                index = iter.foreIndex;
86             }
87
88             ModifyColorMapDialog.showModifyColorMapDialog(this, index);
89          }
90
91          ev.consume();
92          return;
93       }
94    }
95 }
96
Popular Tags