KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > impl > SchemaModelListener


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.axi.impl;
20
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.modules.xml.schema.model.SchemaComponent;
26 import org.netbeans.modules.xml.schema.model.SchemaModel;
27
28 /**
29  *
30  * @author Samaresh (Samaresh.Panda@Sun.Com)
31  */

32 public class SchemaModelListener implements PropertyChangeListener JavaDoc {
33         
34     /**
35      * Creates a new instance of SchemaModelListener
36      */

37     public SchemaModelListener(AXIModelImpl model) {
38         this.model = model;
39     }
40     
41     /**
42      * Returns true if the event pool is not empty,
43      * false otherwise.
44      */

45     boolean needsSync() {
46         return !events.isEmpty();
47     }
48     
49     void syncCompleted() {
50         events.clear();
51     }
52
53     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
54         assert(model != null);
55         if(model.isIntransaction() || !isValidEvent(evt))
56             return;
57
58         events.add(evt);
59         ((ModelAccessImpl)model.getAccess()).setDirty();
60     }
61     
62     private boolean isValidEvent(PropertyChangeEvent JavaDoc evt) {
63         if(evt.getSource() instanceof SchemaModel) {
64             return true;
65         }
66         
67         if(!(evt.getSource() instanceof SchemaComponent))
68             return false;
69         
70         SchemaComponent component = (SchemaComponent)evt.getSource();
71         if( (evt.getOldValue() == null) &&
72             (evt.getNewValue() != null) &&
73             (evt.getNewValue() instanceof SchemaComponent) ) {
74             component = (SchemaComponent)evt.getNewValue();
75         }
76         
77         if( (evt.getNewValue() == null) &&
78             (evt.getOldValue() != null) &&
79             (evt.getOldValue() instanceof SchemaComponent) ) {
80             component = (SchemaComponent)evt.getOldValue();
81         }
82         
83         //query to check if this component affects the model
84
AXIModelBuilderQuery query = new AXIModelBuilderQuery(model);
85         return query.affectsModel(component);
86     }
87     
88     private List JavaDoc<PropertyChangeEvent JavaDoc> events = new ArrayList JavaDoc<PropertyChangeEvent JavaDoc>();
89     private AXIModelImpl model;
90 }
91
Popular Tags