All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.netbeans.modules.welcome.WelcomeComponent Maven / Gradle / Ivy

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
 * Other names may be trademarks of their respective owners.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common
 * Development and Distribution License("CDDL") (collectively, the
 * "License"). You may not use this file except in compliance with the
 * License. You can obtain a copy of the License at
 * http://www.netbeans.org/cddl-gplv2.html
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 * specific language governing permissions and limitations under the
 * License.  When distributing the software, include this License Header
 * Notice in each file and include the License file at
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the GPL Version 2 section of the License file that
 * accompanied this code. If applicable, add the following below the
 * License Header, with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Contributor(s):
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 * Microsystems, Inc. All Rights Reserved.
 *
 * If you wish your version of this file to be governed by only the CDDL
 * or only the GPL Version 2, indicate your decision by adding
 * "[Contributor] elects to include this software in this distribution
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 * single choice of license, a recipient has the option to distribute
 * your version of this file under either the CDDL, the GPL Version 2 or
 * to extend the choice of license to its licensees as provided above.
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 * Version 2 license, then the option applies only if the new code is
 * made subject to such option by the copyright holder.
 *
 * Portions Copyrighted 2012 AgroSense
 */
package org.netbeans.modules.welcome;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.lang.ref.WeakReference;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import org.netbeans.modules.welcome.content.Constants;
import org.netbeans.modules.welcome.ui.StartPageContent;
import org.openide.ErrorManager;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.util.NbPreferences;
import org.openide.windows.TopComponent;
import org.openide.windows.TopComponentGroup;
import org.openide.windows.WindowManager;

/**
 * The welcome screen.
 * @author  Richard Gregor, S. Aubrecht
 */
@NbBundle.Messages({"LBL Tab Title=Start Page",
                    "ACS Welcome DESC=Welcome window providing basic information about IDE."})
public class WelcomeComponent extends TopComponent {
    private static final String ACTIVATE_AT_STARTUP = "activateAtStartup";
    private static final String FIRST_TIME_CLOSE = "firstTimeClose";
    private static final String ID = "Welcome";
    private static final String INITIAL_LAYOUT = "InitialLayout";
    private static final String KEEP_NON_PERSISTENT_TC_IN_MODEL_WHEN_CLOSED = "KeepNonPersistentTCInModelWhenClosed";
    private static final String PROJECT_TAB_LOGICAL_TC = "projectTabLogical_tc";
    private static final String PREFERRED_ID = "WelcomeComponent";
    static final long serialVersionUID=6021472310161712674L;
    private static WeakReference component =
                new WeakReference(null); 
    private JComponent content;

    private boolean initialized = false;
    
    
    
    private WelcomeComponent(){
        setLayout(new BorderLayout());
        setName(Bundle.LBL_Tab_Title());
        content = null;
        initialized = false;
        putClientProperty(ACTIVATE_AT_STARTUP, Boolean.TRUE );
        putClientProperty(KEEP_NON_PERSISTENT_TC_IN_MODEL_WHEN_CLOSED, Boolean.TRUE );
    }
    
    @Override protected String preferredID(){
        return PREFERRED_ID;   
    }
    
    /**
     * #38900 - lazy addition of GUI components
     */    
    
    private void doInitialize() {
        initAccessibility();
        
        if( null == content ) {
            WelcomeOptions.getDefault().incrementStartCounter();

            JScrollPane scroll = new JScrollPane(new StartPageContent());
            scroll.setBorder(BorderFactory.createEmptyBorder());
            scroll.getViewport().setOpaque(false);
            scroll.setOpaque(false);
            scroll.getViewport().setPreferredSize(new Dimension(Constants.START_PAGE_MIN_WIDTH,100));
            JScrollBar vertical = scroll.getVerticalScrollBar();
            if( null != vertical ) {
                vertical.setBlockIncrement(30*Constants.FONT_SIZE);
                vertical.setUnitIncrement(Constants.FONT_SIZE);
            }
            content = scroll;
            add( content, BorderLayout.CENTER );
            setFocusable( false );
        }
    }
        
