platform/samples/window-system-groups/src/org/netbeans/modules/examplemodule03/TestComponent00.java
author Tomas Mysik <tmysik@netbeans.org>
Mon, 12 Jun 2017 09:13:11 +0200
changeset 6391 640ddcff4333
parent 1779 5200163245c9
permissions -rw-r--r--
Return blacklisted methods back

No blacklisted methods right now but keep the code for possible future usage.
jglick@1688
     1
/*
mzlamal@2561
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@1779
     3
 *
mzlamal@2561
     4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
jtulach@1779
     5
 *
mzlamal@2561
     6
 * The contents of this file are subject to the terms of either the GNU
mzlamal@2561
     7
 * General Public License Version 2 only ("GPL") or the Common
mzlamal@2561
     8
 * Development and Distribution License("CDDL") (collectively, the
mzlamal@2561
     9
 * "License"). You may not use this file except in compliance with the
mzlamal@2561
    10
 * License. You can obtain a copy of the License at
mzlamal@2561
    11
 * http://www.netbeans.org/cddl-gplv2.html
mzlamal@2561
    12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
mzlamal@2561
    13
 * specific language governing permissions and limitations under the
mzlamal@2561
    14
 * License.  When distributing the software, include this License Header
mzlamal@2561
    15
 * Notice in each file and include the License file at
mzlamal@2561
    16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
mzlamal@2561
    17
 * particular file as subject to the "Classpath" exception as provided
mzlamal@2561
    18
 * by Sun in the GPL Version 2 section of the License file that
mzlamal@2561
    19
 * accompanied this code. If applicable, add the following below the
mzlamal@2561
    20
 * License Header, with the fields enclosed by brackets [] replaced by
mzlamal@2561
    21
 * your own identifying information:
jtulach@1779
    22
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@1779
    23
 *
mzlamal@2561
    24
 * Contributor(s):
mzlamal@2561
    25
 *
jtulach@1779
    26
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@1779
    27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
jglick@1688
    28
 * Microsystems, Inc. All Rights Reserved.
mzlamal@2561
    29
 *
mzlamal@2561
    30
 * If you wish your version of this file to be governed by only the CDDL
mzlamal@2561
    31
 * or only the GPL Version 2, indicate your decision by adding
mzlamal@2561
    32
 * "[Contributor] elects to include this software in this distribution
mzlamal@2561
    33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
mzlamal@2561
    34
 * single choice of license, a recipient has the option to distribute
mzlamal@2561
    35
 * your version of this file under either the CDDL, the GPL Version 2 or
mzlamal@2561
    36
 * to extend the choice of license to its licensees as provided above.
mzlamal@2561
    37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
mzlamal@2561
    38
 * Version 2 license, then the option applies only if the new code is
mzlamal@2561
    39
 * made subject to such option by the copyright holder.
jglick@1688
    40
 */
jglick@1688
    41
jglick@1688
    42
package org.netbeans.modules.examplemodule03;
jglick@1688
    43
jglick@1688
    44
import org.openide.ErrorManager;
jglick@1688
    45
import org.openide.util.HelpCtx;
jglick@1688
    46
import org.openide.util.NbBundle;
jglick@1688
    47
import org.openide.windows.TopComponent;
jglick@1688
    48
import org.openide.windows.WindowManager;
jglick@1688
    49
jglick@1688
    50
import java.awt.BorderLayout;
jglick@1688
    51
import javax.swing.JButton;
jglick@1688
    52
jglick@1688
    53
/**
jglick@1688
    54
 *
mslama@1751
    55
 * Example implementation of persistent TopComponent.
mslama@1751
    56
 *
jglick@1688
    57
 * @author  Marek Slama
mslama@1751
    58
 *
jglick@1688
    59
 */
jglick@1688
    60
jglick@1688
    61
