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.apisupport.project.ui.wizard.loader;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048: import javax.swing.ButtonGroup;
049: import javax.swing.event.DocumentEvent;
050: import javax.swing.event.DocumentListener;
051: import org.netbeans.modules.apisupport.project.ui.UIUtil;
052: import org.netbeans.modules.apisupport.project.ui.wizard.BasicWizardIterator;
053: import org.openide.WizardDescriptor;
054: import org.openide.util.HelpCtx;
055: import org.openide.util.NbBundle;
056:
057: /**
058: * the first panel in loaders wizard
059: *
060: * @author Milos Kleint
061: */
062: final class FileRecognitionPanel extends BasicWizardIterator.Panel {
063:
064: private static final Pattern EXTENSION_PATTERN = Pattern
065: .compile("([.]?[a-zA-Z0-9_]+){1}([ ,]+[.]?[a-zA-Z0-9_]+)*[ ]*"); // NOI18N
066: private static final Pattern ELEMENT_PATTERN = Pattern
067: .compile("(application/([a-zA-Z0-9_.-])*\\+xml|text/([a-zA-Z0-9_.-])*\\+xml)"); // NOI18N
068: private static final Pattern MIME_TYPE_PATTERN = Pattern
069: .compile("(application|text|image|audio|video)/([a-zA-Z0-9_.+-])+"); // NOI18N
070: private static final String DEFAULT_MIME_TYPE = "text/x-<type>"; // NOI18N
071:
072: private NewLoaderIterator.DataModel data;
073: private ButtonGroup group;
074: private boolean listenersAttached;
075: private DocumentListener docList;
076:
077: /**
078: * Creates new form FileRecognitionPanel
079: */
080: public FileRecognitionPanel(WizardDescriptor setting,
081: NewLoaderIterator.DataModel data) {
082: super (setting);
083: this .data = data;
084: initComponents();
085: initAccessibility();
086: group = new ButtonGroup();
087: group.add(rbByElement);
088: group.add(rbByExtension);
089: ActionListener list = new ActionListener() {
090: public void actionPerformed(ActionEvent event) {
091: txtExtension.setEnabled(rbByExtension.isSelected());
092: txtNamespace.setEnabled(rbByElement.isSelected());
093: checkValidity();
094: }
095: };
096: docList = new UIUtil.DocumentAdapter() {
097: public void insertUpdate(DocumentEvent e) {
098: checkValidity();
099: }
100: };
101:
102: rbByElement.addActionListener(list);
103: rbByExtension.addActionListener(list);
104:
105: putClientProperty("NewFileWizard_Title",
106: getMessage("LBL_LoaderWizardTitle"));
107: }
108:
109: private void checkValidity() {
110: markValid();
111: String txt = txtMimeType.getText().trim();
112:
113: if (txt.length() == 0 || DEFAULT_MIME_TYPE.equals(txt)
114: || (!MIME_TYPE_PATTERN.matcher(txt).matches())) {
115: setError(getMessage("MSG_NotValidMimeType"));
116: } else {
117: if (rbByElement.isSelected()) {
118: if (txtNamespace.getText().trim().length() == 0) {
119: setError(getMessage("MSG_NoNamespace"));
120: } else {
121: Matcher match = ELEMENT_PATTERN.matcher(txt);
122: if (!match.matches()) {
123: setError(getMessage("MSG_BadMimeTypeForXML"));
124: }
125: }
126: } else {
127: if (txtExtension.getText().trim().length() == 0) {
128: setError(getMessage("MSG_NoExtension"));
129: } else {
130: Matcher match = EXTENSION_PATTERN
131: .matcher(txtExtension.getText());
132: if (!match.matches()) {
133: setError(getMessage("MSG_BadExtensionPattern"));
134: }
135: }
136: }
137: }
138: }
139:
140: public void addNotify() {
141: super .addNotify();
142: attachDocumentListeners();
143: checkValidity();
144: }
145:
146: public void removeNotify() {
147: // prevent checking when the panel is not "active"
148: removeDocumentListeners();
149: super .removeNotify();
150: }
151:
152: private void attachDocumentListeners() {
153: if (!listenersAttached) {
154: txtNamespace.getDocument().addDocumentListener(docList);
155: txtExtension.getDocument().addDocumentListener(docList);
156: txtMimeType.getDocument().addDocumentListener(docList);
157: listenersAttached = true;
158: }
159: }
160:
161: private void removeDocumentListeners() {
162: if (listenersAttached) {
163: txtNamespace.getDocument().removeDocumentListener(docList);
164: txtExtension.getDocument().removeDocumentListener(docList);
165: txtMimeType.getDocument().removeDocumentListener(docList);
166: listenersAttached = false;
167: }
168: }
169:
170: protected void storeToDataModel() {
171: data.setMimeType(txtMimeType.getText().trim());
172: data.setExtensionBased(rbByExtension.isSelected());
173: if (data.isExtensionBased()) {
174: data.setExtension(txtExtension.getText().trim());
175: data.setNamespace(null);
176: } else {
177: data.setExtension(null);
178: data.setNamespace(txtNamespace.getText().trim());
179: }
180: }
181:
182: protected void readFromDataModel() {
183: String mime = data.getMimeType();
184: if (mime == null) {
185: mime = DEFAULT_MIME_TYPE;
186: }
187: txtMimeType.setText(mime);
188: if (mime.equals(DEFAULT_MIME_TYPE)) {
189: txtMimeType.select(DEFAULT_MIME_TYPE.length() - 6,
190: DEFAULT_MIME_TYPE.length());
191: }
192: if (data.isExtensionBased()) {
193: rbByExtension.setSelected(true);
194: } else {
195: rbByElement.setSelected(true);
196: }
197: txtExtension.setEnabled(rbByExtension.isSelected());
198: txtNamespace.setEnabled(rbByElement.isSelected());
199: txtExtension.setText(data.getExtension());
200: txtNamespace.setText(data.getNamespace());
201:
202: checkValidity();
203: }
204:
205: protected String getPanelName() {
206: return getMessage("LBL_FileRecognition_Title");
207: }
208:
209: protected HelpCtx getHelp() {
210: return new HelpCtx(FileRecognitionPanel.class);
211: }
212:
213: private static String getMessage(String key) {
214: return NbBundle.getMessage(FileRecognitionPanel.class, key);
215: }
216:
217: /** This method is called from within the constructor to
218: * initialize the form.
219: * WARNING: Do NOT modify this code. The content of this method is
220: * always regenerated by the Form Editor.
221: */
222: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
223: private void initComponents() {
224: java.awt.GridBagConstraints gridBagConstraints;
225:
226: lblMimeType = new javax.swing.JLabel();
227: txtMimeType = new javax.swing.JTextField();
228: rbByExtension = new javax.swing.JRadioButton();
229: lblExtension = new javax.swing.JLabel();
230: txtExtension = new javax.swing.JTextField();
231: rbByElement = new javax.swing.JRadioButton();
232: lblNamespace = new javax.swing.JLabel();
233: txtNamespace = new javax.swing.JTextField();
234:
235: setLayout(new java.awt.GridBagLayout());
236:
237: lblMimeType.setLabelFor(txtMimeType);
238: org.openide.awt.Mnemonics.setLocalizedText(lblMimeType,
239: org.openide.util.NbBundle.getMessage(
240: FileRecognitionPanel.class, "LBL_MimeType"));
241: gridBagConstraints = new java.awt.GridBagConstraints();
242: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
243: add(lblMimeType, gridBagConstraints);
244:
245: gridBagConstraints = new java.awt.GridBagConstraints();
246: gridBagConstraints.gridwidth = 2;
247: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
248: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
249: gridBagConstraints.weightx = 0.1;
250: gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
251: add(txtMimeType, gridBagConstraints);
252:
253: org.openide.awt.Mnemonics.setLocalizedText(rbByExtension,
254: org.openide.util.NbBundle.getMessage(
255: FileRecognitionPanel.class, "LBL_ByExtension"));
256: rbByExtension.setBorder(javax.swing.BorderFactory
257: .createEmptyBorder(0, 0, 0, 0));
258: rbByExtension.setMargin(new java.awt.Insets(0, 0, 0, 0));
259: gridBagConstraints = new java.awt.GridBagConstraints();
260: gridBagConstraints.gridx = 0;
261: gridBagConstraints.gridy = 1;
262: gridBagConstraints.gridwidth = 3;
263: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
264: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
265: add(rbByExtension, gridBagConstraints);
266:
267: lblExtension.setLabelFor(txtExtension);
268: org.openide.awt.Mnemonics.setLocalizedText(lblExtension,
269: org.openide.util.NbBundle.getMessage(
270: FileRecognitionPanel.class, "LBL_Extension"));
271: gridBagConstraints = new java.awt.GridBagConstraints();
272: gridBagConstraints.gridx = 0;
273: gridBagConstraints.gridy = 2;
274: gridBagConstraints.gridwidth = 2;
275: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
276: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
277: add(lblExtension, gridBagConstraints);
278:
279: gridBagConstraints = new java.awt.GridBagConstraints();
280: gridBagConstraints.gridx = 2;
281: gridBagConstraints.gridy = 2;
282: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
283: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
284: gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
285: add(txtExtension, gridBagConstraints);
286:
287: org.openide.awt.Mnemonics.setLocalizedText(rbByElement,
288: org.openide.util.NbBundle.getMessage(
289: FileRecognitionPanel.class, "LBL_ByElement"));
290: rbByElement.setBorder(javax.swing.BorderFactory
291: .createEmptyBorder(0, 0, 0, 0));
292: rbByElement.setMargin(new java.awt.Insets(0, 0, 0, 0));
293: gridBagConstraints = new java.awt.GridBagConstraints();
294: gridBagConstraints.gridx = 0;
295: gridBagConstraints.gridy = 4;
296: gridBagConstraints.gridwidth = 3;
297: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
298: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
299: add(rbByElement, gridBagConstraints);
300:
301: lblNamespace.setLabelFor(txtNamespace);
302: org.openide.awt.Mnemonics.setLocalizedText(lblNamespace,
303: org.openide.util.NbBundle.getMessage(
304: FileRecognitionPanel.class, "LBL_Element"));
305: gridBagConstraints = new java.awt.GridBagConstraints();
306: gridBagConstraints.gridx = 0;
307: gridBagConstraints.gridy = 5;
308: gridBagConstraints.gridwidth = 2;
309: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
310: gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
311: add(lblNamespace, gridBagConstraints);
312:
313: gridBagConstraints = new java.awt.GridBagConstraints();
314: gridBagConstraints.gridx = 2;
315: gridBagConstraints.gridy = 5;
316: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
317: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
318: gridBagConstraints.weighty = 0.1;
319: gridBagConstraints.insets = new java.awt.Insets(6, 6, 0, 0);
320: add(txtNamespace, gridBagConstraints);
321:
322: }
323:
324: // </editor-fold>//GEN-END:initComponents
325:
326: // Variables declaration - do not modify//GEN-BEGIN:variables
327: private javax.swing.JLabel lblExtension;
328: private javax.swing.JLabel lblMimeType;
329: private javax.swing.JLabel lblNamespace;
330: private javax.swing.JRadioButton rbByElement;
331: private javax.swing.JRadioButton rbByExtension;
332: private javax.swing.JTextField txtExtension;
333: private javax.swing.JTextField txtMimeType;
334: private javax.swing.JTextField txtNamespace;
335:
336: // End of variables declaration//GEN-END:variables
337:
338: private void initAccessibility() {
339: this .getAccessibleContext().setAccessibleDescription(
340: getMessage("ACS_FileRecognitionPanel"));
341: rbByElement.getAccessibleContext().setAccessibleDescription(
342: getMessage("ACS_CTL_ByElement"));
343: rbByExtension.getAccessibleContext().setAccessibleDescription(
344: getMessage("ACS_CTL_ByExtension"));
345: txtExtension.getAccessibleContext().setAccessibleDescription(
346: getMessage("ACS_CTL_Extension"));
347: txtNamespace.getAccessibleContext().setAccessibleDescription(
348: getMessage("ACS_CTL_Namespace"));
349: txtMimeType.getAccessibleContext().setAccessibleDescription(
350: getMessage("ACS_CTL_Mimetype"));
351: }
352: }
|