001: /*
002: * AbstractNameValidationDialog.java
003: *
004: * Created on January 28, 2007, 9:11 PM
005: */
006:
007: package org.netbeans.modules.vmd.game.dialog;
008:
009: import java.awt.Color;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.awt.event.FocusEvent;
013: import java.awt.event.FocusListener;
014: import javax.swing.Icon;
015: import javax.swing.ImageIcon;
016: import javax.swing.event.DocumentEvent;
017: import javax.swing.event.DocumentListener;
018: import org.openide.DialogDescriptor;
019: import org.openide.NotifyDescriptor;
020: import org.openide.util.Utilities;
021:
022: /**
023: *
024: * @author kherink
025: */
026: public abstract class AbstractNameValidationDialog extends
027: javax.swing.JPanel implements ActionListener {
028:
029: private static final Icon ICON_ERROR = new ImageIcon(
030: Utilities
031: .loadImage("org/netbeans/modules/vmd/midp/resources/error.gif"));
032: private DialogDescriptor dd;
033:
034: private String initialTextContent;
035:
036: /** Creates new form AbstractNameValidationDialog */
037: public AbstractNameValidationDialog(String initialTextContent) {
038: this .initialTextContent = initialTextContent;
039: initComponents();
040: manualInit();
041: }
042:
043: protected abstract String getInitialStateDescriptionText();
044:
045: protected abstract String getNameLabelText();
046:
047: protected abstract String getDialogNameText();
048:
049: protected abstract String getCurrentStateErrorText();
050:
051: protected abstract void handleOKButton();
052:
053: /** This method is called from within the constructor to
054: * initialize the form.
055: * WARNING: Do NOT modify this code. The content of this method is
056: * always regenerated by the Form Editor.
057: */
058: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
059: private void initComponents() {
060:
061: labelSequenceName = new javax.swing.JLabel();
062: fieldName = new javax.swing.JTextField();
063: jPanel1 = new javax.swing.JPanel();
064: labelError = new javax.swing.JLabel();
065:
066: labelSequenceName.setText(org.openide.util.NbBundle.getMessage(
067: AbstractNameValidationDialog.class,
068: "AbstractNameValidationDialog.labelSequenceName.text")); // NOI18N
069:
070: fieldName.setText(org.openide.util.NbBundle.getMessage(
071: AbstractNameValidationDialog.class,
072: "AbstractNameValidationDialog.fieldName.text")); // NOI18N
073:
074: labelError.setText(org.openide.util.NbBundle.getMessage(
075: AbstractNameValidationDialog.class,
076: "AbstractNameValidationDialog.labelError.text")); // NOI18N
077:
078: org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(
079: jPanel1);
080: jPanel1.setLayout(jPanel1Layout);
081: jPanel1Layout.setHorizontalGroup(jPanel1Layout
082: .createParallelGroup(
083: org.jdesktop.layout.GroupLayout.LEADING).add(
084: labelError,
085: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
086: 364, Short.MAX_VALUE));
087: jPanel1Layout
088: .setVerticalGroup(jPanel1Layout
089: .createParallelGroup(
090: org.jdesktop.layout.GroupLayout.LEADING)
091: .add(
092: org.jdesktop.layout.GroupLayout.TRAILING,
093: jPanel1Layout
094: .createSequentialGroup()
095: .addContainerGap()
096: .add(
097: labelError,
098: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
099: 32, Short.MAX_VALUE)));
100:
101: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
102: this );
103: this .setLayout(layout);
104: layout
105: .setHorizontalGroup(layout
106: .createParallelGroup(
107: org.jdesktop.layout.GroupLayout.LEADING)
108: .add(
109: layout
110: .createSequentialGroup()
111: .addContainerGap()
112: .add(
113: layout
114: .createParallelGroup(
115: org.jdesktop.layout.GroupLayout.LEADING)
116: .add(
117: layout
118: .createSequentialGroup()
119: .add(
120: labelSequenceName)
121: .addPreferredGap(
122: org.jdesktop.layout.LayoutStyle.RELATED)
123: .add(
124: fieldName,
125: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
126: 314,
127: Short.MAX_VALUE))
128: .add(
129: jPanel1,
130: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
131: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
132: Short.MAX_VALUE))
133: .addContainerGap()));
134: layout
135: .setVerticalGroup(layout
136: .createParallelGroup(
137: org.jdesktop.layout.GroupLayout.LEADING)
138: .add(
139: org.jdesktop.layout.GroupLayout.TRAILING,
140: layout
141: .createSequentialGroup()
142: .addContainerGap(36,
143: Short.MAX_VALUE)
144: .add(
145: layout
146: .createParallelGroup(
147: org.jdesktop.layout.GroupLayout.BASELINE)
148: .add(
149: labelSequenceName)
150: .add(
151: fieldName,
152: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
153: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
154: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
155: .add(18, 18, 18)
156: .add(
157: jPanel1,
158: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
159: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
160: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)));
161:
162: fieldName
163: .getAccessibleContext()
164: .setAccessibleName(
165: org.openide.util.NbBundle
166: .getMessage(
167: AbstractNameValidationDialog.class,
168: "AbstractNameValidationDialog.findName.accessible.name")); // NOI18N
169: fieldName
170: .getAccessibleContext()
171: .setAccessibleDescription(
172: org.openide.util.NbBundle
173: .getMessage(
174: AbstractNameValidationDialog.class,
175: "AbstractNameValidationDialog.findName.accessible.description")); // NOI18N
176: }// </editor-fold>//GEN-END:initComponents
177:
178: // Variables declaration - do not modify//GEN-BEGIN:variables
179: protected javax.swing.JTextField fieldName;
180: private javax.swing.JPanel jPanel1;
181: private javax.swing.JLabel labelError;
182: private javax.swing.JLabel labelSequenceName;
183:
184: // End of variables declaration//GEN-END:variables
185:
186: private void manualInit() {
187: this .fieldName.setText(this .initialTextContent);
188: this .fieldName.selectAll();
189: this .fieldName.getDocument().addDocumentListener(
190: new LayerFieldListener());
191:
192: this .labelError.setForeground(Color.RED);
193: this .labelSequenceName.setLabelFor(this .fieldName);
194:
195: this .labelError.setIcon(ICON_ERROR);
196: this .labelError.setText(getInitialStateDescriptionText());
197: }
198:
199: public void setDialogDescriptor(DialogDescriptor dd) {
200: this .dd = dd;
201: this .dd.setValid(false);
202: }
203:
204: private static boolean isValidJavaIdentifier(String str) {
205: if (str == null || "".equals(str)) {
206: return false;
207: }
208:
209: if (!Character.isJavaIdentifierStart(str.charAt(0))) {
210: return false;
211: }
212: for (int i = 1; i < str.length(); i++) {
213: if (!Character.isJavaIdentifierPart(str.charAt(i))) {
214: return false;
215: }
216: }
217: return true;
218: }
219:
220: private class LayerFieldListener implements DocumentListener,
221: FocusListener {
222: private boolean error;
223:
224: public void insertUpdate(DocumentEvent e) {
225: this .handleTextContentChange();
226: }
227:
228: public void removeUpdate(DocumentEvent e) {
229: this .handleTextContentChange();
230: }
231:
232: public void changedUpdate(DocumentEvent e) {
233: this .handleTextContentChange();
234: }
235:
236: private void handleTextContentChange() {
237: String errMsg = null;
238: if (!isValidJavaIdentifier(AbstractNameValidationDialog.this .fieldName
239: .getText())) {
240: errMsg = "Name must be a valid Java identifier.";
241: } else {
242: errMsg = AbstractNameValidationDialog.this
243: .getCurrentStateErrorText();
244: }
245:
246: if (errMsg != null) {
247: AbstractNameValidationDialog.this .labelError
248: .setText(errMsg);
249: AbstractNameValidationDialog.this .labelError
250: .setIcon(ICON_ERROR);
251: AbstractNameValidationDialog.this .dd.setValid(false);
252: } else {
253: AbstractNameValidationDialog.this .labelError
254: .setText("");
255: AbstractNameValidationDialog.this .labelError
256: .setIcon(null);
257: AbstractNameValidationDialog.this .dd.setValid(true);
258: }
259: }
260:
261: public void focusGained(FocusEvent e) {
262: this .handleTextContentChange();
263: }
264:
265: public void focusLost(FocusEvent e) {
266: }
267: }
268:
269: public void actionPerformed(ActionEvent e) {
270: //if OK button pressed create the new layer
271: if (e.getSource() == NotifyDescriptor.OK_OPTION) {
272: this.handleOKButton();
273: }
274: }
275:
276: }
|