1 19 20 25 26 package org.netbeans.modules.xml.schema.abe.nodes.properties; 27 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.util.Collection ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.event.DocumentListener ; 33 import javax.swing.event.ListSelectionEvent ; 34 import javax.swing.event.ListSelectionListener ; 35 36 40 public class NamespacePanel extends javax.swing.JPanel 41 implements ActionListener , ListSelectionListener , DocumentListener { 42 43 static final long serialVersionUID = 1L; 44 public static final String PROP_VALID_SELECTION="validSelection"; 45 private boolean isValid=true; 46 47 public NamespacePanel(String currentTns, Collection <String > uris, Collection <NamespaceEditor.Option> options) { 48 initComponents(); 49 initialize(currentTns,uris,options); 50 } 51 52 57 private void initComponents() { 59 buttonGroup1 = new javax.swing.ButtonGroup (); 60 noNamespaceRadioButton = new javax.swing.JRadioButton (); 61 schemaNamespaceRadioButton = new javax.swing.JRadioButton (); 62 otherNamespaceRadioButton = new javax.swing.JRadioButton (); 63 jScrollPane1 = new javax.swing.JScrollPane (); 64 uriList = new javax.swing.JList (); 65 otherNamespaceTextField = new javax.swing.JTextField (); 66 67 noNamespaceRadioButton.setText(org.openide.util.NbBundle.getBundle(NamespacePanel.class).getString("LBL_NoNamespace")); 68 noNamespaceRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 69 noNamespaceRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 70 buttonGroup1.add(noNamespaceRadioButton); 71 noNamespaceRadioButton.addActionListener(this); 72 73 schemaNamespaceRadioButton.setText(org.openide.util.NbBundle.getBundle(NamespacePanel.class).getString("LBL_SchemaNamespace")); 74 schemaNamespaceRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 75 schemaNamespaceRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 76 buttonGroup1.add(schemaNamespaceRadioButton); 77 schemaNamespaceRadioButton.addActionListener(this); 78 79 otherNamespaceRadioButton.setText(org.openide.util.NbBundle.getBundle(NamespacePanel.class).getString("LBL_OtherNamespace")); 80 otherNamespaceRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 81 otherNamespaceRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 82 buttonGroup1.add(otherNamespaceRadioButton); 83 otherNamespaceRadioButton.addActionListener(this); 84 85 uriList.addListSelectionListener(this); 86 jScrollPane1.setViewportView(uriList); 87 88 otherNamespaceTextField.getDocument().addDocumentListener(this); 89 90 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 91 this.setLayout(layout); 92 layout.setHorizontalGroup( 93 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 94 .add(layout.createSequentialGroup() 95 .addContainerGap() 96 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 97 .add(layout.createSequentialGroup() 98 .add(17, 17, 17) 99 .add(otherNamespaceTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)) 100 .add(layout.createSequentialGroup() 101 .add(17, 17, 17) 102 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)) 103 .add(schemaNamespaceRadioButton) 104 .add(otherNamespaceRadioButton) 105 .add(noNamespaceRadioButton)) 106 .addContainerGap()) 107 ); 108 layout.setVerticalGroup( 109 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 110 .add(layout.createSequentialGroup() 111 .addContainerGap() 112 .add(noNamespaceRadioButton) 113 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 114 .add(schemaNamespaceRadioButton) 115 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 116 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 117 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 118 .add(otherNamespaceRadioButton) 119 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 120 .add(otherNamespaceTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 121 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 122 ); 123 } 125 126 private void initialize(String currentTns, final Collection <String > uris, Collection <NamespaceEditor.Option> options) { 127 if (uris==null || uris.isEmpty()) { 129 schemaNamespaceRadioButton.setVisible(false); 130 uriList.setVisible(false); 131 } else { 132 uriList.setListData(uris.toArray(new String [uris.size()])); 133 } 134 if(!options.contains(NamespaceEditor.Option.None)) { 136 noNamespaceRadioButton.setSelected(false); 137 noNamespaceRadioButton.setVisible(false); 138 } 139 if(currentTns==null || currentTns.trim().length()==0) { 140 uriList.setEnabled(false); 141 if(noNamespaceRadioButton.isVisible()) { 142 noNamespaceRadioButton.setSelected(true); 143 otherNamespaceTextField.setEnabled(false); 144 } else if (otherNamespaceRadioButton.isVisible()) { 145 otherNamespaceRadioButton.setSelected(true); 146 otherNamespaceTextField.setEnabled(true); 147 } 148 } else if(uris!=null && uris.contains(currentTns)) { 149 schemaNamespaceRadioButton.setSelected(true); 150 uriList.setSelectedValue(currentTns,true); 151 uriList.setEnabled(true); 152 otherNamespaceTextField.setEnabled(false); 153 } else { 154 uriList.setEnabled(false); 155 if(otherNamespaceRadioButton.isVisible()) { 156 otherNamespaceRadioButton.setSelected(true); 157 otherNamespaceTextField.setText(currentTns); 158 otherNamespaceTextField.setEnabled(true); 159 } 160 } 161 } 162 163 public void checkValidity() { 164 boolean newValue = false; 165 if(noNamespaceRadioButton.isSelected()) { 166 newValue = true; 167 } else if(schemaNamespaceRadioButton.isSelected()) { 168 newValue = uriList.getSelectedValue()!=null; 169 } else if(otherNamespaceRadioButton.isSelected()) { 170 newValue = !otherNamespaceTextField.getText().equals(""); 171 } 172 firePropertyChange(PROP_VALID_SELECTION,isValid,newValue); 173 isValid = newValue; 174 } 175 176 public String getCurrentSelection() { 177 if(schemaNamespaceRadioButton.isSelected()) { 178 return (String ) uriList.getSelectedValue(); 179 } else if (otherNamespaceRadioButton.isSelected()){ 180 String otherUri = otherNamespaceTextField.getText().trim(); 181 if (otherUri.length()>0) return otherUri; 182 } 183 return null; 184 } 185 186 public void actionPerformed(ActionEvent evt) { 187 Object source = evt.getSource(); 188 if(source.equals(noNamespaceRadioButton)) { 189 uriList.setEnabled(false); 190 otherNamespaceTextField.setEnabled(false); 191 } else if(source.equals(otherNamespaceRadioButton)){ 192 uriList.setEnabled(false); 193 otherNamespaceTextField.setEnabled(true); 194 } else if(source.equals(schemaNamespaceRadioButton)) { 195 otherNamespaceTextField.setEnabled(false); 196 uriList.setEnabled(true); 197 } 198 checkValidity(); 199 } 200 201 public void valueChanged(ListSelectionEvent e) { 202 checkValidity(); 203 } 204 205 public void insertUpdate(DocumentEvent e) { 206 checkValidity(); 207 } 208 209 public void removeUpdate(DocumentEvent e) { 210 checkValidity(); 211 } 212 213 public void changedUpdate(DocumentEvent e) { 214 } 215 216 public javax.swing.ButtonGroup buttonGroup1; 218 public javax.swing.JScrollPane jScrollPane1; 219 public javax.swing.JRadioButton noNamespaceRadioButton; 220 public javax.swing.JRadioButton otherNamespaceRadioButton; 221 public javax.swing.JTextField otherNamespaceTextField; 222 public javax.swing.JRadioButton schemaNamespaceRadioButton; 223 public javax.swing.JList uriList; 224 226 } 227 | Popular Tags |