1 19 20 25 26 package org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer; 27 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import javax.swing.event.ListSelectionEvent ; 32 import javax.swing.event.ListSelectionListener ; 33 import javax.swing.event.TableModelEvent ; 34 import javax.swing.event.TableModelListener ; 35 import javax.swing.table.DefaultTableModel ; 36 import org.openide.util.HelpCtx; 37 import org.netbeans.modules.xml.schema.model.Annotation; 38 import org.netbeans.modules.xml.schema.model.Documentation; 39 import org.netbeans.modules.xml.schema.model.Enumeration; 40 import org.netbeans.modules.xml.schema.model.SchemaComponentFactory; 41 import org.netbeans.modules.xml.schema.model.SchemaComponentReference; 42 import org.netbeans.modules.xml.schema.model.SimpleRestriction; 43 44 49 public class EnumerationCustomizer<T extends SimpleRestriction> 50 extends AbstractSchemaComponentCustomizer<T> 51 { 52 53 static final long serialVersionUID = 1L; 54 55 58 public EnumerationCustomizer(SchemaComponentReference<T> reference) { 59 super(reference); 60 initComponents(); 61 initializeModel(); 62 initializeUISelection(); 63 addListeners(); 64 } 65 66 public void applyChanges() throws IOException { 67 if(valueTable.getCellEditor()!=null) 68 valueTable.getCellEditor().stopCellEditing(); 69 if(enumData!=null&& enumData.isChanged()) { 70 enumData.save(); 71 } 72 } 73 74 public void reset() { 75 removeListeners(); 76 initializeModel(); 77 initializeUISelection(); 78 addListeners(); 79 setSaveEnabled(false); 80 setResetEnabled(false); 81 } 82 83 86 private void initializeModel() { 87 enumData = new EnumData(getReference().get(), 88 (DefaultTableModel )valueTable.getModel()); 89 } 90 91 94 private void initializeUISelection() { 95 Collection <Enumeration> enums = getReference().get().getEnumerations(); 96 DefaultTableModel tableModel = (DefaultTableModel ) valueTable.getModel(); 97 int rowCount = tableModel.getRowCount(); 98 if(rowCount>0) { 99 for (int i=rowCount-1;i>=0;) { 100 tableModel.removeRow(i--); 101 } 102 } 103 for(Enumeration e:enums) { 104 String value = e.getValue(); 105 String description = null; 106 if(e.getAnnotation()!=null && 107 !e.getAnnotation().getDocumentationElements().isEmpty()) { 108 description = e.getAnnotation(). 109 getDocumentationElements().iterator().next(). 110 getContentFragment(); 111 } 112 tableModel.addRow(new String []{value,description}); 113 } 114 } 115 116 private void addListeners() { 117 if(tableListener == null) { 118 tableListener = new TableModelListener () { 119 public void tableChanged(TableModelEvent e) { 120 if(enumData==null) return; 121 if(e.getType()==TableModelEvent.UPDATE) { 122 for(int i=e.getFirstRow();i<=e.getLastRow();i++) 123 enumData.modify(e.getFirstRow()); 124 determineValidity(); 125 } else if(e.getType()==TableModelEvent.INSERT) { 126 for(int i=e.getFirstRow();i<=e.getLastRow();i++) 127 enumData.add(); 128 determineValidity(); 129 } else if(e.getType()==TableModelEvent.DELETE) { 130 for(int i=e.getLastRow();i>=e.getFirstRow();i--) 131 enumData.remove(i); 132 determineValidity(); 133 } 134 } 135 }; 136 } 137 valueTable.getModel().addTableModelListener(tableListener); 138 } 139 140 private void removeListeners() { 141 valueTable.getModel().removeTableModelListener(tableListener); 142 } 143 144 148 private void determineValidity() { 149 boolean valueChange = enumData!=null&&enumData.isChanged(); 150 if(!valueChange) { 151 setSaveEnabled(false); 152 setResetEnabled(false); 153 return; 154 } else { 155 setResetEnabled(true); 156 setSaveEnabled(true); 157 } 158 } 159 160 166 private void initComponents() { 168 valuePane = new javax.swing.JScrollPane (); 169 valueTable = new javax.swing.JTable (); 170 addButton = new javax.swing.JButton (); 171 removeButton = new javax.swing.JButton (); 172 173 valuePane.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1)); 174 valueTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color (128, 128, 128))); 175 valueTable.setModel(new DefaultTableModel ( 176 new Object [][]{}, 177 new String []{org.openide.util.NbBundle.getMessage(EnumerationCustomizer.class, "LBL_Enumeration_Value"), 178 org.openide.util.NbBundle.getMessage(EnumerationCustomizer.class, "LBL_Enumeration_Description"), 179 } 180 )); 181 valueTable.setToolTipText(org.openide.util.NbBundle.getBundle(EnumerationCustomizer.class).getString("HINT_Enumeration_Table")); 182 valueTable.getSelectionModel().addListSelectionListener( 183 new ListSelectionListener () { 184 public void valueChanged(ListSelectionEvent e) { 185 if(valueTable.getSelectedRowCount()<=0) { 186 removeButton.setEnabled(false); 187 } else { 188 removeButton.setEnabled(true); 189 } 190 } 191 }); 192 valuePane.setViewportView(valueTable); 193 valueTable.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getBundle(EnumerationCustomizer.class).getString("HINT_Enumeration_Table")); 194 valueTable.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(EnumerationCustomizer.class).getString("HINT_Enumeration_Table")); 195 196 org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(EnumerationCustomizer.class, "LBL_Enumeration_AddValue")); 197 addButton.setToolTipText(org.openide.util.NbBundle.getBundle(EnumerationCustomizer.class).getString("HINT_Enumeration_AddValue")); 198 addButton.addActionListener(new java.awt.event.ActionListener () { 199 public void actionPerformed(java.awt.event.ActionEvent evt) { 200 addButtonActionPerformed(evt); 201 } 202 }); 203 204 org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(EnumerationCustomizer.class, "LBL_Enumeration_RemoveValue")); 205 removeButton.setToolTipText(org.openide.util.NbBundle.getBundle(EnumerationCustomizer.class).getString("HINT_Enumeration_RemoveValue")); 206 removeButton.setEnabled(false); 207 removeButton.addActionListener(new java.awt.event.ActionListener () { 208 public void actionPerformed(java.awt.event.ActionEvent evt) { 209 removeButtonActionPerformed(evt); 210 } 211 }); 212 213 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 214 this.setLayout(layout); 215 layout.setHorizontalGroup( 216 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 217 .add(layout.createSequentialGroup() 218 .addContainerGap() 219 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 220 .add(valuePane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE) 221 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 222 .add(addButton) 223 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 224 .add(removeButton))) 225 .addContainerGap()) 226 ); 227 228 layout.linkSize(new java.awt.Component [] {addButton, removeButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL); 229 230 layout.setVerticalGroup( 231 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 232 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 233 .addContainerGap() 234 .add(valuePane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE) 235 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 236 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 237 .add(removeButton) 238 .add(addButton)) 239 .addContainerGap()) 240 ); 241 242 layout.linkSize(new java.awt.Component [] {addButton, removeButton}, org.jdesktop.layout.GroupLayout.VERTICAL); 243 244 } 246 private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) { int [] rows = valueTable.getSelectedRows(); 248 if (rows == null || rows.length<1) return; 249 DefaultTableModel tableModel = 250 (DefaultTableModel )valueTable.getModel(); 251 for (int i =rows.length;i>0;i--) { 252 tableModel.removeRow(rows[i-1]); 253 } 254 } 256 private void addButtonActionPerformed(java.awt.event.ActionEvent evt) { DefaultTableModel tableModel = 258 (DefaultTableModel )valueTable.getModel(); 259 tableModel.addRow(new String [] {"",""}); 260 if(valueTable.getCellEditor()!=null) 261 valueTable.getCellEditor().stopCellEditing(); 262 valueTable.requestFocusInWindow(); 263 valueTable.changeSelection(valueTable.getRowCount()-1,0,false,false); 264 } 266 public HelpCtx getHelpCtx() { 267 return new HelpCtx(EnumerationCustomizer.class); 268 } 269 270 public javax.swing.JButton addButton; 272 public javax.swing.JButton removeButton; 273 public javax.swing.JScrollPane valuePane; 274 public javax.swing.JTable valueTable; 275 277 private TableModelListener tableListener; 278 private transient EnumData enumData; 279 280 private static class EnumData { 281 enum State {ADDED,REMOVED,MODIFIED,UNMODIFIED}; 282 private ArrayList <Enumeration> enums; 283 private ArrayList <State> states; 284 private SimpleRestriction str; 285 private DefaultTableModel tModel; 286 287 EnumData(SimpleRestriction str, DefaultTableModel tModel) { 288 enums = new ArrayList <Enumeration>(0); 289 states = new ArrayList <State>(0); 290 for(Enumeration e:str.getEnumerations()) { 291 enums.add(e); 292 states.add(State.UNMODIFIED); 293 } 294 this.str = str; 295 this.tModel = tModel; 296 } 297 298 int getRealIndex(int i) { 299 int idx = -1; 300 for(int ctr = 0; ctr<states.size();ctr++) { 301 if(states.get(ctr)!=State.REMOVED) 302 idx++; 303 if(idx==i) return ctr; 304 } 305 return i; 306 } 307 308 void add() { 309 states.add(State.ADDED); 310 enums.add(null); 311 } 312 313 void remove(int idx) { 314 int i = getRealIndex(idx); 315 if(enums.get(i)==null) { 316 enums.remove(i); 317 states.remove(i); 318 } else { 319 states.set(i,State.REMOVED); 320 } 321 } 322 323 void modify(int idx) { 324 String newValue = (String ) tModel.getValueAt(idx,0); 325 String newDesc = (String ) tModel.getValueAt(idx,1); 326 int i = getRealIndex(idx); 327 Enumeration e = enums.get(i); 328 if(e!=null) { 329 String value = e.getValue(); 330 if(!newValue.equals(e.getValue())) { 331 states.set(i,State.MODIFIED); 332 return; 333 } 334 String description = null; 335 if(e.getAnnotation()!=null && 336 !e.getAnnotation().getDocumentationElements().isEmpty()) { 337 description = e.getAnnotation(). 338 getDocumentationElements().iterator().next(). 339 getContentFragment(); 340 } 341 if(description != null && !description.equals(newDesc) || 342 newDesc != null && !newDesc.equals(description)) { 343 states.set(i,State.MODIFIED); 344 return; 345 } 346 states.set(i,State.UNMODIFIED); 347 } 348 } 349 350 boolean isChanged() { 351 if(states.contains(State.ADDED) || 352 states.contains(State.REMOVED) || 353 states.contains(State.MODIFIED)) 354 return true; 355 return false; 356 } 357 358 void save() { 359 SchemaComponentFactory factory = str.getModel().getFactory(); 360 for(int i=0;i<states.size();i++) { 361 State state = states.get(i); 362 switch (state) { 363 case ADDED: 364 Enumeration e = factory.createEnumeration(); 365 enums.set(i,e); 366 str.addEnumeration(e); 367 e.setValue((String )tModel.getValueAt(i,0)); 368 String newDesc = (String ) tModel.getValueAt(i,1); 369 if(newDesc!=null && !"".equals(newDesc)) { 370 try { 371 Documentation d = factory.createDocumentation(); 372 d.setContentFragment(newDesc); 373 Annotation a = factory.createAnnotation(); 374 a.addDocumentation(d); 375 e.setAnnotation(a); 376 } catch (IOException ex) { 377 } 378 } 379 states.set(i,State.UNMODIFIED); 380 break; 381 case REMOVED: 382 str.removeEnumeration(enums.get(i)); 383 enums.remove(i); 384 states.remove(i--); break; 386 case MODIFIED: 387 e = enums.get(i); 388 assert e != null; 389 e.setValue((String )tModel.getValueAt(i,0)); 390 newDesc = (String ) tModel.getValueAt(i,1); 391 if(newDesc!=null && !"".equals(newDesc)) { 392 try { 393 Documentation d = factory.createDocumentation(); 394 d.setContentFragment(newDesc); 395 Annotation a = factory.createAnnotation(); 396 a.addDocumentation(d); 397 e.setAnnotation(a); 398 } catch (IOException ex) { 399 } 400 } 401 states.set(i,State.UNMODIFIED); 402 break; 403 } 404 } 405 } 406 } 407 408 } 409 | Popular Tags |