alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.examplemodule03;

import org.openide.ErrorManager;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

import java.awt.BorderLayout;
import javax.swing.JButton;

/**
 *
 * @author  Marek Slama
 */

class TestComponent00 extends TopComponent {
    static final long serialVersionUID = 6021472310161712674L;
    private static TestComponent00 component = null;
    private JButton button;
    
    private TestComponent00() {
        setLayout(new BorderLayout());
        setName(NbBundle.getMessage(TestComponent00.class, "LBL_Tab_Title00"));
        button = new JButton(getName());
        add(button);
    }
    
    /* Singleton accessor. As TestComponent00 is persistent singleton this
     * accessor makes sure that TestComponent00 is deserialized by window system.
     * Uses known unique TopComponent ID "test03tc00" to get TestComponent00 instance
     * from window system. "test03tc00" is name of settings file defined in module layer.
     * For example ShowTestAction00 uses this method to create instance if necessary.
     */
    public static synchronized TestComponent00 findDefault() {
        if (component == null) {
            //If settings file is correctly defined call of WindowManager.findTopComponent() will
            //call TestComponent00.getDefault() and it will set static field component.
            TopComponent tc = WindowManager.getDefault().findTopComponent("test03tc00"); // NOI18N
            if (tc != null) {
                if (!(tc instanceof TestComponent00)) {
                    //This should not happen. Possible only if some other module
                    //defines different settings file with the same name but different class.
                    //Incorrect settings file?
                    IllegalStateException exc = new IllegalStateException
                    ("Incorrect settings file. Unexpected class returned." // NOI18N
                    + " Expected:" + TestComponent00.class.getName() // NOI18N
                    + " Returned:" + tc.getClass().getName()); // NOI18N
                    ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
                    //Fallback to accessor reserved for window system.
                    TestComponent00.getDefault();
                }
            } else {
                //This should not happen when settings file is correctly defined in module layer.
                //TestComponent00 cannot be deserialized
                //Fallback to accessor reserved for window system.
                TestComponent00.getDefault();
            }
        }
        return component;
    }
    
    /* Singleton accessor reserved for window system ONLY. Used by window system to create
     * TestComponent00 instance from settings file when method is given. Use findDefault
     * to get correctly deserialized instance of TestComponent00. */
    public static synchronized TestComponent00 getDefault() {
        if (component == null) {
            component = new TestComponent00();
        }
        return component;
    }
    
    /** Overriden to explicitely set persistence type of TestComponent00
     * to PERSISTENCE_ALWAYS */
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }
    
    /** Resolve to singleton instance */
    public Object readResolve() throws java.io.ObjectStreamException {
        return TestComponent00.getDefault();
    }
    
    static void clearRef () {
        component = null;
    }
    
    public void requestFocus(){
        super.requestFocus();
        button.requestFocus();
    }
    
    public boolean requestFocusInWindow(){
        super.requestFocusInWindow();
        return button.requestFocusInWindow();
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.