1 19 20 package org.netbeans.modules.project.libraries.ui; 21 22 import java.awt.Color ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.regex.Pattern ; 26 import org.openide.DialogDescriptor; 27 import org.openide.util.NbBundle; 28 import org.netbeans.modules.project.libraries.LibraryTypeRegistry; 29 import org.netbeans.spi.project.libraries.LibraryTypeProvider; 30 31 32 33 37 public class NewLibraryPanel extends javax.swing.JPanel { 38 39 private LibrariesModel model; 40 private Map <Integer ,String > typeMap; 41 42 private DialogDescriptor dd; 43 44 private static final Pattern VALID_LIBRARY_NAME = Pattern.compile("[-._a-zA-Z0-9]+"); 46 47 public NewLibraryPanel (LibrariesModel model, String preselectedLibraryType) { 48 this.model = model; 49 initComponents(); 50 this.name.setColumns(25); 51 this.name.getDocument().addDocumentListener(new javax.swing.event.DocumentListener () { 52 public void insertUpdate(javax.swing.event.DocumentEvent e) { 53 nameChanged(); 54 } 55 56 public void removeUpdate(javax.swing.event.DocumentEvent e) { 57 nameChanged(); 58 } 59 60 public void changedUpdate(javax.swing.event.DocumentEvent e) { 61 nameChanged(); 62 } 63 64 }); 65 this.initModel (preselectedLibraryType); 66 Color c = javax.swing.UIManager.getColor("nb.errorForeground"); if (c == null) { 68 c = new Color (89,79,191); } 70 status.setForeground(c); 71 } 72 73 void setDialogDescriptor(DialogDescriptor dd) { 74 this.dd = dd; 75 } 76 77 public String getLibraryType () { 78 return typeMap.get(libraryType.getSelectedIndex()); 79 } 80 81 public String getLibraryName () { 82 return this.name.getText(); 83 } 84 85 public void addNotify() { 86 super.addNotify(); 87 this.name.selectAll(); 88 } 89 90 91 private void initModel (String preselectedLibraryType) { 92 this.typeMap = new HashMap <Integer ,String >(); 93 this.name.setText (NbBundle.getMessage (NewLibraryPanel.class,"TXT_NewLibrary")); 94 LibraryTypeRegistry regs = LibraryTypeRegistry.getDefault(); 95 LibraryTypeProvider[] providers = regs.getLibraryTypeProviders(); 96 int index = 0; 97 for (int i=0; i< providers.length; i++) { 98 String type = providers[i].getLibraryType(); 99 if (type.equals(preselectedLibraryType)) { 100 index = i; 101 } 102 typeMap.put(i ,type); 103 String displayName = providers[i].getDisplayName(); 104 if (displayName == null) { 105 displayName = providers[i].getLibraryType(); 106 } 107 this.libraryType.addItem (displayName); 108 } 109 if (this.libraryType.getItemCount() > 0) { 110 this.libraryType.setSelectedIndex(index); 111 } 112 } 113 114 115 private void nameChanged () { 116 String name = this.name.getText(); 117 boolean valid = false; 118 String message; 119 if (name.length() == 0) { 120 message = NbBundle.getMessage(NewLibraryPanel.class,"ERR_InvalidName"); 121 } 122 else { 123 valid = LibrariesCustomizer.isValidName (model, name); 124 if (valid) { 125 if (isReasonableAntProperty(name)) { 126 message = " "; } else { 128 valid = false; 129 message = NbBundle.getMessage(NewLibraryPanel.class,"ERR_InvalidCharacters"); 130 } 131 } 132 else { 133 message = NbBundle.getMessage(NewLibraryPanel.class, "ERR_ExistingName", name); 134 } 135 } 136 if (dd != null) { 137 dd.setValid(valid); 138 } 139 this.status.setText(message); 140 } 141 142 private boolean isReasonableAntProperty(String name) { 143 return VALID_LIBRARY_NAME.matcher(name).matches(); 148 } 149 150 155 private void initComponents() { 157 java.awt.GridBagConstraints gridBagConstraints; 158 159 jLabel2 = new javax.swing.JLabel (); 160 name = new javax.swing.JTextField (); 161 jLabel1 = new javax.swing.JLabel (); 162 libraryType = new javax.swing.JComboBox (); 163 status = new javax.swing.JLabel (); 164 165 setLayout(new java.awt.GridBagLayout ()); 166 167 jLabel2.setLabelFor(name); 168 org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "CTL_LibraryName")); gridBagConstraints = new java.awt.GridBagConstraints (); 170 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 171 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 172 gridBagConstraints.insets = new java.awt.Insets (12, 12, 6, 6); 173 add(jLabel2, gridBagConstraints); 174 gridBagConstraints = new java.awt.GridBagConstraints (); 175 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 176 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 177 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 178 gridBagConstraints.weightx = 1.0; 179 gridBagConstraints.insets = new java.awt.Insets (12, 6, 6, 12); 180 add(name, gridBagConstraints); 181 name.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "AD_LibraryName")); 183 jLabel1.setLabelFor(libraryType); 184 org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "CTL_LibraryType")); gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 187 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 188 gridBagConstraints.insets = new java.awt.Insets (6, 12, 6, 6); 189 add(jLabel1, gridBagConstraints); 190 gridBagConstraints = new java.awt.GridBagConstraints (); 191 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 192 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 193 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 194 gridBagConstraints.weightx = 1.0; 195 gridBagConstraints.insets = new java.awt.Insets (6, 6, 6, 12); 196 add(libraryType, gridBagConstraints); 197 libraryType.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "AD_LibraryType")); 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 201 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 202 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 203 gridBagConstraints.weightx = 1.0; 204 gridBagConstraints.insets = new java.awt.Insets (6, 12, 12, 12); 205 add(status, gridBagConstraints); 206 207 java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle"); getAccessibleContext().setAccessibleDescription(bundle.getString("AD_NewLibraryPanel")); } 211 212 private javax.swing.JLabel jLabel1; 214 private javax.swing.JLabel jLabel2; 215 private javax.swing.JComboBox libraryType; 216 private javax.swing.JTextField name; 217 private javax.swing.JLabel status; 218 220 } 221 | Popular Tags |