1 19 20 package org.netbeans.modules.junit; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Color ; 24 import java.awt.GridLayout ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.ResourceBundle ; 28 import javax.accessibility.AccessibleContext ; 29 import javax.swing.BorderFactory ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JComboBox ; 32 import javax.swing.JComponent ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.JTextArea ; 36 import javax.swing.UIManager ; 37 import javax.swing.border.Border ; 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 48 public final class GuiUtils { 49 50 51 public static final String TEMPLATES_DIR = "Templates/JUnit"; 53 54 public static final String CHK_PUBLIC = "Public"; 56 public static final String CHK_PROTECTED = "Protected"; 58 public static final String CHK_PACKAGE = "Package"; 60 public static final String CHK_PACKAGE_PRIVATE_CLASSES 61 = "PackagePrivateClasses"; 63 public static final String CHK_ABSTRACT_CLASSES 64 = "AbstractImpl"; 66 public static final String CHK_EXCEPTION_CLASSES 67 = "Exceptions"; 69 public static final String CHK_SUITES = "GenerateSuites"; 71 public static final String CHK_SETUP = "SetUp"; 73 public static final String CHK_TEARDOWN = "TearDown"; 75 public static final String CHK_METHOD_BODIES = "Content"; 77 public static final String CHK_JAVADOC = "JavaDoc"; 79 public static final String CHK_HINTS = "Comments"; 81 97 public static JComboBox createTemplateChooser(String defaultTemplate) { 98 FileObject templatesDir = Repository.getDefault().getDefaultFileSystem() 99 .findResource(TEMPLATES_DIR); 100 if (templatesDir == null) { 101 throw new RuntimeException ("Not found: " + TEMPLATES_DIR); } 103 FileObject templates[] = templatesDir.getChildren(); 104 105 109 List <NamedObject> itemList = new ArrayList <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")) { 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 132 JComboBox comboBox; 133 if (itemList.isEmpty()) { 134 comboBox = new JComboBox (); 135 } else { 136 comboBox = new JComboBox (itemList.toArray()); 137 if (defaultItemIndex != -1) { 138 comboBox.setSelectedIndex(defaultItemIndex); 139 } 140 } 141 comboBox.setEditable(false); 142 return comboBox; 143 } 144 145 159 public static JCheckBox [] createCheckBoxes(String [] ids) { 160 JCheckBox [] chkBoxes = new JCheckBox [ids.length]; 161 162 if (chkBoxes.length == 0) { 163 return chkBoxes; 164 } 165 166 ResourceBundle bundle = NbBundle.getBundle(GuiUtils.class); 167 for (int i = 0; i < ids.length; i++) { 168 String id = ids[i]; 169 170 if (id == null) { 171 chkBoxes[i] = null; 172 continue; 173 } 174 175 JCheckBox chkBox = new JCheckBox (); 176 String baseName = "JUnitCfgOfCreate.chk" + id; AccessibleContext accessCtx = chkBox.getAccessibleContext(); 178 Mnemonics.setLocalizedText( 179 chkBox, 180 bundle.getString(baseName + ".text")); chkBox.setToolTipText( 182 bundle.getString(baseName + ".toolTip")); accessCtx.setAccessibleName( 184 bundle.getString(baseName + ".AN")); accessCtx.setAccessibleDescription( 186 bundle.getString(baseName + ".AD")); 188 chkBoxes[i] = chkBox; 189 } 190 return chkBoxes; 191 } 192 193 200 public static JComponent createChkBoxGroup(String title, 201 JCheckBox [] elements) { 202 203 204 JComponent content; 205 if (elements.length == 1) { 206 content = elements[0]; 207 } else { 208 content = new JPanel (new GridLayout (0, 1, 0, 5)); 209 for (int i = 0; i < elements.length; i++) { 210 content.add(elements[i]); 211 } 212 } 213 214 215 JPanel result = new SizeRestrictedPanel(new BorderLayout (), true, true); 216 result.add(new JLabel (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 233 public static JComponent createMultilineLabel(String text) { 234 JTextArea textArea = new JTextArea (text); 235 textArea.setEditable(false); 236 textArea.setFocusable(false); 237 textArea.setLineWrap(true); 238 textArea.setWrapStyleWord(true); 239 240 Color color; 241 242 color = UIManager.getColor("Label.background"); if (color == null) { 244 color = UIManager.getColor("Panel.background"); } 246 if (color != null) { 247 textArea.setBackground(color); 248 } else { 249 textArea.setOpaque(false); 250 } 251 252 color = UIManager.getColor("Label.foreground"); if (color != null) { 254 textArea.setForeground(color); 255 } 256 257 return textArea; 258 } 259 260 268 private static void addBorder(JComponent component, 269 Border newBorder) { 270 Border currentBorder = component.getBorder(); 271 if (currentBorder == null) { 272 component.setBorder(newBorder); 273 } else { 274 component.setBorder(BorderFactory.createCompoundBorder( 275 newBorder, currentBorder)); } 278 } 279 280 } 281 | Popular Tags |