1 19 20 25 26 package org.netbeans.modules.xml.schema.ui.basic.editors; 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 dummyLabel1 = new javax.swing.JLabel (); 67 dummyLabel2 = new javax.swing.JLabel (); 68 69 org.openide.awt.Mnemonics.setLocalizedText(noNamespaceRadioButton, org.openide.util.NbBundle.getMessage(NamespacePanel.class, "LBL_NoNamespace")); 70 noNamespaceRadioButton.setToolTipText(org.openide.util.NbBundle.getMessage(NamespacePanel.class, "HINT_NoNamespace")); 71 noNamespaceRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 72 noNamespaceRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 73 buttonGroup1.add(noNamespaceRadioButton); 74 noNamespaceRadioButton.addActionListener(this); 75 76 org.openide.awt.Mnemonics.setLocalizedText(schemaNamespaceRadioButton, org.openide.util.NbBundle.getMessage(NamespacePanel.class, "LBL_SchemaNamespace")); 77 schemaNamespaceRadioButton.setToolTipText(org.openide.util.NbBundle.getMessage(NamespacePanel.class, "HINT_SchemaNamespace")); 78 schemaNamespaceRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 79 schemaNamespaceRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 80 buttonGroup1.add(schemaNamespaceRadioButton); 81 schemaNamespaceRadioButton.addActionListener(this); 82 83 org.openide.awt.Mnemonics.setLocalizedText(otherNamespaceRadioButton, org.openide.util.NbBundle.getMessage(NamespacePanel.class, "LBL_OtherNamespace")); 84 otherNamespaceRadioButton.setToolTipText(org.openide.util.NbBundle.getMessage(NamespacePanel.class, "HINT_OtherNamespace")); 85 otherNamespaceRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 86 otherNamespaceRadioButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 87 buttonGroup1.add(otherNamespaceRadioButton); 88 otherNamespaceRadioButton.addActionListener(this); 89 90 uriList.addListSelectionListener(this); 91 jScrollPane1.setViewportView(uriList); 92 93 otherNamespaceTextField.setText(org.openide.util.NbBundle.getMessage(NamespacePanel.class, "TXT_defaultTNS")); 94 otherNamespaceTextField.getDocument().addDocumentListener(this); 95 otherNamespaceTextField.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(NamespacePanel.class, "HINT_OtherNamespace")); 96 otherNamespaceTextField.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(NamespacePanel.class, "HINT_OtherNamespace")); 97 98 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 99 this.setLayout(layout); 100 layout.setHorizontalGroup( 101 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 102 .add(layout.createSequentialGroup() 103 .addContainerGap() 104 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 105 .add(layout.createSequentialGroup() 106 .add(17, 17, 17) 107 .add(otherNamespaceTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)) 108 .add(layout.createSequentialGroup() 109 .add(17, 17, 17) 110 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)) 111 .add(layout.createSequentialGroup() 112 .add(schemaNamespaceRadioButton) 113 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 114 .add(dummyLabel2)) 115 .add(otherNamespaceRadioButton) 116 .add(layout.createSequentialGroup() 117 .add(noNamespaceRadioButton) 118 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 119 .add(dummyLabel1))) 120 .addContainerGap()) 121 ); 122 layout.setVerticalGroup( 123 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 124 .add(layout.createSequentialGroup() 125 .add(11, 11, 11) 126 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 127 .add(noNamespaceRadioButton) 128 .add(dummyLabel1)) 129 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 130 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 131 .add(schemaNamespaceRadioButton) 132 .add(dummyLabel2)) 133 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 134 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 135 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 136 .add(otherNamespaceRadioButton) 137 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 138 .add(otherNamespaceTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 139 .addContainerGap(94, Short.MAX_VALUE)) 140 ); 141 } 143 144 private void initialize(String currentTns, final Collection <String > uris, Collection <NamespaceEditor.Option> options) { 145 if (uris==null || uris.isEmpty()) { 147 schemaNamespaceRadioButton.setVisible(false); 148 uriList.setVisible(false); 149 jScrollPane1.setVisible(false); 150 } else { 151 uriList.setListData(uris.toArray(new String [uris.size()])); 152 } 153 if(!options.contains(NamespaceEditor.Option.None)) { 155 noNamespaceRadioButton.setSelected(false); 156 noNamespaceRadioButton.setVisible(false); 157 } 158 if(currentTns==null || currentTns.trim().length()==0) { 159 uriList.setEnabled(false); 160 if(noNamespaceRadioButton.isVisible()) { 161 noNamespaceRadioButton.setSelected(true); 162 otherNamespaceTextField.setEnabled(false); 163 } else if (otherNamespaceRadioButton.isVisible()) { 164 otherNamespaceRadioButton.setSelected(true); 165 otherNamespaceTextField.setEnabled(true); 166 } 167 } else if(uris!=null && uris.contains(currentTns)) { 168 schemaNamespaceRadioButton.setSelected(true); 169 uriList.setSelectedValue(currentTns,true); 170 uriList.setEnabled(true); 171 otherNamespaceTextField.setEnabled(false); 172 } else { 173 uriList.setEnabled(false); 174 if(otherNamespaceRadioButton.isVisible()) { 175 otherNamespaceRadioButton.setSelected(true); 176 otherNamespaceTextField.setText(currentTns); 177 otherNamespaceTextField.setEnabled(true); 178 } 179 } 180 } 181 182 public void checkValidity() { 183 boolean newValue = false; 184 if(noNamespaceRadioButton.isSelected()) { 185 newValue = true; 186 } else if(schemaNamespaceRadioButton.isSelected()) { 187 newValue = uriList.getSelectedValue()!=null; 188 } else if(otherNamespaceRadioButton.isSelected()) { 189 newValue = !otherNamespaceTextField.getText().equals(""); 190 } 191 firePropertyChange(PROP_VALID_SELECTION,isValid,newValue); 192 isValid = newValue; 193 } 194 195 public String getCurrentSelection() { 196 if(schemaNamespaceRadioButton.isSelected()) { 197 return (String ) uriList.getSelectedValue(); 198 } else if (otherNamespaceRadioButton.isSelected()){ 199 String otherUri = otherNamespaceTextField.getText().trim(); 200 if (otherUri.length()>0) return otherUri; 201 } 202 return null; 203 } 204 205 public void actionPerformed(ActionEvent evt) { 206 Object source = evt.getSource(); 207 if(source.equals(noNamespaceRadioButton)) { 208 uriList.setEnabled(false); 209 otherNamespaceTextField.setEnabled(false); 210 } else if(source.equals(otherNamespaceRadioButton)){ 211 uriList.setEnabled(false); 212 otherNamespaceTextField.setEnabled(true); 213 } else if(source.equals(schemaNamespaceRadioButton)) { 214 otherNamespaceTextField.setEnabled(false); 215 uriList.setEnabled(true); 216 } 217 checkValidity(); 218 } 219 220 public void valueChanged(ListSelectionEvent e) { 221 checkValidity(); 222 } 223 224 public void insertUpdate(DocumentEvent e) { 225 checkValidity(); 226 } 227 228 public void removeUpdate(DocumentEvent e) { 229 checkValidity(); 230 } 231 232 public void changedUpdate(DocumentEvent e) { 233 } 234 235 public javax.swing.ButtonGroup buttonGroup1; 237 public javax.swing.JLabel dummyLabel1; 238 public javax.swing.JLabel dummyLabel2; 239 public javax.swing.JScrollPane jScrollPane1; 240 public javax.swing.JRadioButton noNamespaceRadioButton; 241 public javax.swing.JRadioButton otherNamespaceRadioButton; 242 public javax.swing.JTextField otherNamespaceTextField; 243 public javax.swing.JRadioButton schemaNamespaceRadioButton; 244 public javax.swing.JList uriList; 245 247 } 248 | Popular Tags |