KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > customizer > EnumerationCustomizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * EnumerationCustomizer.java
22  *
23  * Created on January 17, 2006, 10:26 PM
24  */

25
26 package org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer;
27
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import javax.swing.event.ListSelectionEvent JavaDoc;
32 import javax.swing.event.ListSelectionListener JavaDoc;
33 import javax.swing.event.TableModelEvent JavaDoc;
34 import javax.swing.event.TableModelListener JavaDoc;
35 import javax.swing.table.DefaultTableModel JavaDoc;
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 /**
45  * Attribute customizer
46  *
47  * @author Ajit Bhate
48  */

49 public class EnumerationCustomizer<T extends SimpleRestriction>
50         extends AbstractSchemaComponentCustomizer<T>
51 {
52     
53     static final long serialVersionUID = 1L;
54     
55     /**
56      * Creates new form EnumerationCustomizer
57      */

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 JavaDoc {
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     /**
84      * initializes non ui elements from model
85      */

86     private void initializeModel() {
87         enumData = new EnumData(getReference().get(),
88                 (DefaultTableModel JavaDoc)valueTable.getModel());
89     }
90     
91     /**
92      * Initializes UI from model values
93      */

94     private void initializeUISelection() {
95         Collection JavaDoc<Enumeration> enums = getReference().get().getEnumerations();
96         DefaultTableModel JavaDoc tableModel = (DefaultTableModel JavaDoc) 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 JavaDoc value = e.getValue();
105             String JavaDoc 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 JavaDoc[]{value,description});
113         }
114     }
115     
116     private void addListeners() {
117         if(tableListener == null) {
118             tableListener = new TableModelListener JavaDoc() {
119                 public void tableChanged(TableModelEvent JavaDoc 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     /**
145      * Based on the current radio button status and node selections, decide
146      * if we are in a valid state for accepting the user's input.
147      */

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     /**
161      * This method is called from within the constructor to
162      * initializeTypeView the form.
163      * WARNING: Do NOT modify this code. The content of this method is
164      * always regenerated by the Form Editor.
165      */

166     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
167
private void initComponents() {
168         valuePane = new javax.swing.JScrollPane JavaDoc();
169         valueTable = new javax.swing.JTable JavaDoc();
170         addButton = new javax.swing.JButton JavaDoc();
171         removeButton = new javax.swing.JButton JavaDoc();
172
173         valuePane.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
174         valueTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color JavaDoc(128, 128, 128)));
175         valueTable.setModel(new DefaultTableModel JavaDoc(
176             new Object JavaDoc [][]{},
177             new String JavaDoc []{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 JavaDoc() {
184                 public void valueChanged(ListSelectionEvent JavaDoc 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 JavaDoc() {
199                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc() {
208                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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 JavaDoc[] {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 JavaDoc[] {addButton, removeButton}, org.jdesktop.layout.GroupLayout.VERTICAL);
243
244         }// </editor-fold>//GEN-END:initComponents
245

246     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
247
int [] rows = valueTable.getSelectedRows();
248         if (rows == null || rows.length<1) return;
249         DefaultTableModel JavaDoc tableModel =
250                 (DefaultTableModel JavaDoc)valueTable.getModel();
251         for (int i =rows.length;i>0;i--) {
252             tableModel.removeRow(rows[i-1]);
253         }
254     }//GEN-LAST:event_removeButtonActionPerformed
255

256     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonActionPerformed
257
DefaultTableModel JavaDoc tableModel =
258                 (DefaultTableModel JavaDoc)valueTable.getModel();
259         tableModel.addRow(new String JavaDoc[] {"",""});
260         if(valueTable.getCellEditor()!=null)
261             valueTable.getCellEditor().stopCellEditing();
262         valueTable.requestFocusInWindow();
263         valueTable.changeSelection(valueTable.getRowCount()-1,0,false,false);
264     }//GEN-LAST:event_addButtonActionPerformed
265

266     public HelpCtx getHelpCtx() {
267         return new HelpCtx(EnumerationCustomizer.class);
268     }
269     
270     // Variables declaration - do not modify//GEN-BEGIN:variables
271
public javax.swing.JButton JavaDoc addButton;
272     public javax.swing.JButton JavaDoc removeButton;
273     public javax.swing.JScrollPane JavaDoc valuePane;
274     public javax.swing.JTable JavaDoc valueTable;
275     // End of variables declaration//GEN-END:variables
276

277     private TableModelListener JavaDoc tableListener;
278     private transient EnumData enumData;
279     
280     private static class EnumData {
281         enum State {ADDED,REMOVED,MODIFIED,UNMODIFIED};
282         private ArrayList JavaDoc<Enumeration> enums;
283         private ArrayList JavaDoc<State> states;
284         private SimpleRestriction str;
285         private DefaultTableModel JavaDoc tModel;
286         
287         EnumData(SimpleRestriction str, DefaultTableModel JavaDoc tModel) {
288             enums = new ArrayList JavaDoc<Enumeration>(0);
289             states = new ArrayList JavaDoc<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 JavaDoc newValue = (String JavaDoc) tModel.getValueAt(idx,0);
325             String JavaDoc newDesc = (String JavaDoc) tModel.getValueAt(idx,1);
326             int i = getRealIndex(idx);
327             Enumeration e = enums.get(i);
328             if(e!=null) {
329                 String JavaDoc value = e.getValue();
330                 if(!newValue.equals(e.getValue())) {
331                     states.set(i,State.MODIFIED);
332                     return;
333                 }
334                 String JavaDoc 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 JavaDoc)tModel.getValueAt(i,0));
368                         String JavaDoc newDesc = (String JavaDoc) 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 JavaDoc 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--); //decrement i
385
break;
386                     case MODIFIED:
387                         e = enums.get(i);
388                         assert e != null;
389                         e.setValue((String JavaDoc)tModel.getValueAt(i,0));
390                         newDesc = (String JavaDoc) 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 JavaDoc ex) {
399                             }
400                         }
401                         states.set(i,State.UNMODIFIED);
402                         break;
403                 }
404             }
405         }
406     }
407     
408 }
409
Popular Tags