KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > JavadocTopComponent


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.navigation;
21
22 import java.awt.Rectangle JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.openide.util.NbBundle;
28 import org.openide.windows.TopComponent;
29 import org.openide.windows.WindowManager;
30 import org.openide.util.Utilities;
31
32 /**
33  * Top component which displays something.
34  *
35  * @author Sandip V. Chitale (Sandip.Chitale@Sun.Com)
36  */

37 public final class JavadocTopComponent extends TopComponent {
38     
39     private static final Logger JavaDoc LOGGER = Logger.getLogger(JavadocTopComponent.class.getName());
40     
41     private static JavadocTopComponent instance;
42     /** path to the icon used by the component and its open action */
43     public static final String JavaDoc ICON_PATH = "org/netbeans/modules/java/navigation/resources/javadoc_action.png";
44     
45     private static final String JavaDoc PREFERRED_ID = "JavadocTopComponent";
46     
47     private JavadocTopComponent() {
48         initComponents();
49         setName(NbBundle.getMessage(JavadocTopComponent.class, "CTL_JavadocTopComponent"));
50         setToolTipText(NbBundle.getMessage(JavadocTopComponent.class, "HINT_JavadocTopComponent"));
51         setIcon(Utilities.loadImage(ICON_PATH, true));
52     }
53     
54     private static final Rectangle JavaDoc ZERO = new Rectangle JavaDoc(0,0,1,1);
55
56     void setJavadoc(String JavaDoc header, String JavaDoc javadoc){
57         if (javadoc == null) {
58             javadocEditorPane.setText("");
59         } else {
60             javadoc = javadoc
61                     .replaceAll("@author ", "<b>Author:</b> ")
62                     .replaceAll("@deprecated ", "<b>Deprecated:</b> ")
63                     .replaceAll("@exception ", "<b>Exception:</b> ")
64                     .replaceAll("@param ", "<b>Parameter:</b> ")
65                     .replaceAll("@return ", "<b>Return:</b> ")
66                     .replaceAll("@see ", "<b>See:</b> ")
67                     .replaceAll("@since ", "<b>Since:</b> ")
68                     .replaceAll("@throws ", "<b>Throws:</b> ")
69                     .replaceAll("@version ", "<b>Version:</b> ")
70                     ;
71             javadocEditorPane.setText(
72                     "<html>" // NOI18N
73
+ "<head>" // NOI18N
74
+ "</head>" // NOI18N
75
+ "<body>" // NOI18N
76
+ (header == null ? "" : ("<b>" + header + "</b><br><hr>"))
77                     + javadoc.replaceAll("\n", "<br>") // NOI18N
78
+ "</body>" // NOI18N
79
+ "</html>" // NOI18N
80
);
81         }
82         SwingUtilities.invokeLater(new Runnable JavaDoc() {
83             public void run() {
84                 javadocEditorPane.scrollRectToVisible(ZERO);
85             }
86         });
87     }
88     
89     public static boolean shouldUpdate() {
90         if ( instance == null ) {
91             return false;
92         }
93         else {
94             return instance.isShowing();
95         }
96     }
97     
98     /** This method is called from within the constructor to
99      * initialize the form.
100      * WARNING: Do NOT modify this code. The content of this method is
101      * always regenerated by the Form Editor.
102      */

103     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
104
private void initComponents() {
105
106         javadocScrollPane = new javax.swing.JScrollPane JavaDoc();
107         javadocEditorPane = new javax.swing.JEditorPane JavaDoc();
108
109         javadocEditorPane.setBackground(new java.awt.Color JavaDoc(255, 255, 222));
110         javadocEditorPane.setContentType("text/html");
111         javadocEditorPane.setEditable(false);
112         javadocScrollPane.setViewportView(javadocEditorPane);
113
114         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
115         this.setLayout(layout);
116         layout.setHorizontalGroup(
117             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
118             .add(javadocScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
119         );
120         layout.setVerticalGroup(
121             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
122             .add(javadocScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
123         );
124     }// </editor-fold>//GEN-END:initComponents
125

126     
127     // Variables declaration - do not modify//GEN-BEGIN:variables
128
private javax.swing.JEditorPane JavaDoc javadocEditorPane;
129     private javax.swing.JScrollPane JavaDoc javadocScrollPane;
130     // End of variables declaration//GEN-END:variables
131

132     /**
133      * Gets default instance. Do not use directly: reserved for *.settings files only,
134      * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
135      * To obtain the singleton instance, use {@link findInstance}.
136      */

137     public static synchronized JavadocTopComponent getDefault() {
138         if (instance == null) {
139             instance = new JavadocTopComponent();
140         }
141         return instance;
142     }
143     
144     /**
145      * Obtain the JavadocTopComponent instance. Never call {@link #getDefault} directly!
146      */

147     public static synchronized JavadocTopComponent findInstance() {
148         TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
149         if (win == null) {
150             LOGGER.log(Level.WARNING,
151                        "Cannot find MyWindow component. It will not be located properly in the window system.");
152             return getDefault();
153         }
154         if (win instanceof JavadocTopComponent) {
155             return (JavadocTopComponent)win;
156         }
157         LOGGER.log(Level.WARNING,
158                 "There seem to be multiple components with the '" + PREFERRED_ID +
159                 "' ID. That is a potential source of errors and unexpected behavior.");
160         return getDefault();
161     }
162     
163     public int getPersistenceType() {
164         return TopComponent.PERSISTENCE_ALWAYS;
165     }
166     
167     public void componentOpened() {
168     }
169     
170     public void componentClosed() {
171     }
172     
173     /** replaces this in object stream */
174     public Object JavaDoc writeReplace() {
175         return new ResolvableHelper();
176     }
177     
178     protected String JavaDoc preferredID() {
179         return PREFERRED_ID;
180     }
181     
182     final static class ResolvableHelper implements Serializable JavaDoc {
183         private static final long serialVersionUID = 1L;
184         public Object JavaDoc readResolve() {
185             return JavadocTopComponent.getDefault();
186         }
187     }
188     
189 }
190
Popular Tags