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 DeclarationTopComponent extends TopComponent { 38 39 private static final Logger LOGGER = Logger.getLogger(DeclarationTopComponent.class.getName()); 40 41 private static DeclarationTopComponent instance; 42 43 public static final String ICON_PATH = "org/netbeans/modules/java/navigation/resources/declaration_action.png"; 44 45 private static final String PREFERRED_ID = "DeclarationTopComponent"; 46 47 private static boolean shouldUpdate; 48 49 private DeclarationTopComponent() { 50 initComponents(); 51 setName(NbBundle.getMessage(DeclarationTopComponent.class, "CTL_DeclarationTopComponent")); 52 setToolTipText(NbBundle.getMessage(DeclarationTopComponent.class, "HINT_DeclarationTopComponent")); 53 setIcon(Utilities.loadImage(ICON_PATH, true)); 54 } 55 56 private static final Rectangle ZERO = new Rectangle (0,0,1,1); 57 58 void setDeclaration(String declaration) { 59 if (declaration == null) { 60 declarationEditorPane.setText(""); 61 } else { 62 declarationEditorPane.setText(declaration); 63 } 64 declarationEditorPane.setCaretPosition(0); 65 SwingUtilities.invokeLater(new Runnable () { 66 public void run() { 67 declarationEditorPane.scrollRectToVisible(ZERO); 68 } 69 }); 70 } 71 72 77 private void initComponents() { 79 80 declarationScrollPane = new javax.swing.JScrollPane (); 81 declarationEditorPane = new javax.swing.JEditorPane (); 82 83 declarationEditorPane.setBackground(new java.awt.Color (238, 238, 255)); 84 declarationEditorPane.setContentType("text/x-java"); 85 declarationEditorPane.setEditable(false); 86 declarationScrollPane.setViewportView(declarationEditorPane); 87 88 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 89 this.setLayout(layout); 90 layout.setHorizontalGroup( 91 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 92 .add(declarationScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE) 93 ); 94 layout.setVerticalGroup( 95 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 96 .add(declarationScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE) 97 ); 98 } 100 101 private javax.swing.JEditorPane declarationEditorPane; 103 private javax.swing.JScrollPane declarationScrollPane; 104 106 111 public static synchronized DeclarationTopComponent getDefault() { 112 if (instance == null) { 113 instance = new DeclarationTopComponent(); 114 } 115 return instance; 116 } 117 118 121 public static synchronized DeclarationTopComponent findInstance() { 122 TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID); 123 if (win == null) { 124 LOGGER.log(Level.WARNING, 125 "Cannot find MyWindow component. It will not be located properly in the window system."); 126 return getDefault(); 127 } 128 if (win instanceof DeclarationTopComponent) { 129 return (DeclarationTopComponent)win; 130 } 131 LOGGER.log(Level.WARNING, 132 "There seem to be multiple components with the '" + PREFERRED_ID + 133 "' ID. That is a potential source of errors and unexpected behavior."); 134 return getDefault(); 135 } 136 137 public static boolean shouldUpdate() { 138 if ( instance == null ) { 139 return false; 140 } 141 else { 142 return instance.isShowing(); 143 } 144 } 145 146 public int getPersistenceType() { 147 return TopComponent.PERSISTENCE_ALWAYS; 148 } 149 150 public void componentOpened() { 151 } 152 153 public void componentClosed() { 154 } 155 156 157 public Object writeReplace() { 158 return new ResolvableHelper(); 159 } 160 161 protected String preferredID() { 162 return PREFERRED_ID; 163 } 164 165 final static class ResolvableHelper implements Serializable { 166 private static final long serialVersionUID = 1L; 167 public Object readResolve() { 168 return DeclarationTopComponent.getDefault(); 169 } 170 } 171 172 } 173 | Popular Tags |