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 1997-2006 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.project.libraries.ui;
043:
044: import java.awt.Color;
045: import java.awt.Component;
046: import java.util.HashMap;
047: import java.util.Map;
048: import java.util.regex.Pattern;
049: import javax.swing.DefaultComboBoxModel;
050: import javax.swing.DefaultListCellRenderer;
051: import javax.swing.JList;
052: import org.openide.DialogDescriptor;
053: import org.openide.util.NbBundle;
054: import org.netbeans.modules.project.libraries.LibraryTypeRegistry;
055: import org.netbeans.spi.project.libraries.LibraryStorageArea;
056: import org.netbeans.spi.project.libraries.LibraryTypeProvider;
057:
058: public class NewLibraryPanel extends javax.swing.JPanel {
059:
060: private LibrariesModel model;
061: private Map<Integer, String> typeMap;
062: private LibraryStorageArea area;
063:
064: private DialogDescriptor dd;
065:
066: private static final Pattern VALID_LIBRARY_NAME = Pattern
067: .compile("[-._a-zA-Z0-9]+"); // NOI18N
068:
069: public NewLibraryPanel(LibrariesModel model,
070: String preselectedLibraryType, LibraryStorageArea area) {
071: this .model = model;
072: this .area = area;
073: initComponents();
074: this .name.setColumns(25);
075: this .name.getDocument().addDocumentListener(
076: new javax.swing.event.DocumentListener() {
077: public void insertUpdate(
078: javax.swing.event.DocumentEvent e) {
079: nameChanged();
080: }
081:
082: public void removeUpdate(
083: javax.swing.event.DocumentEvent e) {
084: nameChanged();
085: }
086:
087: public void changedUpdate(
088: javax.swing.event.DocumentEvent e) {
089: nameChanged();
090: }
091:
092: });
093: initModel(preselectedLibraryType);
094: Color c = javax.swing.UIManager.getColor("nb.errorForeground"); //NOI18N
095: if (c == null) {
096: c = new Color(89, 79, 191); // RGB suggested by Bruce in #28466
097: }
098: status.setForeground(c);
099: }
100:
101: public void setDialogDescriptor(DialogDescriptor dd) {
102: assert this .dd == null;
103: this .dd = dd;
104: nameChanged();
105: }
106:
107: public String getLibraryType() {
108: return typeMap.get(libraryType.getSelectedIndex());
109: }
110:
111: public String getLibraryName() {
112: return this .name.getText();
113: }
114:
115: public void addNotify() {
116: super .addNotify();
117: this .name.selectAll();
118: }
119:
120: private void initModel(String preselectedLibraryType) {
121: this .typeMap = new HashMap<Integer, String>();
122: this .name.setText(NbBundle.getMessage(NewLibraryPanel.class,
123: "TXT_NewLibrary"));
124: LibraryTypeRegistry regs = LibraryTypeRegistry.getDefault();
125: LibraryTypeProvider[] providers = regs
126: .getLibraryTypeProviders();
127: int index = 0;
128: for (int i = 0; i < providers.length; i++) {
129: String type = providers[i].getLibraryType();
130: if (type.equals(preselectedLibraryType)) {
131: index = i;
132: }
133: typeMap.put(i, type);
134: String displayName = providers[i].getDisplayName();
135: if (displayName == null) {
136: displayName = providers[i].getLibraryType();
137: }
138: this .libraryType.addItem(displayName);
139: }
140: if (this .libraryType.getItemCount() > 0) {
141: this .libraryType.setSelectedIndex(index);
142: }
143: }
144:
145: private void nameChanged() {
146: String name = this .name.getText();
147: boolean valid = false;
148: String message;
149: if (name.length() == 0) {
150: message = NbBundle.getMessage(NewLibraryPanel.class,
151: "ERR_InvalidName");
152: } else {
153: valid = LibrariesCustomizer.isValidName(model, name, area);
154: if (valid) {
155: if (isReasonableAntProperty(name)) {
156: message = " "; //NOI18N
157: } else {
158: valid = false;
159: message = NbBundle.getMessage(
160: NewLibraryPanel.class,
161: "ERR_InvalidCharacters");
162: }
163: } else {
164: message = NbBundle.getMessage(NewLibraryPanel.class,
165: "ERR_ExistingName", name);
166: }
167: }
168: if (dd != null) {
169: dd.setValid(valid);
170: }
171: this .status.setText(message);
172: }
173:
174: private boolean isReasonableAntProperty(String name) {
175: // XXX: there is method in PropertyUtils.isUsablePropertyName()
176: // which should be used here but that would create dependency
177: // on ant/project modules which is not desirable.
178: // XXX: The restriction of display name should be fixed in promo F
179: return VALID_LIBRARY_NAME.matcher(name).matches();
180: }
181:
182: /** This method is called from within the constructor to
183: * initialize the form.
184: * WARNING: Do NOT modify this code. The content of this method is
185: * always regenerated by the Form Editor.
186: */
187: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
188: private void initComponents() {
189: java.awt.GridBagConstraints gridBagConstraints;
190:
191: jLabel2 = new javax.swing.JLabel();
192: name = new javax.swing.JTextField();
193: jLabel1 = new javax.swing.JLabel();
194: libraryType = new javax.swing.JComboBox();
195: status = new javax.swing.JLabel();
196:
197: setLayout(new java.awt.GridBagLayout());
198:
199: jLabel2.setLabelFor(name);
200: org.openide.awt.Mnemonics.setLocalizedText(jLabel2,
201: org.openide.util.NbBundle.getMessage(
202: NewLibraryPanel.class, "CTL_LibraryName")); // NOI18N
203: gridBagConstraints = new java.awt.GridBagConstraints();
204: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
205: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
206: gridBagConstraints.insets = new java.awt.Insets(12, 12, 6, 6);
207: add(jLabel2, gridBagConstraints);
208: gridBagConstraints = new java.awt.GridBagConstraints();
209: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
210: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
211: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
212: gridBagConstraints.weightx = 1.0;
213: gridBagConstraints.insets = new java.awt.Insets(12, 6, 6, 12);
214: add(name, gridBagConstraints);
215: name.getAccessibleContext().setAccessibleDescription(
216: org.openide.util.NbBundle.getMessage(
217: NewLibraryPanel.class, "AD_LibraryName")); // NOI18N
218:
219: jLabel1.setLabelFor(libraryType);
220: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
221: org.openide.util.NbBundle.getMessage(
222: NewLibraryPanel.class, "CTL_LibraryType")); // NOI18N
223: gridBagConstraints = new java.awt.GridBagConstraints();
224: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
225: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
226: gridBagConstraints.insets = new java.awt.Insets(6, 12, 6, 6);
227: add(jLabel1, gridBagConstraints);
228: gridBagConstraints = new java.awt.GridBagConstraints();
229: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
230: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
231: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
232: gridBagConstraints.weightx = 1.0;
233: gridBagConstraints.insets = new java.awt.Insets(6, 6, 6, 12);
234: add(libraryType, gridBagConstraints);
235: libraryType.getAccessibleContext().setAccessibleDescription(
236: org.openide.util.NbBundle.getMessage(
237: NewLibraryPanel.class, "AD_LibraryType")); // NOI18N
238:
239: gridBagConstraints = new java.awt.GridBagConstraints();
240: gridBagConstraints.gridx = 0;
241: gridBagConstraints.gridy = 3;
242: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
243: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
244: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
245: gridBagConstraints.weightx = 1.0;
246: gridBagConstraints.insets = new java.awt.Insets(6, 12, 12, 12);
247: add(status, gridBagConstraints);
248:
249: java.util.ResourceBundle bundle = java.util.ResourceBundle
250: .getBundle("org/netbeans/modules/project/libraries/ui/Bundle"); // NOI18N
251: getAccessibleContext().setAccessibleDescription(
252: bundle.getString("AD_NewLibraryPanel")); // NOI18N
253: }// </editor-fold>//GEN-END:initComponents
254:
255: // Variables declaration - do not modify//GEN-BEGIN:variables
256: private javax.swing.JLabel jLabel1;
257: private javax.swing.JLabel jLabel2;
258: private javax.swing.JComboBox libraryType;
259: private javax.swing.JTextField name;
260: private javax.swing.JLabel status;
261: // End of variables declaration//GEN-END:variables
262:
263: }
|