KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > editors > ui > MultiLineField


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * MultiLineField.java -- synopsis.
21  *
22  *
23  */

24 package org.netbeans.modules.j2ee.sun.ide.editors.ui;
25
26 import java.awt.*;
27 import java.awt.font.*;
28 import java.text.*;
29 import java.util.*;
30 import javax.accessibility.*;
31 import javax.swing.*;
32
33 /**
34  * A specialized JComponent for displaying Method declarations.
35  *
36  * @author Joe Warzecha
37  */

38
39 public class MultiLineField extends JComponent implements Accessible {
40
41     /**
42      * The number of characters to display per line.
43      */

44     private static final int CHARS_DISPLAYED = 80;
45
46     /**
47      * The number of lines of text to display.
48      */

49     private static final int LINES_DISPLAYED = 4;
50
51     /**
52      * The default amount of space between lines of text.
53      */

54     private static final int LINE_SPACING = 2;
55
56     /**
57      * The default line width.
58      */

59     private static final int DEFAULT_WIDTH = 400;
60
61     /**
62      * The minimum line width.
63      */

64     private static final int MIN_WIDTH = 50;
65
66     /**
67      * A blank space
68      */

69     private static final String JavaDoc SPACE = " "; // NOI18N
70

71     /**
72      * The default insets for this component.
73      */

74     private static final Insets INSETS = new Insets (0, 5, 5, 5);
75
76     /**
77      * The color to display errors in.
78      */

79     protected static final Color ERROR_COLOR = Color.red;
80
81     private String JavaDoc [] stringsToDisplay;
82     private AttributedString [] attributedStrings;
83     private Object JavaDoc lockObject;
84
85     protected FontMetrics fontMetrics;
86     protected int totalWidth;
87     protected Dimension preferredSize;
88
89     private Insets insets;
90     private int numberLines;
91     private int lineHeight;
92     private int charHeight;
93     private int lineSpacing;
94     private boolean useMonospacedFont;
95
96     private boolean formatText;
97
98     public MultiLineField () {
99     super ();
100     lockObject = new Object JavaDoc ();
101     setText(new String JavaDoc[0]);
102     init (LINES_DISPLAYED);
103     }
104
105     public MultiLineField (String JavaDoc text) {
106         super ();
107
108     lockObject = new Object JavaDoc ();
109     setText (text);
110     init (stringsToDisplay.length);
111     }
112
113     public MultiLineField (String JavaDoc text, boolean format) {
114         this (text);
115
116     formatText = format;
117     }
118
119     public MultiLineField (String JavaDoc [] strs) {
120     super ();
121
122     lockObject = new Object JavaDoc ();
123     setText (strs);
124     init (stringsToDisplay.length);
125     }
126
127     private void init (int nLines) {
128     formatText = false;
129     insets = INSETS;
130     numberLines = nLines;
131     useMonospacedFont = false;
132     
133     /* Initiliaze the minimum/preferred size to something */
134     preferredSize = new Dimension (100, 20);
135     }
136
137     public void setInsets (Insets i) {
138     insets = i;
139     }
140
141     public void useMonospacedFont (boolean b) {
142     useMonospacedFont = b;
143     }
144
145     public void addNotify () {
146     super.addNotify ();
147     
148     int ptSize = getFont ().getSize ();
149     if (useMonospacedFont) {
150         Font font = new Font ("Monospaced", Font.PLAIN, ptSize); //NOI18N
151
setFont (font);
152     }
153     fontMetrics = getFontMetrics (getFont ());
154
155     /*
156      * If we know the strings, then assume that we just want
157      * them displayed as is and figure out the preferred size
158      */

159
160     int width = 0;
161     if (stringsToDisplay == null) {
162         /* Default size is 4 lines high, 80 characters wide */
163         width = fontMetrics.charWidth ('v') * CHARS_DISPLAYED;
164     } else {
165         if (formatText) {
166         width = DEFAULT_WIDTH;
167         } else {
168             for (int i = 0; i < stringsToDisplay.length; i++) {
169             int lineWidth = fontMetrics.stringWidth
170                             (stringsToDisplay [i]);
171             if (lineWidth > width) {
172                 width = lineWidth;
173             }
174         }
175         }
176     }
177     
178     charHeight = fontMetrics.getMaxAscent () + fontMetrics.getMaxDescent ();
179     
180     /*
181      * If the ptSize is greater than 12, then for each 4 pts,
182      * we add one more space between lines.
183      */

184     lineSpacing = 0;
185     if (ptSize > 12) {
186         lineSpacing = LINE_SPACING + ((ptSize - 4) / 4);
187     } else {
188         lineSpacing = LINE_SPACING;
189         }
190     
191     totalWidth = insets.left + insets.right + width;
192     lineHeight = charHeight + lineSpacing;
193     if (formatText) {
194         reformat ();
195     }
196
197     int totalHeight = insets.top + insets.bottom +
198               (charHeight * numberLines) +
199                   (lineSpacing * (numberLines - 1));
200
201     preferredSize = new Dimension (totalWidth, totalHeight);
202     }
203
204     private void reformat () {
205     if ((stringsToDisplay == null) || (stringsToDisplay.length < 1)) {
206         return;
207     }
208
209     String JavaDoc entireString = stringsToDisplay [0];
210     for (int i = 1; i < stringsToDisplay.length; i++) {
211         entireString = entireString.concat (SPACE + stringsToDisplay [i]);
212     }
213
214     reformat (entireString);
215     }
216
217     private void reformat (String JavaDoc s) {
218     if (totalWidth < MIN_WIDTH) {
219         return;
220     }
221
222     int lineWidth = totalWidth - (insets.left + insets.right);
223
224     StringTokenizer st = new StringTokenizer (s, SPACE);
225     Vector v = new Vector ();
226     String JavaDoc prevString = st.nextToken ();
227     int curWidth;
228     String JavaDoc curString;
229     String JavaDoc curToken;
230     
231     while (st.hasMoreTokens ()) {
232         curToken = st.nextToken ();
233         curString = prevString + SPACE + curToken;
234         curWidth = fontMetrics.stringWidth (curString);
235         if (curWidth > lineWidth) {
236         v.addElement (prevString);
237         prevString = curToken;
238         } else {
239         prevString = curString;
240         }
241     }
242
243     v.addElement (prevString);
244
245     numberLines = v.size ();
246     stringsToDisplay = new String JavaDoc [numberLines];
247     v.copyInto (stringsToDisplay);
248     }
249  
250     public Dimension getPreferredSize () {
251     return preferredSize;
252     }
253
254     public Dimension getMinimumSize () {
255     return preferredSize;
256     }
257
258     public void setSize (Dimension d) {
259     if (d.width != totalWidth) {
260         totalWidth = d.width;
261         if (formatText) {
262         reformat ();
263         }
264     }
265
266     super.setSize (d);
267     }
268
269     public void setBounds (Rectangle r) {
270     if (r.width != totalWidth) {
271         totalWidth = r.width;
272         if (formatText) {
273         reformat ();
274         }
275     }
276
277     super.setBounds (r);
278     }
279
280     public void setBounds (int x, int y, int width, int height) {
281     if (width != totalWidth) {
282         totalWidth = width;
283         if (formatText) {
284         reformat ();
285         }
286     }
287
288     super.setBounds (x, y, width, height);
289     }
290
291     public void paint (Graphics g) {
292     Dimension size = getSize ();
293     if (size.width != totalWidth) {
294         totalWidth = size.width;
295     }
296     
297     g.clearRect (0, 0, size.width, size.height);
298
299     /*
300      * If number of lines of text to display is less than the
301      * total number specified, see if it should start being
302      * displayed on a line other than line 1.
303      */

304     synchronized (lockObject) {
305         int offset = insets.top + fontMetrics.getMaxAscent ();
306         int blankLines = (numberLines - stringsToDisplay.length) / 2;
307         if (blankLines > 0) {
308         offset += (lineHeight * blankLines);
309         }
310
311         for (int i = 0; i < stringsToDisplay.length; i++) {
312         if (attributedStrings != null) {
313             attributedStrings [i].addAttribute (TextAttribute.FONT,
314                             getFont ());
315             g.drawString (attributedStrings [i].getIterator (),
316                   insets.left, offset);
317         } else {
318             g.drawString (stringsToDisplay [i], insets.left, offset);
319         }
320         offset += lineHeight;
321         }
322     }
323     }
324
325     public void setText (AttributedString [] strs) {
326     synchronized (lockObject) {
327         attributedStrings = strs;
328     }
329     repaint();
330     }
331
332     public void setText (String JavaDoc [] strs) {
333     synchronized (lockObject) {
334         stringsToDisplay = strs;
335     }
336     repaint();
337     }
338
339     public String JavaDoc [] getText () {
340     String JavaDoc [] retStrings = null;
341     synchronized (lockObject) {
342         if ((stringsToDisplay != null) && (stringsToDisplay.length > 0)) {
343             retStrings = new String JavaDoc [stringsToDisplay.length];
344             System.arraycopy (stringsToDisplay, 0, retStrings, 0,
345                       stringsToDisplay.length);
346         }
347     }
348
349     return retStrings;
350     }
351     
352     public void setText (String JavaDoc s) {
353     synchronized (lockObject) {
354         attributedStrings = null;
355         if (s == null) {
356         stringsToDisplay = new String JavaDoc [1];
357         stringsToDisplay [0] = SPACE;
358         } else {
359         if (formatText) {
360             if (fontMetrics != null) {
361             reformat (s);
362             } else {
363             stringsToDisplay = new String JavaDoc [1];
364             stringsToDisplay [0] = s;
365             }
366         } else {
367                 StringTokenizer st = new StringTokenizer (s, "\n"); // NOI18N
368
int nTokens = st.countTokens ();
369                 stringsToDisplay = new String JavaDoc [nTokens];
370                 for (int i = 0; i < nTokens; i++) {
371                 stringsToDisplay [i] = st.nextToken ();
372             }
373         }
374         }
375     }
376     }
377
378     public void setError (boolean val) {
379     if (val) {
380         if (stringsToDisplay == null) {
381         return;
382         }
383         attributedStrings = new AttributedString [stringsToDisplay.length];
384         for (int i = 0; i < stringsToDisplay.length; i++) {
385         attributedStrings [i] =
386                 new AttributedString (stringsToDisplay [i]);
387         attributedStrings [i].addAttribute (TextAttribute.FOREGROUND,
388                             ERROR_COLOR);
389         }
390     } else {
391         attributedStrings = null;
392     }
393     }
394
395     public AccessibleContext getAccessibleContext() {
396         if (accessibleContext == null) {
397             accessibleContext = new AccessibleMultiLineField();
398         }
399         return accessibleContext;
400     }
401
402     protected class AccessibleMultiLineField extends AccessibleJComponent {
403
404     public AccessibleRole getAccessibleRole () {
405         return AccessibleRole.LABEL;
406     }
407     }
408 }
409
Popular Tags