KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > loader > FileRecognitionPanel


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.apisupport.project.ui.wizard.loader;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import javax.swing.ButtonGroup JavaDoc;
27 import javax.swing.event.DocumentEvent JavaDoc;
28 import javax.swing.event.DocumentListener JavaDoc;
29 import org.netbeans.modules.apisupport.project.ui.UIUtil;
30 import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
31 import org.openide.WizardDescriptor;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.NbBundle;
34
35 /**
36  * the first panel in loaders wizard
37  *
38  * @author Milos Kleint
39  */

40 final class FileRecognitionPanel extends BasicWizardIterator.Panel {
41     
42     private static final Pattern JavaDoc EXTENSION_PATTERN = Pattern.compile("([.]?[a-zA-Z0-9_]+){1}([ ,]+[.]?[a-zA-Z0-9_]+)*[ ]*"); // NOI18N
43
private static final Pattern JavaDoc ELEMENT_PATTERN = Pattern.compile("(application/([a-zA-Z0-9_.-])*\\+xml|text/([a-zA-Z0-9_.-])*\\+xml)"); // NOI18N
44
private static final Pattern JavaDoc MIME_TYPE_PATTERN = Pattern.compile("(application|text|image|audio|video)/([a-zA-Z0-9_.+-])+"); // NOI18N
45
private static final String JavaDoc DEFAULT_MIME_TYPE = "text/x-<type>"; // NOI18N
46

47     private NewLoaderIterator.DataModel data;
48     private ButtonGroup JavaDoc group;
49     private boolean listenersAttached;
50     private DocumentListener JavaDoc docList;
51     
52     /**
53      * Creates new form FileRecognitionPanel
54      */

55     public FileRecognitionPanel(WizardDescriptor setting, NewLoaderIterator.DataModel data) {
56         super(setting);
57         this.data = data;
58         initComponents();
59         initAccessibility();
60         group = new ButtonGroup JavaDoc();
61         group.add(rbByElement);
62         group.add(rbByExtension);
63         ActionListener JavaDoc list = new ActionListener JavaDoc() {
64             public void actionPerformed(ActionEvent JavaDoc event) {
65                 txtExtension.setEnabled(rbByExtension.isSelected());
66                 txtNamespace.setEnabled(rbByElement.isSelected());
67                 checkValidity();
68             }
69         };
70         docList = new UIUtil.DocumentAdapter() {
71             public void insertUpdate(DocumentEvent JavaDoc e) {
72                 checkValidity();
73             }
74         };
75         
76         rbByElement.addActionListener(list);
77         rbByExtension.addActionListener(list);
78         
79         putClientProperty("NewFileWizard_Title", getMessage("LBL_LoaderWizardTitle"));
80     }
81     
82     private void checkValidity() {
83         markValid();
84         String JavaDoc txt = txtMimeType.getText().trim();
85         
86         if (txt.length() == 0 || DEFAULT_MIME_TYPE.equals(txt) || (!MIME_TYPE_PATTERN.matcher(txt).matches())) {
87             setError(getMessage("MSG_NotValidMimeType"));
88         } else {
89             if (rbByElement.isSelected()) {
90                 if (txtNamespace.getText().trim().length() == 0) {
91                     setError(getMessage("MSG_NoNamespace"));
92                 } else {
93                     Matcher JavaDoc match = ELEMENT_PATTERN.matcher(txt);
94                     if (!match.matches()) {
95                         setError(getMessage("MSG_BadMimeTypeForXML"));
96                     }
97                 }
98             } else {
99                 if (txtExtension.getText().trim().length() == 0) {
100                     setError(getMessage("MSG_NoExtension"));
101                 } else {
102                     Matcher JavaDoc match = EXTENSION_PATTERN.matcher(txtExtension.getText());
103                     if (!match.matches()) {
104                         setError(getMessage("MSG_BadExtensionPattern"));
105                     }
106                 }
107             }
108         }
109     }
110     
111     public void addNotify() {
112         super.addNotify();
113         attachDocumentListeners();
114         checkValidity();
115     }
116     
117     public void removeNotify() {
118         // prevent checking when the panel is not "active"
119
removeDocumentListeners();
120         super.removeNotify();
121     }
122     
123     private void attachDocumentListeners() {
124         if (!listenersAttached) {
125             txtNamespace.getDocument().addDocumentListener(docList);
126             txtExtension.getDocument().addDocumentListener(docList);
127             txtMimeType.getDocument().addDocumentListener(docList);
128             listenersAttached = true;
129         }
130     }
131     
132     private void removeDocumentListeners() {
133         if (listenersAttached) {
134             txtNamespace.getDocument().removeDocumentListener(docList);
135             txtExtension.getDocument().removeDocumentListener(docList);
136             txtMimeType.getDocument().removeDocumentListener(docList);
137             listenersAttached = false;
138         }
139     }
140     
141     
142     protected void storeToDataModel() {
143         data.setMimeType(txtMimeType.getText().trim());
144         data.setExtensionBased(rbByExtension.isSelected());
145         if (data.isExtensionBased()) {
146             data.setExtension(txtExtension.getText().trim());
147             data.setNamespace(null);
148         } else {
149             data.setExtension(null);
150             data.setNamespace(txtNamespace.getText().trim());
151         }
152     }
153     
154     protected void readFromDataModel() {
155         String JavaDoc mime = data.getMimeType();
156         if (mime == null) {
157             mime = DEFAULT_MIME_TYPE;
158         }
159         txtMimeType.setText(mime);
160         if (mime.equals(DEFAULT_MIME_TYPE)) {
161             txtMimeType.select(DEFAULT_MIME_TYPE.length() - 6, DEFAULT_MIME_TYPE.length());
162         }
163         if (data.isExtensionBased()) {
164             rbByExtension.setSelected(true);
165         } else {
166             rbByElement.setSelected(true);
167         }
168         txtExtension.setEnabled(rbByExtension.isSelected());
169         txtNamespace.setEnabled(rbByElement.isSelected());
170         txtExtension.setText(data.getExtension());
171         txtNamespace.setText(data.getNamespace());
172         
173         checkValidity();
174     }
175     
176     protected String JavaDoc getPanelName() {
177         return getMessage("LBL_FileRecognition_Title");
178     }
179     
180     protected HelpCtx getHelp() {
181         return new HelpCtx(FileRecognitionPanel.class);
182     }
183     
184     private static String JavaDoc getMessage(String JavaDoc key) {
185         return NbBundle.getMessage(FileRecognitionPanel.class, key);
186     }
187     
188     /** This method is called from within the constructor to
189      * initialize the form.
190      * WARNING: Do NOT modify this code. The content of this method is
191      * always regenerated by the Form Editor.
192      */

193     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
194
private void initComponents() {
195         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
196
197         lblMimeType = new javax.swing.JLabel JavaDoc();
198         txtMimeType = new javax.swing.JTextField JavaDoc();
199         rbByExtension = new javax.swing.JRadioButton JavaDoc();
200         lblExtension = new javax.swing.JLabel JavaDoc();
201         txtExtension = new javax.swing.JTextField JavaDoc();
202         rbByElement = new javax.swing.JRadioButton JavaDoc();
203         lblNamespace = new javax.swing.JLabel JavaDoc();
204         txtNamespace = new javax.swing.JTextField JavaDoc();
205
206         setLayout(new java.awt.GridBagLayout JavaDoc());
207
208         lblMimeType.setLabelFor(txtMimeType);
209         org.openide.awt.Mnemonics.setLocalizedText(lblMimeType, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_MimeType"));
210         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
211         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
212         add(lblMimeType, gridBagConstraints);
213
214         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
215         gridBagConstraints.gridwidth = 2;
216         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
217         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
218         gridBagConstraints.weightx = 0.1;
219         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 0, 0);
220         add(txtMimeType, gridBagConstraints);
221
222         org.openide.awt.Mnemonics.setLocalizedText(rbByExtension, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_ByExtension"));
223         rbByExtension.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
224         rbByExtension.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
225         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
226         gridBagConstraints.gridx = 0;
227         gridBagConstraints.gridy = 1;
228         gridBagConstraints.gridwidth = 3;
229         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
230         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
231         add(rbByExtension, gridBagConstraints);
232
233         lblExtension.setLabelFor(txtExtension);
234         org.openide.awt.Mnemonics.setLocalizedText(lblExtension, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_Extension"));
235         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
236         gridBagConstraints.gridx = 0;
237         gridBagConstraints.gridy = 2;
238         gridBagConstraints.gridwidth = 2;
239         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
240         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
241         add(lblExtension, gridBagConstraints);
242
243         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
244         gridBagConstraints.gridx = 2;
245         gridBagConstraints.gridy = 2;
246         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
247         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
248         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
249         add(txtExtension, gridBagConstraints);
250
251         org.openide.awt.Mnemonics.setLocalizedText(rbByElement, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_ByElement"));
252         rbByElement.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
253         rbByElement.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
254         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
255         gridBagConstraints.gridx = 0;
256         gridBagConstraints.gridy = 4;
257         gridBagConstraints.gridwidth = 3;
258         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
259         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
260         add(rbByElement, gridBagConstraints);
261
262         lblNamespace.setLabelFor(txtNamespace);
263         org.openide.awt.Mnemonics.setLocalizedText(lblNamespace, org.openide.util.NbBundle.getMessage(FileRecognitionPanel.class, "LBL_Element"));
264         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
265         gridBagConstraints.gridx = 0;
266         gridBagConstraints.gridy = 5;
267         gridBagConstraints.gridwidth = 2;
268         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
269         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 0, 0, 0);
270         add(lblNamespace, gridBagConstraints);
271
272         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
273         gridBagConstraints.gridx = 2;
274         gridBagConstraints.gridy = 5;
275         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
276         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
277         gridBagConstraints.weighty = 0.1;
278         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 6, 0, 0);
279         add(txtNamespace, gridBagConstraints);
280
281     }
282     // </editor-fold>//GEN-END:initComponents
283

284     // Variables declaration - do not modify//GEN-BEGIN:variables
285
private javax.swing.JLabel JavaDoc lblExtension;
286     private javax.swing.JLabel JavaDoc lblMimeType;
287     private javax.swing.JLabel JavaDoc lblNamespace;
288     private javax.swing.JRadioButton JavaDoc rbByElement;
289     private javax.swing.JRadioButton JavaDoc rbByExtension;
290     private javax.swing.JTextField JavaDoc txtExtension;
291     private javax.swing.JTextField JavaDoc txtMimeType;
292     private javax.swing.JTextField JavaDoc txtNamespace;
293     // End of variables declaration//GEN-END:variables
294

295     private void initAccessibility() {
296         this.getAccessibleContext().setAccessibleDescription(getMessage("ACS_FileRecognitionPanel"));
297         rbByElement.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_ByElement"));
298         rbByExtension.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_ByExtension"));
299         txtExtension.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_Extension"));
300         txtNamespace.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_Namespace"));
301         txtMimeType.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_Mimetype"));
302     }
303 }
304
Popular Tags