KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AnnotationCustomizer.java
22  *
23  * Created on May 10, 2006, 1:01 PM
24  */

25
26 package org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer;
27
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import org.jdesktop.layout.GroupLayout;
34 import org.openide.util.HelpCtx;
35 import org.netbeans.modules.xml.schema.model.Annotation;
36 import org.netbeans.modules.xml.schema.model.Documentation;
37 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
38
39 /**
40  *
41  * @author Ajit Bhate
42  */

43 public class AnnotationCustomizer extends AbstractSchemaComponentCustomizer<Annotation>
44         implements PropertyChangeListener JavaDoc
45 {
46     static final long serialVersionUID = 1L;
47     
48     private ArrayList JavaDoc<DocumentationPanel> docPanels;
49     
50     /**
51      * Creates new form AnnotationCustomizer
52      */

53     public AnnotationCustomizer(SchemaComponentReference<Annotation> reference)
54     {
55         super(reference);
56         initComponents();
57         initialize();
58     }
59     
60     private void initialize()
61     {
62         Annotation a = getReference().get();
63         if(docPanels!= null)
64         {
65             for(int i = docPanels.size()-1; i>=0;i--)
66             {
67                 DocumentationPanel docPanel = docPanels.get(i);
68                 docPanel.removePropertyChangeListener
69                         (DocumentationPanel.STATE_PROPERTY,this);
70                 docPanels.remove(docPanel);
71                 panel.remove(docPanel);
72             }
73         }
74         docPanels = new ArrayList JavaDoc<DocumentationPanel>();
75         Collection JavaDoc<Documentation> documentations =
76                 a.getDocumentationElements();
77         int idx = 0;
78         for(Documentation doc:documentations)
79         {
80             DocumentationPanel docPanel = new DocumentationPanel(this,doc,false);
81             docPanels.add(docPanel);
82             addDocumentationPanel(docPanel);
83             if(idx++<2) docPanel.setExpanded(true);
84         }
85     }
86
87     private void addDocumentationPanel(DocumentationPanel p)
88     {
89         GroupLayout layout = (GroupLayout)panel.getLayout();
90         GroupLayout.ParallelGroup hGroup =
91                 (GroupLayout.ParallelGroup)layout.getHorizontalGroup();
92         hGroup.add(p);
93         GroupLayout.SequentialGroup vGroup =
94                 (GroupLayout.SequentialGroup)layout.getVerticalGroup();
95         vGroup.add(p);
96     }
97
98     protected void applyChanges() throws IOException JavaDoc
99     {
100         for(int i = docPanels.size()-1; i>=0;i--)
101         {
102             DocumentationPanel docPanel = docPanels.get(i);
103             switch(docPanel.getState())
104             {
105                 case REMOVED:
106                     getReference().get().removeDocumentation(docPanel.getDocumentation());
107                     docPanels.remove(docPanel);
108                     break;
109                 case ADDED:
110                     getReference().get().addDocumentation(docPanel.getDocumentation());
111                     docPanel.getDocumentation().setContentFragment(docPanel.getContent());
112                     docPanel.setState(DocumentationPanel.State.UNMODIFIED);
113                     break;
114                 case MODIFIED:
115                     docPanel.getDocumentation().setContentFragment(docPanel.getContent());
116                     docPanel.setState(DocumentationPanel.State.UNMODIFIED);
117                     break;
118             }
119         }
120     }
121
122     public void reset()
123     {
124         initialize();
125         repaint();
126         revalidate();
127         setSaveEnabled(false);
128         setResetEnabled(false);
129     }
130     
131     /** This method is called from within the constructor to
132      * initialize the form.
133      * WARNING: Do NOT modify this code. The content of this method is
134      * always regenerated by the Form Editor.
135      */

136     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
137
private void initComponents() {
138         jPanel1 = new javax.swing.JPanel JavaDoc();
139         addButton = new javax.swing.JButton JavaDoc();
140         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
141         panel = new javax.swing.JPanel JavaDoc();
142
143         org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
144         jPanel1.setLayout(jPanel1Layout);
145         jPanel1Layout.setHorizontalGroup(
146             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
147             .add(0, 100, Short.MAX_VALUE)
148         );
149         jPanel1Layout.setVerticalGroup(
150             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
151             .add(0, 100, Short.MAX_VALUE)
152         );
153
154         addButton.setText(org.openide.util.NbBundle.getMessage(AnnotationCustomizer.class, "LBL_Add"));
155         addButton.setActionCommand(org.openide.util.NbBundle.getMessage(AnnotationCustomizer.class, "LBL_Add"));
156         addButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
157             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
158                 addButtonActionPerformed(evt);
159             }
160         });
161
162         jScrollPane1.setBorder(null);
163         panel.setLayout(null);
164
165         panel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
166         GroupLayout panelLayout = new GroupLayout(panel);
167         panel.setLayout(panelLayout);
168         panelLayout.setAutocreateGaps(true);
169         panelLayout.setAutocreateContainerGaps(true);
170         GroupLayout.ParallelGroup hGroup =
171         panelLayout.createParallelGroup();
172         panelLayout.setHorizontalGroup(hGroup);
173         GroupLayout.SequentialGroup vGroup =
174         panelLayout.createSequentialGroup();
175         panelLayout.setVerticalGroup(vGroup);
176         jScrollPane1.setViewportView(panel);
177
178         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
179         this.setLayout(layout);
180         layout.setHorizontalGroup(
181             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
182             .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
183                 .addContainerGap()
184                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
185                     .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
186                     .add(addButton))
187                 .addContainerGap())
188         );
189         layout.setVerticalGroup(
190             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
191             .add(layout.createSequentialGroup()
192                 .addContainerGap()
193                 .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
194                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
195                 .add(addButton)
196                 .addContainerGap())
197         );
198     }// </editor-fold>//GEN-END:initComponents
199