    /* Singleton accessor. As WelcomeComponent is persistent singleton this
     * accessor makes sure that WelcomeComponent is deserialized by window system.
     * Uses known unique TopComponent ID "Welcome" to get WelcomeComponent instance
     * from window system. "Welcome" is name of settings file defined in module layer.
     */
    public static WelcomeComponent findComp() {
        WelcomeComponent wc = component.get();
        if (wc == null) {
            TopComponent tc = WindowManager.getDefault().findTopComponent(ID);
            if (tc != null) {
                if (tc instanceof WelcomeComponent) {
                    wc = (WelcomeComponent)tc;
                    component = new WeakReference(wc); 
                } else {
                    //Incorrect settings file?
                    IllegalStateException exc = new IllegalStateException
                    ("Incorrect settings file. Unexpected class returned." // NOI18N
                    + " Expected:" + WelcomeComponent.class.getName() // NOI18N
                    + " Returned:" + tc.getClass().getName()); // NOI18N
                    ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
                    //Fallback to accessor reserved for window system.
                    wc = WelcomeComponent.createComp();
                }
            } else {
                //WelcomeComponent cannot be deserialized
                //Fallback to accessor reserved for window system.
                wc = WelcomeComponent.createComp();
            }
        }       
        return wc;
    }
    
    /* Singleton accessor reserved for window system ONLY. Used by window system to create
     * WelcomeComponent instance from settings file when method is given. Use findComp
     * to get correctly deserialized instance of WelcomeComponent. */
    public static WelcomeComponent createComp() {
        WelcomeComponent wc = component.get();
        if(wc == null) {
            wc = new WelcomeComponent();
            component = new WeakReference(wc);
        }
        return wc;
    }
    
    /** Overriden to explicitely set persistence type of WelcomeComponent
     * to PERSISTENCE_ALWAYS */
    @Override public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ONLY_OPENED;
    }
    
    private void initAccessibility(){
        getAccessibleContext().setAccessibleDescription(Bundle.ACS_Welcome_DESC()); // NOI18N
    }

    /**
     * Called when TopComponent is about to be shown.
     * Shown here means the component is selected or resides in it own cell
     * in container in its Mode. The container is visible and not minimized.
     * 

Note: component * is considered to be shown, even its container window * is overlapped by another window.

* @since 2.18 * * #38900 - lazy addition of GUI components * */ @Override protected void componentShowing() { if (!initialized) { initialized = true; doInitialize(); } if( null != content && getComponentCount() == 0 ) { //notify components down the hierarchy tree that they should //refresh their content (e.g. RSS feeds) add( content, BorderLayout.CENTER ); } super.componentShowing(); setActivatedNodes( new Node[] {} ); } private static boolean firstTimeOpen = true; @Override protected void componentOpened() { super.componentOpened(); if( firstTimeOpen ) { firstTimeOpen = false; if( !WelcomeOptions.getDefault().isShowOnStartup() ) { close(); } } } @Override protected void componentClosed() { super.componentClosed(); TopComponentGroup group = WindowManager.getDefault().findTopComponentGroup(INITIAL_LAYOUT); if( null != group ) { group.open(); boolean firstTimeClose = NbPreferences.forModule(WelcomeComponent.class).getBoolean(FIRST_TIME_CLOSE, true); NbPreferences.forModule(WelcomeComponent.class).putBoolean(FIRST_TIME_CLOSE, false); if( firstTimeClose ) { TopComponent tc = WindowManager.getDefault().findTopComponent(PROJECT_TAB_LOGICAL_TC); if( null != tc && tc.isOpened() ) tc.requestActive(); } } } @Override protected void componentHidden() { super.componentHidden(); if( null != content ) { //notify components down the hierarchy tree that they no long //need to periodically refresh their content (e.g. RSS feeds) remove( content ); } } @Override public void requestFocus() { if( null != content ) content.requestFocus(); } @Override public boolean requestFocusInWindow() { if( null != content ) return content.requestFocusInWindow(); return super.requestFocusInWindow(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy