~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to ide/welcome/src/org/netbeans/modules/welcome/WelcomeComponent.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.welcome;
 
43
 
 
44
import java.lang.ref.WeakReference;
 
45
import org.openide.util.NbBundle;
 
46
import org.openide.windows.*;
 
47
import java.awt.*;
 
48
import javax.swing.*;
 
49
import org.netbeans.modules.welcome.ui.StartPageContent;
 
50
import org.openide.ErrorManager;
 
51
import org.openide.nodes.Node;
 
52
 
 
53
/**
 
54
 * The welcome screen.
 
55
 * @author  Richard Gregor, S. Aubrecht
 
56
 */
 
57
public class WelcomeComponent extends TopComponent {
 
58
    static final long serialVersionUID=6021472310161712674L;
 
59
    private static WeakReference<WelcomeComponent> component =
 
60
                new WeakReference<WelcomeComponent>(null); 
 
61
    private JComponent content;
 
62
 
 
63
    private boolean initialized = false;
 
64
    
 
65
    private WelcomeComponent(){
 
66
        setLayout(new BorderLayout());
 
67
        setName(NbBundle.getMessage(WelcomeComponent.class, "LBL_Tab_Title"));   //NOI18N
 
68
        content = null;
 
69
        initialized = false;
 
70
        putClientProperty( "activateAtStartup", Boolean.TRUE ); //NOI18N
 
71
    }
 
72
    
 
73
    @Override protected String preferredID(){
 
74
        return "WelcomeComponent";    //NOI18N
 
75
    }
 
76
    
 
77
    /**
 
78
     * #38900 - lazy addition of GUI components
 
79
     */    
 
80
    
 
81
    private void doInitialize() {
 
82
        initAccessibility();
 
83
        
 
84
        if( null == content ) {
 
85
            WelcomeOptions.getDefault().incrementStartCounter();
 
86
            content = new StartPageContent();
 
87
 
 
88
            add( content, BorderLayout.CENTER );
 
89
            setFocusable( false );
 
90
        }
 
91
    }
 
92
        
 
93
    /* Singleton accessor. As WelcomeComponent is persistent singleton this
 
94
     * accessor makes sure that WelcomeComponent is deserialized by window system.
 
95
     * Uses known unique TopComponent ID "Welcome" to get WelcomeComponent instance
 
96
     * from window system. "Welcome" is name of settings file defined in module layer.
 
97
     */
 
98
    public static WelcomeComponent findComp() {
 
99
        WelcomeComponent wc = component.get();
 
100
        if (wc == null) {
 
101
            TopComponent tc = WindowManager.getDefault().findTopComponent("Welcome"); // NOI18N
 
102
            if (tc != null) {
 
103
                if (tc instanceof WelcomeComponent) {
 
104
                    wc = (WelcomeComponent)tc;
 
105
                    component = new WeakReference<WelcomeComponent>(wc); 
 
106
                } else {
 
107
                    //Incorrect settings file?
 
108
                    IllegalStateException exc = new IllegalStateException
 
109
                    ("Incorrect settings file. Unexpected class returned." // NOI18N
 
110
                    + " Expected:" + WelcomeComponent.class.getName() // NOI18N
 
111
                    + " Returned:" + tc.getClass().getName()); // NOI18N
 
112
                    ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
 
113
                    //Fallback to accessor reserved for window system.
 
114
                    wc = WelcomeComponent.createComp();
 
115
                }
 
116
            } else {
 
117
                //WelcomeComponent cannot be deserialized
 
118
                //Fallback to accessor reserved for window system.
 
119
                wc = WelcomeComponent.createComp();
 
120
            }
 
121
        }       
 
122
        return wc;
 
123
    }
 
124
    
 
125
    /* Singleton accessor reserved for window system ONLY. Used by window system to create
 
126
     * WelcomeComponent instance from settings file when method is given. Use <code>findComp</code>
 
127
     * to get correctly deserialized instance of WelcomeComponent. */
 
128
    public static WelcomeComponent createComp() {
 
129
        WelcomeComponent wc = component.get();
 
130
        if(wc == null) {
 
131
            wc = new WelcomeComponent();
 
132
            component = new WeakReference<WelcomeComponent>(wc);
 
133
        }
 
134
        return wc;
 
135
    }
 
136
    
 
137
    /** Overriden to explicitely set persistence type of WelcomeComponent
 
138
     * to PERSISTENCE_ALWAYS */
 
139
    @Override public int getPersistenceType() {
 
140
        return TopComponent.PERSISTENCE_NEVER;
 
141
    }
 
142
    
 
143
    private void initAccessibility(){
 
144
        getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(WelcomeComponent.class, "ACS_Welcome_DESC")); // NOI18N
 
145
    }
 
146
 
 
147
    /**
 
148
     * Called when <code>TopComponent</code> is about to be shown.
 
149
     * Shown here means the component is selected or resides in it own cell
 
150
     * in container in its <code>Mode</code>. The container is visible and not minimized.
 
151
     * <p><em>Note:</em> component
 
152
     * is considered to be shown, even its container window
 
153
     * is overlapped by another window.</p>
 
154
     * @since 2.18
 
155
     *
 
156
     * #38900 - lazy addition of GUI components
 
157
     *
 
158
     */
 
159
    @Override protected void componentShowing() {
 
160
        if (!initialized) {
 
161
            initialized = true;
 
162
            doInitialize();
 
163
        }
 
164
        if( null != content && getComponentCount() == 0 ) {
 
165
            //notify components down the hierarchy tree that they should 
 
166
            //refresh their content (e.g. RSS feeds)
 
167
            add( content, BorderLayout.CENTER );
 
168
        }
 
169
        super.componentShowing();
 
170
        setActivatedNodes( new Node[] {} );
 
171
    }
 
172
 
 
173
    private static boolean firstTimeOpen = true;
 
174
    @Override 
 
175
    protected void componentOpened() {
 
176
        super.componentOpened();
 
177
        if( firstTimeOpen ) {
 
178
            firstTimeOpen = false;
 
179
            if( !WelcomeOptions.getDefault().isShowOnStartup() ) {
 
180
                close();
 
181
            }
 
182
        }
 
183
    }
 
184
    
 
185
    @Override protected void componentHidden() {
 
186
        super.componentHidden();
 
187
        if( null != content ) {
 
188
            //notify components down the hierarchy tree that they no long 
 
189
            //need to periodically refresh their content (e.g. RSS feeds)
 
190
            remove( content );
 
191
        }
 
192
    }
 
193
 
 
194
    @Override
 
195
    public void requestFocus() {
 
196
        if( null != content )
 
197
            content.requestFocus();
 
198
    }
 
199
 
 
200
    @Override
 
201
    public boolean requestFocusInWindow() {
 
202
        if( null != content )
 
203
            return content.requestFocusInWindow();
 
204
        return super.requestFocusInWindow();
 
205
    }
 
206
}
 
207