1 19 20 package org.netbeans.modules.java.navigation; 21 22 import java.awt.Rectangle ; 23 import java.io.Serializable ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.swing.SwingUtilities ; 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 37 public final class JavadocTopComponent extends TopComponent { 38 39 private static final Logger LOGGER = Logger.getLogger(JavadocTopComponent.class.getName()); 40 41 private static JavadocTopComponent instance; 42 43 public static final String ICON_PATH = "org/netbeans/modules/java/navigation/resources/javadoc_action.png"; 44 45 private static final String 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 ZERO = new Rectangle (0,0,1,1); 55 56 void setJavadoc(String header, String 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>" + "<head>" + "</head>" + "<body>" + (header == null ? "" : ("<b>" + header + "</b><br><hr>")) 77 + javadoc.replaceAll("\n", "<br>") + "</body>" + "</html>" ); 81 } 82 SwingUtilities.invokeLater(new Runnable () { 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 103 private void initComponents() { 105 106 javadocScrollPane = new javax.swing.JScrollPane (); 107 javadocEditorPane = new javax.swing.JEditorPane (); 108 109 javadocEditorPane.setBackground(new java.awt.Color (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 } 126 127 private javax.swing.JEditorPane javadocEditorPane; 129 private javax.swing.JScrollPane javadocScrollPane; 130 132 137 public static synchronized JavadocTopComponent getDefault() { 138 if (instance == null) { 139 instance = new JavadocTopComponent(); 140 } 141 return instance; 142 } 143 144 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 174 public Object writeReplace() { 175 return new ResolvableHelper(); 176 } 177 178 protected String preferredID() { 179 return PREFERRED_ID; 180 } 181 182 final static class ResolvableHelper implements Serializable { 183 private static final long serialVersionUID = 1L; 184 public Object readResolve() { 185 return JavadocTopComponent.getDefault(); 186 } 187 } 188 189 } 190 | Popular Tags |