1   /*
2    * %W% %E%
3    *
4    * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5    * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6    */
7   
8   package java.util.prefs;
9   
10  import java.io.NotSerializableException;
11  
12  /**
13   * An event emitted by a <tt>Preferences</tt> node to indicate that 
14   * a preference has been added, removed or has had its value changed.<p>
15   * 
16   * Note, that although PreferenceChangeEvent inherits Serializable interface 
17   * from EventObject, it is not intended to be Serializable. Appropriate
18   * serialization methods are implemented to throw NotSerializableException. 
19   *
20   * @author  Josh Bloch
21   * @version %I%, %G%
22   * @see Preferences
23   * @see PreferenceChangeListener
24   * @see NodeChangeEvent
25   * @since   1.4
26   * @serial exclude
27   */
28  public class PreferenceChangeEvent extends java.util.EventObject {
29  
30      /**
31       * Key of the preference that changed.
32       *
33       * @serial
34       */
35      private String key;
36  
37      /**
38       * New value for preference, or <tt>null</tt> if it was removed.
39       *
40       * @serial
41       */
42      private String newValue;
43  
44      /**
45       * Constructs a new <code>PreferenceChangeEvent</code> instance.
46       *
47       * @param node  The Preferences node that emitted the event.
48       * @param key  The key of the preference that was changed.
49       * @param newValue  The new value of the preference, or <tt>null</tt>
50       *                  if the preference is being removed.
51       */
52      public PreferenceChangeEvent(Preferences node, String key,
53                                   String newValue) {
54      super(node);
55      this.key = key;
56      this.newValue = newValue;
57      }
58  
59      /**
60       * Returns the preference node that emitted the event.
61       *
62       * @return  The preference node that emitted the event.
63       */
64      public Preferences getNode() {
65          return (Preferences) getSource();
66      }  
67  
68      /**
69       * Returns the key of the preference that was changed.
70       *
71       * @return  The key of the preference that was changed.
72       */
73      public String getKey() {
74      return key;
75      }
76      
77      /**
78       * Returns the new value for the preference.
79       *
80       * @return  The new value for the preference, or <tt>null</tt> if the
81       *          preference was removed.
82       */
83      public String getNewValue() {
84      return newValue;
85      }
86  
87      /**
88       * Throws NotSerializableException, since NodeChangeEvent objects
89       * are not intended to be serializable.
90       */
91       private void writeObject(java.io.ObjectOutputStream out) 
92                                                 throws NotSerializableException {
93           throw new NotSerializableException("Not serializable.");
94       }
95  
96      /**
97       * Throws NotSerializableException, since PreferenceChangeEvent objects 
98       * are not intended to be serializable.
99       */
100      private void readObject(java.io.ObjectInputStream in) 
101                                                throws NotSerializableException {
102          throw new NotSerializableException("Not serializable.");
103      }
104 
105     // Defined so that this class isn't flagged as a potential problem when
106     // searches for missing serialVersionUID fields are done.
107     private static final long serialVersionUID = 793724513368024975L;
108 }
109