KickJava   Java API By Example, From Geeks To Geeks.

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


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 DeclarationTopComponent extends TopComponent {
38     
39     private static final Logger JavaDoc LOGGER = Logger.getLogger(DeclarationTopComponent.class.getName());
40     
41     private static DeclarationTopComponent 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/declaration_action.png";
44     
45     private static final String JavaDoc 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 JavaDoc ZERO = new Rectangle JavaDoc(0,0,1,1);
57     
58     void setDeclaration(String JavaDoc declaration) {
59         if (declaration == null) {
60             declarationEditorPane.setText("");
61         } else {
62             declarationEditorPane.setText(declaration);
63         }
64         declarationEditorPane.setCaretPosition(0);
65         SwingUtilities.invokeLater(new Runnable JavaDoc() {
66             public void run() {
67                 declarationEditorPane.scrollRectToVisible(ZERO);
68             }
69         });
70     }
71     
72     /** This method is called from within the constructor to
73      * initialize the form.
74      * WARNING: Do NOT modify this code. The content of this method is
75      * always regenerated by the Form Editor.
76      */

77     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
78
private void initComponents() {
79
80         declarationScrollPane = new javax.swing.JScrollPane JavaDoc();
81         declarationEditorPane = new javax.swing.JEditorPane JavaDoc();
82
83         declarationEditorPane.setBackground(new java.awt.Color JavaDoc(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     }// </editor-fold>//GEN-END:initComponents
99

100     
101     // Variables declaration - do not modify//GEN-BEGIN:variables
102
private javax.swing.JEditorPane JavaDoc declarationEditorPane;
103     private javax.swing.JScrollPane JavaDoc declarationScrollPane;
104     // End of variables declaration//GEN-END:variables
105

106     /**
107      * Gets default instance. Do not use directly: reserved for *.settings files only,
108      * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
109      * To obtain the singleton instance, use {@link findInstance}.
110      */

111     public static synchronized DeclarationTopComponent getDefault() {
112         if (instance == null) {
113             instance = new DeclarationTopComponent();
114         }
115         return instance;
116     }
117     
118     /**
119      * Obtain the DeclarationTopComponent instance. Never call {@link #getDefault} directly!
120      */

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     /** replaces this in object stream */
157     public Object JavaDoc writeReplace() {
158         return new ResolvableHelper();
159     }
160     
161     protected String JavaDoc preferredID() {
162         return PREFERRED_ID;
163     }
164     
165     final static class ResolvableHelper implements Serializable JavaDoc {
166         private static final long serialVersionUID = 1L;
167         public Object JavaDoc readResolve() {
168             return DeclarationTopComponent.getDefault();
169         }
170     }
171     
172 }
173
Popular Tags