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-2007 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: package org.netbeans.modules.visualweb.xhtml;
042:
043: //XXX <POST_MIGRATION>
044: // This file does not belong here. Should be part of Property Editors
045: // Revisit post 5.x migration - Winston
046:
047: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
048:
049: import java.util.Collection;
050: import java.util.Iterator;
051: import javax.swing.event.*;
052: import org.netbeans.api.project.Project;
053:
054: import org.openide.DialogDescriptor;
055: import org.openide.NotifyDescriptor;
056: import org.openide.filesystems.FileObject;
057: import org.openide.loaders.DataFolder;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * Panel used for entering the name of a web form to be created
062: */
063: public class FormNamePanel extends javax.swing.JPanel implements
064: DocumentListener {
065:
066: private DialogDescriptor descriptor;
067:
068: private Project project;
069: private String basename;
070:
071: public FormNamePanel(Project project2, String basename) {
072: this .project = project2;
073: this .basename = basename;
074: initComponents();
075: // Pick Default Name
076: String name = getDefaultFormName();
077: nameField.setText(name);
078: nameField.selectAll();
079:
080: nameField.getDocument().addDocumentListener(this );
081: }
082:
083: String getDefaultFormName() {
084: int num = 0;
085: while (true) {
086: num++;
087: String name = basename + Integer.toString(num);
088: if (!isUsedName(name)) {
089: return name;
090: }
091: }
092: }
093:
094: private boolean isUsedName(String name) {
095: FileObject wfolder = null;
096: FileObject bfolder = null;
097:
098: assert project != null;
099: wfolder = JsfProjectUtils.getDocumentRoot(project);
100: bfolder = JsfProjectUtils.getPageBeanRoot(project);
101:
102: // Does the file exist as a jsp?
103: if (wfolder.getFileObject(name, "jsp") != null) {
104: return true;
105: }
106: if (wfolder.getFileObject(name, "jspf") != null) {
107: return true;
108: }
109: if (bfolder.getFileObject(name, "java") != null) {
110: return true;
111: }
112: return false;
113: }
114:
115: private void validateName() {
116: String name = nameField.getText().trim();
117:
118: boolean validName;
119: validName = JsfProjectUtils.isValidJavaFileName(name);
120: if (!validName) {
121: String errorMsg = NbBundle.getMessage(FormNamePanel.class,
122: "NotValidName"); // NOI18N
123: errorLabel.setText(errorMsg);
124: if ((descriptor != null) && descriptor.isValid()) {
125: descriptor.setValid(false);
126: }
127: } else if (isUsedName(name)) {
128: String errorMsg = NbBundle.getMessage(FormNamePanel.class,
129: "UsedName"); // NOI18N
130: errorLabel.setText(errorMsg);
131: if ((descriptor != null) && descriptor.isValid()) {
132: descriptor.setValid(false);
133: }
134: } else {
135: errorLabel.setText("");
136: if ((descriptor != null) && !descriptor.isValid()) {
137: descriptor.setValid(true);
138: }
139: }
140: }
141:
142: // Implements DocumentListener
143:
144: public void changedUpdate(DocumentEvent e) {
145: validateName();
146: }
147:
148: public void insertUpdate(DocumentEvent e) {
149: validateName();
150: }
151:
152: public void removeUpdate(DocumentEvent e) {
153: validateName();
154: }
155:
156: public String getFragmentName() {
157: return nameField.getText().trim();
158: }
159:
160: public void setDescriptor(DialogDescriptor descriptor) {
161: this .descriptor = descriptor;
162: }
163:
164: /** This method is called from within the constructor to
165: * initialize the form.
166: * WARNING: Do NOT modify this code. The content of this method is
167: * always regenerated by the Form Editor.
168: */
169: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
170: private void initComponents() {
171: java.awt.GridBagConstraints gridBagConstraints;
172:
173: nameLabel = new javax.swing.JLabel();
174: nameField = new javax.swing.JTextField();
175: errorLabel = new javax.swing.JLabel();
176:
177: setLayout(new java.awt.GridBagLayout());
178:
179: setPreferredSize(new java.awt.Dimension(400, 200));
180: nameLabel.setDisplayedMnemonic(java.util.ResourceBundle
181: .getBundle(
182: "org/netbeans/modules/visualweb/xhtml/Bundle")
183: .getString("FORM_NAME_LABEL_DISPLAYED_MNEMONIC")
184: .charAt(0));
185: nameLabel.setLabelFor(nameField);
186: nameLabel.setText(java.util.ResourceBundle.getBundle(
187: "org/netbeans/modules/visualweb/xhtml/Bundle")
188: .getString("NewFormLabel"));
189: gridBagConstraints = new java.awt.GridBagConstraints();
190: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
191: gridBagConstraints.insets = new java.awt.Insets(12, 12, 11, 0);
192: add(nameLabel, gridBagConstraints);
193: nameLabel.getAccessibleContext().setAccessibleName(
194: java.util.ResourceBundle.getBundle(
195: "org/netbeans/modules/visualweb/xhtml/Bundle")
196: .getString("NewFormLabelAccessibleName"));
197: nameLabel.getAccessibleContext().setAccessibleDescription(
198: java.util.ResourceBundle.getBundle(
199: "org/netbeans/modules/visualweb/xhtml/Bundle")
200: .getString("NewFormLabelAccessibleDesc"));
201:
202: gridBagConstraints = new java.awt.GridBagConstraints();
203: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
204: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
205: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
206: gridBagConstraints.weightx = 0.8;
207: gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 11);
208: add(nameField, gridBagConstraints);
209: nameField.getAccessibleContext().setAccessibleName(
210: java.util.ResourceBundle.getBundle(
211: "org/netbeans/modules/visualweb/xhtml/Bundle")
212: .getString("NewFormTextFieldAccessibleName"));
213: nameField.getAccessibleContext().setAccessibleDescription(
214: java.util.ResourceBundle.getBundle(
215: "org/netbeans/modules/visualweb/xhtml/Bundle")
216: .getString("NewFormTextFieldAccessibleDesc"));
217:
218: errorLabel.setForeground(java.awt.Color.red);
219: gridBagConstraints = new java.awt.GridBagConstraints();
220: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
221: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
222: gridBagConstraints.insets = new java.awt.Insets(12, 12, 11, 11);
223: add(errorLabel, gridBagConstraints);
224:
225: }// </editor-fold>//GEN-END:initComponents
226:
227: // Variables declaration - do not modify//GEN-BEGIN:variables
228: private javax.swing.JLabel errorLabel;
229: private javax.swing.JTextField nameField;
230: private javax.swing.JLabel nameLabel;
231: // End of variables declaration//GEN-END:variables
232:
233: }
|