1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.action; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import javax.swing.JFileChooser ; 28 import javax.swing.JTextField ; 29 import javax.swing.event.DocumentEvent ; 30 import javax.swing.event.DocumentListener ; 31 import javax.swing.text.JTextComponent ; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles; 34 import org.netbeans.modules.apisupport.project.ui.UIUtil; 35 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator; 36 import org.openide.WizardDescriptor; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.NbBundle; 39 import org.openide.util.Utilities; 40 41 46 final class NameIconLocationPanel extends BasicWizardIterator.Panel { 47 48 private static final String ENTER_LABEL = getMessage("CTL_EnterLabel"); 49 private static final String NONE_LABEL = getMessage("CTL_None"); 50 51 private final DataModel data; 52 private final DocumentListener updateListener; 53 54 private String smallIconPath; 55 private String largeIconPath; 56 57 58 public NameIconLocationPanel(final WizardDescriptor setting, final DataModel data) { 59 super(setting); 60 this.data = data; 61 initComponents(); 62 initAccessibility(); 63 if (data.getPackageName() != null) { 64 packageName.setSelectedItem(data.getPackageName()); 65 } 66 putClientProperty("NewFileWizard_Title", getMessage("LBL_ActionWizardTitle")); 67 className.select(0, className.getText().length()); 68 updateListener = new UIUtil.DocumentAdapter() { 69 public void insertUpdate(DocumentEvent e) { 70 updateData(); 71 } 72 }; 73 } 74 75 private void addListeners() { 76 className.getDocument().addDocumentListener(updateListener); 77 displayName.getDocument().addDocumentListener(updateListener); 78 Component editorComp = packageName.getEditor().getEditorComponent(); 79 if (editorComp instanceof JTextComponent ) { 80 ((JTextComponent ) editorComp).getDocument().addDocumentListener(updateListener); 81 } 82 } 83 84 private void removeListeners() { 85 className.getDocument().removeDocumentListener(updateListener); 86 displayName.getDocument().removeDocumentListener(updateListener); 87 Component editorComp = packageName.getEditor().getEditorComponent(); 88 if (editorComp instanceof JTextComponent ) { 89 ((JTextComponent ) editorComp).getDocument().removeDocumentListener(updateListener); 90 } 91 } 92 93 protected String getPanelName() { 94 return getMessage("LBL_NameIconLocation_Title"); 95 } 96 97 protected void storeToDataModel() { 98 removeListeners(); 99 storeBaseData(); 100 } 101 102 protected void readFromDataModel() { 103 updateData(); 104 addListeners(); 105 } 106 107 private void updateData() { 108 storeBaseData(); 109 if (checkValidity()) { 110 CreatedModifiedFiles files = data.getCreatedModifiedFiles(); 111 createdFiles.setText(UIUtil.generateTextAreaContent(files.getCreatedPaths())); 112 modifiedFiles.setText(UIUtil.generateTextAreaContent(files.getModifiedPaths())); 113 } 114 } 115 116 117 private void storeBaseData() { 118 data.setClassName(getClassName()); 119 data.setPackageName(packageName.getEditor().getItem().toString()); 120 data.setIconPath(smallIconPath); 121 data.setLargeIconPath(largeIconPath); 122 data.setDisplayName(displayName.getText()); 123 } 124 125 private String getIconPath() { 126 return icon.getText().equals(NONE_LABEL) ? null : icon.getText(); 127 } 128 129 private boolean checkValidity() { 130 String pName = packageName.getEditor().getItem() == null ? "" : packageName.getEditor().getItem().toString().trim(); 131 if (!Utilities.isJavaIdentifier(getClassName())) { 132 setError(getMessage("MSG_ClassNameMustBeValidJavaIdentifier")); 133 } else if (getDisplayName().equals("") || getDisplayName().equals(ENTER_LABEL)) { 134 setError(getMessage("MSG_DisplayNameMustBeEntered")); 135 } else if (pName.length() == 0 || !UIUtil.isValidPackageName(pName)) { 136 setError(getMessage("ERR_Package_Invalid")); 137 } else if (classAlreadyExists()) { 138 setError(getMessage("MSG_ClassAlreadyExists")); 139 } else if (data.isToolbarEnabled() && getIconPath() == null) { 140 setError(getMessage("MSG_IconRequiredForToolbar")); 141 } else { 142 markValid(); 143 checkIconValidity(); 144 return true; 145 } 146 return false; 147 } 148 149 private void checkIconValidity() { 150 if (smallIconPath == null) { 151 setWarning(UIUtil.getNoIconSelectedWarning(16,16)); 152 } else if (!UIUtil.isValidIcon(new File (smallIconPath),16,16)) { 153 setWarning(UIUtil.getIconDimensionWarning(new File (smallIconPath), 16, 16)); 154 } else if (data.isToolbarEnabled() && largeIconPath == null) { 155 File smallIconFile = new File (smallIconPath); 156 assert smallIconFile.getParentFile() != null; 157 String name = getName(smallIconFile); 158 String ext = getExt(smallIconFile); 159 StringBuffer sb = new StringBuffer (); 160 sb.append(name).append("24"); if (ext != null) { 162 sb.append('.').append(ext); 163 } 164 setWarning(NbBundle.getMessage(NameIconLocationPanel.class, 165 "MSG_NoLargeIconSelected", sb.toString(), smallIconFile.getParent())); } 167 } 168 169 private static String getName(final File smallIconFile) { 170 String name = smallIconFile.getName(); 171 int i = name.lastIndexOf('.'); 172 return (i <= 0) ? name : name.substring(0, i); 173 } 174 175 private static String getExt(final File smallIconFile) { 176 String name = smallIconFile.getName(); 177 int i = name.lastIndexOf('.') + 1; 178 return ((i <= 1) || (i == name.length())) ? "" : name.substring(i); 179 } 180 181 private static Set getPossibleIcons(final String iconPath) { 182 File icon = new File (iconPath); 183 String [] resultSuffixes = { "16", "24", "" }; Set results = new HashSet (); 185 String iconName = icon.getName(); 186 int idx = iconName.lastIndexOf('.'); 187 String name = (idx != -1) ? iconName.substring(0,idx) : iconName; 188 String extension = (idx != -1) ? iconName.substring(idx+1) : null; 189 boolean hasSuffix = (name.endsWith("24")) || (name.endsWith("16")); name = hasSuffix ? name.substring(0,name.length()-2) : name; 191 for (int i = 0; i < resultSuffixes.length; i++) { 192 String resultSuffix = resultSuffixes[i]; 193 String resultName = name + resultSuffix; 194 if (extension != null) { 195 resultName = resultName + '.' + extension; 196 } 197 File f = new File (icon.getParentFile(),resultName); 198 if (f.exists()) { 199 results.add(f); 200 } 201 } 202 return results; 203 } 204 205 private boolean classAlreadyExists() { 206 return data.classExists(); 207 } 208 209 private String getDisplayName() { 210 return displayName.getText().trim(); 211 } 212 213 private String getClassName() { 214 return className.getText().trim(); 215 } 216 217 protected HelpCtx getHelp() { 218 return new HelpCtx(NameIconLocationPanel.class); 219 } 220 221 private static String getMessage(String key) { 222 return NbBundle.getMessage(NameIconLocationPanel.class, key); 223 } 224 225 230 private void initComponents() { 232 java.awt.GridBagConstraints gridBagConstraints; 233 234 classNameTxt = new javax.swing.JLabel (); 235 className = new javax.swing.JTextField (); 236 displayNameTxt = new javax.swing.JLabel (); 237 displayName = new javax.swing.JTextField (); 238 iconTxt = new javax.swing.JLabel (); 239 icon = new javax.swing.JTextField (); 240 iconButton = new javax.swing.JButton (); 241 projectTxt = new javax.swing.JLabel (); 242 project = new JTextField (ProjectUtils.getInformation(data.getProject()).getDisplayName()); 243 packageNameTxt = new javax.swing.JLabel (); 244 packageName = UIUtil.createPackageComboBox(data.getSourceRootGroup()); 245 createdFilesTxt = new javax.swing.JLabel (); 246 createdFiles = new javax.swing.JTextArea (); 247 modifiedFilesTxt = new javax.swing.JLabel (); 248 modifiedFiles = new javax.swing.JTextArea (); 249 250 setLayout(new java.awt.GridBagLayout ()); 251 252 classNameTxt.setLabelFor(className); 253 org.openide.awt.Mnemonics.setLocalizedText(classNameTxt, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_ClassName")); 254 gridBagConstraints = new java.awt.GridBagConstraints (); 255 gridBagConstraints.gridx = 0; 256 gridBagConstraints.gridy = 0; 257 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 258 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 12); 259 add(classNameTxt, gridBagConstraints); 260 261 className.setText(org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "CTL_SampleClassName")); 262 gridBagConstraints = new java.awt.GridBagConstraints (); 263 gridBagConstraints.gridx = 1; 264 gridBagConstraints.gridy = 0; 265 gridBagConstraints.gridwidth = 2; 266 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 267 gridBagConstraints.weightx = 1.0; 268 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 269 add(className, gridBagConstraints); 270 271 displayNameTxt.setLabelFor(displayName); 272 org.openide.awt.Mnemonics.setLocalizedText(displayNameTxt, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_DisplayName")); 273 gridBagConstraints = new java.awt.GridBagConstraints (); 274 gridBagConstraints.gridx = 0; 275 gridBagConstraints.gridy = 1; 276 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 277 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 12); 278 add(displayNameTxt, gridBagConstraints); 279 280 displayName.setText(org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "CTL_EnterLabel")); 281 gridBagConstraints = new java.awt.GridBagConstraints (); 282 gridBagConstraints.gridx = 1; 283 gridBagConstraints.gridy = 1; 284 gridBagConstraints.gridwidth = 2; 285 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 286 gridBagConstraints.weightx = 1.0; 287 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 288 add(displayName, gridBagConstraints); 289 290 iconTxt.setLabelFor(icon); 291 org.openide.awt.Mnemonics.setLocalizedText(iconTxt, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_Icon")); 292 gridBagConstraints = new java.awt.GridBagConstraints (); 293 gridBagConstraints.gridx = 0; 294 gridBagConstraints.gridy = 2; 295 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 296 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 12); 297 add(iconTxt, gridBagConstraints); 298 299 icon.setEditable(false); 300 icon.setText(org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "CTL_None")); 301 gridBagConstraints = new java.awt.GridBagConstraints (); 302 gridBagConstraints.gridx = 1; 303 gridBagConstraints.gridy = 2; 304 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 305 gridBagConstraints.weightx = 1.0; 306 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 307 add(icon, gridBagConstraints); 308 309 org.openide.awt.Mnemonics.setLocalizedText(iconButton, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_Icon_Browse")); 310 iconButton.addActionListener(new java.awt.event.ActionListener () { 311 public void actionPerformed(java.awt.event.ActionEvent evt) { 312 iconButtonActionPerformed(evt); 313 } 314 }); 315 316 gridBagConstraints = new java.awt.GridBagConstraints (); 317 gridBagConstraints.gridx = 2; 318 gridBagConstraints.gridy = 2; 319 gridBagConstraints.insets = new java.awt.Insets (12, 12, 0, 0); 320 add(iconButton, gridBagConstraints); 321 322 projectTxt.setLabelFor(project); 323 org.openide.awt.Mnemonics.setLocalizedText(projectTxt, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_ProjectName")); 324 gridBagConstraints = new java.awt.GridBagConstraints (); 325 gridBagConstraints.gridx = 0; 326 gridBagConstraints.gridy = 3; 327 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 328 gridBagConstraints.insets = new java.awt.Insets (18, 0, 6, 12); 329 add(projectTxt, gridBagConstraints); 330 331 project.setEditable(false); 332 gridBagConstraints = new java.awt.GridBagConstraints (); 333 gridBagConstraints.gridx = 1; 334 gridBagConstraints.gridy = 3; 335 gridBagConstraints.gridwidth = 2; 336 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 337 gridBagConstraints.weightx = 1.0; 338 gridBagConstraints.insets = new java.awt.Insets (18, 0, 6, 0); 339 add(project, gridBagConstraints); 340 341 packageNameTxt.setLabelFor(packageName); 342 org.openide.awt.Mnemonics.setLocalizedText(packageNameTxt, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_PackageName")); 343 gridBagConstraints = new java.awt.GridBagConstraints (); 344 gridBagConstraints.gridx = 0; 345 gridBagConstraints.gridy = 4; 346 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 347 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 348 add(packageNameTxt, gridBagConstraints); 349 350 packageName.setEditable(true); 351 gridBagConstraints = new java.awt.GridBagConstraints (); 352 gridBagConstraints.gridx = 1; 353 gridBagConstraints.gridy = 4; 354 gridBagConstraints.gridwidth = 2; 355 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 356 gridBagConstraints.weightx = 1.0; 357 add(packageName, gridBagConstraints); 358 359 createdFilesTxt.setLabelFor(createdFiles); 360 org.openide.awt.Mnemonics.setLocalizedText(createdFilesTxt, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_CreatedFiles")); 361 gridBagConstraints = new java.awt.GridBagConstraints (); 362 gridBagConstraints.gridx = 0; 363 gridBagConstraints.gridy = 5; 364 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 365 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 366 gridBagConstraints.weighty = 0.5; 367 gridBagConstraints.insets = new java.awt.Insets (36, 0, 6, 12); 368 add(createdFilesTxt, gridBagConstraints); 369 370 createdFiles.setBackground(javax.swing.UIManager.getDefaults().getColor("Label.background")); 371 createdFiles.setColumns(20); 372 createdFiles.setEditable(false); 373 createdFiles.setRows(5); 374 createdFiles.setBorder(null); 375 gridBagConstraints = new java.awt.GridBagConstraints (); 376 gridBagConstraints.gridx = 1; 377 gridBagConstraints.gridy = 5; 378 gridBagConstraints.gridwidth = 2; 379 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 380 gridBagConstraints.weightx = 1.0; 381 gridBagConstraints.weighty = 0.5; 382 gridBagConstraints.insets = new java.awt.Insets (36, 0, 6, 0); 383 add(createdFiles, gridBagConstraints); 384 385 modifiedFilesTxt.setLabelFor(modifiedFiles); 386 org.openide.awt.Mnemonics.setLocalizedText(modifiedFilesTxt, org.openide.util.NbBundle.getMessage(NameIconLocationPanel.class, "LBL_ModifiedFiles")); 387 gridBagConstraints = new java.awt.GridBagConstraints (); 388 gridBagConstraints.gridx = 0; 389 gridBagConstraints.gridy = 6; 390 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 391 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 392 gridBagConstraints.weighty = 0.5; 393 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 394 add(modifiedFilesTxt, gridBagConstraints); 395 396 modifiedFiles.setBackground(javax.swing.UIManager.getDefaults().getColor("Label.background")); 397 modifiedFiles.setColumns(20); 398 modifiedFiles.setEditable(false); 399 modifiedFiles.setRows(5); 400 modifiedFiles.setToolTipText("modifiedFilesValue"); 401 modifiedFiles.setBorder(null); 402 gridBagConstraints = new java.awt.GridBagConstraints (); 403 gridBagConstraints.gridx = 1; 404 gridBagConstraints.gridy = 6; 405 gridBagConstraints.gridwidth = 2; 406 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 407 gridBagConstraints.weightx = 1.0; 408 gridBagConstraints.weighty = 0.5; 409 add(modifiedFiles, gridBagConstraints); 410 411 } 412 414 private void iconButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = UIUtil.getIconFileChooser(icon.getText()); 416 int ret = chooser.showDialog(this, getMessage("LBL_Select")); 417 if (ret == JFileChooser.APPROVE_OPTION) { 418 File iconFile = chooser.getSelectedFile(); 419 icon.setText(iconFile.getAbsolutePath()); 420 { 421 Set allFiles = getPossibleIcons(getIconPath()); 422 assert allFiles.contains(iconFile); 423 allFiles.remove(iconFile); 424 boolean isIconSmall = UIUtil.isValidIcon(iconFile, 16, 16); 425 426 File secondIcon = null; 427 boolean isSecondIconSmall = false; 428 for (Iterator it = allFiles.iterator(); it.hasNext() && !isSecondIconSmall;) { 429 File f = (File ) it.next(); 430 isSecondIconSmall = (isIconSmall) ? 431 UIUtil.isValidIcon(f, 24, 24) : UIUtil.isValidIcon(f, 16, 16); 432 if (isSecondIconSmall) { 433 secondIcon = f; 434 break; 435 } 436 } 437 438 if (secondIcon != null) { 439 smallIconPath = (isIconSmall) ? iconFile.getAbsolutePath() : secondIcon.getAbsolutePath(); 440 largeIconPath = (isIconSmall) ? secondIcon.getAbsolutePath() : iconFile.getAbsolutePath(); 441 } else { 442 smallIconPath = iconFile.getAbsolutePath(); 443 largeIconPath = null; 444 } 445 446 } 447 updateData(); 448 } 449 } 451 452 private javax.swing.JTextField className; 454 private javax.swing.JLabel classNameTxt; 455 private javax.swing.JTextArea createdFiles; 456 private javax.swing.JLabel createdFilesTxt; 457 private javax.swing.JTextField displayName; 458 private javax.swing.JLabel displayNameTxt; 459 private javax.swing.JTextField icon; 460 private javax.swing.JButton iconButton; 461 private javax.swing.JLabel iconTxt; 462 private javax.swing.JTextArea modifiedFiles; 463 private javax.swing.JLabel modifiedFilesTxt; 464 private javax.swing.JComboBox packageName; 465 private javax.swing.JLabel packageNameTxt; 466 private javax.swing.JTextField project; 467 private javax.swing.JLabel projectTxt; 468 470 private void initAccessibility() { 471 this.getAccessibleContext().setAccessibleDescription(getMessage("ACS_NameAndLocationPanel")); 472 className.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_ClassName")); 473 createdFiles.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_CreatedFiles")); 474 displayName.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_DisplayName")); 475 icon.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_Icon")); 476 iconButton.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_IconButton")); 477 modifiedFiles.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_ModifiedFiles")); 478 packageName.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_PackageName")); 479 project.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_Project")); 480 } 481 482 } 483 | Popular Tags |