Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/fluidsynth/api/Status.java


1   /*
2    * Copyright (C) 2003 Ken Ellinwood.
3    * 
4    * This file is part of FluidGUI.
5    * 
6    * FluidGUI is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation; either version 2 of the License, or
9    * (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20  
21  /*
22   * Status.java
23   *
24   * Created on July 24, 2003, 1:33 PM
25   */
26  
27  package org.fluidsynth.api;
28  
29  import java.beans.*;
30  import java.util.logging.*;
31  
32  /**
33   * Manger of status messages displayed in progress bars, and the
34   * status panel. The only instance of this class listens to INFO level
35   * log messages from {@link Log}, formats these messages, broadcasts
36   * the formatted messages via property change events, and maintains a
37   * record of the last message sent (see {@link #getStatus}).  Status
38   * messages can be generated by any object in the system using {@link
39   * Log#info Log.info()}.
40   */
41  public class Status extends Object implements java.io.Serializable {
42  
43      /** Property name of the status property. */
44      public static final String PROP_STATUS = "status.message";
45      
46      // The current value of our status property (value of the last message sent).
47      private String status;  
48      
49      private PropertyChangeSupport propertySupport;
50      
51      private static Status me = new Status();
52  
53      /** Return the singleon instance of this class. */
54      public static Status onlyInstance()
55      {
56          return me;
57      }
58      
59      /** Create a Status instance. */
60      private Status()
61      {
62          propertySupport = new PropertyChangeSupport( this );
63  
64          // Register as a log handler. Receive, format, and rebroadcast
65          // INFO level messages as property change events.
66          Handler localHandler =
67              new Handler()
68              {
69                  public void close() {}
70                  public void flush() {}
71                  public void publish( LogRecord rec) {
72                      if (Level.INFO.equals( rec.getLevel()))
73                          setStatus( getFormatter().formatMessage( rec));
74                  }
75                  
76                  // Ignore all but INFO level messages.
77                  public boolean isLoggable(LogRecord record)
78                  {
79                      return Level.INFO.equals( record.getLevel());
80                  }
81              };
82          localHandler.setFormatter( new SimpleFormatter());
83          Log.onlyInstance().getLogger().addHandler( localHandler);
84          
85      }
86  
87      /** Get the current status. */
88      public String getStatus() {
89          return status;
90      }
91      
92      /** Set the current status. Property change listeners are notified of the change. */
93      private void setStatus(String value) {
94          String oldValue = status;
95          status = value;
96          propertySupport.firePropertyChange(PROP_STATUS, oldValue, status);
97      }
98      
99      
100     /** Add a property change listener. */
101     public void addPropertyChangeListener(PropertyChangeListener listener) {
102         propertySupport.addPropertyChangeListener(listener);
103     }
104     
105     /** Remove a property change listener. */
106     public void removePropertyChangeListener(PropertyChangeListener listener) {
107         propertySupport.removePropertyChangeListener(listener);
108     }
109     
110 }