KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.Image JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.awt.datatransfer.StringSelection JavaDoc;
26 import java.beans.*;
27 import java.util.*;
28 import javax.swing.JPanel JavaDoc;
29
30
31 import org.openide.nodes.*;
32 import org.openide.util.actions.SystemAction;
33 import org.openide.util.actions.CallableSystemAction;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.datatransfer.NewType;
36 import org.openide.util.datatransfer.PasteType;
37
38 /** Usefull class for testin Node events.
39  */

40 public class GoldenEvent {
41
42     private String JavaDoc name;
43     private Object JavaDoc oldValue, newValue;
44     private Node source;
45     private boolean isAdd;
46     private Node[] delta;
47     private int[] indices;
48     private int[] permutation;
49
50     
51     /** Proprerty change event
52     */

53     public GoldenEvent( Node source, String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue ) {
54         this.source = source;
55         this.name = name;
56         this.oldValue = oldValue;
57         this.newValue = newValue;
58     }
59
60     /** Children event
61      */

62     public GoldenEvent( Node source, boolean isAdd, Node[] delta, int[] indices ) {
63         this.source = source;
64         this.isAdd = isAdd;
65         this.delta = delta;
66         this.indices = indices;
67     }
68
69     /** Children event
70      */

71     public GoldenEvent( Node source, int[] permutation ) {
72         this.source = source;
73         this.permutation = permutation;
74     }
75     
76     public Class JavaDoc getRepresentedClass() {
77         if ( name == null ) {
78             if ( permutation == null ) {
79                 return NodeMemberEvent.class;
80             }
81             else {
82                 return NodeReorderEvent.class;
83             }
84         }
85         else {
86             return PropertyChangeEvent.class;
87         }
88     }
89
90     /* Compares the GoldenEvent against another event
91      */

92
93     public boolean compareTo( Object JavaDoc ev ) {
94         if ( getRepresentedClass() != ev.getClass() ) {
95             return false;
96         }
97
98         if ( getRepresentedClass() == PropertyChangeEvent.class ) {
99             PropertyChangeEvent pe = (PropertyChangeEvent)ev;
100             return source.equals( pe.getSource() ) &&
101                    name.equals( pe.getPropertyName() ) &&
102                    oldValue == null ? pe.getOldValue() == null : oldValue.equals( pe.getOldValue() ) &&
103                    newValue == null ? pe.getNewValue() == null : newValue.equals( pe.getNewValue() );
104         }
105
106         else if ( getRepresentedClass() == NodeMemberEvent.class ) {
107             NodeMemberEvent nme = (NodeMemberEvent) ev;
108
109             return source.equals( nme.getNode() ) &&
110                    isAdd == nme.isAddEvent() &&
111                    Arrays.equals( delta, nme.getDelta() ) &&
112                    Arrays.equals( indices, nme.getDeltaIndices() );
113         }
114         
115         else if ( getRepresentedClass() == NodeReorderEvent.class ) {
116             NodeReorderEvent nre = (NodeReorderEvent) ev;
117
118             return source.equals( nre.getNode() ) &&
119                    Arrays.equals( permutation, nre.getPermutation() );
120         }
121         
122         else {
123             return false;
124         }
125     }
126
127     /** Compares list of event with array of GoldenEvents. If the
128      * parameter. If the eventClass param is not null only events of
129      * given class are compared.
130      */

131     public static boolean compare( List events, GoldenEvent[] goldenEvents, Class JavaDoc eventClass ) {
132
133         List filteredEvents = new ArrayList();
134         if ( eventClass != null ) {
135             for ( Iterator it = events.iterator(); it.hasNext(); ) {
136                 Object JavaDoc e = it.next();
137                 if ( e.getClass() == eventClass ) {
138                     filteredEvents.add( e );
139                 }
140             }
141         }
142         else {
143             filteredEvents = events;
144         }
145
146         if ( filteredEvents.size() != goldenEvents.length ) {
147             return false;
148         }
149
150         for ( int i = 0; i < filteredEvents.size(); i++ ) {
151             if ( !goldenEvents[i].compareTo( filteredEvents.get( i ) ) ) {
152                 return false;
153             }
154         }
155
156         return true;
157     }
158
159     public static void printEvents( List events ) {
160
161         for ( Iterator it = events.iterator(); it.hasNext(); ) {
162             Object JavaDoc e = it.next();
163
164             if ( e instanceof PropertyChangeEvent ) {
165                 System.out.println("PCHG : " + ((PropertyChangeEvent)e).getPropertyName() + " : " + ((PropertyChangeEvent)e).getSource() );
166                 System.out.println(" new : " + ((PropertyChangeEvent)e).getOldValue() );
167                 System.out.println(" old : " + ((PropertyChangeEvent)e).getNewValue() );
168             }
169
170             if ( e instanceof NodeMemberEvent ) {
171                 NodeMemberEvent ne = (NodeMemberEvent) e;
172                 System.out.println( ( ne.isAddEvent() ? "cADD : " : "cRMV : " ) + ne.getNode().getName() );
173
174                 Node[] delta = ne.getDelta();
175                 if ( delta == null ) {
176                     System.out.println("d : " + null );
177                 }
178                 else {
179                     System.out.println("d : " );
180                     for( int i = 0; i < delta.length; i++ ) {
181                         System.out.println(" " + delta[i].getName() );
182                     }
183                 }
184
185                 int[] deltaIdx = ne.getDeltaIndices();
186                 if ( deltaIdx == null ) {
187                     System.out.println("di : " + null );
188                 }
189                 else {
190                     System.out.println("di : " );
191                     for( int i = 0; i < deltaIdx.length; i++ ) {
192                         System.out.println(" " + deltaIdx[i] );
193                     }
194                 }
195
196             }
197
198             if ( e instanceof NodeReorderEvent ) {
199                 NodeReorderEvent ne = (NodeReorderEvent) e;
200                 System.out.println( ( "RORD: " ) + ne.getNode().getName() );
201
202                 int[] perm = ne.getPermutation();
203                 if ( perm == null ) {
204                     System.out.println("d : " + null );
205                 }
206                 else {
207                     System.out.println("d : " );
208                     for( int i = 0; i < perm.length; i++ ) {
209                         System.out.println(" " + perm[i] );
210                     }
211                 }
212
213             }
214             
215         }
216     }
217     
218     
219     public static class Listener implements NodeListener {
220         
221         private List events = new ArrayList();
222         
223         
224         public void propertyChange(PropertyChangeEvent evt) {
225             events.add( evt );
226         }
227                 
228         public void nodeDestroyed(NodeEvent evt) {
229             events.add( evt );
230         }
231                 
232         public void childrenReordered(NodeReorderEvent evt) {
233             events.add( evt );
234         }
235                 
236         public void childrenRemoved(NodeMemberEvent evt) {
237             events.add( evt );
238         }
239                 
240         public void childrenAdded(NodeMemberEvent evt) {
241             events.add( evt );
242         }
243         
244         public List getEvents() {
245             return events;
246         }
247         
248     }
249
250 }
251     
252
Popular Tags