KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > welcome > WelcomeComponent


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.welcome;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import org.netbeans.modules.welcome.content.ContentPanel;
24 import org.openide.util.NbBundle;
25 import org.openide.windows.*;
26 import java.awt.*;
27 import javax.swing.*;
28 import org.netbeans.modules.welcome.content.ContentFactory;
29 import org.openide.ErrorManager;
30 import org.openide.nodes.Node;
31
32 /**
33  * The welcome screen.
34  * @author Richard Gregor
35  */

36 public class WelcomeComponent extends TopComponent {
37     static final long serialVersionUID=6021472310161712674L;
38     private static WeakReference JavaDoc<WelcomeComponent> component =
39                 new WeakReference JavaDoc<WelcomeComponent>(null);
40     private JComponent content;
41
42     private boolean initialized = false;
43     
44     private WelcomeComponent(){
45         setLayout(new BorderLayout());
46         setName(NbBundle.getMessage(WelcomeComponent.class, "LBL_Tab_Title")); //NOI18N
47
content = null;
48         initialized = false;
49     }
50     
51     protected String JavaDoc preferredID(){
52         return "WelcomeComponent"; //NOI18N
53
}
54     
55     /**
56      * #38900 - lazy addition of GUI components
57      */

58     
59     private void doInitialize() {
60         initAccessibility();
61         
62         content = ContentFactory.createContentPane();
63         if( null == content )
64             return;
65
66         add( content, BorderLayout.CENTER );
67         setFocusable( false );
68     }
69         
70     /* Singleton accessor. As WelcomeComponent is persistent singleton this
71      * accessor makes sure that WelcomeComponent is deserialized by window system.
72      * Uses known unique TopComponent ID "Welcome" to get WelcomeComponent instance
73      * from window system. "Welcome" is name of settings file defined in module layer.
74      */

75     public static WelcomeComponent findComp() {
76         WelcomeComponent wc = component.get();
77         if (wc == null) {
78             TopComponent tc = WindowManager.getDefault().findTopComponent("Welcome"); // NOI18N
79
if (tc != null) {
80                 if (tc instanceof WelcomeComponent) {
81                     wc = (WelcomeComponent)tc;
82                     component = new WeakReference JavaDoc<WelcomeComponent>(wc);
83                 } else {
84                     //Incorrect settings file?
85
IllegalStateException JavaDoc exc = new IllegalStateException JavaDoc
86                     ("Incorrect settings file. Unexpected class returned." // NOI18N
87
+ " Expected:" + WelcomeComponent.class.getName() // NOI18N
88
+ " Returned:" + tc.getClass().getName()); // NOI18N
89
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
90                     //Fallback to accessor reserved for window system.
91
wc = WelcomeComponent.createComp();
92                 }
93             } else {
94                 //WelcomeComponent cannot be deserialized
95
//Fallback to accessor reserved for window system.
96
wc = WelcomeComponent.createComp();
97             }
98         }
99         return wc;
100     }
101     
102     /* Singleton accessor reserved for window system ONLY. Used by window system to create
103      * WelcomeComponent instance from settings file when method is given. Use <code>findComp</code>
104      * to get correctly deserialized instance of WelcomeComponent. */

105     public static WelcomeComponent createComp() {
106         WelcomeComponent wc = (WelcomeComponent)component.get();
107         if(wc == null) {
108             wc = new WelcomeComponent();
109             component = new WeakReference JavaDoc<WelcomeComponent>(wc);
110         }
111         return wc;
112     }
113     
114     /** Overriden to explicitely set persistence type of WelcomeComponent
115      * to PERSISTENCE_ALWAYS */

116     public int getPersistenceType() {
117         return TopComponent.PERSISTENCE_NEVER;
118     }
119     
120     private void initAccessibility(){
121         getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(WelcomeComponent.class, "ACS_Welcome_DESC")); // NOI18N
122
}
123
124     /**
125      * #38900 - lazy addition of GUI components
126      */

127     public void addNotify() {
128         if (!initialized) {
129             initialized = true;
130             doInitialize();
131         }
132         super.addNotify();
133     }
134     
135     /**
136      * Called when <code>TopComponent</code> is about to be shown.
137      * Shown here means the component is selected or resides in it own cell
138      * in container in its <code>Mode</code>. The container is visible and not minimized.
139      * <p><em>Note:</em> component
140      * is considered to be shown, even its container window
141      * is overlapped by another window.</p>
142      * @since 2.18
143      *
144      * #38900 - lazy addition of GUI components
145      *
146      */

147     protected void componentShowing() {
148         if (!initialized) {
149             initialized = true;
150             doInitialize();
151         }
152         super.componentShowing();
153         setActivatedNodes( new Node[] {} );
154     }
155
156     private static boolean firstTimeOpen = true;
157     protected void componentOpened() {
158         super.componentOpened();
159         if( firstTimeOpen ) {
160             firstTimeOpen = false;
161             if( !WelcomeOptions.getDefault().isShowOnStartup() ) {
162                 close();
163             }
164         }
165     }
166
167     protected void componentActivated() {
168         super.componentActivated();
169         focusAnyContentPanel( content );
170     }
171
172     private boolean focusAnyContentPanel( Container comp ) {
173         Component[] children = comp.getComponents();
174         for( int i=0; i<children.length; i++ ) {
175             if( children[i] instanceof ContentPanel ) {
176                 ((ContentPanel)children[i]).switchFocus();
177                 return true;
178             } else if( children[i] instanceof Container
179                     && focusAnyContentPanel( (Container)children[i] ) ) {
180                 return true;
181             }
182         }
183         return false;
184     }
185 }
186
187
Popular Tags