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.
     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.examplemodule03;
    43 
    44 import org.openide.ErrorManager;
    45 import org.openide.util.HelpCtx;
    46 import org.openide.util.NbBundle;
    47 import org.openide.windows.TopComponent;
    48 import org.openide.windows.WindowManager;
    49 
    50 import java.awt.BorderLayout;
    51 import javax.swing.JButton;
    52 
    53 /**
    54  *
    55  * Example implementation of persistent TopComponent.
    56  *
    57  * @author  Marek Slama
    58  *
    59  */
    60 
    61 class TestComponent00 extends TopComponent {
    62 
    63     static final long serialVersionUID = 6021472310161712674L;
    64 
    65     private static TestComponent00 component = null;
    66 
    67     private static final String TC_ID = "test03tc00";
    68 
    69     private JButton button;
    70 
    71     private boolean isGUICreated = false;
    72     
    73     private TestComponent00() {
    74         setName(NbBundle.getMessage(TestComponent00.class, "LBL_Tab_Title00"));
    75     }
    76 
    77     private void initGUI () {
    78         setLayout(new BorderLayout());
    79         button = new JButton(getName());
    80         add(button);
    81         isGUICreated = true;
    82     }
    83 
    84     protected void componentShowing () {
    85         if (!isGUICreated) {
    86             initGUI();
    87         }
    88     }
    89 
    90     protected String preferredID () {
    91         return TC_ID;
    92     }
    93     
    94     /* Singleton accessor. As TestComponent00 is persistent singleton this
    95      * accessor makes sure that TestComponent00 is deserialized by window system.
    96      * Uses known unique TopComponent ID "test03tc00" to get TestComponent00 instance
    97      * from window system. "test03tc00" is name of settings file defined in module layer.
    98      * For example ShowTestAction00 uses this method to create instance if necessary.
    99      */
   100     public static synchronized TestComponent00 findDefault() {
   101         if (component == null) {
   102             //If settings file is correctly defined call of WindowManager.findTopComponent() will
   103             //call TestComponent00.getDefault() and it will set static field component.
   104             TopComponent tc = WindowManager.getDefault().findTopComponent(TC_ID);
   105             if (tc != null) {
   106                 if (!(tc instanceof TestComponent00)) {
   107                     //This should not happen. Possible only if some other module
   108                     //defines different settings file with the same name but different class.
   109                     //Incorrect settings file?
   110                     IllegalStateException exc = new IllegalStateException
   111                     ("Incorrect settings file. Unexpected class returned." // NOI18N
   112                     + " Expected:" + TestComponent00.class.getName() // NOI18N
   113                     + " Returned:" + tc.getClass().getName()); // NOI18N
   114                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
   115                     //Fallback to accessor reserved for window system.
   116                     TestComponent00.getDefault();
   117                 }
   118             } else {
   119                 //This should not happen when settings file is correctly defined in module layer.
   120                 //TestComponent00 cannot be deserialized
   121                 //Fallback to accessor reserved for window system.
   122                 ErrorManager.getDefault().log(ErrorManager.WARNING,
   123                 "Cannot deserialize TopComponent for tcID:'" + TC_ID + "'"); // NOI18N
   124                 TestComponent00.getDefault();
   125             }
   126         }
   127         return component;
   128     }
   129     
   130     /* Singleton accessor reserved for window system ONLY. Used by window system to create
   131      * TestComponent00 instance from settings file when method is given. Use <code>findDefault</code>
   132      * to get correctly deserialized instance of TestComponent00. */
   133     public static synchronized TestComponent00 getDefault() {
   134         if (component == null) {
   135             component = new TestComponent00();
   136         }
   137         return component;
   138     }
   139     
   140     /** Overriden to explicitely set persistence type of TestComponent00
   141      * to PERSISTENCE_ALWAYS */
   142     public int getPersistenceType() {
   143         return TopComponent.PERSISTENCE_ALWAYS;
   144     }
   145     
   146     /** Resolve to singleton instance */
   147     public Object readResolve() throws java.io.ObjectStreamException {
   148         return TestComponent00.getDefault();
   149     }
   150     
   151     static void clearRef () {
   152         component = null;
   153     }
   154     
   155     public void requestFocus(){
   156         super.requestFocus();
   157         button.requestFocus();
   158     }
   159     
   160     public boolean requestFocusInWindow(){
   161         super.requestFocusInWindow();
   162         return button.requestFocusInWindow();
   163     }
   164 }