KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > looks > SampleRepObject


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
20 package org.netbeans.spi.looks;
21
22 import org.netbeans.spi.looks.GoldenValue;
23
24 import java.beans.*;
25 import java.util.Hashtable JavaDoc;
26
27 /** Sample represented object which can tell whether some methods
28  * were called
29  */

30 public class SampleRepObject {
31
32     private int attachCalled;
33     private int detachCalled;
34     private int setNameCalled;
35     private int destroyCalled;
36
37     private Hashtable JavaDoc properties = new Hashtable JavaDoc();
38
39     // Special propert for testing destroy events
40
// see SampleLook.TestSubstitute
41
public static final String JavaDoc DESTROY = "Destroy";
42     
43     private GoldenValue[] goldenValues;
44     
45     private PropertyChangeSupport propertyChangeSupport;
46     
47     public SampleRepObject() {}
48     
49     public SampleRepObject( GoldenValue[] goldenValues ) {
50         this.goldenValues = goldenValues;
51     }
52     
53     // Methods for testin calls to void methods --------------------------------
54

55     public void attach() {
56         attachCalled++;
57     }
58     
59     public void detach() {
60         detachCalled++;
61     }
62     
63     public void setName() {
64         setNameCalled++;
65     }
66     
67     public void destroy() {
68         destroyCalled++;
69     }
70    
71     public int getAttachCalled() {
72         int r = attachCalled;
73         attachCalled = 0;
74         return r;
75     }
76     
77     public int getDetachCalled() {
78         int r = detachCalled;
79         detachCalled = 0;
80         return r;
81     }
82     
83     public int getSetNameCalled() {
84         int r = setNameCalled;
85         setNameCalled = 0;
86         return r;
87     }
88                         
89     public int getDestroyCalled() {
90         int r = destroyCalled;
91         destroyCalled = 0;
92         return r;
93     }
94     
95     // Method for properties ---------------------------------------------------
96

97     public Object JavaDoc getProperty( String JavaDoc key ) {
98         return properties.get( key );
99     }
100     
101     public void setProperty( String JavaDoc key, Object JavaDoc o ) {
102         Object JavaDoc oldValue = getProperty( key );
103         properties.put( key, o );
104         
105         if ( propertyChangeSupport != null ) {
106             propertyChangeSupport.firePropertyChange( key, oldValue, o );
107         }
108     }
109
110     // Golden value methods ----------------------------------------------------
111

112     public Object JavaDoc getValue( long key ) {
113         
114         if ( goldenValues == null ) {
115             return null;
116         }
117         
118         for( int i = 0; i < goldenValues.length; i++ ) {
119             if ( key == goldenValues[i].key ) {
120                 return goldenValues[i].result;
121             }
122         }
123         return null;
124     }
125     
126     public void setValue( long key, Object JavaDoc value ) {
127         
128         if ( goldenValues == null ) {
129             goldenValues = new GoldenValue[] {
130                 new GoldenValue( key, value )
131             };
132             return;
133         }
134         
135         // Try to find the key
136
int index = -1;
137         
138         for( int i = 0; i < goldenValues.length; i++ ) {
139             if ( key == goldenValues[i].key ) {
140                 index = i;
141             }
142         }
143         
144         Object JavaDoc oldValue = null;
145         
146         // If not found create new array
147
if ( index == -1 ) {
148             GoldenValue[] newGvs = new GoldenValue[ goldenValues.length + 1 ];
149             index = goldenValues.length;
150             System.arraycopy( goldenValues, 0, newGvs, 0, goldenValues.length );
151             goldenValues = newGvs;
152         }
153         else {
154             oldValue = goldenValues[index].result;
155         }
156         
157         goldenValues[index] = new GoldenValue( key, value );
158         
159         if ( propertyChangeSupport != null ) {
160             propertyChangeSupport.firePropertyChange(
161                 Long.toString( goldenValues[index].key ),
162                 oldValue,
163                 value );
164             
165             
166         }
167         
168         
169     }
170     
171     // Regisatration of listeners ----------------------------------------------
172

173     public void addPropertyChangeListener( PropertyChangeListener listener ) {
174         if ( propertyChangeSupport == null ) {
175             propertyChangeSupport = new PropertyChangeSupport( this );
176         }
177         
178         propertyChangeSupport.addPropertyChangeListener( listener );
179     }
180     
181     
182     public void removePropertyChangeListener( PropertyChangeListener listener ) {
183         if ( propertyChangeSupport == null ) {
184             return;
185         }
186         
187         propertyChangeSupport.removePropertyChangeListener( listener );
188     }
189     
190 }
191
192  
Popular Tags