KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DocumentationPanel.java
22  *
23  * Created on May 10, 2006, 10:04 AM
24  */

25
26 package org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer;
27
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import org.netbeans.modules.xml.schema.model.Documentation;
31 import org.openide.util.NbBundle;
32
33 /**
34  *
35  * @author Ajit Bhate
36  */

37 public class DocumentationPanel extends javax.swing.JPanel JavaDoc
38 {
39     static final long serialVersionUID = 1L;
40     public static final String JavaDoc STATE_PROPERTY = "state";
41     
42     private transient Documentation documentation;
43     private transient DocumentListener JavaDoc contentListener;
44     private transient AnnotationCustomizer owner;
45     private transient State state;
46     /**
47      * Creates new form DocumentationPanel
48      */

49     public DocumentationPanel(AnnotationCustomizer owner,
50             Documentation documentation, boolean isAdded)
51     {
52         this.owner = owner;
53         this.documentation = documentation;
54         initComponents();
55         initUI();
56         setState(State.UNMODIFIED);
57         addPropertyChangeListener(STATE_PROPERTY,owner);
58         if(isAdded)
59             updateState(State.ADDED);
60     }
61
62     private void initUI()
63     {
64         innerPanel.setVisible(false);
65         if(contentListener == null)
66         {
67             contentListener = new DocumentListener JavaDoc() {
68                 public void changedUpdate(DocumentEvent JavaDoc e)
69                 {
70                     updateState(State.MODIFIED);
71                 }
72                 public void insertUpdate(DocumentEvent JavaDoc e)
73                 {
74                     updateState(State.MODIFIED);
75                 }
76                 public void removeUpdate(DocumentEvent JavaDoc e)
77                 {
78                     updateState(State.MODIFIED);
79                 }
80             };
81         } else
82         {
83             contentEditorPane.getDocument().
84                     removeDocumentListener(contentListener);
85         }
86         contentEditorPane.setText(getDocumentation().getContentFragment());
87         contentEditorPane.getDocument().
88                 addDocumentListener(contentListener);
89     }
90
91     public Documentation getDocumentation()
92     {
93         return documentation;
94     }
95     
96     private boolean isModified()
97     {
98         return !getContent().equals(getDocumentation().getContentFragment());
99     }
100
101     public String JavaDoc getContent()
102     {
103         return contentEditorPane.getText();
104     }
105     
106     private String JavaDoc getTitleButtonText()
107     {
108         String JavaDoc content = "";
109         if(!titleButton.isSelected())
110         {
111             content = getDocumentation().getContentFragment().trim();
112             if(content.length()==0)
113             {
114                 content = NbBundle.getMessage(DocumentationPanel.class,
115                         "LBL_DocumentationEmptyContent");
116             }
117             else if (content.length()>20)
118             {
119                 content = content.substring(0,20);
120                 content = content.replaceAll("<","&lt;");
121             }
122         }
123         return NbBundle.getMessage(DocumentationPanel.class,
124                 "LBL_Documentation", content);
125     }
126
127     public State getState()
128     {
129         return state;
130     }
131
132     void setState(State state)
133     {
134         this.state = state;
135     }
136
137     private void updateState(final State state)
138     {
139         State newState = state;
140         State currentState = getState();
141         // its marked added so if its not removed do not change state
142
if(currentState==State.ADDED && newState!= State.REMOVED) return;
143         if(newState==State.MODIFIED && !isModified())
144         {
145             // actually it is not modified
146
newState = State.UNMODIFIED;
147         }
148         setState(newState);
149         firePropertyChange(STATE_PROPERTY,currentState,newState);
150     }
151
152     public enum State {ADDED,REMOVED,MODIFIED,UNMODIFIED}
153
154     /** This method is called from within the constructor to
155      * initialize the form.
156      * WARNING: Do NOT modify this code. The content of this method is
157      * always regenerated by the Form Editor.
158      */

159     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
160
private void initComponents()
161     {
162         titleButton = new javax.swing.JButton JavaDoc();
163         innerPanel = new javax.swing.JPanel JavaDoc();
164         removeButton = new javax.swing.JButton JavaDoc();
165         contentScrollPane = new javax.swing.JScrollPane JavaDoc();
166         contentEditorPane = new javax.swing.JEditorPane JavaDoc()
167         {
168             static final long serialVersionUID = 1L;
169             protected void processMouseEvent(java.awt.event.MouseEvent JavaDoc e)
170             {
171                 if(e.getButton()==java.awt.event.MouseEvent.BUTTON3)
172                 {
173                     e.consume();
174                     return;
175                 }
176                 super.processMouseEvent(e);
177             }
178         };
179
180         setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
181         titleButton.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/schema/ui/nodes/resources/plus.gif")));
182         titleButton.setText(getTitleButtonText());
183         titleButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
184         titleButton.setBorderPainted(false);
185         titleButton.setContentAreaFilled(false);
186         titleButton.setFocusPainted(false);
187         titleButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
188         titleButton.setSelectedIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/org/netbeans/modules/xml/schema/ui/nodes/resources/minus.gif")));
189         titleButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
190         {
191             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
192             {
193                 manageInnerPanel(evt);
194             }
195         });
196
197         innerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color JavaDoc(204, 204, 204)));
198         removeButton.setText(org.openide.util.NbBundle.getMessage(DocumentationPanel.class, "LBL_Remove"));
199         removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
200         {
201             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
202             {
203                 removeButtonActionPerformed(evt);
204             }
205         });
206
207         contentScrollPane.setBorder(null);
208         contentEditorPane.setContentType("text/xml");
209         contentScrollPane.setViewportView(contentEditorPane);
210
211         org.jdesktop.layout.GroupLayout innerPanelLayout = new org.jdesktop.layout.GroupLayout(innerPanel);
212         innerPanel.setLayout(innerPanelLayout);
213         innerPanelLayout.setHorizontalGroup(
214             innerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
215             .add(innerPanelLayout.createSequentialGroup()
216                 .addContainerGap()
217                 .add(innerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
218                     .add(org.jdesktop.layout.GroupLayout.TRAILING, removeButton)
219                     .add(contentScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
220                 .addContainerGap())
221         );
222         innerPanelLayout.setVerticalGroup(
223             innerPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
224             .add(org.jdesktop.layout.GroupLayout.TRAILING, innerPanelLayout.createSequentialGroup()
225                 .add(contentScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
226                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
227                 .add(removeButton)
228                 .addContainerGap())
229         );
230
231         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
232         this.setLayout(layout);
233         layout.setHorizontalGroup(
234             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
235             .add(innerPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
236             .add(titleButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
237         );
238         layout.setVerticalGroup(
239             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
240             .add(layout.createSequentialGroup()
241                 .add(titleButton)
242                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
243                 .add(innerPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
244         );
245     }// </editor-fold>//GEN-END:initComponents
246

247     private void manageInnerPanel(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_manageInnerPanel
248
{//GEN-HEADEREND:event_manageInnerPanel
249
boolean expand = !titleButton.isSelected();
250         setExpanded(expand);
251     }//GEN-LAST:event_manageInnerPanel
252

253     void setExpanded(final boolean expand)
254     {
255         titleButton.setSelected(expand);
256         titleButton.setText(getTitleButtonText());
257         innerPanel.setVisible(expand);
258     }
259
260     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt)//GEN-FIRST:event_removeButtonActionPerformed
261
{//GEN-HEADEREND:event_removeButtonActionPerformed
262
updateState(State.REMOVED);
263     }//GEN-LAST:event_removeButtonActionPerformed
264

265     
266     // Variables declaration - do not modify//GEN-BEGIN:variables
267
private javax.swing.JEditorPane JavaDoc contentEditorPane;
268     private javax.swing.JScrollPane JavaDoc contentScrollPane;
269     private javax.swing.JPanel JavaDoc innerPanel;
270     private javax.swing.JButton JavaDoc removeButton;
271     private javax.swing.JButton JavaDoc titleButton;
272     // End of variables declaration//GEN-END:variables
273

274 }
275
Popular Tags