KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > StatusDisplayer


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.openide.awt;
20
21 import org.openide.util.Lookup;
22
23 import java.util.*;
24
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27
28
29 /** Permits control of a status line.
30  * The default instance may correspond to the NetBeans status line in the main window.
31  * @author Jesse Glick
32  * @since 3.14
33  */

34 public abstract class StatusDisplayer {
35     private static StatusDisplayer INSTANCE = null;
36
37     /** Subclass constructor. */
38     protected StatusDisplayer() {
39     }
40
41     /** Get the default status displayer.
42      * @return the default instance from lookup
43      */

44     public static synchronized StatusDisplayer getDefault() {
45         if (INSTANCE == null) {
46             INSTANCE = (StatusDisplayer) Lookup.getDefault().lookup(StatusDisplayer.class);
47
48             if (INSTANCE == null) {
49                 INSTANCE = new Trivial();
50             }
51         }
52
53         return INSTANCE;
54     }
55
56     /** Get the currently displayed text.
57      * <p>Modules should <strong>not</strong> need to call this method.
58      * If you think you really do, please explain why on nbdev.
59      * The implementation of the GUI component (if any) which displays
60      * the text naturally needs to call it.
61      * @return some text
62      */

63     public abstract String JavaDoc getStatusText();
64
65     /** Show text in the status line.
66      * Can be called at any time, but remember the text may not be updated
67      * until the AWT event queue is ready for it - so if you are hogging
68      * the event queue the text will not appear until you release it
69      * (finish your work or display a modal dialog, for example).
70      * <p class="nonnormative">Default implementation of status line in NetBeans
71      * displays the text in status line and clears it after a while.
72      * Also there is no guarantee how long the text will be displayed as
73      * it can be replaced with new call to this method at any time.
74      * @param text the text to be shown
75      */

76     public abstract void setStatusText(String JavaDoc text);
77
78     /** Add a listener for when the text changes.
79      * @param l a listener
80      */

81     public abstract void addChangeListener(ChangeListener JavaDoc l);
82
83     /** Remove a listener for the text.
84      * @param l a listener
85      */

86     public abstract void removeChangeListener(ChangeListener JavaDoc l);
87
88     /**
89      * Trivial default impl for standalone usage.
90      * @see "#32154"
91      */

92     private static final class Trivial extends StatusDisplayer {
93         private List<ChangeListener JavaDoc> listeners = null;
94         private String JavaDoc text = ""; // NOI18N
95

96         public synchronized String JavaDoc getStatusText() {
97             return text;
98         }
99
100         public synchronized void setStatusText(String JavaDoc text) {
101             if (text.equals(this.text)) {
102                 return;
103             }
104
105             this.text = text;
106
107             if (text.length() > 0) {
108                 System.err.println("(" + text + ")"); // NOI18N
109
}
110
111             fireChange();
112         }
113
114         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
115             if (listeners == null) {
116                 listeners = new ArrayList<ChangeListener JavaDoc>();
117             }
118
119             listeners.add(l);
120         }
121
122         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
123             if (listeners != null) {
124                 listeners.remove(l);
125             }
126         }
127
128         protected final void fireChange() {
129             if ((listeners != null) && !listeners.isEmpty()) {
130                 ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
131                 Iterator<ChangeListener JavaDoc> it = listeners.iterator();
132
133                 while (it.hasNext()) {
134                     it.next().stateChanged(ev);
135                 }
136             }
137         }
138     }
139 }
140
Popular Tags