001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2004-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.junit;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Color;
046: import java.awt.GridLayout;
047: import java.util.ArrayList;
048: import java.util.List;
049: import java.util.ResourceBundle;
050: import javax.accessibility.AccessibleContext;
051: import javax.swing.BorderFactory;
052: import javax.swing.JCheckBox;
053: import javax.swing.JComboBox;
054: import javax.swing.JComponent;
055: import javax.swing.JLabel;
056: import javax.swing.JPanel;
057: import javax.swing.JTextArea;
058: import javax.swing.UIManager;
059: import javax.swing.border.Border;
060: import org.openide.awt.Mnemonics;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.Repository;
063: import org.openide.util.NbBundle;
064:
065: /**
066: * Various utility method for creating and manipulating with GUI elements.
067: *
068: * @author Marian Petras
069: */
070: public final class GuiUtils {
071:
072: /** */
073: public static final String TEMPLATES_DIR = "Templates/JUnit"; //NOI18N
074:
075: /** */
076: public static final String CHK_PUBLIC = "Public"; //NOI18N
077: /** */
078: public static final String CHK_PROTECTED = "Protected"; //NOI18N
079: /** */
080: public static final String CHK_PACKAGE = "Package"; //NOI18N
081: /** */
082: public static final String CHK_PACKAGE_PRIVATE_CLASSES = "PackagePrivateClasses"; //NOI18N
083: /** */
084: public static final String CHK_ABSTRACT_CLASSES = "AbstractImpl"; //NOI18N
085: /** */
086: public static final String CHK_EXCEPTION_CLASSES = "Exceptions"; //NOI18N
087: /** */
088: public static final String CHK_SUITES = "GenerateSuites"; //NOI18N
089: /** */
090: public static final String CHK_SETUP = "SetUp"; //NOI18N
091: /** */
092: public static final String CHK_TEARDOWN = "TearDown"; //NOI18N
093: /** */
094: public static final String CHK_METHOD_BODIES = "Content"; //NOI18N
095: /** */
096: public static final String CHK_JAVADOC = "JavaDoc"; //NOI18N
097: /** */
098: public static final String CHK_HINTS = "Comments"; //NOI18N
099:
100: /**
101: * Creates a combo-box for choosing a template.
102: * The combo-box will contain <code>FileObject</code>s representing
103: * available JUnit templates, wrapped using class {@link NamedObject},
104: * so that the file's names are displayed in the combo-box.
105: * <p>
106: * To get the currently selected template from the combo-box, use:
107: * <blockquote><pre>
108: * NamedObject namedObject = (NamedObject) comboBox.getSelectedItem();
109: * FileObject template = (FileObject) namedObject.object;
110: * </pre></blockquote>
111: *
112: *
113: * @param defaultTemplate path to the default template
114: * @return non-editable combo-box displaying names of templates
115: */
116: public static JComboBox createTemplateChooser(String defaultTemplate) {
117: FileObject templatesDir = Repository.getDefault()
118: .getDefaultFileSystem().findResource(TEMPLATES_DIR);
119: if (templatesDir == null) {
120: throw new RuntimeException("Not found: " + TEMPLATES_DIR); //NOI18N
121: }
122: FileObject templates[] = templatesDir.getChildren();
123:
124: /*
125: * collect a list of templates and identify the default template
126: * among them:
127: */
128: List<NamedObject> itemList = new ArrayList<NamedObject>(
129: templates.length);
130: int defaultItemIndex = -1;
131: int itemIndex = 0;
132:
133: for (int i = 0; i < templates.length; i++) {
134: FileObject template = templates[i];
135:
136: if (!template.getExt().equals("java")) { //NOI18N
137: continue;
138: }
139:
140: itemList.add(new NamedObject(template, template.getName()));
141:
142: if ((defaultItemIndex == -1) && (defaultTemplate != null)
143: && template.getPath().equals(defaultTemplate)) {
144: defaultItemIndex = itemIndex;
145: }
146:
147: itemIndex++;
148: }
149:
150: /* create the combo-box and select the default template: */
151: JComboBox comboBox;
152: if (itemList.isEmpty()) {
153: comboBox = new JComboBox();
154: } else {
155: comboBox = new JComboBox(itemList.toArray());
156: if (defaultItemIndex != -1) {
157: comboBox.setSelectedIndex(defaultItemIndex);
158: }
159: }
160: comboBox.setEditable(false);
161: return comboBox;
162: }
163:
164: /**
165: * Creates a specified set of checkboxes.
166: * The checkboxes are specified by unique identifiers.
167: * The identifiers are given by this class's constants <code>CHK_xxx</code>.
168: * <p>
169: * The array of strings passed as the argument may also contain
170: * <code>null</code> items. In such a case, the resulting array
171: * of check-boxes will contain <code>null</code>s on the corresponding
172: * possitions.
173: *
174: * @param ids identifiers of the checkboxes to be created
175: * @return array of checkboxes corresponding to the array of identifiers
176: * passed as the argument
177: */
178: public static JCheckBox[] createCheckBoxes(String[] ids) {
179: JCheckBox[] chkBoxes = new JCheckBox[ids.length];
180:
181: if (chkBoxes.length == 0) {
182: return chkBoxes;
183: }
184:
185: ResourceBundle bundle = NbBundle.getBundle(GuiUtils.class);
186: for (int i = 0; i < ids.length; i++) {
187: String id = ids[i];
188:
189: if (id == null) {
190: chkBoxes[i] = null;
191: continue;
192: }
193:
194: JCheckBox chkBox = new JCheckBox();
195: String baseName = "JUnitCfgOfCreate.chk" + id; //NOI18N
196: AccessibleContext accessCtx = chkBox.getAccessibleContext();
197: Mnemonics.setLocalizedText(chkBox, bundle
198: .getString(baseName + ".text")); //NOI18N
199: chkBox.setToolTipText(bundle.getString(baseName
200: + ".toolTip")); //NOI18N
201: accessCtx.setAccessibleName(bundle.getString(baseName
202: + ".AN")); //NOI18N
203: accessCtx.setAccessibleDescription(bundle
204: .getString(baseName + ".AD")); //NOI18N
205:
206: chkBoxes[i] = chkBox;
207: }
208: return chkBoxes;
209: }
210:
211: /**
212: * Creates a labelled group of checkboxes.
213: *
214: * @param title title for the group of checkboxes
215: * @param elements checkboxes - members of the group
216: * @return visual component representing the group
217: */
218: public static JComponent createChkBoxGroup(String title,
219: JCheckBox[] elements) {
220:
221: /* create a component representing the group without title: */
222: JComponent content;
223: if (elements.length == 1) {
224: content = elements[0];
225: } else {
226: content = new JPanel(new GridLayout(0, 1, 0, 5));
227: for (int i = 0; i < elements.length; i++) {
228: content.add(elements[i]);
229: }
230: }
231:
232: /* add the title and insets to the group: */
233: JPanel result = new SizeRestrictedPanel(new BorderLayout(),
234: true, true);
235: result.add(new JLabel(title), BorderLayout.NORTH);
236: addBorder(content, BorderFactory.createEmptyBorder(6, 12, 0, 0));
237: result.add(content, BorderLayout.CENTER);
238:
239: return result;
240: }
241:
242: /**
243: * Creates a text component to be used as a multi-line, automatically
244: * wrapping label.
245: * <p>
246: * <strong>Restriction:</strong><br>
247: * The component may have its preferred size very wide.
248: *
249: * @param text text of the label
250: * @return created multi-line text component
251: */
252: public static JComponent createMultilineLabel(String text) {
253: JTextArea textArea = new JTextArea(text);
254: textArea.setEditable(false);
255: textArea.setFocusable(false);
256: textArea.setLineWrap(true);
257: textArea.setWrapStyleWord(true);
258: textArea.setColumns(25);
259:
260: Color color;
261:
262: color = UIManager.getColor("Label.background"); //NOI18N
263: if (color == null) {
264: color = UIManager.getColor("Panel.background"); //NOI18N
265: }
266: if (color != null) {
267: textArea.setBackground(color);
268: } else {
269: textArea.setOpaque(false);
270: }
271:
272: color = UIManager.getColor("Label.foreground"); //NOI18N
273: if (color != null) {
274: textArea.setForeground(color);
275: }
276:
277: return textArea;
278: }
279:
280: /**
281: * Adds a given border to a given component.
282: * If the component already has some border, the given border is put
283: * around the existing border.
284: *
285: * @param component component the border should be added to
286: * @param border the border to be added
287: */
288: private static void addBorder(JComponent component, Border newBorder) {
289: Border currentBorder = component.getBorder();
290: if (currentBorder == null) {
291: component.setBorder(newBorder);
292: } else {
293: component.setBorder(BorderFactory.createCompoundBorder(
294: newBorder, //outside
295: currentBorder)); //inside
296: }
297: }
298:
299: }
|