1 19 20 package org.netbeans.modules.xml.schema.ui.basic.wizard; 21 22 import java.awt.Component ; 24 import java.awt.Container ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.List ; 28 import javax.swing.JTextField ; 29 import javax.swing.event.ChangeListener ; 30 import javax.swing.event.DocumentEvent ; 31 import javax.swing.event.DocumentListener ; 32 import javax.swing.text.BadLocationException ; 33 import org.netbeans.spi.project.ui.templates.support.Templates; 34 import org.openide.WizardDescriptor; 35 import org.openide.awt.Mnemonics; 36 import org.openide.loaders.TemplateWizard; 37 38 import org.openide.util.NbBundle; 40 41 47 final class SchemaAdditionalInfoGUI extends javax.swing.JPanel { 48 49 public static final String DEFAULT_TARGET_NAMESPACE = NbBundle.getMessage(SchemaAdditionalInfoGUI.class,"TXT_defaultTNS"); 51 private static final long serialVersionUID = 1L; 52 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 53 private boolean namespaceChangedByUser; 54 private JTextField fileField; 55 WizardDescriptor.Panel parentPanel; 56 57 60 public SchemaAdditionalInfoGUI() { 61 initComponents(); 62 targetNamespaceTextField.setText(DEFAULT_TARGET_NAMESPACE); 63 namespaceChangedByUser = false; 64 targetNamespaceTextField.getDocument().addDocumentListener( 65 new DocumentListener (){ 66 public void removeUpdate(DocumentEvent e) { 67 namespaceChangedByUser = true; 68 } 69 70 public void insertUpdate(DocumentEvent e) { 71 namespaceChangedByUser = true; 72 } 73 74 public void changedUpdate(DocumentEvent e) { 75 } 77 78 }); 79 } 80 81 84 public String getTargetNamespace() { 85 return targetNamespaceTextField.getText(); 86 } 87 88 89 private JTextField findFileNameField(Component panel, String text) { 90 Collection <Component > allComponents = new ArrayList <Component >(); 91 getAllComponents(new Component [] {panel}, allComponents); 92 for (Component c : allComponents) { 93 if (c instanceof JTextField ) { 95 JTextField tf = (JTextField ) c; 96 return tf; 98 } 100 } 101 return null; 102 } 103 104 107 public static void getAllComponents( Component [] components, Collection <Component > allComponents ) { 108 for( int i = 0; i < components.length; i++ ) { 109 if( components[i] != null ) { 110 allComponents.add( components[i] ); 111 if( ( ( Container )components[i] ).getComponentCount() != 0 ) { 112 getAllComponents( ( ( Container )components[i] ).getComponents(), allComponents ); 113 } 114 } 115 } 116 } 117 118 void setParentPanel(WizardDescriptor.Panel panel) { 119 this.parentPanel = panel; 120 } 121 122 public void attachListenerToFileName(TemplateWizard wizard) { 123 if (fileField != null) { 124 return; 125 } 126 127 fileField = findFileNameField( 128 parentPanel.getComponent(), 129 Templates.getTemplate(wizard).getName()); 130 if (fileField != null) { 131 fileField.getDocument().addDocumentListener( 132 new DocumentListener () { 133 public void removeUpdate(DocumentEvent e) { 134 getText(e); 135 } 136 137 public void insertUpdate(DocumentEvent e) { 138 getText(e); 139 } 140 141 public void changedUpdate(DocumentEvent e) { 142 143 } 144 145 private void getText(DocumentEvent e) { 146 try { 147 String t = 148 e.getDocument().getText(0, e.getDocument().getLength()); 149 changeDefaultURL(t); 150 } catch (BadLocationException ex) { 151 } 153 154 } 155 } 156 ); 157 } 158 } 159 160 private void changeDefaultURL(String fileName) { 161 if (!namespaceChangedByUser && (fileName != null)) { 162 String currentNamespace = targetNamespaceTextField.getText(); 163 String generatedNamespace = 164 currentNamespace.substring(0, currentNamespace.lastIndexOf("/")+1); generatedNamespace += fileName; 166 targetNamespaceTextField.setText(generatedNamespace); 167 namespaceChangedByUser = false; 168 } 169 } 170 171 174 public void addChangeListener(ChangeListener l) { 175 listeners.add(l); 176 } 177 178 181 public void removeChangeListener(ChangeListener l) { 182 listeners.remove(l); 183 } 184 185 200 206 private void initComponents() { 208 targetNamespaceLabel = new javax.swing.JLabel (); 209 Mnemonics.setLocalizedText(targetNamespaceLabel, NbBundle.getMessage(SchemaAdditionalInfoGUI.class, "LBL_TargetNamespace")); 210 targetNamespaceTextField = new javax.swing.JTextField (); 211 212 targetNamespaceLabel.setLabelFor(targetNamespaceTextField); 213 214 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 215 this.setLayout(layout); 216 layout.setHorizontalGroup( 217 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 218 .add(layout.createSequentialGroup() 219 .add(targetNamespaceLabel) 220 .add(10, 10, 10) 221 .add(targetNamespaceTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)) 222 ); 223 layout.setVerticalGroup( 224 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 225 .add(layout.createSequentialGroup() 226 .addContainerGap() 227 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 228 .add(targetNamespaceLabel) 229 .add(targetNamespaceTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 230 .addContainerGap(21, Short.MAX_VALUE)) 231 ); 232 } 234 235 private javax.swing.JLabel targetNamespaceLabel; 237 private javax.swing.JTextField targetNamespaceTextField; 238 240 } 241 | Popular Tags |