200     private void addButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_addButtonActionPerformed
201
{//GEN-HEADEREND:event_addButtonActionPerformed
202
Documentation doc = getReference().get().getModel().
203                 getFactory().createDocumentation();
204         DocumentationPanel docPanel = new DocumentationPanel(this,doc,true);
205         docPanels.add(docPanel);
206         addDocumentationPanel(docPanel);
207         docPanel.setExpanded(true);
208         revalidate();
209     }//GEN-LAST:event_addButtonActionPerformed
210

211     public void propertyChange(PropertyChangeEvent JavaDoc evt)
212     {
213         Object JavaDoc source = evt.getSource();
214         String JavaDoc property = evt.getPropertyName();
215         // checking of property name not needed, but lets keep it
216
if (property.equals(DocumentationPanel.STATE_PROPERTY) &&
217                 source instanceof DocumentationPanel)
218         {
219             DocumentationPanel p = (DocumentationPanel)source;
220             DocumentationPanel.State newState =
221                     (DocumentationPanel.State) evt.getNewValue();
222             boolean modified = false;
223             if(newState == DocumentationPanel.State.UNMODIFIED)
224             {
225                 for(DocumentationPanel docPanel:docPanels)
226                 {
227                     if(docPanel.getState() != DocumentationPanel.State.UNMODIFIED)
228                     {
229                         modified = true;
230                         break;
231                     }
232                 }
233             }
234             else if(newState == DocumentationPanel.State.REMOVED)
235             {
236                 modified = getReference().get().getDocumentationElements().
237                         contains(p.getDocumentation());
238                 if(!modified)
239                 {
240                     docPanels.remove(p);
241                     for(DocumentationPanel docPanel:docPanels)
242                     {
243                         if(docPanel.getState() != DocumentationPanel.State.UNMODIFIED)
244                         {
245                             modified = true;
246                             break;
247                         }
248                     }
249                 }
250                 p.removePropertyChangeListener
251                         (DocumentationPanel.STATE_PROPERTY,this);
252                 panel.remove(p);
253                 repaint();
254                 revalidate();
255             }
256             else
257             {
258                 modified = true;
259             }
260             setSaveEnabled(modified);
261             setResetEnabled(modified);
262         }
263     }
264
265     public HelpCtx getHelpCtx()
266     {
267         return new HelpCtx(AnnotationCustomizer.class);
268     }
269     
270     
271     // Variables declaration - do not modify//GEN-BEGIN:variables
272
private javax.swing.JButton JavaDoc addButton;
273     private javax.swing.JPanel JavaDoc jPanel1;
274     private javax.swing.JScrollPane JavaDoc jScrollPane1;
275     private javax.swing.JPanel JavaDoc panel;
276     // End of variables declaration//GEN-END:variables
277

278 }
279
Popular Tags