KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > GuiUtils


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 2004-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.GridLayout JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import javax.accessibility.AccessibleContext JavaDoc;
29 import javax.swing.BorderFactory JavaDoc;
30 import javax.swing.JCheckBox JavaDoc;
31 import javax.swing.JComboBox JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.JTextArea JavaDoc;
36 import javax.swing.UIManager JavaDoc;
37 import javax.swing.border.Border JavaDoc;
38 import org.openide.awt.Mnemonics;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.Repository;
41 import org.openide.util.NbBundle;
42
43 /**
44  * Various utility method for creating and manipulating with GUI elements.
45  *
46  * @author Marian Petras
47  */

48 public final class GuiUtils {
49     
50     /** */
51     public static final String JavaDoc TEMPLATES_DIR = "Templates/JUnit"; //NOI18N
52

53     /** */
54     public static final String JavaDoc CHK_PUBLIC = "Public"; //NOI18N
55
/** */
56     public static final String JavaDoc CHK_PROTECTED = "Protected"; //NOI18N
57
/** */
58     public static final String JavaDoc CHK_PACKAGE = "Package"; //NOI18N
59
/** */
60     public static final String JavaDoc CHK_PACKAGE_PRIVATE_CLASSES
61                                = "PackagePrivateClasses"; //NOI18N
62
/** */
63     public static final String JavaDoc CHK_ABSTRACT_CLASSES
64                                = "AbstractImpl"; //NOI18N
65
/** */
66     public static final String JavaDoc CHK_EXCEPTION_CLASSES
67                                = "Exceptions"; //NOI18N
68
/** */
69     public static final String JavaDoc CHK_SUITES = "GenerateSuites"; //NOI18N
70
/** */
71     public static final String JavaDoc CHK_SETUP = "SetUp"; //NOI18N
72
/** */
73     public static final String JavaDoc CHK_TEARDOWN = "TearDown"; //NOI18N
74
/** */
75     public static final String JavaDoc CHK_METHOD_BODIES = "Content"; //NOI18N
76
/** */
77     public static final String JavaDoc CHK_JAVADOC = "JavaDoc"; //NOI18N
78
/** */
79     public static final String JavaDoc CHK_HINTS = "Comments"; //NOI18N
80

81     /**
82      * Creates a combo-box for choosing a template.
83      * The combo-box will contain <code>FileObject</code>s representing
84      * available JUnit templates, wrapped using class {@link NamedObject},
85      * so that the file's names are displayed in the combo-box.
86      * <p>
87      * To get the currently selected template from the combo-box, use:
88      * <blockquote><pre>
89      * NamedObject namedObject = (NamedObject) comboBox.getSelectedItem();
90      * FileObject template = (FileObject) namedObject.object;
91      * </pre></blockquote>
92      *
93      *
94      * @param defaultTemplate path to the default template
95      * @return non-editable combo-box displaying names of templates
96      */

97     public static JComboBox JavaDoc createTemplateChooser(String JavaDoc defaultTemplate) {
98         FileObject templatesDir = Repository.getDefault().getDefaultFileSystem()
99                                   .findResource(TEMPLATES_DIR);
100         if (templatesDir == null) {
101             throw new RuntimeException JavaDoc("Not found: " + TEMPLATES_DIR); //NOI18N
102
}
103         FileObject templates[] = templatesDir.getChildren();
104         
105         /*
106          * collect a list of templates and identify the default template
107          * among them:
108          */

109         List JavaDoc<NamedObject> itemList = new ArrayList JavaDoc<NamedObject>(templates.length);
110         int defaultItemIndex = -1;
111         int itemIndex = 0;
112         
113         for (int i = 0; i < templates.length; i++) {
114             FileObject template = templates[i];
115             
116             if (!template.getExt().equals("java")) { //NOI18N
117
continue;
118             }
119             
120             itemList.add(new NamedObject(template, template.getName()));
121             
122             if ((defaultItemIndex == -1)
123                     && (defaultTemplate != null)
124                     && template.getPath().equals(defaultTemplate)) {
125                 defaultItemIndex = itemIndex;
126             }
127             
128             itemIndex++;
129         }
130         
131         /* create the combo-box and select the default template: */
132         JComboBox JavaDoc comboBox;
133         if (itemList.isEmpty()) {
134             comboBox = new JComboBox JavaDoc();
135         } else {
136             comboBox = new JComboBox JavaDoc(itemList.toArray());
137             if (defaultItemIndex != -1) {
138                 comboBox.setSelectedIndex(defaultItemIndex);
139             }
140         }
141         comboBox.setEditable(false);
142         return comboBox;
143     }
144     
145     /**
146      * Creates a specified set of checkboxes.
147      * The checkboxes are specified by unique identifiers.
148      * The identifiers are given by this class's constants <code>CHK_xxx</code>.
149      * <p>
150      * The array of strings passed as the argument may also contain
151      * <code>null</code> items. In such a case, the resulting array
152      * of check-boxes will contain <code>null</code>s on the corresponding
153      * possitions.
154      *
155      * @param ids identifiers of the checkboxes to be created
156      * @return array of checkboxes corresponding to the array of identifiers
157      * passed as the argument
158      */

159     public static JCheckBox JavaDoc[] createCheckBoxes(String JavaDoc[] ids) {
160         JCheckBox JavaDoc[] chkBoxes = new JCheckBox JavaDoc[ids.length];
161         
162         if (chkBoxes.length == 0) {
163             return chkBoxes;
164         }
165         
166         ResourceBundle JavaDoc bundle = NbBundle.getBundle(GuiUtils.class);
167         for (int i = 0; i < ids.length; i++) {
168             String JavaDoc id = ids[i];
169             
170             if (id == null) {
171                 chkBoxes[i] = null;
172                 continue;
173             }
174             
175             JCheckBox JavaDoc chkBox = new JCheckBox JavaDoc();
176             String JavaDoc baseName = "JUnitCfgOfCreate.chk" + id; //NOI18N
177
AccessibleContext JavaDoc accessCtx = chkBox.getAccessibleContext();
178             Mnemonics.setLocalizedText(
179                     chkBox,
180                     bundle.getString(baseName + ".text")); //NOI18N
181
chkBox.setToolTipText(
182                     bundle.getString(baseName + ".toolTip")); //NOI18N
183
accessCtx.setAccessibleName(
184                     bundle.getString(baseName + ".AN")); //NOI18N
185
accessCtx.setAccessibleDescription(
186                     bundle.getString(baseName + ".AD")); //NOI18N
187

188             chkBoxes[i] = chkBox;
189         }
190         return chkBoxes;
191     }
192     
193     /**
194      * Creates a labelled group of checkboxes.
195      *
196      * @param title title for the group of checkboxes
197      * @param elements checkboxes - members of the group
198      * @return visual component representing the group
199      */

200     public static JComponent JavaDoc createChkBoxGroup(String JavaDoc title,
201                                                JCheckBox JavaDoc[] elements) {
202         
203         /* create a component representing the group without title: */
204         JComponent JavaDoc content;
205         if (elements.length == 1) {
206             content = elements[0];
207         } else {
208             content = new JPanel JavaDoc(new GridLayout JavaDoc(0, 1, 0, 5));
209             for (int i = 0; i < elements.length; i++) {
210                 content.add(elements[i]);
211             }
212         }
213         
214         /* add the title and insets to the group: */
215         JPanel JavaDoc result = new SizeRestrictedPanel(new BorderLayout JavaDoc(), true, true);
216         result.add(new JLabel JavaDoc(title), BorderLayout.NORTH);
217         addBorder(content, BorderFactory.createEmptyBorder(6, 12, 0, 0));
218         result.add(content, BorderLayout.CENTER);
219         
220         return result;
221     }
222     
223     /**
224      * Creates a text component to be used as a multi-line, automatically
225      * wrapping label.
226      * <p>
227      * <strong>Restriction:</strong><br>
228      * The component may have its preferred size very wide.
229      *
230      * @param text text of the label
231      * @return created multi-line text component
232      */

233     public static JComponent JavaDoc createMultilineLabel(String JavaDoc text) {
234         JTextArea JavaDoc textArea = new JTextArea JavaDoc(text);
235         textArea.setEditable(false);
236         textArea.setFocusable(false);
237         textArea.setLineWrap(true);
238         textArea.setWrapStyleWord(true);
239         
240         Color JavaDoc color;
241         
242         color = UIManager.getColor("Label.background"); //NOI18N
243
if (color == null) {
244             color = UIManager.getColor("Panel.background"); //NOI18N
245
}
246         if (color != null) {
247             textArea.setBackground(color);
248         } else {
249             textArea.setOpaque(false);
250         }
251         
252         color = UIManager.getColor("Label.foreground"); //NOI18N
253
if (color != null) {
254             textArea.setForeground(color);
255         }
256         
257         return textArea;
258     }
259     
260     /**
261      * Adds a given border to a given component.
262      * If the component already has some border, the given border is put
263      * around the existing border.
264      *
265      * @param component component the border should be added to
266      * @param border the border to be added
267      */

268     private static void addBorder(JComponent JavaDoc component,
269                                   Border JavaDoc newBorder) {
270         Border JavaDoc currentBorder = component.getBorder();
271         if (currentBorder == null) {
272             component.setBorder(newBorder);
273         } else {
274             component.setBorder(BorderFactory.createCompoundBorder(
275                     newBorder, //outside
276
currentBorder)); //inside
277
}
278     }
279     
280 }
281
Popular Tags