class TestComponent00 extends TopComponent {
mslama@1751
    62
jglick@1688
    63
    static final long serialVersionUID = 6021472310161712674L;
mslama@1751
    64
jglick@1688
    65
    private static TestComponent00 component = null;
mslama@1751
    66
mslama@1751
    67
    private static final String TC_ID = "test03tc00";
mslama@1751
    68
jglick@1688
    69
    private JButton button;
mslama@1751
    70
mslama@1751
    71
    private boolean isGUICreated = false;
jglick@1688
    72
    
jglick@1688
    73
    private TestComponent00() {
mslama@1751
    74
        setName(NbBundle.getMessage(TestComponent00.class, "LBL_Tab_Title00"));
mslama@1751
    75
    }
mslama@1751
    76
mslama@1751
    77
    private void initGUI () {
jglick@1688
    78
        setLayout(new BorderLayout());
jglick@1688
    79
        button = new JButton(getName());
jglick@1688
    80
        add(button);
mslama@1751
    81
        isGUICreated = true;
mslama@1751
    82
    }
mslama@1751
    83
mslama@1751
    84
    protected void componentShowing () {
mslama@1751
    85
        if (!isGUICreated) {
mslama@1751
    86
            initGUI();
mslama@1751
    87
        }
mslama@1751
    88
    }
mslama@1751
    89
mslama@1751
    90
    protected String preferredID () {
mslama@1751
    91
        return TC_ID;
jglick@1688
    92
    }
jglick@1688
    93
    
jglick@1688
    94
    /* Singleton accessor. As TestComponent00 is persistent singleton this
jglick@1688
    95
     * accessor makes sure that TestComponent00 is deserialized by window system.
jglick@1688
    96
     * Uses known unique TopComponent ID "test03tc00" to get TestComponent00 instance
jglick@1688
    97
     * from window system. "test03tc00" is name of settings file defined in module layer.
jglick@1688
    98
     * For example ShowTestAction00 uses this method to create instance if necessary.
jglick@1688
    99
     */
jglick@1688
   100
    public static synchronized TestComponent00 findDefault() {
jglick@1688
   101
        if (component == null) {
jglick@1688
   102
            //If settings file is correctly defined call of WindowManager.findTopComponent() will
jglick@1688
   103
            //call TestComponent00.getDefault() and it will set static field component.
mslama@1751
   104
            TopComponent tc = WindowManager.getDefault().findTopComponent(TC_ID);
jglick@1688
   105
            if (tc != null) {
jglick@1688
   106
                if (!(tc instanceof TestComponent00)) {
jglick@1688
   107
                    //This should not happen. Possible only if some other module
jglick@1688
   108
                    //defines different settings file with the same name but different class.
jglick@1688
   109
                    //Incorrect settings file?
jglick@1688
   110
                    IllegalStateException exc = new IllegalStateException
jglick@1688
   111
                    ("Incorrect settings file. Unexpected class returned." // NOI18N
jglick@1688
   112
                    + " Expected:" + TestComponent00.class.getName() // NOI18N
jglick@1688
   113
                    + " Returned:" + tc.getClass().getName()); // NOI18N
jglick@1688
   114
                    ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
jglick@1688
   115
                    //Fallback to accessor reserved for window system.
jglick@1688
   116
                    TestComponent00.getDefault();
jglick@1688
   117
                }
jglick@1688
   118
            } else {
jglick@1688
   119
                //This should not happen when settings file is correctly defined in module layer.
jglick@1688
   120
                //TestComponent00 cannot be deserialized
jglick@1688
   121
                //Fallback to accessor reserved for window system.
mslama@1751
   122
                ErrorManager.getDefault().log(ErrorManager.WARNING,
mslama@1751
   123
                "Cannot deserialize TopComponent for tcID:'" + TC_ID + "'"); // NOI18N
jglick@1688
   124
                TestComponent00.getDefault();
jglick@1688
   125
            }
jglick@1688
   126
        }
jglick@1688
   127
        return component;
jglick@1688
   128
    }
jglick@1688
   129
    
jglick@1688
   130
    /* Singleton accessor reserved for window system ONLY. Used by window system to create
jglick@1688
   131
     * TestComponent00 instance from settings file when method is given. Use <code>findDefault</code>
jglick@1688
   132
     * to get correctly deserialized instance of TestComponent00. */
jglick@1688
   133
    public static synchronized TestComponent00 getDefault() {
jglick@1688
   134
        if (component == null) {
jglick@1688
   135
            component = new TestComponent00();
jglick@1688
   136
        }
jglick@1688
   137
        return component;
jglick@1688
   138
    }
jglick@1688
   139
    
jglick@1688
   140
    /** Overriden to explicitely set persistence type of TestComponent00
jglick@1688
   141
     * to PERSISTENCE_ALWAYS */
jglick@1688
   142
    public int getPersistenceType() {
jglick@1688
   143
        return TopComponent.PERSISTENCE_ALWAYS;
jglick@1688
   144
    }
jglick@1688
   145
    
jglick@1688
   146
    /** Resolve to singleton instance */
jglick@1688
   147
    public Object readResolve() throws java.io.ObjectStreamException {
jglick@1688
   148
        return TestComponent00.getDefault();
jglick@1688
   149
    }
jglick@1688
   150
    
jglick@1688
   151
    static void clearRef () {
jglick@1688
   152
        component = null;
jglick@1688
   153
    }
jglick@1688
   154
    
jglick@1688
   155
    public void requestFocus(){
jglick@1688
   156
        super.requestFocus();
jglick@1688
   157
        button.requestFocus();
jglick@1688
   158
    }
jglick@1688
   159
    
jglick@1688
   160
    public boolean requestFocusInWindow(){
jglick@1688
   161
        super.requestFocusInWindow();
jglick@1688
   162
        return button.requestFocusInWindow();
jglick@1688
   163
    }
jglick@1688
   164
}