KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > LabelDisplay


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 LabelDisplay extends JComponent implements MouseListener
10 {
11    protected int lines;
12    protected AttributedString[] left;
13
14    protected AttributedString[] right;
15    protected int[] right_widths; // store the widths of the rightmost strings
16

17    public void setNumberOfLines(int l)
18    {
19       lines = l;
20       left = new AttributedString[l];
21       right = new AttributedString[l];
22       right_widths = new int[l];
23
24       revalidate();
25    }
26
27    public int getTotalLines()
28    {
29       return lines;
30    }
31
32    public Dimension getPreferredSize()
33    {
34       int potheight = (lines * TextSource.fontMetrics.getHeight()) + 2;
35      
36       return new Dimension(Integer.MAX_VALUE, potheight);
37    }
38
39    public void setLine(String JavaDoc left_text, String JavaDoc right_text, int lineno)
40    {
41       lineno = left.length - (lineno + 1);
42
43       left[lineno] = AttributedString.CreateAttributedString(left_text);
44       right[lineno] = AttributedString.CreateAttributedString(right_text);
45  
46       left[lineno].assignWidths();
47       right[lineno].assignWidths();
48
49       right_widths[lineno] = right[lineno].getAttributedText().getWidth();
50    }
51
52    public LabelDisplay()
53    {
54       setNumberOfLines(1);
55       addMouseListener(this);
56    }
57
58    public void paint (Graphics g)
59    {
60       TextSource.initGraphics(g);
61
62       int checkY = (g.getClipBounds()).y - 9; // reverse these if
63
int checkH = checkY+(g.getClipBounds()).height + 20; // painting fucks up
64

65       int width = super.getWidth();
66       int height = super.getHeight();
67
68       int baseline = height - TextSource.fontMetrics.getDescent() - 1; // gives us a 5 pixel buffer was 5
69
// between the textbox and the textarea
70

71       //g.drawLine(0, baseline, 1024, baseline);
72

73
74       for (int x = 0; x < left.length && baseline > 0 && left[x] != null; x++)
75       {
76          if (baseline <= checkH && baseline >= checkY)
77          {
78             TextSource.drawText(g, left[x].getAttributedText(), 0, baseline);
79          }
80
81          baseline -= (TextSource.fontMetrics.getHeight());
82       }
83
84       baseline = height - TextSource.fontMetrics.getDescent() - 1; // gives us a 5 pixel buffer was 5
85
// between the textbox and the textarea
86
for (int x = 0; x < right.length && baseline > 0 && right[x] != null; x++)
87       {
88          if (baseline <= checkH && baseline >= checkY)
89          {
90             TextSource.drawText(g, right[x].getAttributedText(), width - right_widths[x], baseline);
91          }
92
93          baseline -= (TextSource.fontMetrics.getHeight());
94       }
95    }
96
97    public int translateToLineNumber(int pixely)
98    {
99       int baseline = getHeight() - TextSource.fontMetrics.getDescent() - 1; // gives us a 5 pixel buffer was 5
100
// between the textbox and the textarea
101

102       for (int x = 0; x < left.length && baseline > 0 && left[x] != null; x++)
103       {
104          if (pixely >= (baseline - TextSource.fontMetrics.getHeight() - 0))
105          {
106             return x;
107          }
108
109          baseline -= (TextSource.fontMetrics.getHeight() + 0);
110       }
111
112       return 0;
113    }
114
115    public void mousePressed(MouseEvent ev) { }
116    public void mouseEntered(MouseEvent ev) { }
117    public void mouseExited(MouseEvent ev) { }
118    public void mouseReleased(MouseEvent ev) { }
119
120    public void mouseClicked(MouseEvent ev)
121    {
122       if (ev.isShiftDown())
123       {
124          int lineno = translateToLineNumber(ev.getY());
125
126          int width;
127          AttributedText iter;
128
129          if (ev.getX() >= (getWidth() - right_widths[lineno]))
130          {
131             width = getWidth() - right_widths[lineno];
132             iter = right[lineno].getAttributedText();
133          }
134          else
135          {
136             width = 0;
137             iter = left[lineno].getAttributedText();
138          }
139
140          while (iter != null && (iter.width + width) < ev.getX())
141          {
142             width = width + iter.width;
143             iter = iter.next;
144          }
145
146          if (iter != null)
147          {
148             int index;
149
150             if (ev.isControlDown() && iter.backIndex > -1)
151             {
152                index = iter.backIndex;
153             }
154             else
155             {
156                index = iter.foreIndex;
157             }
158
159             ModifyColorMapDialog.showModifyColorMapDialog(this, index);
160          }
161          return;
162       }
163    }
164
165    
166 }
167
Popular Tags