001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.navigation;
043:
044: import java.awt.Rectangle;
045: import java.io.Serializable;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.SwingUtilities;
049: import org.openide.util.NbBundle;
050: import org.openide.windows.TopComponent;
051: import org.openide.windows.WindowManager;
052: import org.openide.util.Utilities;
053:
054: /**
055: * Top component which displays something.
056: *
057: * @author Sandip V. Chitale (Sandip.Chitale@Sun.Com)
058: */
059: public final class DeclarationTopComponent extends TopComponent {
060:
061: private static final Logger LOGGER = Logger
062: .getLogger(DeclarationTopComponent.class.getName());
063:
064: private static DeclarationTopComponent instance;
065: /** path to the icon used by the component and its open action */
066: public static final String ICON_PATH = "org/netbeans/modules/java/navigation/resources/declaration_action.png";
067:
068: private static final String PREFERRED_ID = "DeclarationTopComponent";
069:
070: private static boolean shouldUpdate;
071:
072: private DeclarationTopComponent() {
073: initComponents();
074: setName(NbBundle.getMessage(DeclarationTopComponent.class,
075: "CTL_DeclarationTopComponent"));
076: setToolTipText(NbBundle.getMessage(
077: DeclarationTopComponent.class,
078: "HINT_DeclarationTopComponent"));
079: setIcon(Utilities.loadImage(ICON_PATH, true));
080:
081: // Don't highlight caret row
082: declarationEditorPane
083: .putClientProperty(
084: "HighlightsLayerExcludes", // NOI18N
085: "^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$" // NOI18N
086: );
087: }
088:
089: private static final Rectangle ZERO = new Rectangle(0, 0, 1, 1);
090:
091: void setDeclaration(String declaration) {
092: if (declaration == null) {
093: declarationEditorPane.setText("");
094: } else {
095: declarationEditorPane.setText(declaration);
096: }
097: declarationEditorPane.setCaretPosition(0);
098: SwingUtilities.invokeLater(new Runnable() {
099: public void run() {
100: declarationEditorPane.scrollRectToVisible(ZERO);
101: }
102: });
103: }
104:
105: /** This method is called from within the constructor to
106: * initialize the form.
107: * WARNING: Do NOT modify this code. The content of this method is
108: * always regenerated by the Form Editor.
109: */
110: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
111: private void initComponents() {
112:
113: declarationScrollPane = new javax.swing.JScrollPane();
114: declarationEditorPane = new javax.swing.JEditorPane();
115:
116: declarationEditorPane.setBackground(new java.awt.Color(238,
117: 238, 255));
118: declarationEditorPane.setContentType("text/x-java");
119: declarationEditorPane.setEditable(false);
120: declarationScrollPane.setViewportView(declarationEditorPane);
121:
122: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
123: this );
124: this .setLayout(layout);
125: layout.setHorizontalGroup(layout.createParallelGroup(
126: org.jdesktop.layout.GroupLayout.LEADING).add(
127: declarationScrollPane,
128: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400,
129: Short.MAX_VALUE));
130: layout.setVerticalGroup(layout.createParallelGroup(
131: org.jdesktop.layout.GroupLayout.LEADING).add(
132: declarationScrollPane,
133: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300,
134: Short.MAX_VALUE));
135: }// </editor-fold>//GEN-END:initComponents
136:
137: // Variables declaration - do not modify//GEN-BEGIN:variables
138: private javax.swing.JEditorPane declarationEditorPane;
139: private javax.swing.JScrollPane declarationScrollPane;
140:
141: // End of variables declaration//GEN-END:variables
142:
143: /**
144: * Gets default instance. Do not use directly: reserved for *.settings files only,
145: * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
146: * To obtain the singleton instance, use {@link findInstance}.
147: */
148: public static synchronized DeclarationTopComponent getDefault() {
149: if (instance == null) {
150: instance = new DeclarationTopComponent();
151: }
152: return instance;
153: }
154:
155: /**
156: * Obtain the DeclarationTopComponent instance. Never call {@link #getDefault} directly!
157: */
158: public static synchronized DeclarationTopComponent findInstance() {
159: TopComponent win = WindowManager.getDefault().findTopComponent(
160: PREFERRED_ID);
161: if (win == null) {
162: LOGGER
163: .log(
164: Level.WARNING,
165: "Cannot find MyWindow component. It will not be located properly in the window system.");
166: return getDefault();
167: }
168: if (win instanceof DeclarationTopComponent) {
169: return (DeclarationTopComponent) win;
170: }
171: LOGGER
172: .log(
173: Level./* Shut up! Logged dozens of times in every session. */FINE,
174: "There seem to be multiple components with the '"
175: + PREFERRED_ID
176: + "' ID. That is a potential source of errors and unexpected behavior.");
177: return getDefault();
178: }
179:
180: public static boolean shouldUpdate() {
181: if (instance == null) {
182: return false;
183: } else {
184: return instance.isShowing();
185: }
186: }
187:
188: public int getPersistenceType() {
189: return TopComponent.PERSISTENCE_ALWAYS;
190: }
191:
192: public void componentOpened() {
193: }
194:
195: public void componentClosed() {
196: }
197:
198: @Override
199: protected void componentShowing() {
200: super .componentShowing();
201: CaretListeningFactory.runAgain();
202: }
203:
204: /** replaces this in object stream */
205: public Object writeReplace() {
206: return new ResolvableHelper();
207: }
208:
209: protected String preferredID() {
210: return PREFERRED_ID;
211: }
212:
213: final static class ResolvableHelper implements Serializable {
214: private static final long serialVersionUID = 1L;
215:
216: public Object readResolve() {
217: return DeclarationTopComponent.getDefault();
218: }
219: }
220:
221: }
|