KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > timers > TimeComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.timers;
20
21 import java.awt.GridBagConstraints JavaDoc;
22 import java.awt.GridBagLayout JavaDoc;
23 import org.openide.ErrorManager;
24 import org.openide.util.NbBundle;
25 import org.openide.util.Utilities;
26 import org.openide.windows.TopComponent;
27 import org.openide.windows.WindowManager;
28
29 /**
30  *
31  * @author Jan Lahoda
32  */

33 public class TimeComponent extends TopComponent {
34
35     private static final String JavaDoc PREFERRED_ID = "timers"; //NOI18N
36
static final String JavaDoc ICON_PATH = "org/netbeans/modules/timers/resources/timer.png"; //NOI18N
37
private static TimeComponent INSTANCE;
38     
39     /**
40      * Creates a new instance of TimeComponent
41      */

42     public TimeComponent() {
43         setName ("timers"); //NOI18N
44
setDisplayName (NbBundle.getMessage ( TimeComponent.class, "LBL_TimeComponent" )); //NOI18N
45
setIcon(Utilities.loadImage(ICON_PATH));
46         
47         setLayout(new GridBagLayout JavaDoc());
48         
49         GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
50         
51         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
52         gridBagConstraints.weightx = 1.0;
53         gridBagConstraints.weighty = 1.0;
54
55         add(new TimeComponentPanel(), gridBagConstraints);
56     }
57
58     public @Override JavaDoc String JavaDoc preferredID () {
59         return PREFERRED_ID;
60     }
61     
62
63     public @Override JavaDoc int getPersistenceType () {
64         return PERSISTENCE_ALWAYS;
65     }
66     
67     /**
68      * Gets default instance. Do not use directly: reserved for *.settings files only,
69      * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
70      * To obtain the singleton instance, use {@link findInstance}.
71      */

72     public static synchronized TimeComponent getDefault() {
73         if (INSTANCE == null) {
74             INSTANCE = new TimeComponent();
75         }
76         return INSTANCE;
77     }
78     
79     public static synchronized TimeComponent findInstance() {
80         TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
81         if (win == null) {
82             ErrorManager.getDefault().log(ErrorManager.WARNING,
83                     "Cannot find TimeComponent component. It will not be located properly in the window system.");
84             return getDefault();
85         }
86         if (win instanceof TimeComponent) {
87             return (TimeComponent)win;
88         }
89         ErrorManager.getDefault().log(ErrorManager.WARNING,
90                 "There seem to be multiple components with the '" + PREFERRED_ID +
91                 "' ID. That is a potential source of errors and unexpected behavior.");
92         return getDefault();
93     }
94     
95 }
96
Popular Tags