1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.loader; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import javax.swing.ButtonGroup ; 27 import javax.swing.event.DocumentEvent ; 28 import javax.swing.event.DocumentListener ; 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 40 final class FileRecognitionPanel extends BasicWizardIterator.Panel { 41 42 private static final Pattern EXTENSION_PATTERN = Pattern.compile("([.]?[a-zA-Z0-9_]+){1}([ ,]+[.]?[a-zA-Z0-9_]+)*[ ]*"); private static final Pattern ELEMENT_PATTERN = Pattern.compile("(application/([a-zA-Z0-9_.-])*\\+xml|text/([a-zA-Z0-9_.-])*\\+xml)"); private static final Pattern MIME_TYPE_PATTERN = Pattern.compile("(application|text|image|audio|video)/([a-zA-Z0-9_.+-])+"); private static final String DEFAULT_MIME_TYPE = "text/x-<type>"; 47 private NewLoaderIterator.DataModel data; 48 private ButtonGroup group; 49 private boolean listenersAttached; 50 private DocumentListener docList; 51 52 55 public FileRecognitionPanel(WizardDescriptor setting, NewLoaderIterator.DataModel data) { 56 super(setting); 57 this.data = data; 58 initComponents(); 59 initAccessibility(); 60 group = new ButtonGroup (); 61 group.add(rbByElement); 62 group.add(rbByExtension); 63 ActionListener list = new ActionListener () { 64 public void actionPerformed(ActionEvent 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 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 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 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 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 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 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 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 getMessage(String key) { 185 return NbBundle.getMessage(FileRecognitionPanel.class, key); 186 } 187 188 193 private void initComponents() { 195 java.awt.GridBagConstraints gridBagConstraints; 196 197 lblMimeType = new javax.swing.JLabel (); 198 txtMimeType = new javax.swing.JTextField (); 199 rbByExtension = new javax.swing.JRadioButton (); 200 lblExtension = new javax.swing.JLabel (); 201 txtExtension = new javax.swing.JTextField (); 202 rbByElement = new javax.swing.JRadioButton (); 203 lblNamespace = new javax.swing.JLabel (); 204 txtNamespace = new javax.swing.JTextField (); 205 206 setLayout(new java.awt.GridBagLayout ()); 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 (); 211 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 212 add(lblMimeType, gridBagConstraints); 213 214 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (0, 0, 0, 0)); 225 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (); 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 (6, 0, 0, 0); 241 add(lblExtension, gridBagConstraints); 242 243 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (0, 0, 0, 0)); 254 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (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 (); 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 (6, 0, 0, 0); 270 add(lblNamespace, gridBagConstraints); 271 272 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (6, 6, 0, 0); 279 add(txtNamespace, gridBagConstraints); 280 281 } 282 284 private javax.swing.JLabel lblExtension; 286 private javax.swing.JLabel lblMimeType; 287 private javax.swing.JLabel lblNamespace; 288 private javax.swing.JRadioButton rbByElement; 289 private javax.swing.JRadioButton rbByExtension; 290 private javax.swing.JTextField txtExtension; 291 private javax.swing.JTextField txtMimeType; 292 private javax.swing.JTextField txtNamespace; 293 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 |