KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > DescriptionComponent


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 package org.openide.explorer.propertysheet;
20
21 import javax.accessibility.Accessible JavaDoc;
22 import javax.accessibility.AccessibleContext JavaDoc;
23 import org.openide.util.Utilities;
24
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Image JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32 import java.awt.event.MouseEvent JavaDoc;
33 import java.awt.event.MouseListener JavaDoc;
34 import javax.accessibility.AccessibleRole JavaDoc;
35
36 import javax.swing.BorderFactory JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JComponent.AccessibleJComponent;
41 import javax.swing.JLabel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.JTextArea JavaDoc;
44 import javax.swing.SwingUtilities JavaDoc;
45 import javax.swing.UIManager JavaDoc;
46 import org.openide.util.NbBundle;
47
48
49 /**
50  * A component which can display a description, a title and a button.
51  *
52  * @author Tim Boudreau
53  */

54 class DescriptionComponent extends JComponent JavaDoc implements ActionListener JavaDoc, MouseListener JavaDoc, Accessible JavaDoc {
55     private static int fontHeight = -1;
56     private JTextArea JavaDoc jta;
57     private JLabel JavaDoc lbl;
58     private JButton JavaDoc btn;
59     private JScrollPane JavaDoc jsc;
60
61     /** Creates a new instance of SplitLowerComponent */
62     public DescriptionComponent() {
63         init();
64     }
65
66     private void init() {
67         setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
68
69         jta = new JTextArea JavaDoc();
70         jta.setLineWrap(true);
71         jta.setWrapStyleWord(true);
72         jta.setOpaque(false);
73         jta.setBackground(getBackground());
74         jta.setEditable(false);
75         jta.setOpaque(false);
76         jta.getAccessibleContext().setAccessibleName( NbBundle.getMessage(DescriptionComponent.class, "ACS_Description") );
77         jta.getAccessibleContext().setAccessibleDescription( NbBundle.getMessage(DescriptionComponent.class, "ACSD_Description") );
78
79         //We use a JScrollPane to suppress the changes in layout that will be
80
//caused by adding the raw JTextArea directly - JTextAreas can fire
81
//preferred size changes from within their paint methods, leading to
82
//cyclic revalidation problems
83
jsc = new JScrollPane JavaDoc(jta);
84         jsc.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
85         jsc.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
86         jsc.setBorder(BorderFactory.createEmptyBorder());
87         jsc.setViewportBorder(jsc.getBorder());
88         jsc.setOpaque(false);
89         jsc.setBackground(getBackground());
90         jsc.getViewport().setOpaque(false);
91
92         Font JavaDoc f = UIManager.getFont("Tree.font"); //NOI18N
93

94         if (f != null) {
95             jta.setFont(f);
96         }
97
98         btn = new JButton JavaDoc();
99         btn.addActionListener(this);
100
101         Image JavaDoc help = Utilities.loadImage("org/openide/resources/propertysheet/propertySheetHelp.gif", true); //NOI18N
102

103         ImageIcon JavaDoc helpIcon = new ImageIcon JavaDoc(help); //NOI18N
104
btn.setIcon(helpIcon);
105         btn.setPreferredSize(new Dimension JavaDoc(helpIcon.getIconWidth(), helpIcon.getIconHeight()));
106         btn.setBorder(BorderFactory.createEmptyBorder());
107         btn.setBorderPainted(false);
108         btn.setFocusable(false);
109
110         lbl = new JLabel JavaDoc("Label"); //NOI18N
111

112         lbl.setFont(new Font JavaDoc(lbl.getFont().getName(), Font.BOLD, lbl.getFont().getSize()));
113
114         add(jsc);
115         add(lbl);
116         add(btn);
117         jta.addMouseListener(this);
118         jsc.addMouseListener(this);
119         lbl.addMouseListener(this);
120         btn.addMouseListener(this);
121         jsc.getViewport().addMouseListener(this);
122     }
123
124     public void doLayout() {
125         Insets JavaDoc ins = getInsets();
126         Dimension JavaDoc bttn = btn.getMinimumSize();
127         Dimension JavaDoc lbll = lbl.getPreferredSize();
128
129         int height = Math.max(bttn.height, lbll.height);
130         int right = getWidth() - (ins.right + bttn.width);
131
132         btn.setBounds(right, ins.top, bttn.width, height);
133
134         lbl.setBounds(ins.left, ins.top, right, height);
135
136         jsc.setBounds(ins.left, height, getWidth() - (ins.left + ins.right), getHeight() - height);
137     }
138
139     public void setDescription(String JavaDoc title, String JavaDoc txt) {
140         if (title == null) {
141             title = "";
142         }
143
144         if (txt == null) {
145             txt = "";
146         }
147
148         lbl.setText(title);
149
150         if (title.equals(txt)) {
151             jta.setText("");
152         } else {
153             jta.setText(txt);
154         }
155     }
156
157     public void setHelpEnabled(boolean val) {
158         btn.setEnabled(val);
159     }
160
161     /**
162      * Overridden to calculate a font height on the first paint
163      */

164     public void paint(Graphics JavaDoc g) {
165         if (fontHeight == -1) {
166             fontHeight = g.getFontMetrics(lbl.getFont()).getHeight();
167         }
168
169         super.paint(g);
170     }
171
172     /** Overridden to ensure the description area doesn't grow too big
173      * with large amounts of text */

174     public Dimension JavaDoc getPreferredSize() {
175         Dimension JavaDoc d = new Dimension JavaDoc(super.getPreferredSize());
176
177         if (fontHeight > 0) {
178             Insets JavaDoc ins = getInsets();
179             d.height = Math.max(50, Math.max(d.height, (4 * fontHeight) + ins.top + ins.bottom + 12));
180         } else {
181             d.height = Math.min(d.height, 64);
182         }
183
184         return d;
185     }
186
187     public Dimension JavaDoc getMinimumSize() {
188         if (fontHeight < 0) {
189             return super.getMinimumSize();
190         }
191
192         Dimension JavaDoc d = new Dimension JavaDoc(4 * fontHeight, 4 * fontHeight);
193
194         return d;
195     }
196
197     public void actionPerformed(ActionEvent JavaDoc actionEvent) {
198         PSheet sheet = (PSheet) SwingUtilities.getAncestorOfClass(PSheet.class, this);
199
200         if (sheet != null) {
201             sheet.helpRequested();
202         }
203     }
204
205     private PSheet findSheet() {
206         return (PSheet) SwingUtilities.getAncestorOfClass(PSheet.class, this);
207     }
208
209     public void mouseClicked(MouseEvent JavaDoc e) {
210     }
211
212     public void mouseEntered(MouseEvent JavaDoc e) {
213     }
214
215     public void mouseExited(MouseEvent JavaDoc e) {
216     }
217
218     /**
219      * Forward events that might invoke the popup menu
220      */

221     public void mousePressed(MouseEvent JavaDoc e) {
222         PSheet sh = findSheet();
223
224         if (sh != null) {
225             sh.mousePressed(e);
226         }
227     }
228
229     /**
230      * Forward events that might invoke the popup menu
231      */

232     public void mouseReleased(MouseEvent JavaDoc e) {
233         PSheet sh = findSheet();
234
235         if (sh != null) {
236             sh.mousePressed(e);
237         }
238     }
239
240     public AccessibleContext JavaDoc getAccessibleContext() {
241
242         if( null == accessibleContext ) {
243             accessibleContext = new AccessibleJComponent() {
244                         public AccessibleRole JavaDoc getAccessibleRole() {
245                             return AccessibleRole.SWING_COMPONENT;
246                         }
247                     };
248         
249             accessibleContext.setAccessibleName( NbBundle.getMessage(DescriptionComponent.class, "ACS_Description") );
250             accessibleContext.setAccessibleDescription( NbBundle.getMessage(DescriptionComponent.class, "ACSD_Description") );
251         }
252         
253         return accessibleContext;
254     }
255 }
256
Popular Tags