KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > libraries > ui > NewLibraryPanel


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

19
20 package org.netbeans.modules.project.libraries.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
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 /**
34  *
35  * @author tom
36  */

37 public class NewLibraryPanel extends javax.swing.JPanel JavaDoc {
38
39     private LibrariesModel model;
40     private Map JavaDoc<Integer JavaDoc,String JavaDoc> typeMap;
41
42     private DialogDescriptor dd;
43
44     private static final Pattern JavaDoc VALID_LIBRARY_NAME = Pattern.compile("[-._a-zA-Z0-9]+"); // NOI18N
45

46     /** Creates new form NewLibraryPanel */
47     public NewLibraryPanel (LibrariesModel model, String JavaDoc preselectedLibraryType) {
48         this.model = model;
49         initComponents();
50         this.name.setColumns(25);
51         this.name.getDocument().addDocumentListener(new javax.swing.event.DocumentListener JavaDoc () {
52             public void insertUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
53                 nameChanged();
54             }
55
56             public void removeUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
57                 nameChanged();
58             }
59
60             public void changedUpdate(javax.swing.event.DocumentEvent JavaDoc e) {
61                 nameChanged();
62             }
63
64         });
65         this.initModel (preselectedLibraryType);
66         Color JavaDoc c = javax.swing.UIManager.getColor("nb.errorForeground"); //NOI18N
67
if (c == null) {
68             c = new Color JavaDoc(89,79,191); // RGB suggested by Bruce in #28466
69
}
70         status.setForeground(c);
71     }
72     
73     void setDialogDescriptor(DialogDescriptor dd) {
74         this.dd = dd;
75     }
76     
77     public String JavaDoc getLibraryType () {
78         return typeMap.get(libraryType.getSelectedIndex());
79     }
80     
81     public String JavaDoc 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 JavaDoc preselectedLibraryType) {
92         this.typeMap = new HashMap JavaDoc<Integer JavaDoc,String JavaDoc>();
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 JavaDoc type = providers[i].getLibraryType();
99             if (type.equals(preselectedLibraryType)) {
100                 index = i;
101             }
102             typeMap.put(i ,type);
103             String JavaDoc 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 JavaDoc name = this.name.getText();
117         boolean valid = false;
118         String JavaDoc 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 = " "; //NOI18N
127
} 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 JavaDoc name) {
143         // XXX: there is method in PropertyUtils.isUsablePropertyName()
144
// which should be used here but that would create dependency
145
// on ant/project modules which is not desirable.
146
// XXX: The restriction of display name should be fixed in promo F
147
return VALID_LIBRARY_NAME.matcher(name).matches();
148     }
149     
150     /** This method is called from within the constructor to
151      * initialize the form.
152      * WARNING: Do NOT modify this code. The content of this method is
153      * always regenerated by the Form Editor.
154      */

155     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
156
private void initComponents() {
157         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
158
159         jLabel2 = new javax.swing.JLabel JavaDoc();
160         name = new javax.swing.JTextField JavaDoc();
161         jLabel1 = new javax.swing.JLabel JavaDoc();
162         libraryType = new javax.swing.JComboBox JavaDoc();
163         status = new javax.swing.JLabel JavaDoc();
164
165         setLayout(new java.awt.GridBagLayout JavaDoc());
166
167         jLabel2.setLabelFor(name);
168         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "CTL_LibraryName")); // NOI18N
169
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
170         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
171         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
172         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 6, 6);
173         add(jLabel2, gridBagConstraints);
174         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(12, 6, 6, 12);
180         add(name, gridBagConstraints);
181         name.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "AD_LibraryName")); // NOI18N
182

183         jLabel1.setLabelFor(libraryType);
184         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "CTL_LibraryType")); // NOI18N
185
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
187         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
188         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 12, 6, 6);
189         add(jLabel1, gridBagConstraints);
190         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(6, 6, 6, 12);
196         add(libraryType, gridBagConstraints);
197         libraryType.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(NewLibraryPanel.class, "AD_LibraryType")); // NOI18N
198

199         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(6, 12, 12, 12);
205         add(status, gridBagConstraints);
206
207         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle"); // NOI18N
208
getAccessibleContext().setAccessibleDescription(bundle.getString("AD_NewLibraryPanel")); // NOI18N
209
}// </editor-fold>//GEN-END:initComponents
210

211     
212     // Variables declaration - do not modify//GEN-BEGIN:variables
213
private javax.swing.JLabel JavaDoc jLabel1;
214     private javax.swing.JLabel JavaDoc jLabel2;
215     private javax.swing.JComboBox JavaDoc libraryType;
216     private javax.swing.JTextField JavaDoc name;
217     private javax.swing.JLabel JavaDoc status;
218     // End of variables declaration//GEN-END:variables
219

220 }
221
Popular Tags